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

import market.Com.mapper.ComBaseMachineMapper;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Dev.mapper.DevRollerGrindingMapper;
import market.Mes.Dev.mapper.DevRollerInfoMapper;
import market.Mes.Dev.mapper.DevRollerPairMMapper;
import market.Mes.Dev.mapper.DevStandInfoMapper;
import market.Mes.Dev.service.DevStandInfoService;
import market.Mes.Dev.vo.DevStandInfo;

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="DevStandInfoService")
public class DevStandInfoServiceImpl extends BaseServiceImpl<DevStandInfo,Object> implements DevStandInfoService {

	/**
	 * memo1:机台代码  V_MCH_CODE
	 * */
	public ResultModel doQueryWeb(CommonPage<DevStandInfo> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(DevStandInfoMapper.class).doQueryWeb(oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
     
	/**
	 * memo1:机台代码  V_MCH_CODE
	 * memo2:机架号  stand_id
	 * */
	public ResultModel doAddWeb(CommonPage<DevStandInfo> sum){
		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;
			}
			if(sum.getMemo2()==null || "".equals(sum.getMemo2())  || "".equals(sum.getMemo2().trim()) ){
				rm.setState("500");
				rm.setMsgInfo("参数错误：机架号不能为空！");
				return rm;
			}
		    //验证机台基础信息存在
			ComBaseMachine machine = new ComBaseMachine();
			machine.setMachine_code(sum.getMemo1());
			int v_cnt = this.getMapper(ComBaseMachineMapper.class).count(machine);
			if (v_cnt <= 0) {
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 机台基础信息不存在！");
				return rm;
			}
			DevStandInfo dsi=new DevStandInfo();
			dsi.setMachine_code(sum.getMemo1());
			dsi.setStand_id(sum.getMemo2());
			int count = this.getMapper(DevStandInfoMapper.class).count(dsi);
			if(count>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 已存在机架号{"+sum.getMemo2()+"}！");
				return rm;
			}
			
			//显示顺序
			Long d_cnt=this.getMapper(DevStandInfoMapper.class).doQueryCount2(sum.getMemo1());
			DevStandInfo dsin=new DevStandInfo();
			dsin.setMachine_code(sum.getMemo1());
			dsin.setStand_id(sum.getMemo2());
			dsin.setDsp_order(d_cnt);
			dsin.setCreate_opr(userName);
			dsin.setCreate_time(new Date());
			this.getMapper(DevStandInfoMapper.class).insert(dsin);
			rm.setMsgInfo("新增成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("新增失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * memo1:机台代码  V_MCH_CODE
	 * memo2:机架号  stand_id
	 * memo3:原机架号  stand_id（即修改之前的机架号）
	 * */
	public ResultModel doUpdateWeb(CommonPage<DevStandInfo> sum){
		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;
			}
			if(sum.getMemo2()==null || "".equals(sum.getMemo2())){
				rm.setState("500");
				rm.setMsgInfo("参数错误：机架号不能为空！");
				return rm;
			}
			if(sum.getMemo3()==null || "".equals(sum.getMemo3())){
				rm.setState("500");
				rm.setMsgInfo("参数错误：机架号不能为空！");
				return rm;
			}
			int v_cnt = this.getMapper(DevStandInfoMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo2()+"'");
			if(v_cnt>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 已存在机架号{"+sum.getMemo2()+"}！");
				return rm;
			}
			List<DevStandInfo> ldsi = this.getMapper(DevStandInfoMapper.class).findWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo3()+"'",null,0,0);
			if(ldsi==null || ldsi.size()<=0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 不存在机架号{"+sum.getMemo3()+"}！");
				return rm;
			}
			v_cnt = this.getMapper(DevRollerInfoMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo3()+"'");
			if(v_cnt>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}机架号{"+sum.getMemo3()+"} 已有轧辊基础信息，不允许修改！");
				return rm;
			}
			v_cnt=this.getMapper(DevRollerGrindingMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo3()+"'");
			if(v_cnt>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}机架号{"+sum.getMemo3()+"} 存在修磨记录，不允许修改！");
				return rm;
			}
			v_cnt=this.getMapper(DevRollerPairMMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo3()+"'");
			if(v_cnt>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}机架号{"+sum.getMemo3()+"} 存在使用记录，不允许修改！");
				return rm;
			}
		    v_cnt = this.getMapper(DevStandInfoMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}下没有机架号 ");
				return rm;
			}
			
			this.getMapper(DevStandInfoMapper.class).delete(sum.getMemo1(),sum.getMemo3());
			
			DevStandInfo dbgp=new DevStandInfo();
			dbgp.setMachine_code(sum.getMemo1());
			dbgp.setStand_id(sum.getMemo2());
			dbgp.setCreate_opr(ldsi.get(0).getCreate_opr());
			dbgp.setCreate_time(ldsi.get(0).getCreate_time());
			dbgp.setDsp_order(ldsi.get(0).getDsp_order());
			dbgp.setUpdate_opr(userName);
			dbgp.setUpdate_time(new Date());
			this.getMapper(DevStandInfoMapper.class).insert(dbgp);
			rm.setMsgInfo("修改成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("修改失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 *objectlist:
	 *machine_code:机台代码
	 *stand_id:机架号
	 *dsp_order:显示顺序
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSaveWeb(CommonPage<DevStandInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		List<DevStandInfo> dbg_list = sum.getObjectList();
		if(dbg_list==null || dbg_list.size()<=0){
			rm.setState("500");
			rm.setMsgInfo("参数错误：未指定需要修改显示顺序的机台代码！");
			return rm;
		}
		Integer v_cnt=null;
		for(DevStandInfo dbg: dbg_list){
			if("".equals(dbg.getStand_id()) || "".equals(dbg.getDsp_order()) || dbg.getDsp_order()==null ||dbg.getStand_id()==null){
				rm.setState("500");
				rm.setMsgInfo("参数错误：机架号 与 显示顺序 个数不匹配！");
				return rm;
			}
			if(dbg.getDsp_order()>999){
				rm.setState("500");
				rm.setMsgInfo("显示顺序范围为【1-999】");
				return rm;
			}
			v_cnt = this.getMapper(DevStandInfoMapper.class).countWithCondition(null, "machine_code='"+dbg.getMachine_code()+"' and stand_id='"+dbg.getStand_id()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 下不存在机架号{"+sum.getMemo2()+"}！");
				return rm;
			}
		}
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		if(userName==null){
			userName="";
		}
		for(DevStandInfo dbgp: dbg_list){
			DevStandInfo dbb=new DevStandInfo();
			dbb.setDsp_order(dbgp.getDsp_order());
			dbb.setUpdate_opr(userName);
			dbb.setUpdate_time(new Date());
			dbb.setMachine_code(dbgp.getMachine_code());
			dbb.setStand_id(dbgp.getStand_id());
			this.getMapper(DevStandInfoMapper.class).update(dbb);
		}
		rm.setMsgInfo("保存成功");
		rm.setState("200");
	    return rm;
	}
	
	/**
	 * memo1:机台代码  V_MCH_CODE
	 * memo2:机架号  stand_id
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<DevStandInfo> sum) throws Exception{
		ResultModel rm=new ResultModel();
		
		if(sum.getMemo1()==null || "".equals(sum.getMemo1())){
			rm.setState("500");
			rm.setMsgInfo("参数错误：未指定需要修改信息的机台代码！");
			return rm;
		}
		if(sum.getMemo2()==null || "".equals(sum.getMemo2())){
			rm.setState("500");
			rm.setMsgInfo("参数错误：机架号不能为空！");
			return rm;
		}
		Integer v_cnt = this.getMapper(DevStandInfoMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo2()+"'");
		if(v_cnt<=0){
			rm.setState("500");
			rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 下不存在机架号{"+sum.getMemo2()+"}！");
			return rm;
		}
		v_cnt = this.getMapper(DevRollerInfoMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo2()+"'");
		if(v_cnt>0){
			rm.setState("500");
			rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}机架号{"+sum.getMemo2()+"} 已有轧辊基础信息，不允许删除！");
			return rm;
		}
		v_cnt=this.getMapper(DevRollerGrindingMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo2()+"'");
		if(v_cnt>0){
			rm.setState("500");
			rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}机架号{"+sum.getMemo2()+"} 存在修磨记录，不允许删除！");
			return rm;
		}
		v_cnt=this.getMapper(DevRollerPairMMapper.class).countWithCondition(null, "machine_code='"+sum.getMemo1()+"' and stand_id='"+sum.getMemo2()+"'");
		if(v_cnt>0){
			rm.setState("500");
			rm.setMsgInfo("机台代码{"+sum.getMemo1()+"}机架号{"+sum.getMemo2()+"} 存在使用记录，不允许删除！");
			return rm;
		}
		
		this.getMapper(DevStandInfoMapper.class).doDelete(sum.getMemo1(),sum.getMemo2());
		rm.setMsgInfo("删除成功");
		rm.setState("200");
	    return rm;
	}
	
	
	// 补零
	public static String lpad(String str, int num, String pad) {
		String n_str = str;
		if (str == null && "".equals(str))
			n_str = " ";
		for (int i = str.length(); i < num; i++) {
			n_str = pad + n_str;
		}
		return n_str;
	}

}
