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

import market.Cm.mapper.BaseFiscalPeriodDMapper;
import market.Cm.mapper.CmReportProdDetailMapper;
import market.Cm.mapper.CmReportProdMiddleMapper;
import market.Cm.service.CmReportProdMiddleService;
import market.Cm.vo.BaseFiscalPeriodD;
import market.Cm.vo.CmReportProdDetail;
import market.Cm.vo.CmReportProdMiddle;
import market.Cm.vo.CostAcmProd;
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="CmReportProdMiddleService")
public class CmReportProdMiddleServiceImpl extends BaseServiceImpl<CmReportProdMiddle,Object> implements CmReportProdMiddleService {
	
	/**
	 * 查询成本组织的本期产量和本年累计产量
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmReportProdMiddle(CommonPage<CmReportProdMiddle> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmReportProdMiddle rpm = cp.getObject();		
		try {			
			List<CmReportProdMiddle> list = this.getMapper(CmReportProdMiddleMapper.class).doQuaryCrpmForCrpp(rpm);
 			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 新增产量计算中间表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertCmReportProdMiddleVersion1(CommonPage<CmReportProdMiddle> cp) throws Exception{
		ResultModel rm = new ResultModel();
		CmReportProdMiddle rpc = cp.getObject();
		String msg = "生成产量计算中间表失败:";
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpc.getPrd_num());
		if (bfpd==null) {
			throw new Exception(msg+"成本期间传递有误");
		}
		
		//判断当前成本期间是否核算
		CmReportProdMiddle RPM = new CmReportProdMiddle();
		RPM.setPrd_num(rpc.getPrd_num());
		int count = this.getMapper(CmReportProdMiddleMapper.class).count(RPM);
		if (count > 0) {
			throw new Exception(msg+"成本期间【"+rpc.getPrd_num()+"】的产量计算中间表已经存在数据");
		}
		
		//1.本期的产量明细中间表的数据
		CmReportProdDetail rpd = new CmReportProdDetail();
		rpd.setPrd_num(rpc.getPrd_num());			
		List<CmReportProdDetail> rpdList = this.getMapper(CmReportProdDetailMapper.class).doQuaryCmReportProdDetailGbOrg(rpd);
		if (rpdList==null || rpdList.size()<=0) {
			throw new Exception(msg+"未找到成本期间【"+rpc.getPrd_num()+"】的产量明细中间表数据");
		}
		//2.获取本年累计的产量明细中间表
		List<CmReportProdDetail> totalrpdList = this.getMapper(CmReportProdDetailMapper.class).doQueryTotalCostAcmProdGBOrg(rpd);
		//3.获取同期的产量明细中间表
		CmReportProdMiddle sameRpm = new CmReportProdMiddle();
		String samePrd_num = (Long.parseLong(rpd.getPrd_num().substring(0,4))-1)+rpd.getPrd_num().substring(rpd.getPrd_num().length()-2);		
		sameRpm.setPrd_num(samePrd_num);		
		List<CmReportProdMiddle> sameRpmList = this.getMapper(CmReportProdMiddleMapper.class).find(sameRpm, 0, 0);
		
		for (CmReportProdDetail RPD : rpdList) {
			CmReportProdMiddle rpm = new CmReportProdMiddle();
			rpm.setOrg_code(RPD.getOrg_code());
			rpm.setOrg_name(RPD.getOrg_name());
			rpm.setPrd_num(RPD.getPrd_num());
			rpm.setOrder_model(RPD.getOrder_model());
			rpm.setItem_type(RPD.getItem_name6());//牌号类型
			rpm.setInput_type(RPD.getItem_name4());//投料类型
			rpm.setConsume_value(RPD.getConsume_value());//本期实际用量
			rpm.setWeight_value(RPD.getWeight_value());//本期实际产量
			rpm.setAmount_value(RPD.getAmount_value());//本期实际成本
			if (totalrpdList!=null && totalrpdList.size()>0) {
				for (CmReportProdDetail trpd : totalrpdList) {
					if (trpd.getOrg_code().equals(RPD.getOrg_code()) && 
							trpd.getOrder_model().equals(RPD.getOrder_model()) &&
							trpd.getItem_name4().equals(RPD.getItem_name4()) && 
							trpd.getItem_name6().equals(RPD.getItem_name6())) {
						rpm.setTotal_consume(trpd.getTotal_consume());//本年累计用量
						rpm.setTotal_weight(trpd.getTotal_weight());//本年累计产量
						rpm.setTotal_amount(trpd.getTotal_amount());//本年累计成本
						continue;
					}
				}
			}
			if (sameRpmList!=null && sameRpmList.size()>0) {
				for (CmReportProdMiddle srpm : sameRpmList) {
					if (srpm.getOrg_code().equals(RPD.getOrg_code()) && 
							srpm.getOrder_model().equals(RPD.getOrder_model()) &&
							srpm.getInput_type().equals(RPD.getItem_name4()) &&
							srpm.getItem_type().equals(RPD.getItem_name6())) {
						rpm.setSame_consume_value(srpm.getConsume_value());//同期实际用量
						rpm.setSame_weight_value(srpm.getWeight_value());//同期实际产量
						rpm.setSame_amount_value(srpm.getAmount_value());//同期实际金额
						rpm.setSame_total_consume(srpm.getTotal_consume());//同期累计用量
						rpm.setSame_total_weight(srpm.getTotal_weight());//同期累计产量
						rpm.setSame_total_amount(srpm.getTotal_amount());//同期累计金额
						continue;
					}
				}
			}
			this.getMapper(CmReportProdMiddleMapper.class).insert(rpm);
		}
		rm.setState("200");
		rm.setMsgInfo("新增产量计算中间表成功");
		return rm;
	}
	
	/**
	 * 新增产量计算中间表
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertCmReportProdMiddle(CommonPage<CmReportProdMiddle> cp) throws Exception{
		ResultModel rm = new ResultModel();
		CmReportProdMiddle rpc = cp.getObject();
		String msg = "生成产量计算中间表失败:";
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpc.getPrd_num());
		if (bfpd==null) {
			throw new Exception(msg+"成本期间传递有误");
		}
		
		//判断当前成本期间是否核算
		CmReportProdMiddle RPM = new CmReportProdMiddle();
		RPM.setPrd_num(rpc.getPrd_num());
		int count = this.getMapper(CmReportProdMiddleMapper.class).count(RPM);
		if (count > 0) {
			throw new Exception(msg+"成本期间【"+rpc.getPrd_num()+"】的产量计算中间表已经存在数据");
		}
		
		//1.本期的【投入产出归集】的数据
		CostAcmProd cap = new CostAcmProd();
		cap.setPrd_num(rpc.getPrd_num());
		List<CostAcmProd> capList = this.getMapper(CmReportProdMiddleMapper.class).doQuaryCcapForCrpm(cap);
		if (capList==null || capList.size()<=0) {
			throw new Exception(msg+"未找到成本期间【"+rpc.getPrd_num()+"】的【投入产出归集】数据");
		}
		//2.获取本年累计的【投入产出归集】的数据
		List<CostAcmProd> totalcapList = this.getMapper(CmReportProdMiddleMapper.class).doQuaryToTalCcapForCrpm(cap);
		//3.获取同期的【产量计算中间表】的数据
		CmReportProdMiddle sameRpm = new CmReportProdMiddle();
		String samePrd_num = (Long.parseLong(rpc.getPrd_num().substring(0,4))-1)+rpc.getPrd_num().substring(rpc.getPrd_num().length()-2);		
		sameRpm.setPrd_num(samePrd_num);//【同期的成本期间】	
		List<CmReportProdMiddle> sameRpmList = this.getMapper(CmReportProdMiddleMapper.class).find(sameRpm, 0, 0);
		
		for (CostAcmProd CAP : capList) {
			CmReportProdMiddle rpm = new CmReportProdMiddle();
			rpm.setOrg_code(CAP.getOrg_code());
			rpm.setOrg_name(CAP.getOrg_name());
			rpm.setPrd_num(CAP.getPrd_num());
			rpm.setOrder_model(CAP.getOrder_model());
			rpm.setItem_type(CAP.getParent_item_name());//牌号类型
			rpm.setInput_type(CAP.getInput_type());//投料类型
			rpm.setConsume_value(CAP.getInput_wgt_value());//本期实际用量
			rpm.setWeight_value(CAP.getOutput_wgt_value());//本期实际产量
			rpm.setAmount_value(CAP.getAmount_value());//本期产出成本(待反写)
			rpm.setConsume_amount(CAP.getOriginal_amount_value());//本期投入成本=初始投料金额(待反写)
			if (totalcapList!=null && totalcapList.size()>0) {
				for (CostAcmProd tcap : totalcapList) {
					if (tcap.getOrg_code().equals(CAP.getOrg_code()) && 
							tcap.getOrder_model().equals(CAP.getOrder_model()) &&
							tcap.getInput_type().equals(CAP.getInput_type()) && 
							tcap.getParent_item_name().equals(CAP.getParent_item_name())) {
						rpm.setTotal_consume(tcap.getInput_wgt_value());//本年累计用量
						rpm.setTotal_weight(tcap.getOutput_wgt_value());//本年累计产量
						rpm.setTotal_amount(tcap.getAmount_value());//本年产出成本(待反写)
						continue;
					}
				}
			}
			if (sameRpmList!=null && sameRpmList.size()>0) {
				for (CmReportProdMiddle srpm : sameRpmList) {
					if (srpm.getOrg_code().equals(rpm.getOrg_code()) && 
							srpm.getOrder_model().equals(rpm.getOrder_model()) &&
							srpm.getInput_type().equals(rpm.getInput_type()) &&
							srpm.getItem_type().equals(rpm.getItem_type())) {
						rpm.setSame_consume_value(srpm.getConsume_value());//同期实际用量
						rpm.setSame_weight_value(srpm.getWeight_value());//同期实际产量
						rpm.setSame_amount_value(srpm.getAmount_value());//同期产出成本
						rpm.setSame_total_consume(srpm.getTotal_consume());//同期累计用量
						rpm.setSame_total_weight(srpm.getTotal_weight());//同期累计产量
						rpm.setSame_total_amount(srpm.getTotal_amount());//同期累计产出成本
						rpm.setSame_consume_amount(srpm.getConsume_amount());//同期投入成本
						continue;
					}
				}
			}
			this.getMapper(CmReportProdMiddleMapper.class).insert(rpm);
		}
		rm.setState("200");
		rm.setMsgInfo("新增产量计算中间表成功");
		return rm;
	}

}
