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

import market.Cm.mapper.BaseCostItemMapper;
import market.Cm.mapper.BaseFiscalPeriodDMapper;
import market.Cm.mapper.CmReportCostMiddleMapper;
import market.Cm.mapper.CmReportProdMiddleMapper;
import market.Cm.mapper.CmReportProdProcessMapper;
import market.Cm.mapper.CostAcmProdMapper;
import market.Cm.service.CmReportProdProcessService;
import market.Cm.vo.BaseCostItem;
import market.Cm.vo.BaseFiscalPeriodD;
import market.Cm.vo.CmReportCostMiddle;
import market.Cm.vo.CmReportProdMiddle;
import market.Cm.vo.CmReportProdProcess;
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;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="CmReportProdProcessService")
public class CmReportProdProcessServiceImpl extends BaseServiceImpl<CmReportProdProcess,Object> implements CmReportProdProcessService {

	/**
	 * 加工成本统计：查询功能
	 * @param cp object[选传 item_code,prd_num,org_code,org_name,valid_flg,audit_flg等]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmReportProdProcess(CommonPage<CmReportProdProcess> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmReportProdProcess rpp = cp.getObject();		
		try {			
			List<CmReportProdProcess> list = this.getMapper(CmReportProdProcessMapper.class).doQueryCmReportProdProcess(rpp);
			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 doInsertCmReportProdProcess(CommonPage<CmReportProdProcess> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		CmReportProdProcess rpp = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpp.getPrd_num());
		if (bfpd==null) {
			throw new Exception("成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportProdProcess RPP = new CmReportProdProcess();
		RPP.setPrd_num(rpp.getPrd_num());//成本期间
		int count = this.getMapper(CmReportProdProcessMapper.class).count(RPP);
		if (count > 0) {
			throw new Exception("成本期间【"+rpp.getPrd_num()+"】的加工成本统计已经核算");
		}

		//1.查询【产量计算中间表】获取【产量】
		CmReportProdMiddle rpm = new CmReportProdMiddle();
		rpm.setPrd_num(rpp.getPrd_num());//成本期间
		List<CmReportProdMiddle> rpmList = this.getMapper(CmReportProdMiddleMapper.class).doQuaryCrpmForCrpp(rpm);
		if (rpmList==null || rpmList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpp.getPrd_num()+"】的【产量计算中间表】数据");
		}
		
		//2.查询【成本计算中间表】获取【用量】和【成本】	
		CmReportCostMiddle crcm = new CmReportCostMiddle();
		crcm.setPrd_num(rpp.getPrd_num());
		List<CmReportCostMiddle> crcmList = this.getMapper(CmReportCostMiddleMapper.class).doQuaryCrcmForCrpp(crcm);
		if (crcmList==null || crcmList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpp.getPrd_num()+"】的【成本计算中间表】数据");
		}
		for (CmReportCostMiddle cm : crcmList) {
			CmReportProdProcess crpp = new CmReportProdProcess();
			crpp.setOrg_code(cm.getOrg_code());
			crpp.setOrg_name(cm.getOrg_name());
			crpp.setPrd_num(cm.getPrd_num());//成本期间
			crpp.setItem_code(cm.getItem_code());
			crpp.setItem_name(cm.getItem_name());
			crpp.setParent_item_code(cm.getParent_item_code());
			crpp.setParent_item_name(cm.getParent_item_name());
			crpp.setItem_type(cm.getItem_type());
			crpp.setItem_grade(cm.getItem_grade());
			crpp.setMsr_unit(cm.getMsr_unit());
			crpp.setItem_ctrl(cm.getItem_ctrl());//成本性态[0变动、1固定]
			crpp.setItem_prop(cm.getItem_prop());//责任属性[0不可控、1可控]			
			
			//本期产量,本年累计产量,同期产量,同期累计产量 【成本计算中间表】
			for (CmReportProdMiddle RPM : rpmList) {
				if (cm.getPrd_num().equals(RPM.getPrd_num()) && 
						cm.getOrg_code().equals(RPM.getOrg_code())) {
					crpp.setWeight_value(RPM.getWeight_value());//本期产量
					crpp.setTotal_weight(RPM.getTotal_weight());//本年累计产量
					crpp.setSame_weight_value(RPM.getSame_weight_value());//同期产量
					crpp.setSame_total_weight(RPM.getSame_total_weight());//同期累计产量
					continue;
				}
			}			
			crpp.setConsume_value(cm.getConsume_value());//本期实际用量 
			crpp.setAmount_value(cm.getAmount_value());//本期实际成本
			crpp.setTotal_consume(cm.getTotal_consume());//本年累计用量
			crpp.setTotal_amount(cm.getTotal_amount());//本年累计成本
			crpp.setSame_total_consume(cm.getSame_total_consume());//同期累计用量 
			crpp.setSame_total_amount(cm.getSame_total_amount());//同期累计成本
			
			//本期单位用量 = 本期实际用量/本期实际产量
			if (crpp.getConsume_value()!=null && crpp.getWeight_value()!=null && crpp.getWeight_value()!=0) {
				crpp.setConsume_per(crpp.getConsume_value()/crpp.getWeight_value());				
			}
			//本期单位成本 = 本期实际成本/本期实际产量
			if (crpp.getAmount_value()!=null && crpp.getWeight_value()!=null && crpp.getWeight_value()!=0) {
				crpp.setAmount_per(crpp.getAmount_value()/crpp.getWeight_value());				
			}								
			//本年累计单位用量 = 本年累计用量/本年累计产量
			if (crpp.getTotal_consume()!=null && crpp.getTotal_weight()!=null && crpp.getTotal_weight()!=0) {
				crpp.setTotal_consume_per(crpp.getTotal_consume()/crpp.getTotal_weight());				
			}
			//本年累计单位成本 = 本年累计成本/本年累计产量
			if (crpp.getTotal_amount()!=null && crpp.getTotal_weight()!=null && crpp.getTotal_weight()!=0) {
				crpp.setTotal_amount_per(crpp.getTotal_amount()/crpp.getTotal_weight());				
			}			
			//同期累计单位用量 = 同期累计用量/同期累计产量			
			if (crpp.getSame_total_consume()!=null && crpp.getSame_total_weight()!=null && crpp.getSame_total_weight()!=0) {
				crpp.setSame_total_consume_per(crpp.getSame_total_consume()/crpp.getSame_total_weight());				
			}
			//同期累计单位成本 = 同期累计成本/同期累计产量
			if (crpp.getSame_total_amount()!=null && crpp.getSame_total_weight()!=null && crpp.getSame_total_weight()!=0) {
				crpp.setSame_total_amount_per(crpp.getSame_total_amount()/crpp.getSame_total_weight());				
			}											
			crpp.setMemo(null);
			crpp.setValid_flg("1");
			crpp.setAudit_flg("0");
			crpp.setCreate_man(userName);
			crpp.setCreate_time(now);
			this.getMapper(CmReportProdProcessMapper.class).insert(crpp);		
		}
		
		//合并数据到加工成本合计,并且反写投入产出的加工成本
		doInsertCmReportProdProcessSum(rpp);		

		rm.setState("200");
		rm.setMsgInfo("加工成本统计核算成功");
		return rm;
	}
	
	/**
	 * 加工成本统计报表：审核及反审
	 * @param cp object[org_code成本组织 prd_num成本期间] memo1[1审核 0反审]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeCmReportProdProcessAuditFlg(CommonPage<CmReportProdProcess> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		CmReportProdProcess rpp = cp.getObject();
		if (rpp.getOrg_code()==null || "".equals(rpp.getOrg_code())) {
			throw new Exception("请传入成本组织");
		}
		if (rpp.getPrd_num()==null || "".equals(rpp.getPrd_num())) {
			throw new Exception("请传入成本期间");
		}
		Date now = new Date();
		//通过成本期间和成本组织进行【审核】和【反审】
		CmReportProdProcess RPP = new CmReportProdProcess();
		RPP.setOrg_code(rpp.getOrg_code());
		RPP.setPrd_num(rpp.getPrd_num());
		RPP.setAudit_flg("1".equals(opr)?"1":"0");//
		List<CmReportProdProcess> rppList = this.getMapper(CmReportProdProcessMapper.class).find(RPP, 0, 0);
		if (rppList!=null && rppList.size()>0) {
			throw new Exception("成本组织【"+rpp.getOrg_name()+"】成本期间【"+rpp.getPrd_num()+"】中存在"+("1".equals(opr)?"已审核":"未审核")+"的数据");
		}
		rpp.setAudit_flg(opr);
		if ("1".equals(opr)) {//审核操作，传递审核人和审核时间			
			rpp.setAudit_man(userName);
			rpp.setAudit_time(now);
		}
		this.getMapper(CmReportProdProcessMapper.class).doUpdateCmReportProdProcess(rpp);	
		rm.setState("200");
		rm.setMsgInfo("成本组织【"+rpp.getOrg_name()+"】成本期间【"+rpp.getPrd_num()+"】"+("0".equals(cp.getMemo1())?"反审":"审核") + "操作成功");
		return rm;
	}
	
	/**
	 * 加工成本统计报表：撤销核算
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteCmReportProdProcess(CommonPage<CmReportProdProcess> cp) throws Exception{
		ResultModel rm = new ResultModel();
		CmReportProdProcess rpp = cp.getObject();
		/*if (rpp.getOrg_code()==null || "".equals(rpp.getOrg_code())) {
			throw new Exception("请传入成本组织");
		}*/
		if (rpp.getPrd_num()==null || "".equals(rpp.getPrd_num())) {
			throw new Exception("请传入成本期间");
		}
		
		CmReportProdProcess crpp = new CmReportProdProcess();
		crpp.setPrd_num(rpp.getPrd_num());
		this.getMapper(CmReportProdProcessMapper.class).doDeleteCmReportProdProcess(crpp);		
		rm.setState("200");
		rm.setMsgInfo("删除加工成本统计成功");
		return rm;		
	}
	
	/**
	 * 加工成本统计：合计统计功能
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public void doInsertCmReportProdProcessSum(CmReportProdProcess rpp) throws Exception{		 
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();		
		
		//1.查询【产量计算中间表】获取【产量】
		CmReportProdMiddle rpm = new CmReportProdMiddle();
		rpm.setPrd_num(rpp.getPrd_num());//成本期间
		List<CmReportProdMiddle> rpmList = this.getMapper(CmReportProdMiddleMapper.class).doQuaryCrpmForCrpp(rpm);
		if (rpmList==null || rpmList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpp.getPrd_num()+"】的【产量计算中间表】数据");
		}
		
		//2.查询【成本计算中间表】获取【用量】和【成本】	
		CmReportCostMiddle crcm = new CmReportCostMiddle();
		crcm.setPrd_num(rpp.getPrd_num());
		List<CmReportCostMiddle> crcmList = this.getMapper(CmReportCostMiddleMapper.class).doQuaryCrcmSumForCrpp(crcm);
		if (crcmList==null || crcmList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpp.getPrd_num()+"】的【成本计算中间表】数据");
		}
		
		for (CmReportCostMiddle rcm : crcmList) {
			//累计到加工成本合计
			CmReportProdProcess crpp = new CmReportProdProcess();
			crpp.setOrg_code(rcm.getOrg_code());
			crpp.setOrg_name(rcm.getOrg_name());
			crpp.setPrd_num(rcm.getPrd_num());//成本期间
			crpp.setItem_code("CT01");
			BaseCostItem bci = this.getMapper(BaseCostItemMapper.class).findById(crpp.getItem_code());
			if (bci!=null) {				
				crpp.setItem_name(bci.getItem_name());
				crpp.setParent_item_code(bci.getParent_item_code());
				crpp.setParent_item_name(bci.getParent_item_name());
				crpp.setItem_type(bci.getItem_type());
				crpp.setItem_grade(bci.getItem_grade());
				crpp.setMsr_unit(bci.getMsr_unit());
				crpp.setItem_ctrl(bci.getItem_ctrl());//成本性态[0变动、1固定]
				crpp.setItem_prop(bci.getItem_prop());//责任属性[0不可控、1可控]			
			}		
			//本期产量,本年累计产量,同期产量,同期累计产量 【成本计算中间表】
			for (CmReportProdMiddle RPM : rpmList) {
				if (rcm.getPrd_num().equals(RPM.getPrd_num()) && rcm.getOrg_code().equals(RPM.getOrg_code())) {
					crpp.setWeight_value(RPM.getWeight_value());//本期产量
					crpp.setTotal_weight(RPM.getTotal_weight());//本年累计产量
					crpp.setSame_weight_value(RPM.getSame_weight_value());//同期产量
					crpp.setSame_total_weight(RPM.getSame_total_weight());//同期累计产量
					continue;
				}
			}			
			crpp.setConsume_value(rcm.getConsume_value());//本期实际用量 
			crpp.setAmount_value(rcm.getAmount_value());//本期实际成本
			crpp.setTotal_consume(rcm.getTotal_consume());//本年累计用量
			crpp.setTotal_amount(rcm.getTotal_amount());//本年累计成本
			crpp.setSame_total_consume(rcm.getSame_total_consume());//同期累计用量 
			crpp.setSame_total_amount(rcm.getSame_total_amount());//同期累计成本
			
			//本期单位用量 = 本期实际用量/本期实际产量
			if (crpp.getConsume_value()!=null && crpp.getWeight_value()!=null && crpp.getWeight_value()!=0) {
				crpp.setConsume_per(crpp.getConsume_value()/crpp.getWeight_value());				
			}
			//本期单位成本 = 本期实际成本/本期实际产量
			if (crpp.getAmount_value()!=null && crpp.getWeight_value()!=null && crpp.getWeight_value()!=0) {
				crpp.setAmount_per(crpp.getAmount_value()/crpp.getWeight_value());				
			}								
			//本年累计单位用量 = 本年累计用量/本年累计产量
			if (crpp.getTotal_consume()!=null && crpp.getTotal_weight()!=null && crpp.getTotal_weight()!=0) {
				crpp.setTotal_consume_per(crpp.getTotal_consume()/crpp.getTotal_weight());				
			}
			//本年累计单位成本 = 本年累计成本/本年累计产量
			if (crpp.getTotal_amount()!=null && crpp.getTotal_weight()!=null && crpp.getTotal_weight()!=0) {
				crpp.setTotal_amount_per(crpp.getTotal_amount()/crpp.getTotal_weight());				
			}			
			//同期累计单位用量 = 同期累计用量/同期累计产量			
			if (crpp.getSame_total_consume()!=null && crpp.getSame_total_weight()!=null && crpp.getSame_total_weight()!=0) {
				crpp.setSame_total_consume_per(crpp.getSame_total_consume()/crpp.getSame_total_weight());				
			}
			//同期累计单位成本 = 同期累计成本/同期累计产量
			if (crpp.getSame_total_amount()!=null && crpp.getSame_total_weight()!=null && crpp.getSame_total_weight()!=0) {
				crpp.setSame_total_amount_per(crpp.getSame_total_amount()/crpp.getSame_total_weight());				
			}											
			crpp.setMemo(null);
			crpp.setValid_flg("1");
			crpp.setAudit_flg("0");
			crpp.setCreate_man(userName);
			crpp.setCreate_time(now);
			this.getMapper(CmReportProdProcessMapper.class).insert(crpp);
			
			//反写投入产出的单位加工成本
			CostAcmProd cap = new CostAcmProd();
			cap.setOrg_code(crpp.getOrg_code());
			cap.setPrd_num(crpp.getPrd_num());
			if (crpp.getAmount_value()!=null && crpp.getWeight_value()!=null && crpp.getWeight_value()!=0) {
				cap.setProcess_price(crpp.getAmount_value()/crpp.getWeight_value());
			}
			this.getMapper(CostAcmProdMapper.class).doUpdateCostAcmProd(cap);
		}		
	}
	
}
