/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-05-08
 * 描述            ：物料属性配置表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Report.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseMachineMapper;
import market.Com.mapper.PbModelDbMapper;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Report.mapper.ReportProducePlanMapper;
import market.Mes.Report.service.ReportProducePlanService;
import market.Mes.Report.vo.ReportProducePlan;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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="ReportProducePlanService")
public class ReportProducePlanServiceImpl extends BaseServiceImpl<ReportProducePlan,Object> implements ReportProducePlanService {

	/**
	 * 查询
	 * @param cp
	 * @return
	 */
	 public ResultModel doQuaryReportProducePlan(CommonPage<ReportProducePlan> cp){
		 ResultModel rm = new ResultModel();
		 ReportProducePlan rpp = cp.getObject();
		 String condition = "";
		 if (StringUtils.isNotEmpty(rpp.getWrk_month())) {
			condition += " and wrk_month = '"+rpp.getWrk_month()+"'";
		 }
		 if (StringUtils.isNotEmpty(rpp.getMachine_code())) {
			condition += " and machine_code = '"+rpp.getMachine_code()+"'";
		}
		 List<ReportProducePlan> rppList = this.getMapper(ReportProducePlanMapper.class).findWithCondition(null, condition, "wrk_month,order_seq", 0, 0);
		 rm.setState("200");
		 rm.setData(rppList);
		 rm.setMsgInfo("查询成功");
		 return rm;
	 }
	 
	 /**
	  * 新增
	  * @param cp
	  * @return
	  */
	 public ResultModel doInsertReportProducePlan(CommonPage<ReportProducePlan> cp){
		 String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		 Date now = new Date();
		 ResultModel rm = new ResultModel();
		 ReportProducePlan rpp = cp.getObject();
		 //项目名称
		 //机组
		 ComBaseMachine cbm = this.getMapper(ComBaseMachineMapper.class).findById(rpp.getMachine_code());
		 if (cbm!=null) {			 
			 rpp.setMachine_name(cbm.getMachine_name());
			 if (cbm.getMachine_name().contains("退火")) {
				 rpp.setMachine_name("退火机组");
			 }
		 }
		 String condition = "wrk_month = '"+rpp.getWrk_month()+"' and machine_code = '"+rpp.getMachine_code()+"'";
		 int count = this.getMapper(ReportProducePlanMapper.class).countWithCondition(null, condition);
		 if (count > 0) {
			 rm.setState("500");
			 rm.setMsgInfo("月份【"+rpp.getWrk_month()+"】【"+rpp.getMachine_name()+"】已经存在计划量，无法继续新增");
			 return rm;
		 }
		 rpp.setCreate_man(userName);
		 rpp.setCreate_time(now);
		 this.getMapper(ReportProducePlanMapper.class).insert(rpp);
		 rm.setState("200");
		 rm.setMsgInfo("新增成功");
		 return rm;
	 }
	 
	 /**
	  * 修改基价
	  * @param cp  
	  * @return
	  */
	 @Transactional(value = "tm", rollbackFor = Exception.class)
	 public ResultModel doUpdateReportProducePlan(CommonPage<ReportProducePlan> cp){
		 String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		 String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		 ResultModel rm = new ResultModel();
		 List<ReportProducePlan> rppList = cp.getObjectList();
		 if (CollectionUtils.isNotEmpty(rppList)) {
			for (ReportProducePlan rpp : rppList) {
				String sql = "update REPORT_PRODUCE_PLAN t "
						+ "set t.plan_weight = "+rpp.getPlan_weight()+","
						+ "t.update_man = '"+userName+"',"
						+ "t.update_time = to_date('"+now+"','yyyy-MM-dd hh24:mi:ss')"
						+ "where t.wrk_month = '"+rpp.getWrk_month()+"' "
						+ "and t.machine_code = '"+rpp.getMachine_code()+"'";
				this.getMapper(PbModelDbMapper.class).doUpdateSql(sql);
			}
		 }
		 rm.setState("200");
		 rm.setMsgInfo("修改成功");
		 return rm;
	 }
	 
	 /**
	  * 删除
	  * @param cp
	  * @return
	  */
	 @Transactional(value = "tm", rollbackFor = Exception.class)
	 public ResultModel doDeleteReportProducePlan(CommonPage<ReportProducePlan> cp){
		 ResultModel rm = new ResultModel();
		 List<ReportProducePlan> rppList = cp.getObjectList();
		 if (CollectionUtils.isNotEmpty(rppList)) {
			 for (ReportProducePlan rpp : rppList) {
				 String sql = "delete from report_produce_plan t "
						 + "where t.wrk_month = '"+rpp.getWrk_month()+"' "
						 + "and t.machine_code = '"+rpp.getMachine_code()+"'";
				 this.getMapper(PbModelDbMapper.class).doDeleteSql(sql);
			}
		 }
		 rm.setState("200");
		 rm.setMsgInfo("删除成功");
		 return rm;
	 }
	 
	 /**
	  * 复制基价
	  * @param cp [{"memo1":"2020-09","memo2":"2020-10"}]  memo1：需要复制的月份  memo2：复制到哪个月份
	  * @return
	 * @throws Exception 
	  */
	 @Transactional(value = "tm", rollbackFor = Exception.class)
	 public ResultModel doCopyReportProducePlan(CommonPage<ReportProducePlan> cp) throws Exception{
		 String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		 Date now = new Date();
		 ResultModel rm = new ResultModel();
		 //1.1 判断复制到月份memo2是否存在数据
		 String condition = "wrk_month = '"+cp.getMemo2()+"'";
		 int count = this.getMapper(ReportProducePlanMapper.class).countWithCondition(null, condition);
		 if (count > 0) {
			throw new Exception("月份【"+cp.getMemo2()+"】已经存在基价数据，无法复制");
		 }
		 //1.2 判断复制来源月份memo1是否存在数据
		 condition = "wrk_month = '"+cp.getMemo1()+"'";
		 count = this.getMapper(ReportProducePlanMapper.class).countWithCondition(null, condition);
		 if (count <= 0) {
			throw new Exception("未找到月份【"+cp.getMemo1()+"】存在基价数据，无法复制");
		 }
		 ReportProducePlan r = new ReportProducePlan();
		 r.setWrk_month(cp.getMemo1());
		 List<ReportProducePlan> rppList = this.getMapper(ReportProducePlanMapper.class).find(r,0,0);
		 if (CollectionUtils.isNotEmpty(rppList)) {
			for (ReportProducePlan RBP : rppList) {
				ReportProducePlan rpp = new ReportProducePlan();
				rpp.setWrk_month(cp.getMemo2());
				rpp.setPlan_weight(RBP.getPlan_weight());
				rpp.setMachine_code(RBP.getMachine_code());
				rpp.setMachine_name(RBP.getMachine_name());
				rpp.setCreate_man(userName);
				rpp.setCreate_time(now);
				this.getMapper(ReportProducePlanMapper.class).insert(rpp);
			}
		 }
		 rm.setState("200");
		 rm.setMsgInfo("复制成功");
		 return rm;
	 }
	
}
