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

import market.Cm.mapper.BaseFiscalPeriodDMapper;
import market.Cm.mapper.CmReportProdCostMapper;
import market.Cm.mapper.CmReportProdDetailMapper;
import market.Cm.service.CmReportProdCostService;
import market.Cm.vo.BaseFiscalPeriodD;
import market.Cm.vo.CmReportProdCost;
import market.Cm.vo.CmReportProdDetail;
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="CmReportProdCostService")
public class CmReportProdCostServiceImpl extends BaseServiceImpl<CmReportProdCost,Object> implements CmReportProdCostService {
	
	/**
	 * 产品成本统计：查询功能
	 * @param cp object[选传prd_num,org_code,org_name,valid_flg,audit_flg等]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmReportProdCost(CommonPage<CmReportProdCost> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmReportProdCost rpc = cp.getObject();		
		try {
			List<CmReportProdCost> list = this.getMapper(CmReportProdCostMapper.class).doQueryCmReportProdCost(rpc);
			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 doInsertCmReportProdCostVersion1(CommonPage<CmReportProdCost> cp) throws Exception{
		ResultModel rm = new ResultModel();
		Date now = new Date();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		CmReportProdCost rpc = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpc.getPrd_num());
		if (bfpd==null) {
			throw new Exception("成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportProdCost RPC = new CmReportProdCost();
		RPC.setPrd_num(rpc.getPrd_num());
		int count = this.getMapper(CmReportProdCostMapper.class).count(RPC);
		if (count > 0) {
			throw new Exception("成本期间【"+rpc.getPrd_num()+"】的产品成本统计已经核算");
		}
		
		//查询【产量明细中间表】
		CmReportProdDetail rpd = new CmReportProdDetail();
		rpd.setPrd_num(rpc.getPrd_num());
		List<CmReportProdDetail> rpdList = this.getMapper(CmReportProdDetailMapper.class).doQuaryCrpdForCrpc(rpd);
		if (rpdList==null || rpdList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpc.getPrd_num()+"】的产量明细数据");
		}
		for (CmReportProdDetail RPD : rpdList) {
			CmReportProdCost crpc = new CmReportProdCost();
			crpc.setOrg_code(RPD.getOrg_code());
			crpc.setOrg_name(RPD.getOrg_name());
			crpc.setPrd_num(rpc.getPrd_num());
			crpc.setItem_code(RPD.getItem_code());//成本项目
			crpc.setItem_name(RPD.getItem_name());
			crpc.setObject_code(RPD.getObject_code());
			crpc.setObject_name(RPD.getObject_name());
			crpc.setParent_object_code(RPD.getParent_object_code());
			crpc.setParent_object_name(RPD.getParent_object_name());			 			
			crpc.setOrder_model(RPD.getOrder_model());//业务模式[A自制，B委托]
			crpc.setWeight_value(RPD.getWeight_value()==null?0:RPD.getWeight_value());//总产量
			crpc.setTotal_amount(RPD.getAmount_value()==null?0:RPD.getAmount_value());//总成本
			if (crpc.getWeight_value()!=0) {				
				//单位成本=总成本/总产量
				crpc.setAmount_per(crpc.getTotal_amount()/crpc.getWeight_value());
			}
			crpc.setProd_state(RPD.getProd_state());//产品状态
			crpc.setDelivery_state_code(null);//交货状态
			crpc.setDelivery_state_desc(RPD.getItem_name5());
			crpc.setMemo(null);
			crpc.setValid_flg("1");
			crpc.setAudit_flg("0");
			crpc.setCreate_man(userName);
			crpc.setCreate_time(now);
			this.getMapper(CmReportProdCostMapper.class).insert(crpc);
		}
		rm.setState("200");
		rm.setMsgInfo("产品成本统计核算成功");
		return rm;
	}
	
	/**
	 * 产品成本统计：审核及反审
	 * @param cp object[org_code成本组织 prd_num成本期间] memo1[1审核 0反审]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeCmReportProdCostAuditFlg(CommonPage<CmReportProdCost> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		CmReportProdCost rpc = cp.getObject();
		if (rpc.getOrg_code()==null || "".equals(rpc.getOrg_code())) {
			throw new Exception("请传入成本组织");
		}
		if (rpc.getPrd_num()==null || "".equals(rpc.getPrd_num())) {
			throw new Exception("请传入成本期间");
		}
		Date now = new Date();
		//通过成本期间和成本组织进行【审核】和【反审】
		CmReportProdCost RPC = new CmReportProdCost();
		RPC.setOrg_code(rpc.getOrg_code());
		RPC.setPrd_num(rpc.getPrd_num());
		RPC.setAudit_flg("1".equals(opr)?"1":"0");//
		List<CmReportProdCost> rpcList = this.getMapper(CmReportProdCostMapper.class).find(RPC, 0, 0);
		if (rpcList!=null && rpcList.size()>0) {
			throw new Exception("成本组织【"+rpc.getOrg_name()+"】成本期间【"+rpc.getPrd_num()+"】中存在"+("1".equals(opr)?"已审核":"未审核")+"的数据");
		}
		rpc.setAudit_flg(opr);
		if ("1".equals(opr)) {
			rpc.setAudit_man(userName);
			rpc.setAudit_time(now);			
		}
		this.getMapper(CmReportProdCostMapper.class).doUpdateCmReportProdCost(rpc);	
		rm.setState("200");
		rm.setMsgInfo("成本组织【"+rpc.getOrg_name()+"】成本期间【"+rpc.getPrd_num()+"】"+("0".equals(cp.getMemo1())?"反审":"审核") + "操作成功");
		return rm;
	}
	
	/**
	 * 产品成本统计：删除
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteCmReportProdCost(CommonPage<CmReportProdCost> cp) throws Exception{
		ResultModel rm = new ResultModel();
		CmReportProdCost rpc = cp.getObject();
		/*if (rpc.getOrg_code()==null || "".equals(rpc.getOrg_code())) {
			throw new Exception("请传入成本组织");
		}*/
		if (rpc.getPrd_num()==null || "".equals(rpc.getPrd_num())) {
			throw new Exception("请传入成本期间");
		}
		CmReportProdCost crpc = new CmReportProdCost();
		crpc.setPrd_num(rpc.getPrd_num());
		this.getMapper(CmReportProdCostMapper.class).doDeleteCmReportProdCost(crpc);		
		rm.setState("200");
		rm.setMsgInfo("删除产品成本统计成功");
		return rm;		
	}
	
	/**
	 * 产品成本统计：核算功能
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doInsertCmReportProdCost(CommonPage<CmReportProdCost> cp) throws Exception{
		ResultModel rm = new ResultModel();
		Date now = new Date();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		CmReportProdCost rpc = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpc.getPrd_num());
		if (bfpd==null) {
			throw new Exception("成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportProdCost RPC = new CmReportProdCost();
		RPC.setPrd_num(rpc.getPrd_num());
		int count = this.getMapper(CmReportProdCostMapper.class).count(RPC);
		if (count > 0) {
			throw new Exception("成本期间【"+rpc.getPrd_num()+"】的产品成本统计已经核算");
		}
		
		//查询【投入产出归集表】		
		CostAcmProd cap = new CostAcmProd();
		cap.setPrd_num(rpc.getPrd_num());
		List<CostAcmProd> capList = this.getMapper(CmReportProdCostMapper.class).doQuaryCcapForCrpc(cap);
		if (capList==null || capList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpc.getPrd_num()+"】的【投入产出归集】数据");
		}
		for (CostAcmProd ccap : capList) {
			CmReportProdCost crpc = new CmReportProdCost();
			crpc.setOrg_code(ccap.getOrg_code());
			crpc.setOrg_name(ccap.getOrg_name());
			crpc.setPrd_num(ccap.getPrd_num());
			crpc.setItem_code(ccap.getItem_code());//成本项目
			crpc.setItem_name(ccap.getItem_name());
			crpc.setObject_code(ccap.getObject_code());
			crpc.setObject_name(ccap.getObject_name());
			crpc.setParent_object_code(ccap.getParent_item_code());
			crpc.setParent_object_name(ccap.getParent_item_name());			 			
			crpc.setOrder_model(ccap.getOrder_model());//业务模式[A自制，B委托]
			crpc.setProd_state(ccap.getProd_state());//产品状态
			crpc.setDelivery_state_code(ccap.getDelivery_state_code());//交货状态
			crpc.setDelivery_state_desc(ccap.getDelivery_state_desc());
			crpc.setWeight_value(ccap.getOutput_wgt_value()==null?0:ccap.getOutput_wgt_value());//总产量
			crpc.setTotal_amount(ccap.getAmount_value()==null?0:ccap.getAmount_value());//总成本
			if (crpc.getWeight_value()!=0) {				
				//单位成本=总成本/总产量
				crpc.setAmount_per(crpc.getTotal_amount()/crpc.getWeight_value());
			}
			crpc.setMemo(null);
			crpc.setValid_flg("1");
			crpc.setAudit_flg("0");
			crpc.setCreate_man(userName);
			crpc.setCreate_time(now);
			this.getMapper(CmReportProdCostMapper.class).insert(crpc);
		}
		rm.setState("200");
		rm.setMsgInfo("产品成本统计核算成功");
		return rm;
	}
	
	
}
