/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-07-10
 * 描述            ：成本计算中间表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.List;

import market.Cm.mapper.BaseFiscalPeriodDMapper;
import market.Cm.mapper.CmCostDstGrindMapper;
import market.Cm.mapper.CmCostDstResultMapper;
import market.Cm.mapper.CmReportCostMiddleMapper;
import market.Cm.mapper.CostAcmMatMapper;
import market.Cm.service.CmReportCostMiddleService;
import market.Cm.vo.BaseFiscalPeriodD;
import market.Cm.vo.CmCostDstGrind;
import market.Cm.vo.CmCostDstResult;
import market.Cm.vo.CmReportCostMiddle;
import market.Cm.vo.CostAcmMat;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="CmReportCostMiddleService")
public class CmReportCostMiddleServiceImpl extends BaseServiceImpl<CmReportCostMiddle,Object> implements CmReportCostMiddleService {
	
	/**
	 * 成本计算中间表 :总体归集
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertCmReportCostMiddle(CommonPage<CmReportCostMiddle> cp) throws Exception{
		ResultModel rm = new ResultModel();
		CmReportCostMiddle rcm = cp.getObject();
		String msg = "生成成本计算中间表失败:";
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rcm.getPrd_num());
		if (bfpd==null) {
			throw new Exception(msg+"成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportCostMiddle RPM = new CmReportCostMiddle();
		RPM.setPrd_num(rcm.getPrd_num());
		int count = this.getMapper(CmReportCostMiddleMapper.class).count(RPM);
		if (count > 0) {
			throw new Exception(msg+"成本期间【"+rcm.getPrd_num()+"】的成本计算中间表已经存在数据");
		}
		
		//数据来源：【修磨原值摊销】，【分配结果实绩】					
		//5.1 【修磨原值摊销】到成本计算中间表
		//20191129 取消刀片和轧辊到加工成本
		//doInsertByCmCostDstGrind(rcm);
		
		//5.2 【分配结果实绩】到成本计算中间表
		doInsertByCmCostDstResult(rcm);
		
		//5.3【资材备件归集】取轧辊，轴承，油脂
		doInsertByCmCostAcmMat(rcm);
		
		rm.setState("200");
		rm.setMsgInfo("新增成本计算中间表成功");
		return rm;
	}
	 
	/**
	 * 【修磨原值摊销】到成本计算中间表
	 * @param camList
	 * @param bciList
	 * @param sameRcmList
	 * @param rpd
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public void doInsertByCmCostDstGrind(CmReportCostMiddle rcm) throws Exception{
		//获取本期的实际用量和实际成本
		CmCostDstGrind cdg = new CmCostDstGrind();
		cdg.setPrd_num(rcm.getPrd_num());
		cdg.setValid_flg("1");
		List<CmCostDstGrind> cdgList = this.getMapper(CmCostDstGrindMapper.class).doQuaryCcdgForCrcm(cdg);
		if (cdgList==null || cdgList.size()<=0) {
			throw new Exception("生成成本计算中间表失败:"+"未找到成本期间【"+rcm.getPrd_num()+"】的修磨原值摊销数据");
		}
		//获取本年的实际用量和实际成本
		List<CmCostDstGrind> totalList = this.getMapper(CmCostDstGrindMapper.class).doQuaryTotalCcdgForCrcm(cdg);
		//获取同期的实际用量和实际成本
		List<CmCostDstGrind> sameList = this.getMapper(CmCostDstGrindMapper.class).doQuarySameCcdgForCrcm(cdg);
		//获取同期的累计用量和累计成本
		List<CmCostDstGrind> sameTotalList = this.getMapper(CmCostDstGrindMapper.class).doQuarySameTotalCcdgForCrcm(cdg);
				
		for (CmCostDstGrind ccdg : cdgList) {
			CmReportCostMiddle crcm = new CmReportCostMiddle();
			crcm.setItem_code(ccdg.getItem_code());
			crcm.setItem_name(ccdg.getItem_name());
			crcm.setOrg_code(ccdg.getOrg_code());
			crcm.setOrg_name(ccdg.getOrg_name());
			crcm.setPrd_num(ccdg.getPrd_num());
			crcm.setParent_item_code(ccdg.getParent_item_code());
			crcm.setParent_item_name(ccdg.getParent_item_name());
			crcm.setItem_type(ccdg.getItem_type());
			crcm.setItem_grade(ccdg.getItem_grade());
			crcm.setMsr_unit(ccdg.getMsr_unit());
			//本期实际用量 and 本期实际成本
			crcm.setConsume_value(ccdg.getGrd_thick_value());
			crcm.setAmount_value(ccdg.getAmount_value());
			//本年累计用量 and 本年累计成本
			if (totalList!=null && totalList.size()>0) {
				for (CmCostDstGrind totalCdg : totalList) {
					if (totalCdg.getOrg_code().equals(ccdg.getOrg_code())) {						
						crcm.setTotal_consume(totalCdg.getGrd_thick_value());
						crcm.setTotal_amount(totalCdg.getAmount_value());
						continue;
					}
				}
			}
			//同期实际用量  and //同期实际成本
			if (sameList!=null && sameList.size()>0) {
				for (CmCostDstGrind sameCdg : sameList) {
					if (sameCdg.getOrg_code().equals(ccdg.getOrg_code())) {
						crcm.setSame_consume_value(sameCdg.getGrd_thick_value());
						crcm.setSame_amount_value(sameCdg.getAmount_value());
						continue;
					}
				}
			}			
			//同期累计用量 and 同期累计成本
			if (sameTotalList!=null && sameTotalList.size()>0) {
				for (CmCostDstGrind sameTotalCdg : sameTotalList) {
					if (sameTotalCdg.getOrg_code().equals(ccdg.getOrg_code())) {
						crcm.setSame_consume_value(sameTotalCdg.getGrd_thick_value());
						crcm.setSame_amount_value(sameTotalCdg.getAmount_value());
						continue;
					}
				}
			}
			//本期单位用量=本期实际用量/本期实际产量
			/*if (crcm.getConsume_value()!=null && rpd.getWeight_value()!=null && rpd.getWeight_value()!=0) {				
				crcm.setConsume_per(crcm.getConsume_value()/rpd.getWeight_value());
			}
			//本年累计单位用量=本年实际用量/本年实际产量
			if (crcm.getTotal_consume()!=null && rpd.getTotal_weight()!=null && rpd.getTotal_weight()!=0) {
				crcm.setTotal_consume_per(crcm.getTotal_consume()/rpd.getTotal_weight());
			}
			//本期单位成本=本期实际成本/本期实际产量
			if (crcm.getAmount_value()!=null && rpd.getWeight_value()!=null && rpd.getWeight_value()!=0) {				
				crcm.setAmount_per(crcm.getAmount_value()/rpd.getWeight_value());
			}
			//本年累计单位成本=本年实际成本/本年实际产量
			if (crcm.getTotal_amount()!=null && rpd.getTotal_weight()!=null && rpd.getTotal_weight()!=0) {
				crcm.setTotal_amount_per(crcm.getTotal_amount()/rpd.getTotal_weight());
			}
			if (sameRcmList!=null && sameRcmList.size()>0) {
				for (CmReportCostMiddle srcm : sameRcmList) {
					if (srcm.getItem_code().equals(ccdg.getItem_code())) {
						crcm.setSame_total_consume_per(srcm.getTotal_consume_per());//同期累计单位用量
						crcm.setSame_total_amount_per(srcm.getTotal_amount_per());//同期累计单位成本			
						continue;
					}
				}
			}*/
			this.getMapper(CmReportCostMiddleMapper.class).insert(crcm);			
		}
	}
	
	/**
	 * 【分配结果实绩】到成本计算中间表
	 * @param rcm 成本期间
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public void doInsertByCmCostDstResult(CmReportCostMiddle rcm) throws Exception{
		//获取本期的实际用量和实际成本
		CmCostDstResult cdr = new CmCostDstResult();
		cdr.setPrd_num(rcm.getPrd_num());
		cdr.setValid_flg("1");
		List<CmCostDstResult> cdrList = this.getMapper(CmCostDstResultMapper.class).doQuaryCcdrForCrcm(cdr);
		if (cdrList==null || cdrList.size()<=0) {
			throw new Exception("生成成本计算中间表失败:"+"未找到成本期间【"+rcm.getPrd_num()+"】的分配结果实绩数据");
		}
		//获取本年的实际用量和实际成本
		List<CmCostDstResult> totalList = this.getMapper(CmCostDstResultMapper.class).doQuaryTotalCcdrForCrcm(cdr);
		//获取同期的实际用量和实际成本
		List<CmCostDstResult> sameList = this.getMapper(CmCostDstResultMapper.class).doQuarySameCcdrForCrcm(cdr);
		//获取同期的累计用量和累计成本
		List<CmCostDstResult> sameTotalList = this.getMapper(CmCostDstResultMapper.class).doQuarySameTotalCcdrForCrcm(cdr);
		for (CmCostDstResult ccdr : cdrList) {
			CmReportCostMiddle crcm = new CmReportCostMiddle();
			crcm.setItem_code(ccdr.getItem_code());
			crcm.setItem_name(ccdr.getItem_name());
			crcm.setOrg_code(ccdr.getOrg_code());
			crcm.setOrg_name(ccdr.getOrg_name());
			crcm.setPrd_num(ccdr.getPrd_num());
			crcm.setParent_item_code(ccdr.getParent_item_code());
			crcm.setParent_item_name(ccdr.getParent_item_name());
			crcm.setItem_type(ccdr.getItem_type());
			crcm.setItem_grade(ccdr.getItem_grade());
			crcm.setMsr_unit(ccdr.getMsr_unit());
			//本期实际用量 and 本期实际成本
			crcm.setConsume_value(ccdr.getConsume_value());
			crcm.setAmount_value(ccdr.getAmount_value());
			//本年累计用量 and 本年累计成本
			if (totalList!=null && totalList.size()>0) {
				for (CmCostDstResult totalCdr : totalList) {
					if (totalCdr.getOrg_code().equals(ccdr.getOrg_code()) &&
							totalCdr.getItem_code().equals(ccdr.getItem_code())) {						
						crcm.setTotal_consume(totalCdr.getConsume_value());
						crcm.setTotal_amount(totalCdr.getAmount_value());
						continue;
					}
				}
			}
			//同期实际用量  and //同期实际成本
			if (sameList!=null && sameList.size()>0) {
				for (CmCostDstResult sameCdr : sameList) {
					if (sameCdr.getOrg_code().equals(ccdr.getOrg_code()) &&
							sameCdr.getItem_code().equals(ccdr.getItem_code())) {
						crcm.setSame_consume_value(sameCdr.getConsume_value());
						crcm.setSame_amount_value(sameCdr.getAmount_value());
						continue;
					}
				}
			}			
			//同期累计用量 and 同期累计成本
			if (sameTotalList!=null && sameTotalList.size()>0) {
				for (CmCostDstResult sameTotalCdr : sameTotalList) {
					if (sameTotalCdr.getOrg_code().equals(ccdr.getOrg_code()) &&
							sameTotalCdr.getItem_code().equals(ccdr.getItem_code())) {
						crcm.setSame_consume_value(sameTotalCdr.getConsume_value());
						crcm.setSame_amount_value(sameTotalCdr.getAmount_value());
						continue;
					}
				}
			}			
			//本期单位用量=本期实际用量/本期实际产量
			/*if (crcm.getConsume_value()!=null && rpd.getWeight_value()!=null && rpd.getWeight_value()!=0) {				
				crcm.setConsume_per(crcm.getConsume_value()/rpd.getWeight_value());
			}
			//本年累计单位用量=本年实际用量/本年实际产量
			if (crcm.getTotal_consume()!=null && rpd.getTotal_weight()!=null && rpd.getTotal_weight()!=0) {
				crcm.setTotal_consume_per(crcm.getTotal_consume()/rpd.getTotal_weight());
			}
			//本期单位成本=本期实际成本/本期实际产量
			if (crcm.getAmount_value()!=null && rpd.getWeight_value()!=null && rpd.getWeight_value()!=0) {				
				crcm.setAmount_per(crcm.getAmount_value()/rpd.getWeight_value());
			}
			//本年累计单位成本=本年实际成本/本年实际产量
			if (crcm.getTotal_amount()!=null && rpd.getTotal_weight()!=null && rpd.getTotal_weight()!=0) {
				crcm.setTotal_amount_per(crcm.getTotal_amount()/rpd.getTotal_weight());
			}
			if (sameRcmList!=null && sameRcmList.size()>0) {
				for (CmReportCostMiddle srcm : sameRcmList) {
					if (srcm.getItem_code().equals(ccdr.getItem_code())) {
						crcm.setSame_total_consume_per(srcm.getTotal_consume_per());//同期累计单位用量
						crcm.setSame_total_amount_per(srcm.getTotal_amount_per());//同期累计单位成本			
						continue;
					}
				}
			}*/
			this.getMapper(CmReportCostMiddleMapper.class).insert(crcm);			
		}
	}
	
	/**
	 * 【资材备件归集】取轧辊，轴承，油脂
	 * @param rcm
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public void doInsertByCmCostAcmMat(CmReportCostMiddle rcm) throws Exception{
		//获取本期的实际用量和实际成本
		CostAcmMat cacm  = new CostAcmMat();
		cacm.setPrd_num(rcm.getPrd_num());
		cacm.setAudit_flg("1");
		List<CostAcmMat> camList = this.getMapper(CostAcmMatMapper.class).doQuaryCcamForCrcm(cacm);
		if (camList==null || camList.size()<=0) {
			throw new Exception("生成成本计算中间表失败:"+"未找到成本期间【"+rcm.getPrd_num()+"】已审核的资材备件归集数据");
		}
		//获取本年的实际用量和实际成本
		List<CostAcmMat> totalList = this.getMapper(CostAcmMatMapper.class).doQuaryTotalCcamForCrcm(cacm);
		//获取同期的实际用量和实际成本
		List<CostAcmMat> sameList = this.getMapper(CostAcmMatMapper.class).doQuarySameCcamForCrcm(cacm);
		//获取同期的累计用量和累计成本
		List<CostAcmMat> sameTotalList = this.getMapper(CostAcmMatMapper.class).doQuarySameTotalCcamForCrcm(cacm);
		
		for (CostAcmMat cam : camList) {
			CmReportCostMiddle crcm = new CmReportCostMiddle();
			crcm.setItem_code(cam.getItem_code());
			crcm.setItem_name(cam.getItem_name());
			crcm.setOrg_code(cam.getOrg_code());
			crcm.setOrg_name(cam.getOrg_name());
			crcm.setPrd_num(cam.getPrd_num());
			crcm.setParent_item_code("CT010102");
			crcm.setParent_item_name("辅助材料");
			crcm.setItem_type("B");//项目类型(A汇总项、B明细项)
			crcm.setItem_grade(4L);
			crcm.setMsr_unit("个");
			//本期实际用量 and 本期实际成本
			crcm.setConsume_value(cam.getQuantity_value());
			crcm.setAmount_value(cam.getAmount_value());
			//本年累计用量 and 本年累计成本
			if (totalList!=null && totalList.size()>0) {
				for (CostAcmMat totalCam : totalList) {
					if (totalCam.getOrg_code().equals(cam.getOrg_code()) &&
							totalCam.getSrot_code().equals(cam.getSrot_code())) {						
						crcm.setTotal_consume(totalCam.getQuantity_value());
						crcm.setTotal_amount(totalCam.getAmount_value());
						continue;
					}
				}
			}
			//同期实际用量  and //同期实际成本
			if (sameList!=null && sameList.size()>0) {
				for (CostAcmMat sameCam : sameList) {
					if (sameCam.getOrg_code().equals(cam.getOrg_code()) &&
							sameCam.getSrot_code().equals(cam.getSrot_code())) {
						crcm.setSame_consume_value(sameCam.getQuantity_value());
						crcm.setSame_amount_value(sameCam.getAmount_value());
						continue;
					}
				}
			}			
			//同期累计用量 and 同期累计成本
			if (sameTotalList!=null && sameTotalList.size()>0) {
				for (CostAcmMat sameTotalCam : sameTotalList) {
					if (sameTotalCam.getOrg_code().equals(cam.getOrg_code()) &&
							sameTotalCam.getSrot_code().equals(cam.getSrot_code())) {
						crcm.setSame_consume_value(sameTotalCam.getQuantity_value());
						crcm.setSame_amount_value(sameTotalCam.getAmount_value());
						continue;
					}
				}
			}			
			this.getMapper(CmReportCostMiddleMapper.class).insert(crcm);			
		}
	}
	
}
