/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.CostAcmProdMapper;
import market.Cm.service.CmReportProdDetailService;
import market.Cm.vo.BaseFiscalPeriodD;
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;

@RemoteService(path="CmReportProdDetailService")
public class CmReportProdDetailServiceImpl extends BaseServiceImpl<CmReportProdDetail,Object> implements CmReportProdDetailService {

	/**
	 * 新增产量明细中间表数据
	 * @param cp object[prd_num成本期间]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertCmReportProdDetail(CommonPage<CmReportProdDetail> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		String msg = "生成产量明细中间表失败:";
		CmReportProdDetail rpd = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpd.getPrd_num());
		if (bfpd==null) {
			throw new Exception(msg+"成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportProdDetail RPD = new CmReportProdDetail();
		RPD.setPrd_num(rpd.getPrd_num());
		int count = this.getMapper(CmReportProdDetailMapper.class).count(RPD);
		if (count > 0) {
			throw new Exception(msg+"成本期间【"+rpd.getPrd_num()+"】的产量明细中间表已经存在数据");
		}
		
		//查询投入产出归集数据
		CostAcmProd cap = new CostAcmProd();
		cap.setPrd_num(rpd.getPrd_num());
		cap.setValid_flg("1");
		cap.setAudit_flg("1");
		List<CostAcmProd> capList = this.getMapper(CostAcmProdMapper.class).doQuaryCcapForCrpd(cap);
		if (capList==null || capList.size()<=0) {
			throw new Exception(msg+"未找到成本期间【"+rpd.getPrd_num()+"】已审核的投入产出归集数据");
		}
		//新增数据
		for (CostAcmProd CAP : capList) {
			CmReportProdDetail crpd = new CmReportProdDetail();
			crpd.setAcm_id(CAP.getAcm_id());//主键Id
			crpd.setItem_code(CAP.getItem_code());
			crpd.setItem_name(CAP.getItem_name());
			crpd.setOrg_code(CAP.getOrg_code());
			crpd.setOrg_name(CAP.getOrg_name());
			crpd.setPrd_num(CAP.getPrd_num());
			crpd.setItem_type(CAP.getItem_type());
			crpd.setItem_grade(CAP.getItem_grade());
			crpd.setMsr_unit(CAP.getMsr_unit());//计量单位			
			crpd.setProd_state(CAP.getProd_state());
			crpd.setItem_name2("直接材料");
			crpd.setItem_name3("原料及主料");
			crpd.setItem_name4(CAP.getInput_type());//A原料、B自制、C委托INPUT_TYPE
			crpd.setItem_name5(CAP.getDelivery_state_desc());//交货状态
			crpd.setItem_name6(CAP.getParent_item_name());//合金钢/弹簧钢
			crpd.setOrder_model(CAP.getOrder_model());//业务类型(A自制加工 B委托加工)	
			crpd.setConsume_value(CAP.getInput_wgt_value()==null?0:CAP.getInput_wgt_value());//本期实际用量
			crpd.setWeight_value(CAP.getOutput_wgt_value()==null?0:CAP.getOutput_wgt_value());//本期实际产量
			//本期单位用量(本期用量/本期产量)				
			if (crpd.getWeight_value()!=0) {
				crpd.setConsume_per(crpd.getConsume_value()/crpd.getWeight_value());
			}
			this.getMapper(CmReportProdDetailMapper.class).insert(crpd);			
		}
		rm.setMsgInfo("新增产量明细中间表成功");
		rm.setState("200");
		return rm;	
	}
	
}
