/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-07-15
 * 描述            ：资材备件汇总表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Cm.impl;
import java.util.Date;
import java.util.List;

import market.Cm.mapper.BaseFiscalPeriodDMapper;
import market.Cm.mapper.CmReportMatAcmMapper;
import market.Cm.mapper.CostAcmMatMapper;
import market.Cm.service.CmReportMatAcmService;
import market.Cm.vo.BaseFiscalPeriodD;
import market.Cm.vo.CmReportMatAcm;
import market.Cm.vo.CostAcmMat;
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="CmReportMatAcmService")
public class CmReportMatAcmServiceImpl extends BaseServiceImpl<CmReportMatAcm,Object> implements CmReportMatAcmService {
	
	/**
	 * 资材备件汇总表：查询
	 * @param cp  object[prd_num成本期间]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryCmReportMatAcm(CommonPage<CmReportMatAcm> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		CmReportMatAcm crma = cp.getObject();
		try {
			String swhere = "";
			if (crma.getPrd_num()!=null && !"".equals(crma.getPrd_num())) {
				swhere+=" and prd_num like '%"+crma.getPrd_num()+"%'";//成本期间
			}
			List<CmReportMatAcm> list = this.getMapper(CmReportMatAcmMapper.class).findWithCondition(null, swhere, "org_code", 0,0);					
			rm.setState("200");
			rm.setMsgInfo("查询成功");
			rm.setData(list);
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
			
	/**
	 * 资材备件汇总表：核算
	 * @param cp object[prd_num成本期间]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertCmReportMatAcm(CommonPage<CmReportMatAcm> cp) throws Exception{
		ResultModel rm = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		Date now = new Date();
		CmReportMatAcm rpc = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpc.getPrd_num());
		if (bfpd==null) {
			throw new Exception("成本期间传递有误");
		}
		//判断当前成本期间是否核算
		CmReportMatAcm RPC = new CmReportMatAcm();
		RPC.setPrd_num(rpc.getPrd_num());
		int count = this.getMapper(CmReportMatAcmMapper.class).count(RPC);
		if (count > 0) {
			throw new Exception("成本期间【"+rpc.getPrd_num()+"】的资材备件汇总表已经存在数据");
		}
		//根据成本组织,查询【资材备件归集表】
		CostAcmMat cam = new CostAcmMat();
		cam.setPrd_num(rpc.getPrd_num());
		cam.setValid_flg("1");
		cam.setAudit_flg("1");		
		List<CostAcmMat> bciList = this.getMapper(CostAcmMatMapper.class).doQauryCostAcmMatForCrma(cam);		
		if (bciList==null || bciList.size()<=0) {
			throw new Exception("未找到成本期间【"+rpc.getPrd_num()+"】的已审核的资材备件归集数据");
		}
		//写入数据到【资材备件汇总表】
		for (CostAcmMat CAM : bciList) {
			CmReportMatAcm crma = new CmReportMatAcm();
			crma.setItem_code(CAM.getItem_code());
			crma.setItem_name(CAM.getItem_name());
			crma.setOrg_code(CAM.getOrg_code());
			crma.setOrg_name(CAM.getOrg_name());
			crma.setPrd_num(rpc.getPrd_num());
			crma.setAmount_value(CAM.getAmount_value());
			//crma.setMemo(CAM.getMemo());
			crma.setValid_flg("1");
			//crma.setAudit_flg(CAM.getAudit_flg());
			//crma.setAudit_man(CAM.getAudit_man());
			//crma.setAudit_time(CAM.getAudit_time());
			crma.setCreate_man(userName);
			crma.setCreate_time(now);
			//crma.setUpdate_man(CAM.getUpdate_man());
			//crma.setUpdate_time(CAM.getUpdate_time());
			this.getMapper(CmReportMatAcmMapper.class).insert(crma);
		}
		rm.setState("200");
		rm.setMsgInfo("资材备件汇总成功");
		return rm;
	}
	
	/**
	 * 撤销核算 
	 * @param cp object[prd_num 成本期间]
	 * @return
	 * @throws Exception
	 */
	public ResultModel doDeleteCmReportMatAcm(CommonPage<CmReportMatAcm> cp) throws Exception{
		ResultModel rm = new ResultModel();		
		CmReportMatAcm rpc = cp.getObject();
		BaseFiscalPeriodD bfpd = this.getMapper(BaseFiscalPeriodDMapper.class).findById(rpc.getPrd_num());
		if (bfpd==null) {
			throw new Exception("成本期间传递有误");
		}
		this.getMapper(CmReportMatAcmMapper.class).doDeleteCmReportMatAcm(rpc);		
		rm.setState("200");
		rm.setMsgInfo("撤销核算资材备件汇总成功");
		return rm;
	}
	
}
