/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-11-29
 * 描述            ：工器具-轧辊配辊主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Dev.impl;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Dev.mapper.DevRollerInfoMapper;
import market.Mes.Dev.mapper.DevRollerPairDMapper;
import market.Mes.Dev.mapper.DevRollerPairMMapper;
import market.Mes.Dev.service.DevRollerPairMService;
import market.Mes.Dev.vo.DevRollerInfo;
import market.Mes.Dev.vo.DevRollerPairD;
import market.Mes.Dev.vo.DevRollerPairM;
import market.Mes.Rolling.Performance.mapper.MesRcmMMapper;
import market.Mes.Rolling.Performance.vo.MesRcmM;

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="DevRollerPairMService")
public class DevRollerPairMServiceImpl extends BaseServiceImpl<DevRollerPairM,Object> implements DevRollerPairMService {
	
	/**
	 * 根据轧辊类型及其他条件查询页面主表
	 * object:
	 * roller_type:选中节点表示的轧辊类型   轧辊类型（W：工作辊、B：支承辊、I：中间辊）
	 * machine_code:机台代码
	 * memo1:配辊日期开始        比如：2018-11-27 00:00:00
	 * memo2:配辊日期结束        比如：2018-12-04 23:59:59
	 * memo3:机架号
	 * memo4:轧辊编号
	 * */
	public ResultModel doQueryWeb(CommonPage<DevRollerPairM> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(DevRollerPairMMapper.class).doQueryWeb(oci.getObject(),oci.getMemo1(),oci.getMemo2(),oci.getMemo3(),oci.getMemo4()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 轧辊使用管理
	 * 根据轧辊类型及其他条件查询页面主表
	 * object:
	 * roller_type:选中节点表示的轧辊类型   轧辊类型（W：工作辊、B：支承辊、I：中间辊）
	 * machine_code:机台代码
	 * status:【状态（1：配对、2：上机、3：下机、0：拆卸）】     下机+拆卸时，传"30"
	 * memo1:下机日期开始        比如：2018-11-27 00:00:00
	 * memo2:下机日期结束        比如：2018-12-04 23:59:59
	 * memo3:机架号
	 * memo4:轧辊编号
	 * */
	public ResultModel doQueryRollerMWeb(CommonPage<DevRollerPairM> oci){
		ResultModel rm=new ResultModel();
		try{
			if(oci.getObject()==null){
				rm.setState("500");
				rm.setMsgInfo("请输入参数");
				return rm;
			}
			String sWhere=" and 1=1 ";
			if("1".equals(oci.getObject().getStatus())){//
				sWhere+=" and t.status='1' ";
			}else if("2".equals(oci.getObject().getStatus())){//
				sWhere+=" and t.status='2' ";
			}else if("30".equals(oci.getObject().getStatus())){//
				sWhere+=" and (t.status='3' or t.status='0') ";
			}
			rm.setData(this.getMapper(DevRollerPairMMapper.class).doQueryRollerMWeb(oci.getObject(),oci.getMemo1(),oci.getMemo2(),oci.getMemo3(),oci.getMemo4(),sWhere));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 动作处理_上机按钮
	 * object:
	 * mount_time:上机时间    比如：2018-11-27 09:11:43
	 * mount_reason_code：上机原因代码（390203%）
	 * mount_reason_desc：上机原因描述
	 * stand_id:机架号
	 * mount_memo：上机备注
	 * 
	 * memo1:配辊记录号
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doRollerPairUpWeb(CommonPage<DevRollerPairM> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(userName==null){
				userName="";
			}
			String wrk_ord = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
			String wrk_grp = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);
			//String wrk_date = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_TIME);
			if(wrk_ord==null){
				wrk_ord=sum.getObject()==null?new DevRollerPairM().getMount_ord():sum.getObject().getMount_ord();
			}
			if(wrk_grp==null){
				wrk_grp=sum.getObject()==null?new DevRollerPairM().getMount_grp():sum.getObject().getMount_grp();
			}
			
			DevRollerPairM drpm_view = sum.getObject()==null?new DevRollerPairM():sum.getObject();
			
			if(sum.getMemo1()==null || "".equals(sum.getMemo1())){
				rm.setState("500");
				rm.setMsgInfo("未指定配辊记录号！");
				return rm;
			}
			Integer v_cnt=this.getMapper(DevRollerPairMMapper.class).countWithCondition(null, "serial_no='"+sum.getMemo1()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 配辊记录不存在！");
				return rm;
			}
			List<DevRollerPairM> ldbgg = this.getMapper(DevRollerPairMMapper.class).findWithCondition(null, "serial_no='"+sum.getMemo1()+"'", null, 0, 0);
			DevRollerPairM v_rec_grd = ldbgg.get(0);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			if("2".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已上机，不可重复操作！");
				return rm;
			}else if("3".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已下机，不允许此操作！");
				return rm;
			}else if("0".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已拆卸，不允许此操作！");
				return rm;
			}
			if(!"1".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("只有配对状态的数据允许上机！");
				return rm;
			}
			
			if(v_rec_grd.getT_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("上辊轧辊编号为空，信息出错！");
				return rm;
			}
			if(v_rec_grd.getB_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("下辊轧辊编号为空，信息出错！");
				return rm;
			}
			
			//轧辊状态（0：可配辊、1：待修磨、2：修磨中、3：已配辊、4：在线、5：报废）
			DevRollerInfo dri_t=new DevRollerInfo();
			dri_t.setRoller_no(v_rec_grd.getT_roller_no());
			dri_t.setStatus("4");
			dri_t.setStand_id(drpm_view.getStand_id());
			this.getMapper(DevRollerInfoMapper.class).update(dri_t);
			DevRollerInfo dri_b=new DevRollerInfo();
			dri_b.setRoller_no(v_rec_grd.getB_roller_no());
			dri_b.setStatus("4");
			dri_b.setStand_id(drpm_view.getStand_id());
			this.getMapper(DevRollerInfoMapper.class).update(dri_b);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			DevRollerPairM drpm=new DevRollerPairM();
			drpm.setSerial_no(sum.getMemo1());
			drpm.setMount_ord(wrk_ord);
			drpm.setMount_grp(wrk_grp);
			drpm.setMount_reason_code(drpm_view.getMount_reason_code());
			drpm.setMount_reason_desc(drpm_view.getMount_reason_desc());
			/*if (condition) {
				drpm.setMount_reason_code("39020303");
				drpm.setMount_reason_desc("补录装辊");
			}*/
			drpm.setMount_memo(drpm_view.getMount_memo());
			drpm.setStand_id(drpm_view.getStand_id());
			drpm.setMount_time(drpm_view.getMount_time());
			drpm.setStatus("2");
			drpm.setUpdate_opr(userName);
			drpm.setUpdate_time(new Date());
			
			this.getMapper(DevRollerPairMMapper.class).update(drpm);
			
			//??？此时需要更新子表和轧辊基础信息表的机架号吗？？？
			
			DevRollerPairD t_drpd=new DevRollerPairD();
			t_drpd.setSerial_no(v_rec_grd.getSerial_no());
			//轧辊位置（T：上辊、B：下辊）
			t_drpd.setRoller_position("T");
			t_drpd.setStand_id(drpm_view.getStand_id());
			t_drpd.setUpdate_opr(userName);
			t_drpd.setUpdate_time(new Date());
			this.getMapper(DevRollerPairDMapper.class).update(t_drpd);
			
			DevRollerPairD b_drpd=new DevRollerPairD();
			b_drpd.setSerial_no(v_rec_grd.getSerial_no());
			//轧辊位置（T：上辊、B：下辊）
			b_drpd.setRoller_position("B");
			b_drpd.setStand_id(drpm_view.getStand_id());
			b_drpd.setUpdate_opr(userName);
			b_drpd.setUpdate_time(new Date());
			this.getMapper(DevRollerPairDMapper.class).update(b_drpd);
			
			//提交事务
			rm.setMsgInfo("上机成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			throw new Exception("上机失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 动作处理_撤销上机按钮
	 * memo1:配辊记录号
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doRollerPairUpCancelWeb(CommonPage<DevRollerPairM> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(userName==null){
				userName="";
			}
			if(sum.getMemo1()==null || "".equals(sum.getMemo1())){
				rm.setState("500");
				rm.setMsgInfo("未指定配辊记录号！");
				return rm;
			}
			Integer v_cnt=this.getMapper(DevRollerPairMMapper.class).countWithCondition(null, "serial_no='"+sum.getMemo1()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 配辊记录不存在！");
				return rm;
			}
			List<DevRollerPairM> ldbgg = this.getMapper(DevRollerPairMMapper.class).findWithCondition(null, "serial_no='"+sum.getMemo1()+"'", null, 0, 0);
			DevRollerPairM v_rec_grd = ldbgg.get(0);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			if("3".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已下机，不允许此操作！");
				return rm;
			}else if("0".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已拆卸，不允许此操作！");
				return rm;
			}else if("1".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 未进行上机操作，不允许此操作！");
				return rm;
			}
			if(!"2".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("只有上机状态的数据允许撤销上机！");
				return rm;
			}
			
			if(v_rec_grd.getT_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("上辊轧辊编号为空，信息出错！");
				return rm;
			}
			if(v_rec_grd.getB_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("下辊轧辊编号为空，信息出错！");
				return rm;
			}
			List<DevRollerInfo> b_dri_list=this.getMapper(DevRollerInfoMapper.class).findWithCondition(null, "roller_no='"+v_rec_grd.getB_roller_no()+"'", null, 0, 0);
			if(b_dri_list==null || b_dri_list.size()<=0){
				rm.setState("500");
				rm.setMsgInfo("轧辊编号{"+sum.getMemo3()+"} 系统中不存在此轧辊编号！");
				return rm;
			}
			
			
			
			//轧辊状态（0：可配辊、1：待修磨、2：修磨中、3：已配辊、4：在线、5：报废）
			DevRollerInfo dri_t=new DevRollerInfo();
			dri_t.setRoller_no(v_rec_grd.getT_roller_no());
			dri_t.setStatus("3");
			dri_t.setStand_id("");
			this.getMapper(DevRollerInfoMapper.class).update(dri_t);
			DevRollerInfo dri_b=new DevRollerInfo();
			dri_b.setRoller_no(v_rec_grd.getB_roller_no());
			dri_b.setStatus("3");
			dri_b.setStand_id("");
			this.getMapper(DevRollerInfoMapper.class).update(dri_b);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			DevRollerPairM drpm=new DevRollerPairM();
			drpm.setSerial_no(sum.getMemo1());
			drpm.setMount_ord(null);
			drpm.setMount_grp(null);
			drpm.setMount_reason_code(null);
			drpm.setMount_reason_desc(null);
			drpm.setMount_memo(null);
			drpm.setStand_id(b_dri_list.get(0).getStand_id());//？？？
			drpm.setMount_time(null);
			drpm.setStatus("1");
			drpm.setUpdate_opr(userName);
			drpm.setUpdate_time(new Date());
			this.getMapper(DevRollerPairMMapper.class).update(drpm);
			
			
			DevRollerPairD t_drpd=new DevRollerPairD();
			t_drpd.setSerial_no(v_rec_grd.getSerial_no());
			//轧辊位置（T：上辊、B：下辊）
			t_drpd.setRoller_position("T");
			t_drpd.setUpdate_opr(userName);
			t_drpd.setUpdate_time(new Date());
			t_drpd.setStand_id("");
			this.getMapper(DevRollerPairDMapper.class).update(t_drpd);
			
			DevRollerPairD b_drpd=new DevRollerPairD();
			b_drpd.setSerial_no(v_rec_grd.getSerial_no());
			//轧辊位置（T：上辊、B：下辊）
			b_drpd.setRoller_position("B");
			b_drpd.setUpdate_opr(userName);
			b_drpd.setUpdate_time(new Date());
			b_drpd.setStand_id("");
			this.getMapper(DevRollerPairDMapper.class).update(b_drpd);
			
			//提交事务
			rm.setMsgInfo("撤销上机成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			throw new Exception("撤销上机失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * 动作处理_下机按钮
	 * object:
	 * dismount_time:下机时间    比如：2018-11-27 09:11:43
	 * dismount_reason_code：下机原因代码（390203%）
	 * dismount_reason_desc：下机原因描述
	 * rll_num:轧制卷数
	 * rll_len:轧制里程数（km）
	 * rll_wgt:轧制吨数（吨）
	 * dismount_memo：下机备注
	 * 
	 * memo1:配辊记录号
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doRollerPairDownWeb(CommonPage<DevRollerPairM> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String wrk_ord = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		String wrk_grp = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);
		DevRollerPairM drpm_view = sum.getObject()==null?new DevRollerPairM():sum.getObject();
		try{			
			if(sum.getMemo1()==null || "".equals(sum.getMemo1())){
				rm.setState("500");
				rm.setMsgInfo("未指定配辊记录号！");
				return rm;
			}
			Integer v_cnt=this.getMapper(DevRollerPairMMapper.class).countWithCondition(null, "serial_no='"+sum.getMemo1()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 配辊记录不存在！");
				return rm;
			}
			List<DevRollerPairM> ldbgg = this.getMapper(DevRollerPairMMapper.class).findWithCondition(null, "serial_no='"+sum.getMemo1()+"'", null, 0, 0);
			DevRollerPairM v_rec_grd = ldbgg.get(0);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			if("3".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已下机，不可重复操作！");
				return rm;
			}else if("0".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已拆卸，不允许此操作！");
				return rm;
			}else if("1".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 还未上机，不允许此操作！");
				return rm;
			}
			
			if(v_rec_grd.getT_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("上辊轧辊编号为空，信息出错！");
				return rm;
			}
			if(v_rec_grd.getB_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("下辊轧辊编号为空，信息出错！");
				return rm;
			}
			
			//轧辊状态（0：可配辊、1：待修磨、2：修磨中、3：已配辊、4：在线、5：报废）
//			DevRollerInfo dri_t=new DevRollerInfo();
//			dri_t.setRoller_no(v_rec_grd.getT_roller_no());
//			dri_t.setStatus("4");
//			this.getMapper(DevRollerInfoMapper.class).update(dri_t);
//			DevRollerInfo dri_b=new DevRollerInfo();
//			dri_b.setRoller_no(v_rec_grd.getB_roller_no());
//			dri_b.setStatus("4");
//			this.getMapper(DevRollerInfoMapper.class).update(dri_b);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			DevRollerPairM drpm=new DevRollerPairM();
			drpm.setSerial_no(sum.getMemo1());
			drpm.setDismount_ord(wrk_ord);
			drpm.setDismount_grp(wrk_grp);
			drpm.setDismount_reason_code(drpm_view.getDismount_reason_code());
			drpm.setDismount_reason_desc(drpm_view.getDismount_reason_desc());
			drpm.setDismount_memo(drpm_view.getDismount_memo());
			drpm.setDismount_time(drpm_view.getDismount_time());
			/*
			 * rll_num:轧制卷数
			 * rll_len:轧制里程数（km）
			 * rll_wgt:轧制吨数（吨）
			 * 轧辊信息表没有更新？？子表也没更新？？
			*/
			drpm.setRll_len(drpm_view.getRll_len());
			drpm.setRll_num(drpm_view.getRll_num());
			drpm.setRll_wgt(drpm_view.getRll_wgt());
			
			drpm.setStatus("3");
			drpm.setUpdate_opr(userName);
			drpm.setUpdate_time(new Date());
			this.getMapper(DevRollerPairMMapper.class).update(drpm);
			
			DevRollerPairD t_drpd=new DevRollerPairD();
			t_drpd.setSerial_no(v_rec_grd.getSerial_no());		
			t_drpd.setRoller_position("T");//轧辊位置（T：上辊、B：下辊）
			List<DevRollerPairD> t_drpd_list = this.getMapper(DevRollerPairDMapper.class).find(t_drpd, 0, 0);
			t_drpd.setUpdate_opr(userName);
			t_drpd.setUpdate_time(new Date());
			t_drpd.setCur_use_times(t_drpd_list.get(0).getCur_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			t_drpd.setCur_rll_len(t_drpd_list.get(0).getCur_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			t_drpd.setCur_rll_wgt(t_drpd_list.get(0).getCur_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));
			t_drpd.setTtl_use_times(t_drpd_list.get(0).getTtl_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			t_drpd.setTtl_rll_len(t_drpd_list.get(0).getTtl_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			t_drpd.setTtl_rll_wgt(t_drpd_list.get(0).getTtl_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));		
			this.getMapper(DevRollerPairDMapper.class).update(t_drpd);
			
			DevRollerPairD b_drpd=new DevRollerPairD();
			b_drpd.setSerial_no(v_rec_grd.getSerial_no());			
			b_drpd.setRoller_position("B");//轧辊位置（T：上辊、B：下辊）
			List<DevRollerPairD> b_drpd_list = this.getMapper(DevRollerPairDMapper.class).find(b_drpd, 0, 0);
			b_drpd.setUpdate_opr(userName);
			b_drpd.setUpdate_time(new Date());
			b_drpd.setCur_use_times(b_drpd_list.get(0).getCur_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			b_drpd.setCur_rll_len(b_drpd_list.get(0).getCur_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			b_drpd.setCur_rll_wgt(b_drpd_list.get(0).getCur_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));
			b_drpd.setTtl_use_times(b_drpd_list.get(0).getTtl_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			b_drpd.setTtl_rll_len(b_drpd_list.get(0).getTtl_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			b_drpd.setTtl_rll_wgt(b_drpd_list.get(0).getTtl_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));		
			this.getMapper(DevRollerPairDMapper.class).update(b_drpd);
			
			DevRollerInfo t_dri=new DevRollerInfo();
			t_dri.setRoller_no(v_rec_grd.getT_roller_no());
			List<DevRollerInfo> t_dri_list = this.getMapper(DevRollerInfoMapper.class).find(t_dri, 0, 0);
			t_dri.setCur_use_times(t_dri_list.get(0).getCur_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			t_dri.setCur_rll_len(t_dri_list.get(0).getCur_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			t_dri.setCur_rll_wgt(t_dri_list.get(0).getCur_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));
			t_dri.setTtl_use_times(t_dri_list.get(0).getTtl_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			t_dri.setTtl_rll_len(t_dri_list.get(0).getTtl_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			t_dri.setTtl_rll_wgt(t_dri_list.get(0).getTtl_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));
			t_dri.setUpdate_opr(userName);
			t_dri.setUpdate_time(new Date());
			this.getMapper(DevRollerInfoMapper.class).update(t_dri);
			
			DevRollerInfo b_dri=new DevRollerInfo();
			b_dri.setRoller_no(v_rec_grd.getB_roller_no());
			List<DevRollerInfo> b_dri_list = this.getMapper(DevRollerInfoMapper.class).find(b_dri, 0, 0);
			b_dri.setCur_use_times(b_dri_list.get(0).getCur_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			b_dri.setCur_rll_len(b_dri_list.get(0).getCur_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			b_dri.setCur_rll_wgt(b_dri_list.get(0).getCur_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));
			b_dri.setTtl_use_times(b_dri_list.get(0).getTtl_use_times()+(drpm_view.getRll_num()==null?0:drpm_view.getRll_num()));
			b_dri.setTtl_rll_len(b_dri_list.get(0).getTtl_rll_len()+(drpm_view.getRll_len()==null?0:drpm_view.getRll_len()));
			b_dri.setTtl_rll_wgt(b_dri_list.get(0).getTtl_rll_wgt()+(drpm_view.getRll_wgt()==null?0:drpm_view.getRll_wgt()));
			b_dri.setUpdate_opr(userName);
			b_dri.setUpdate_time(new Date());
			this.getMapper(DevRollerInfoMapper.class).update(b_dri);
						
			//提交事务
			rm.setMsgInfo("下机成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			throw new Exception("下机失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 动作处理_撤销下机按钮
	 * memo1:配辊记录号
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doRollerPairDownCancelWeb(CommonPage<DevRollerPairM> sum) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(userName==null){
				userName="";
			}
			if(sum.getMemo1()==null || "".equals(sum.getMemo1())){
				rm.setState("500");
				rm.setMsgInfo("未指定配辊记录号！");
				return rm;
			}
			Integer v_cnt=this.getMapper(DevRollerPairMMapper.class).countWithCondition(null, "serial_no='"+sum.getMemo1()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 配辊记录不存在！");
				return rm;
			}
			List<DevRollerPairM> ldbgg = this.getMapper(DevRollerPairMMapper.class).findWithCondition(null, "serial_no='"+sum.getMemo1()+"'", null, 0, 0);
			DevRollerPairM v_rec_grd = ldbgg.get(0);
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			if("0".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 已拆卸，不允许此操作！");
				return rm;
			}else if("1".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 未进行下机操作，不允许此操作！");
				return rm;
			}else if("2".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("配辊记录号{"+sum.getMemo1()+"} 上机状态，不允许此操作！");
				return rm;
			}
			if(!"3".equals(v_rec_grd.getStatus())){
				rm.setState("500");
				rm.setMsgInfo("只有下机状态的数据允许撤销下机！");
				return rm;
			}
			
			if(v_rec_grd.getT_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("上辊轧辊编号为空，信息出错！");
				return rm;
			}
			if(v_rec_grd.getB_roller_no()==null){
				rm.setState("500");
				rm.setMsgInfo("下辊轧辊编号为空，信息出错！");
				return rm;
			}
			
			//轧辊状态（0：可配辊、1：待修磨、2：修磨中、3：已配辊、4：在线、5：报废）
//			DevRollerInfo dri_t=new DevRollerInfo();
//			dri_t.setRoller_no(v_rec_grd.getT_roller_no());
//			dri_t.setStatus("3");
//			this.getMapper(DevRollerInfoMapper.class).update(dri_t);
//			DevRollerInfo dri_b=new DevRollerInfo();
//			dri_b.setRoller_no(v_rec_grd.getB_roller_no());
//			dri_b.setStatus("3");
//			this.getMapper(DevRollerInfoMapper.class).update(dri_b);
			
			
			
			DevRollerPairD t_drpd=new DevRollerPairD();
			t_drpd.setSerial_no(v_rec_grd.getSerial_no());
			//轧辊位置（T：上辊、B：下辊）
			t_drpd.setRoller_position("T");
			List<DevRollerPairD> t_drpd_list = this.getMapper(DevRollerPairDMapper.class).find(t_drpd, 0, 0);
			t_drpd.setUpdate_opr(userName);
			t_drpd.setUpdate_time(new Date());
			t_drpd.setCur_use_times(t_drpd_list.get(0).getCur_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			t_drpd.setCur_rll_len(t_drpd_list.get(0).getCur_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			t_drpd.setCur_rll_wgt(t_drpd_list.get(0).getCur_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			t_drpd.setTtl_use_times(t_drpd_list.get(0).getTtl_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			t_drpd.setTtl_rll_len(t_drpd_list.get(0).getTtl_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			t_drpd.setTtl_rll_wgt(t_drpd_list.get(0).getTtl_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			
			this.getMapper(DevRollerPairDMapper.class).update(t_drpd);
			
			DevRollerPairD b_drpd=new DevRollerPairD();
			b_drpd.setSerial_no(v_rec_grd.getSerial_no());
			//轧辊位置（T：上辊、B：下辊）
			b_drpd.setRoller_position("B");
			List<DevRollerPairD> b_drpd_list = this.getMapper(DevRollerPairDMapper.class).find(b_drpd, 0, 0);
			b_drpd.setUpdate_opr(userName);
			b_drpd.setUpdate_time(new Date());
			b_drpd.setCur_use_times(b_drpd_list.get(0).getCur_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			b_drpd.setCur_rll_len(b_drpd_list.get(0).getCur_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			b_drpd.setCur_rll_wgt(b_drpd_list.get(0).getCur_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			b_drpd.setTtl_use_times(b_drpd_list.get(0).getTtl_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			b_drpd.setTtl_rll_len(b_drpd_list.get(0).getTtl_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			b_drpd.setTtl_rll_wgt(b_drpd_list.get(0).getTtl_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			
			this.getMapper(DevRollerPairDMapper.class).update(b_drpd);
			
			DevRollerInfo t_dri=new DevRollerInfo();
			t_dri.setRoller_no(v_rec_grd.getT_roller_no());
			List<DevRollerInfo> t_dri_list = this.getMapper(DevRollerInfoMapper.class).find(t_dri, 0, 0);
			t_dri.setCur_use_times(t_dri_list.get(0).getCur_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			t_dri.setCur_rll_len(t_dri_list.get(0).getCur_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			t_dri.setCur_rll_wgt(t_dri_list.get(0).getCur_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			t_dri.setTtl_use_times(t_dri_list.get(0).getTtl_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			t_dri.setTtl_rll_len(t_dri_list.get(0).getTtl_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			t_dri.setTtl_rll_wgt(t_dri_list.get(0).getTtl_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			
			t_dri.setUpdate_opr(userName);
			t_dri.setUpdate_time(new Date());
			this.getMapper(DevRollerInfoMapper.class).update(t_dri);
			
			DevRollerInfo b_dri=new DevRollerInfo();
			b_dri.setRoller_no(v_rec_grd.getB_roller_no());
			List<DevRollerInfo> b_dri_list = this.getMapper(DevRollerInfoMapper.class).find(b_dri, 0, 0);
			b_dri.setCur_use_times(b_dri_list.get(0).getCur_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			b_dri.setCur_rll_len(b_dri_list.get(0).getCur_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			b_dri.setCur_rll_wgt(b_dri_list.get(0).getCur_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			b_dri.setTtl_use_times(b_dri_list.get(0).getTtl_use_times()-(v_rec_grd.getRll_num()==null?0:v_rec_grd.getRll_num()));
			b_dri.setTtl_rll_len(b_dri_list.get(0).getTtl_rll_len()-(v_rec_grd.getRll_len()==null?0:v_rec_grd.getRll_len()));
			b_dri.setTtl_rll_wgt(b_dri_list.get(0).getTtl_rll_wgt()-(v_rec_grd.getRll_wgt()==null?0:v_rec_grd.getRll_wgt()));
			
			b_dri.setUpdate_opr(userName);
			b_dri.setUpdate_time(new Date());
			this.getMapper(DevRollerInfoMapper.class).update(b_dri);
			
			
			//状态（1：配对、2：上机、3：下机、0：拆卸）
			DevRollerPairM drpm=new DevRollerPairM();
			drpm.setSerial_no(sum.getMemo1());
			drpm.setDismount_ord(null);
			drpm.setDismount_grp(null);
			drpm.setDismount_reason_code(null);
			drpm.setDismount_reason_desc(null);
			drpm.setDismount_memo(null);
			drpm.setDismount_time(null);
			/*
			 * rll_num:轧制卷数
			 * rll_len:轧制里程数（km）
			 * rll_wgt:轧制吨数（吨）
			 * 轧辊信息表没有更新？？子表也没更新？？
			*/
			drpm.setRll_len(null);
			drpm.setRll_num(null);
			drpm.setRll_wgt(null);
			drpm.setStatus("2");
			drpm.setUpdate_opr(userName);
			drpm.setUpdate_time(new Date());
			this.getMapper(DevRollerPairMMapper.class).update(drpm);
			
			//提交事务
			rm.setMsgInfo("撤销下机成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			throw new Exception("撤销下机失败!"+ex.getMessage());
		}
		return rm;
	}
	
	public ResultModel doQueryRollCoilDetail(CommonPage<DevRollerPairM> cp){
		ResultModel rm=new ResultModel();
		try{
			if (StringUtils.isEmpty(cp.getStartTime()) || StringUtils.isEmpty(cp.getEndTime())) {
				//没传上机时间或下机时间，
				rm.setData(null);
				return rm;
			}
			String condition = "feeding_time >= to_date('"+cp.getStartTime()+"','yyyy-MM-dd HH24:mi:ss') "
					+ "and tp_end_time <= to_date('"+cp.getEndTime()+"','yyyy-MM-dd HH24:mi:ss')";
			List<MesRcmM> mrmList = this.getMapper(MesRcmMMapper.class).findWithCondition(null, condition, "feeding_time", 0, 0);
			rm.setData(mrmList);
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 平整机轧辊使用管理：转配辊功能
	 * @param sum
	 * @return
	 * @throws Exception
	 */
	public ResultModel doReturnMatchRoll(CommonPage<DevRollerPairM> cp) throws Exception{
		ResultModel rm = new ResultModel();
		DevRollerPairM drpm = cp.getObject();
		if ("".equals(drpm.getStatus()) || "".equals(drpm.getStatus())) {
			
		}
		//上辊编号
		DevRollerInfo drpT = this.getMapper(DevRollerInfoMapper.class).findById(drpm.getT_roller_no());
		if ("".equals(drpT.getStatus())) {
			drpT.setStatus("0");//可配辊状态
			this.getMapper(DevRollerInfoMapper.class).update(drpT);
		}
		//下辊编号
		DevRollerInfo drpB = this.getMapper(DevRollerInfoMapper.class).findById(drpm.getB_roller_no());
		if ("".equals(drpB.getStatus())) {
			drpB.setStatus("0");//可配辊状态
			this.getMapper(DevRollerInfoMapper.class).update(drpB);
		}
		rm.setState("200");
		rm.setMsgInfo("操作成功");
		return rm;
	}
	
	
}
