/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-08-07
 * 描述            ：各机组调度日志
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.anneal.impl;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.anneal.mapper.PlnSchedulingMchMapper;
import market.Mes.anneal.mapper.WorkReportTempMapper;
import market.Mes.anneal.service.PlnSchedulingMchService;
import market.Mes.anneal.vo.PlnSchedulingMch;
import market.Mes.anneal.vo.WorkReportTemp;

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="PlnSchedulingMchService")
public class PlnSchedulingMchServiceImpl extends BaseServiceImpl<PlnSchedulingMch,Object> implements PlnSchedulingMchService {
	
	/**
	 * 调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 ,大小纵剪 ,M0001,M0002
	 * 
	 * memo1:月份字符串   比如："2019-05"
	 * */
	public ResultModel doDiaoduQueryWeb_4mm(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb_4mm(oci.getObject().getMachine_code(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 ,M0003，3mm开平
	 * 
	 * memo1:月份字符串   比如："2019-05"
	 * */
	public ResultModel doDiaoduQueryWeb_3mm(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb_3mm(oci.getObject().getMachine_code(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 ,M0004   退火炉
	 * 
	 * memo1:月份字符串   比如："2019-05"
	 * */
	public ResultModel doDiaoduQueryWeb_anneal(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb_anneal(oci.getObject().getMachine_code(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 ,M0007   平整机
	 * 
	 * memo1:月份字符串   比如："2019-05"
	 * */
	public ResultModel doDiaoduQueryWeb_tpm(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb_tpm(oci.getObject().getMachine_code(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 ,M0008   轧机
	 * 
	 * memo1:月份字符串   比如："2019-05"
	 * */
	public ResultModel doDiaoduQueryWeb_rcm(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb_rcm(oci.getObject().getMachine_code(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 ,M0010   酸洗
	 * 
	 * memo1:月份字符串   比如："2019-05"
	 * */
	public ResultModel doDiaoduQueryWeb_suanxi(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb_suanxi(oci.getObject().getMachine_code(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	
	/**
	 * 各机组调度日志
	 * ----
	 * object:
	 * machine_code:机台代码 
	 * wrk_date:月份    2018-05
	 * 
	 * */
	public ResultModel doDiaoduQueryWeb(CommonPage<PlnSchedulingMch> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(PlnSchedulingMchMapper.class).doDiaoduQueryWeb(oci.getObject(),oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 调度日志
	 * 修改
	 * objectList:
	 * machine_code：机台号代码
	 * 。。。。.。。
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDiaoduUpdate(CommonPage<PlnSchedulingMch> oci) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			String vOprName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(vOprName==null){
				vOprName="";
			}
			if(oci.getObjectList()==null || oci.getObjectList().size()<=0){
				rm.setState("500");
				rm.setMsgInfo("请输入参数");
				return rm;
			}
			//this.getMapper(PlnSchedulingMchMapper.class).doDeleteJindu(oci.getObjectList().get(0),oci.getObjectList().get(0).getWrk_date().substring(0, 7));
			for(PlnSchedulingMch pscp:oci.getObjectList()){
				pscp.setCreate_time(pscp.getCreate_time());
				pscp.setCreate_opr(vOprName);
				
				PlnSchedulingMch pscp11=new PlnSchedulingMch();
				pscp11.setWrk_date(pscp.getWrk_date());
				pscp11.setWrk_ord(pscp.getWrk_ord());
				pscp11.setMachine_code(pscp.getMachine_code());
				List<PlnSchedulingMch> list = this.getMapper(PlnSchedulingMchMapper.class).find(pscp11,0,0);
				if(list!=null && list.size()>0){
					try{
						if(pscp.getWrk_jihua()!=null && pscp.getWrk_zhix()!=null){
							try{
								Integer wei=Integer.parseInt(pscp.getWrk_zhix())-Integer.parseInt(pscp.getWrk_jihua());
								pscp.setWrk_qian(wei+"");
							}catch(Exception e){
								e.printStackTrace();
							}
						}
						this.getMapper(PlnSchedulingMchMapper.class).update(pscp);
					}catch(Exception ex){
						
					}
				}else{
					if(pscp.getWrk_jihua()!=null && pscp.getWrk_zhix()!=null){
						try{
							Integer wei=Integer.parseInt(pscp.getWrk_zhix())-Integer.parseInt(pscp.getWrk_jihua());
							pscp.setWrk_qian(wei+"");
						}catch(Exception e){
							e.printStackTrace();
						}
					}
					this.getMapper(PlnSchedulingMchMapper.class).doDelete(pscp11,"");
					this.getMapper(PlnSchedulingMchMapper.class).insert(pscp);
				}
				
			}
			rm.setMsgInfo("保存成功！");
			rm.setState("200");
		}catch(Exception ex){
			throw new Exception(ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 日作业率，月作业率，年作业率
	 * @param oci
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryWorkTemp(CommonPage<WorkReportTemp> cp) throws Exception{
		ResultModel rm=new ResultModel();
		String day = cp.getMemo1();
		String month = day.substring(0,7);
		String year = day.substring(0,4);
		try{
			String sql = "select t.machine_code,cbm.machine_name, sum(t.param7)/sum(t.param6)*100 work_day, "
					+ "sum(t.param7_month)/sum(t.param6_month)*100 work_month, sum(t.param7_year)/sum(t.param6_year)*100 work_year "
					+ "from WORK_REPORT_TEMP t "
					+ "left join com_base_machine cbm on t.machine_code = cbm.machine_code "
					+ "where t.wrk_date = '"+day+"' and t.machine_code not in ('M0004') "
					+ "group by t.machine_code,cbm.machine_name"
					+ " union "
					+ "select t.machine_code,'退火炉宝信' machine_name, "    
					+" sum(t.param9) / sum(t.param8) * 100 work_day, "
					+"(select sum(t.param9) / sum(t.param8) * 100 from WORK_REPORT_TEMP t where t.wrk_date like '"+month+"%' and t.machine_code = 'M0004') work_month, "
					+"(select sum(t.param9) / sum(t.param8) * 100 from WORK_REPORT_TEMP t where t.wrk_date like '"+year+"%' and t.machine_code = 'M0004') work_year "
					+"from WORK_REPORT_TEMP t "
					+"where t.wrk_date = '"+day+"' and t.machine_code in ('M0004')"
					+ "group by t.machine_code";
			rm.setData(this.getMapper(WorkReportTempMapper.class).doQueryWorkTemp(sql));
			rm.setMsgInfo("查询成功！");
			rm.setState("200");
		}catch(Exception ex){
			throw new Exception(ex.getMessage());
		}
		return rm;
	}
	
	
}
