/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.CmReportProdMiddleMapper;
import market.Cm.mapper.CmReportProdTotalMapper;
import market.Cm.service.CmReportProdTotalService;
import market.Cm.vo.BaseCostItem;
import market.Cm.vo.BaseFiscalPeriodD;
import market.Cm.vo.CmReportProdMiddle;
import market.Cm.vo.CmReportProdTotal;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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

@RemoteService(path="CmReportProdTotalService")
public class CmReportProdTotalServiceImpl extends BaseServiceImpl<CmReportProdTotal,Object> implements CmReportProdTotalService {
	
	/**
	 * 生产成本统计：查询功能
	 * @param cp object[选传 item_code,prd_num,org_code,org_name,valid_flg,audit_flg等]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmReportProdTotal(CommonPage<CmReportProdTotal> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmReportProdTotal rpt = cp.getObject();
		try {
			String swhere = "";			
			if (rpt.getOrg_code()!=null && !"".equals(rpt.getOrg_code())) {
				swhere+=" and org_code = '"+rpt.getOrg_code()+"'";//成本组织代码
			}
			if (rpt.getOrg_name()!=null && !"".equals(rpt.getOrg_name())) {
				swhere+=" and org_name like '%"+rpt.getOrg_name()+"%'";//成本组织
			}
			if (rpt.getPrd_num()!=null && !"".equals(rpt.getPrd_num())) {
				swhere+=" and prd_num like '%"+rpt.getPrd_num()+"%'";//成本期间
			}
			 
			if (rpt.getOrder_model()!=null && !"".equals(rpt.getOrder_model())) {
				swhere+=" and order_model = '"+rpt.getOrder_model()+"'";//成本期间
			}
			if (rpt.getValid_flg()!=null && !"".equals(rpt.getValid_flg())) {
				swhere+=" and valid_flg = '"+rpt.getValid_flg()+"'";//是否有效
			}
			if (rpt.getAudit_flg()!=null && !"".equals(rpt.getAudit_flg())) {
				swhere += " and audit_flg = '"+rpt.getAudit_flg()+"'";//是否审核
			}
			List<CmReportProdTotal> list = this.getMapper(CmReportProdTotalMapper.class).findWithCondition(null, swhere, "prd_num desc,org_code", 0, 0);				
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}

	/**
	 * 生产成本统计：查询功能(合计)
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmReportProdTotalGB(CommonPage<CmReportProdTotal> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmReportProdTotal rpt = cp.getObject();
		try {			
			List<CmReportProdTotal> list = this.getMapper(CmReportProdTotalMapper.class).doQueryCmReportProdTotalGB(rpt);				
			rm.setState("200");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}

	
	/**
	 * 生产成本统计：核算功能
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doInsertCmReportProdTotal(CommonPage<CmReportProdTotal> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		CmReportProdTotal rpt = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpt.getPrd_num());
		if (bfpd==null) {
			throw new Exception("成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportProdTotal RPT = new CmReportProdTotal();
		RPT.setPrd_num(rpt.getPrd_num());
		int count = this.getMapper(CmReportProdTotalMapper.class).count(RPT);
		if (count > 0) {
			throw new Exception("成本期间【"+rpt.getPrd_num()+"】的生产成本统计已经核算");
		}
		//查询所有的有效的成本项目
		BaseCostItem bci = new BaseCostItem();
		bci.setValid_flg("1");
		List<BaseCostItem> bciList = this.getMapper(BaseCostItemMapper.class).find(bci, 0, 0);
		if (bciList==null || bciList.size()<=0) {
			throw new Exception("未找到有效的成本组织");
		}
		//2.查询【产量计算中间表】的实际产量
		CmReportProdMiddle rpm = new CmReportProdMiddle();
		rpm.setPrd_num(rpt.getPrd_num());//成本期间
		List<CmReportProdMiddle> crpmList = this.getMapper(CmReportProdMiddleMapper.class).doQuaryCrpmForCrpt(rpm);
		if (crpmList==null || crpmList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpm.getPrd_num()+"】的产量数据");
		}
		for (CmReportProdMiddle RPM : crpmList) {			
			CmReportProdTotal crpt = new CmReportProdTotal();		
			crpt.setOrg_code(RPM.getOrg_code());
			crpt.setOrg_name(RPM.getOrg_name());
			crpt.setPrd_num(rpt.getPrd_num());
			crpt.setOrder_model(RPM.getOrder_model());
			crpt.setWeight_value(RPM.getWeight_value());//本期实际产量
			crpt.setAmount_value(RPM.getAmount_value());//本期实际成本
			crpt.setTotal_weight(RPM.getTotal_weight());//本年累计产量
			crpt.setTotal_amount(RPM.getTotal_amount());//本年累计成本			
			crpt.setMemo(null);
			crpt.setValid_flg("1");
			crpt.setAudit_flg("0");
			crpt.setCreate_man(userName);
			crpt.setCreate_time(now);
			this.getMapper(CmReportProdTotalMapper.class).insert(crpt);
		}
		rm.setState("200");
		rm.setMsgInfo("生产成本统计核算成功");
		return rm;
	}
	
	/**
	 * 生产成本统计：审核及反审
	 * @param cp object[org_code成本组织 prd_num成本期间] memo1[1审核 0反审]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doChangeCmReportProdTotalAuditFlg(CommonPage<CmReportProdTotal> cp) throws Exception{
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		String opr = cp.getMemo1();
		CmReportProdTotal rpt = cp.getObject();
		if (rpt.getPrd_num()==null || "".equals(rpt.getPrd_num())) {
			throw new Exception("请传入成本期间");
		}
		Date now = new Date();
		//通过成本期间和成本组织进行【审核】和【反审】
		CmReportProdTotal RPT = new CmReportProdTotal();
		RPT.setPrd_num(rpt.getPrd_num());
		RPT.setAudit_flg("1".equals(opr)?"1":"0");//
		List<CmReportProdTotal> rptList = this.getMapper(CmReportProdTotalMapper.class).find(RPT, 0, 0);
		if (rptList!=null && rptList.size()>0) {
			throw new Exception("成本期间【"+rpt.getPrd_num()+"】中存在"+("1".equals(opr)?"已审核":"未审核")+"的数据");
		}
		rpt.setAudit_flg(opr);
		rpt.setAudit_man(userName);
		rpt.setAudit_time(now);
		this.getMapper(CmReportProdTotalMapper.class).doUpdateCmReportProdTotal(rpt);	
		rm.setState("200");
		rm.setMsgInfo("成本期间【"+rpt.getPrd_num()+"】"+("0".equals(cp.getMemo1())?"反审":"审核") + "操作成功");
		return rm;
	}
	
	/**
	 * 生产成本统计：删除
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteCmReportProdTotal(CommonPage<CmReportProdTotal> cp) throws Exception{
		ResultModel rm = new ResultModel();
		CmReportProdTotal rpt = cp.getObject();
		/*if (rpt.getOrg_code()==null || "".equals(rpt.getOrg_code())) {
			throw new Exception("请传入成本组织");
		}*/
		if (rpt.getPrd_num()==null || "".equals(rpt.getPrd_num())) {
			throw new Exception("请传入成本期间");
		}
		//判断是否已经审核(已审核的数据不能进行删除操作)
		/*CmReportProdTotal RPT = new CmReportProdTotal();
		//RPT.setOrg_code(rpt.getOrg_code());
		RPT.setPrd_num(rpt.getPrd_num());
		RPT.setAudit_flg("1");
		List<CmReportProdTotal> rptList = this.getMapper(CmReportProdTotalMapper.class).find(RPT, 0, 0);
		if (rptList!=null && rptList.size()>0) {
			throw new Exception("成本期间【"+rpt.getPrd_num()+"】中存在已审核的数据，无法删除");
			//throw new Exception("成本组织【"+rpt.getOrg_name()+"】成本期间【"+rpt.getPrd_num()+"】中存在已审核的数据，无法删除");			
		}*/
		this.getMapper(CmReportProdTotalMapper.class).doDeleteCmReportProdTotal(rpt);		
		rm.setState("200");
		rm.setMsgInfo("删除生产成本统计成功");
		return rm;
	}
	
}
