/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-09-28
 * 描述            ：工器具-刀片分组信息
 * 注意事项     ：
 * 遗留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.DevBladeGrindingMapper;
import market.Mes.Dev.mapper.DevBladeGrpMapper;
import market.Mes.Dev.mapper.DevBladeInfoMapper;
import market.Mes.Dev.mapper.DevBladeUseGrpMapper;
import market.Mes.Dev.service.DevBladeGrpService;
import market.Mes.Dev.vo.DevBladeGrp;

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="DevBladeGrpService")
public class DevBladeGrpServiceImpl extends BaseServiceImpl<DevBladeGrp,Object> implements DevBladeGrpService {
   
	
	/**
	 * 刀片生产指定
	 * 动作处理_刷新刀片分组列表按钮
	 * 动作处理_日期班次记录双击
	 * 刀片分组列表
	 * memo1:机台代码  V_MCH_CODE
	 * memo2:日期  V_WRK_DATE（最近一次双击选定的日期）
	 * memo3:V_WRK_ORD（最近一次双击选定的班次代码）
	 * */
	public ResultModel doQueryShiftWeb(CommonPage<DevBladeGrp> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(DevBladeGrpMapper.class).doQueryShiftWeb(oci.getMemo1(),oci.getMemo2(),oci.getMemo3()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 动作处理_刷新（分组信息）按钮
	 * memo1:机台代码  V_MCH_CODE
	 * */
	public ResultModel doQueryWeb2(CommonPage<DevBladeGrp> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(DevBladeGrpMapper.class).doQueryWeb2(oci.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	
	/**
	 * memo1:机台代码  V_MCH_CODE
	 * */
	public ResultModel doQueryWeb(CommonPage<DevBladeGrp> oci){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(DevBladeGrpMapper.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:分组名称  V_GRP_NAME
	 * memo5:is_stock
	 * */
	public ResultModel doAddWeb(CommonPage<DevBladeGrp> 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;
			}
		    //验证机台基础信息存在
			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;
			}
			//1、4获取机台基础信息表中，配置的机台设备代号（设备编号，如1,2,3）
			ComBaseMachine machine2 = new ComBaseMachine();
			machine2.setMachine_code(sum.getMemo1());
			List<ComBaseMachine> lcbm = this.getMapper(ComBaseMachineMapper.class)
					.find(machine2, 0, 0);
			String v_mch_mark=lcbm.get(0).getMachine_mark();
			if (v_mch_mark == null) {
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 设备编号未配置，无法处理！");
				return rm;
			}
			DevBladeGrp dbg=new DevBladeGrp();
			dbg.setMachine_code(sum.getMemo1());
			dbg.setGrp_name(sum.getMemo2());
			int count = this.getMapper(DevBladeGrpMapper.class).count(dbg);
			if(count>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+sum.getMemo1()+"} 已存在刀片分组{"+sum.getMemo2()+"}！");
				return rm;
			}
			String v_grp_code="G"+lpad(v_mch_mark, 2, "0");
			Integer d_cnt=this.getMapper(DevBladeGrpMapper.class).doQueryCount(sum.getMemo1(),v_grp_code);
			if(d_cnt==null){
				d_cnt=1;
			}else if(d_cnt>999){
				rm.setState("500");
				rm.setMsgInfo("分组代码超出规则范围，流水号超出最大值999！");
				return rm;
			}
			v_grp_code=v_grp_code +lpad(d_cnt+"", 3, "0");
			//显示顺序
			d_cnt=this.getMapper(DevBladeGrpMapper.class).doQueryCount2(sum.getMemo1());
			DevBladeGrp dbgp=new DevBladeGrp();
			dbgp.setGrp_code(v_grp_code);
			dbgp.setPrd_num(sum.getMemo3());//成本期间
			dbgp.setGrp_name(sum.getMemo2());
			dbgp.setMachine_code(sum.getMemo1());
			//Long dsp_order=Long.parseLong(d_cnt+"");//方法一
			dbgp.setDsp_order(d_cnt.longValue());
			dbgp.setCreate_opr(userName);
			dbgp.setCreate_time(new Date());
			dbgp.setIs_stock(sum.getMemo5());
			this.getMapper(DevBladeGrpMapper.class).insert(dbgp);
			rm.setMsgInfo("新增成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("新增失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * memo1:分组代码  V_GRP_CODE
	 * memo2:分组名称  V_GRP_NAME
	 * memo5:is_stock
	 * */
	public ResultModel doUpdateWeb(CommonPage<DevBladeGrp> 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;
			}
		    int v_cnt = this.getMapper(DevBladeGrpMapper.class).countWithCondition(null, "grp_code='"+sum.getMemo1()+"'");
			if(v_cnt<=0){
				rm.setState("500");
				rm.setMsgInfo("分组代码{"+sum.getMemo1()+"} 刀片分组信息不存在！");
				return rm;
			}
			DevBladeGrp dbg = this.getMapper(DevBladeGrpMapper.class).findById(sum.getMemo1());
			v_cnt=this.getMapper(DevBladeGrpMapper.class).doQueryCount3(sum.getMemo1(), dbg.getMachine_code(),sum.getMemo2());
			if(v_cnt>0){
				rm.setState("500");
				rm.setMsgInfo("机台代码{"+dbg.getMachine_code()+"} 已存在刀片分组{"+sum.getMemo2()+"}！");
				return rm;
			}
			DevBladeGrp dbgp=new DevBladeGrp();
			dbgp.setGrp_code(sum.getMemo1());
			dbgp.setGrp_name(sum.getMemo2());
			dbgp.setPrd_num("".equals(sum.getMemo3().trim()) ? " ":sum.getMemo3());//成本期间
			dbgp.setUpdate_opr(userName);
			dbgp.setUpdate_time(new Date());
			dbgp.setIs_stock(sum.getMemo5());
			this.getMapper(DevBladeGrpMapper.class).update(dbgp);
			rm.setMsgInfo("修改成功");
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("修改失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 *objectlist:
	 *grp_code:分组代码
	 *dsp_order:显示顺序
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSaveWeb(CommonPage<DevBladeGrp> sum) throws Exception{
		ResultModel rm=new ResultModel();
		List<DevBladeGrp> dbg_list = sum.getObjectList();
		if(dbg_list==null || dbg_list.size()<=0){
			rm.setState("500");
			rm.setMsgInfo("参数错误：未指定需要修改显示顺序的分组代码！");
			return rm;
		}
		for(DevBladeGrp dbg: dbg_list){
			if("".equals(dbg.getGrp_code()) || "".equals(dbg.getDsp_order()) || dbg.getDsp_order()==null){
				rm.setState("500");
				rm.setMsgInfo("参数错误：分组代码 与 显示顺序 个数不匹配！");
				return rm;
			}
			if(dbg.getDsp_order()>999){
				rm.setState("500");
				rm.setMsgInfo("显示顺序范围为【1-999】");
				return rm;
			}
		}
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		if(userName==null){
			userName="";
		}
		for(DevBladeGrp dbgp: dbg_list){
			DevBladeGrp dbb=new DevBladeGrp();
			dbb.setDsp_order(dbgp.getDsp_order());
			dbb.setUpdate_opr(userName);
			dbb.setUpdate_time(new Date());
			dbb.setGrp_code(dbgp.getGrp_code());
			this.getMapper(DevBladeGrpMapper.class).update(dbb);
		}
		rm.setMsgInfo("保存成功");
		rm.setState("200");
	    return rm;
	}
	
	/**
    * memo1:分组代码  V_GRP_CODE
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<DevBladeGrp> sum) throws Exception{
		ResultModel rm=new ResultModel();
		
		if(sum.getMemo1()==null || "".equals(sum.getMemo1())){
			rm.setState("500");
			rm.setMsgInfo("参数错误：未指定需要修改信息的分组代码！");
			return rm;
		}
		int v_cnt = this.getMapper(DevBladeGrpMapper.class).countWithCondition(null, "grp_code='"+sum.getMemo1()+"'");
		if(v_cnt<=0){
			rm.setState("500");
			rm.setMsgInfo("分组代码{"+sum.getMemo1()+"} 刀片分组信息不存在！");
			return rm;
		}
		v_cnt = this.getMapper(DevBladeInfoMapper.class).countWithCondition(null, "grp_code='"+sum.getMemo1()+"'");
		if(v_cnt>0){
			rm.setState("500");
			rm.setMsgInfo("分组代码{"+sum.getMemo1()+"} 此分组下已有刀片信息，不允许删除！");
			return rm;
		}
		v_cnt=this.getMapper(DevBladeGrindingMapper.class).countWithCondition(null, "grp_code='"+sum.getMemo1()+"'");
		if(v_cnt>0){
			rm.setState("500");
			rm.setMsgInfo("分组代码{"+sum.getMemo1()+"} 存在修磨记录，不允许删除！");
			return rm;
		}
		v_cnt=this.getMapper(DevBladeUseGrpMapper.class).countWithCondition(null, "grp_code='"+sum.getMemo1()+"'");
		if(v_cnt>0){
			rm.setState("500");
			rm.setMsgInfo("分组代码{"+sum.getMemo1()+"} 存在使用记录，不允许删除！");
			return rm;
		}
		
		this.getMapper(DevBladeGrpMapper.class).delete(sum.getMemo1());
		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;
	}
	
}
