/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2020-12-24
 * 描述            ：金属平衡报表-填报
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Report.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.mapper.ComBaseMachineMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Report.mapper.ReportMetalBalanceMapper;
import market.Mes.Report.service.ReportMetalBalanceService;
import market.Mes.Report.vo.ReportMetalBalance;

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="ReportMetalBalanceService")
public class ReportMetalBalanceServiceImpl extends BaseServiceImpl<ReportMetalBalance,Object> implements ReportMetalBalanceService {

	/**
	 * 查询
	 * @param cp
	 * @return
	 */
	 public ResultModel doQuaryReportMetalBalance(CommonPage<ReportMetalBalance> cp){
		 ResultModel rm = new ResultModel();
		 ReportMetalBalance rmb = cp.getObject();
		 String condition = "";
		 if (StringUtils.isNotEmpty(rmb.getWrk_month())) {
			condition += " and wrk_month = '"+rmb.getWrk_month()+"'";
		 }
		 if (StringUtils.isNotEmpty(rmb.getMachine_code())) {
			condition += " and machine_code = '"+rmb.getMachine_code()+"'";
		}
		 List<ReportMetalBalance> rmbList = this.getMapper(ReportMetalBalanceMapper.class).findWithCondition(null, condition, "wrk_month,machine_code,item_code", 0, 0);
		 rm.setState("200");
		 rm.setData(rmbList);
		 rm.setMsgInfo("查询成功");
		 return rm;
	 }
	 
	 /**
	  * 新增
	  * @param cp
	  * @return
	  */
	 public ResultModel doInsertReportMetalBalance(CommonPage<ReportMetalBalance> cp){
		 String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		 Date now = new Date();
		 ResultModel rm = new ResultModel();
		 ReportMetalBalance rmb = cp.getObject();
		 //项目名称
		 ComBaseInfo cbi = this.getMapper(ComBaseInfoMapper.class).findById(rmb.getItem_code());
		 if (cbi!=null) {
			rmb.setItem_name(cbi.getBase_name());
		 }
		 //机组
		 ComBaseMachine cbm = this.getMapper(ComBaseMachineMapper.class).findById(rmb.getMachine_code());
		 if (cbm!=null) {
			 rmb.setMachine_name(cbm.getMachine_name());
			 if (cbm.getMachine_name().contains("退火")) {
				 rmb.setMachine_name("退火机组");
			 }
		 }
		 String condition = "wrk_month = '"+rmb.getWrk_month()+"' and machine_code = '"+rmb.getMachine_code()+"' and item_code = '"+rmb.getItem_code()+"'";
		 int count = this.getMapper(ReportMetalBalanceMapper.class).countWithCondition(null, condition);
		 if (count > 0) {
			 rm.setState("500");
			 rm.setMsgInfo("月份【"+rmb.getWrk_month()+"】【"+rmb.getMachine_name()+"】【"+rmb.getItem_name()+"】已经存在录入值，无法继续新增");
			 return rm;
		 }
		 rmb.setCreate_man(userName);
		 rmb.setCreate_time(now);
		 this.getMapper(ReportMetalBalanceMapper.class).insert(rmb);
		 rm.setState("200");
		 rm.setMsgInfo("新增成功");
		 return rm;
	 }
	 
	 /**
	  * 修改基价
	  * @param cp  
	  * @return
	  */
	 @Transactional(value = "tm", rollbackFor = Exception.class)
	 public ResultModel doUpdateReportMetalBalance(CommonPage<ReportMetalBalance> 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<ReportMetalBalance> rmbList = cp.getObjectList();
		 if (CollectionUtils.isNotEmpty(rmbList)) {
			for (ReportMetalBalance rmb : rmbList) {
				String sql = "update report_metal_balance t "
						+ "set t.base_value = "+rmb.getBase_value()+","
						+ "t.update_man = '"+userName+"',"
						+ "t.update_time = to_date('"+now+"','yyyy-MM-dd hh24:mi:ss')"
						+ "where t.wrk_month = '"+rmb.getWrk_month()+"' "
						+ "and t.item_code = '"+rmb.getItem_code()+"' "
						+ "and t.machine_code = '"+rmb.getMachine_code()+"'";
				this.getMapper(ReportMetalBalanceMapper.class).doUpdateReportMetalBalance(sql);
			}
		 }
		 rm.setState("200");
		 rm.setMsgInfo("修改成功");
		 return rm;
	 }
	 
	 /**
	  * 删除
	  * @param cp
	  * @return
	  */
	 @Transactional(value = "tm", rollbackFor = Exception.class)
	 public ResultModel doDeleteReportMetalBalance(CommonPage<ReportMetalBalance> cp){
		 ResultModel rm = new ResultModel();
		 List<ReportMetalBalance> rmbList = cp.getObjectList();
		 if (CollectionUtils.isNotEmpty(rmbList)) {
			 for (ReportMetalBalance rmb : rmbList) {
				 String sql = "delete from report_metal_balance t "
						 + "where t.wrk_month = '"+rmb.getWrk_month()+"' "
						 + "and t.item_code = '"+rmb.getItem_code()+"' "
						 + "and t.machine_code = '"+rmb.getMachine_code()+"'";
				 this.getMapper(ReportMetalBalanceMapper.class).doDeleteReportMetalBalance(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 doCopyReportMetalBalance(CommonPage<ReportMetalBalance> 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(ReportMetalBalanceMapper.class).countWithCondition(null, condition);
		 if (count > 0) {
			throw new Exception("月份【"+cp.getMemo2()+"】已经存在录入值，无法复制");
		 }
		 //1.2 判断复制来源月份memo1是否存在数据
		 condition = "wrk_month = '"+cp.getMemo1()+"'";
		 count = this.getMapper(ReportMetalBalanceMapper.class).countWithCondition(null, condition);
		 if (count <= 0) {
			throw new Exception("未找到月份【"+cp.getMemo1()+"】存在录入值，无法复制");
		 }
		 ReportMetalBalance r = new ReportMetalBalance();
		 r.setWrk_month(cp.getMemo1());
		 List<ReportMetalBalance> rmbList = this.getMapper(ReportMetalBalanceMapper.class).find(r,0,0);
		 if (CollectionUtils.isNotEmpty(rmbList)) {
			for (ReportMetalBalance RMB : rmbList) {
				ReportMetalBalance rmb = new ReportMetalBalance();
				rmb.setWrk_month(cp.getMemo2());
				rmb.setItem_code(RMB.getItem_code());
				rmb.setItem_name(RMB.getItem_name());
				rmb.setBase_value(RMB.getBase_value());
				rmb.setMachine_code(RMB.getMachine_code());
				rmb.setMachine_name(RMB.getMachine_name());
				rmb.setCreate_man(userName);
				rmb.setCreate_time(now);
				this.getMapper(ReportMetalBalanceMapper.class).insert(rmb);
			}
		 }
		 rm.setState("200");
		 rm.setMsgInfo("复制成功");
		 return rm;
	 }
	 
	 
	 
	
}
