/***文档注释***********************************************
 * 作者            ：宋燕辉
 * 创建日期     ：2018-03-21
 * 描述            ：车间作业人员基础表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Shift.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.Shift.mapper.MesShiftBaseEmpMapper;
import market.Mes.Shift.mapper.MesShiftBasePostMapper;
import market.Mes.Shift.service.MesShiftBaseEmpService;
import market.Mes.Shift.vo.MesShiftBaseEmp;
import market.Mes.Shift.vo.MesShiftBasePost;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="MesShiftBaseEmpService")
public class MesShiftBaseEmpServiceImpl extends BaseServiceImpl<MesShiftBaseEmp,Object> implements MesShiftBaseEmpService {
	/**
	 * 查询
	 * @param sum
	 * memo1 包含离职，勾选了:"1"  未勾选："0"
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<MesShiftBaseEmp> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(MesShiftBaseEmpMapper.class).doQuery(sum.getObject(),sum.getMemo1()));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 新增
	 * @param sum
	 * memo1 登录用户名
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<MesShiftBaseEmp> sum){
		ResultModel rm=new ResultModel();
		try{
			String wrk_grp= sum.getObject().getWrk_grp();
			if(wrk_grp!=null && !"".equals(wrk_grp)){
				if(!"1".equals(wrk_grp)&&!"2".equals(wrk_grp)&&!"3".equals(wrk_grp)&&!"4".equals(wrk_grp)){
					rm.setState("500");
					rm.setMsgInfo("班组代码："+wrk_grp+"，系统无法识别！\n系统识别班组如下：1甲、2乙、3丙、4丁");
					return rm;
				}
			}
			String machine_code=sum.getObject().getMachine_code();
			int icount=0;
			if(machine_code!=null && !"".equals(machine_code)){
				ComBaseMachine cbm=new ComBaseMachine();
				cbm.setMachine_code(machine_code);
				 icount=this.getMapper(ComBaseMachineMapper.class).count(cbm);
				if(icount==0){
					rm.setState("500");
					rm.setMsgInfo("机台代码："+cbm.getMachine_code()+"，在系统中不存在！");
					return rm;
				}
			}
			String post_code=sum.getObject().getPost_code();
			if(post_code!=null && !"".equals(post_code)){
				MesShiftBasePost msbp=new MesShiftBasePost();
				msbp.setPost_code(post_code);
				icount=this.getMapper(MesShiftBasePostMapper.class).count(msbp);
				if(icount==0){
					rm.setState("500");
					rm.setMsgInfo("岗位代码："+msbp.getPost_code()+"，在系统中不存在！");
					return rm;
				}
			}
			//SELECT EMP_NO,EMP_NAME,MACHINE_CODE,WRK_GRP,POST_CODE,VALID_FLAG,CREATE_OPR,CREATE_TIME,UPDATE_OPR,UPDATE_TIME FROM MES_SHIFT_BASE_EMP ORDER BY emp_no
			List<MesShiftBaseEmp> lmsbp=this.getMapper(MesShiftBaseEmpMapper.class).findWithCondition(null, null, " emp_no ", 0, 0);
			if(lmsbp==null||lmsbp.size()==0){
				sum.getObject().setEmp_no("E001");
			}else{
				int i=1;
				for(MesShiftBaseEmp msbe:lmsbp){
					int m=Integer.parseInt(msbe.getEmp_no().trim().substring(1));
					if(i!=m){
						sum.getObject().setEmp_no("E"+String.format("%03d", i));
						break;
					}
					i++;
					if(i>999){
						rm.setState("500");
						rm.setMsgInfo("数据条数[001-999]超出范围!");
						return rm;
					}
				}
				if(i>lmsbp.size()){
					sum.getObject().setEmp_no("E"+String.format("%03d", i));
				}
			}
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			sum.getObject().setCreate_opr(userName);
			sum.getObject().setCreate_time(new Date());
			this.getMapper(MesShiftBaseEmpMapper.class).insert(sum.getObject());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败！"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 修改
	 * @param sum
	 * memo1 登录用户名
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<MesShiftBaseEmp> sum){
		ResultModel rm=new ResultModel();
		try{
			MesShiftBaseEmp msbe=new MesShiftBaseEmp();
			msbe.setEmp_no(sum.getObject().getEmp_no());
			int icount=this.getMapper(MesShiftBaseEmpMapper.class).count(msbe);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("编号："+msbe.getEmp_no()+"，\n在系统中不存在此编号人员信息！");
				return rm;
			}
			String wrk_grp= sum.getObject().getWrk_grp();
			if(wrk_grp!=null && !"".equals(wrk_grp)){
				if(!"1".equals(wrk_grp)&&!"2".equals(wrk_grp)&&!"3".equals(wrk_grp)&&!"4".equals(wrk_grp)){
					rm.setState("500");
					rm.setMsgInfo("班组代码："+wrk_grp+"，系统无法识别！\n系统识别班组如下：1甲、2乙、3丙、4丁");
					return rm;
				}
			}
			String machine_code=sum.getObject().getMachine_code();
			if(machine_code!=null && !"".equals(machine_code)){
				ComBaseMachine cbm=new ComBaseMachine();
				cbm.setMachine_code(machine_code);
				 icount=this.getMapper(ComBaseMachineMapper.class).count(cbm);
				if(icount==0){
					rm.setState("500");
					rm.setMsgInfo("机台代码："+cbm.getMachine_code()+"，在系统中不存在！");
					return rm;
				}
			}
			String post_code=sum.getObject().getPost_code();
			if(post_code!=null && !"".equals(post_code)){
				MesShiftBasePost msbp=new MesShiftBasePost();
				msbp.setPost_code(post_code);
				icount=this.getMapper(MesShiftBasePostMapper.class).count(msbp);
				if(icount==0){
					rm.setState("500");
					rm.setMsgInfo("岗位代码："+msbp.getPost_code()+"，在系统中不存在！");
					return rm;
				}
			}
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			this.getMapper(MesShiftBaseEmpMapper.class).doUpdate(sum.getObject(),userName);
			
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败！"+ex.getMessage());
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 删除
	 * @param sum
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<MesShiftBaseEmp> sum){
		ResultModel rm=new ResultModel();
		try{
			MesShiftBaseEmp msbe=new MesShiftBaseEmp();
			msbe.setEmp_no(sum.getObject().getEmp_no());
			int icount=this.getMapper(MesShiftBaseEmpMapper.class).count(msbe);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("编号："+msbe.getEmp_no()+"，在系统中不存在此编号人员信息！");
				return rm;
			}
			this.getMapper(MesShiftBaseEmpMapper.class).doDeleteById(msbe.getEmp_no());
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败！"+ex.getMessage());
		}
		rm.setState("200");
		return rm;
	}
}
