/***文档注释***********************************************
 * 作者            ：宋燕辉
 * 创建日期     ：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.MesShiftBasePostMapper;
import market.Mes.Shift.mapper.MesShiftConfigPostMapper;
import market.Mes.Shift.service.MesShiftConfigPostService;
import market.Mes.Shift.vo.MesShiftBasePost;
import market.Mes.Shift.vo.MesShiftConfigPost;

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="MesShiftConfigPostService")
public class MesShiftConfigPostServiceImpl extends BaseServiceImpl<MesShiftConfigPost,Object> implements MesShiftConfigPostService {
 
	/**
	 * 
	 * 操作岗位配置查询
	 */
	public ResultModel doQueryWeb(CommonPage<MesShiftConfigPost> params) {
		ResultModel rm = new ResultModel();
		try {
			MesShiftConfigPost mscp = params.getObject()==null?new MesShiftConfigPost():params.getObject();
			rm.setData(this.getMapper(MesShiftConfigPostMapper.class).doQuery(mscp));
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			e.getStackTrace();
			rm.setMsgInfo("查询失败！"+e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 
	 * 新增按钮
	 * memo1:机台名称
	 * memo2:岗位名称
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<MesShiftConfigPost> params) throws Exception{
		ResultModel rm =new ResultModel();
		if(params==null||params.getObject()==null||params.getObject().getPost_code()==null||"".equals(params.getObject().getPost_code())){
			rm.setState("500");
			rm.setMsgInfo("请传入正确的参数!");
			return rm;
		}
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		MesShiftConfigPost mscp =params.getObject();
		MesShiftConfigPost mscp1 =new MesShiftConfigPost();
		ComBaseMachine cbm =new ComBaseMachine();
		MesShiftBasePost msbp = new MesShiftBasePost();
		cbm.setMachine_code(mscp.getMachine_code());
		msbp.setPost_code(mscp.getPost_code());
		int count = this.getMapper(ComBaseMachineMapper.class).count(cbm);
		if(count==0){
			rm.setState("500");
			rm.setMsgInfo("机台代码："+mscp.getMachine_code()+"，在系统中不存在！");
			throw new Exception("机台代码："+mscp.getMachine_code()+"，在系统中不存在！");
		}
		count = this.getMapper(MesShiftBasePostMapper.class).count(msbp);
		if(count==0){
			rm.setState("500");
			rm.setMsgInfo("岗位代码："+mscp.getPost_code()+"，在系统中不存在！");
			throw new Exception("岗位代码："+mscp.getPost_code()+"，在系统中不存在！");
		}
		mscp1.setMachine_code(mscp.getMachine_code());
		mscp1.setPost_code(mscp.getPost_code());
		count = this.getMapper(MesShiftConfigPostMapper.class).count(mscp1);
		if(count>0){
			rm.setState("500");
			rm.setMsgInfo("机台："+mscp.getMachine_code()+"\n岗位："+mscp.getPost_code()+"在系统中已经存在，不能重复！");
			throw new Exception("机台："+mscp.getMachine_code()+"\n岗位："+mscp.getPost_code()+"在系统中已经存在，不能重复！");
		}
		Long order = this.getMapper(MesShiftConfigPostMapper.class).findDisplayOrder();
		
		if (order==null)
			order = (long) 0;
		order ++;
		mscp1.setDisplay_order(order);
		mscp1.setMachine_code(mscp.getMachine_code());
		mscp1.setPost_code(mscp.getPost_code());
		if(mscp.getPost_emp_num()==null||mscp.getPost_emp_num()<1 || mscp.getPost_emp_num()>999){
			rm.setState("500");
			rm.setMsgInfo("配置人数不能为空且必须在1-999之间！");
			throw new Exception("配置人数不能为空且必须在1-999之间！");
		}
		mscp1.setPost_emp_num(mscp.getPost_emp_num());
		mscp1.setCreate_opr(username);
		mscp1.setCreate_time(new Date());
		this.getMapper(MesShiftConfigPostMapper.class).insert(mscp1);  //插入一条  岗位配置表 记录
		rm.setState("200");
		rm.setMsgInfo("增加岗位成功！");
		return rm;
	}
   
	/**
	 * 
	 * 修改按钮
	 * memo1:机台名称
	 * memo2:岗位名称
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public  ResultModel doEditWeb(CommonPage<MesShiftConfigPost> params) throws Exception{
		ResultModel rm = new ResultModel();
		if(params==null||params.getObject()==null){
			rm.setState("500");
			rm.setMsgInfo("请传入参数值！谢谢");
			throw new Exception("请传入参数值！谢谢");
		}
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		MesShiftConfigPost mscp =params.getObject();
		MesShiftConfigPost mscp1 =new MesShiftConfigPost();
		mscp1.setMachine_code(mscp.getMachine_code());
		mscp1.setPost_code(mscp.getPost_code());
		int count = this.getMapper(MesShiftConfigPostMapper.class).count(mscp1);
		if(count==0){
			rm.setState("500");
			rm.setMsgInfo("机台："+params.getMemo1()+"\n岗位："+params.getMemo2()+"\n在系统中不存在，无法修改信息！");
			throw new Exception("机台："+params.getMemo1()+"\n岗位："+params.getMemo2()+"\n在系统中不存在，无法修改信息！");
		}
		if(mscp.getPost_emp_num()==null||mscp.getPost_emp_num()<1 || mscp.getPost_emp_num()>999){
			rm.setState("500");
			rm.setMsgInfo("配置人数不能为空且必须在1-999之间！谢谢");
			throw new Exception("配置人数不能为空且必须在1-999之间！谢谢");
		}
        mscp1.setUpdate_opr(username);
        mscp1.setUpdate_time(new Date());
        mscp1.setPost_emp_num(mscp.getPost_emp_num());
        this.getMapper(MesShiftConfigPostMapper.class).update(mscp1);  //更新岗位配置信息表
		rm.setState("200");
		rm.setMsgInfo("修改成功！");
		return rm;
	}
	
	/**
	 * 
	 * 保存显示按钮
	 * params 
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public ResultModel doSaveShowWeb(CommonPage<MesShiftConfigPost> params) throws Exception{
		 ResultModel rm = new ResultModel();
		 if(params==null||params.getObjectList()==null){
			 	rm.setState("500");
				rm.setMsgInfo("请传入参数值！谢谢");
				throw new Exception("请传入参数值！谢谢");
			}
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			
			List<MesShiftConfigPost> mscp =params.getObjectList();
			for (MesShiftConfigPost mscp1 : mscp) {
				
			MesShiftConfigPost mscp2 = new MesShiftConfigPost();
			mscp2.setMachine_code(mscp1.getMachine_code());
			mscp2.setPost_code(mscp1.getPost_code());
			int count = this.getMapper(MesShiftConfigPostMapper.class).count(mscp2);
			if(count==0){
				rm.setState("500");
				rm.setMsgInfo("机台："+mscp1.getMachine_name()+"\n岗位："+mscp1.getPost_name()+"\n在系统中不存在！");
				throw new Exception("机台："+mscp1.getMachine_name()+"\n岗位："+mscp1.getPost_name()+"\n在系统中不存在！");
			}
			if(mscp1.getDisplay_order()==null){
				rm.setState("500");
				rm.setMsgInfo("显示顺序输入不正确！显示顺序不能为空且必须为自然数！谢谢");
				throw new Exception("显示顺序输入不正确！显示顺序不能为空且必须为自然数！谢谢");
			}
			mscp2.setUpdate_opr(username);
	        mscp2.setUpdate_time(new Date());
	        mscp2.setDisplay_order(mscp1.getDisplay_order());
	        this.getMapper(MesShiftConfigPostMapper.class).update(mscp2);   //更新岗位配置信息表
			
		}
		 rm.setState("200");
		 rm.setMsgInfo("修改成功！");
		 return rm;
	}
	
	/**
	 * 
	 * 删除按钮
	 * memo1:机台名称
	 * memo2:岗位名称
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<MesShiftConfigPost> params) throws Exception{
		ResultModel rm = new ResultModel();
		if(params==null||params.getObject()==null){
			rm.setState("500");
			rm.setMsgInfo("请传入参数值！谢谢");
			throw new Exception("请传入参数值！谢谢");
		}
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		MesShiftConfigPost mscp =params.getObject();
		MesShiftConfigPost mscp1 =new MesShiftConfigPost();
		mscp1.setMachine_code(mscp.getMachine_code());
		mscp1.setPost_code(mscp.getPost_code());
		int count = this.getMapper(MesShiftConfigPostMapper.class).count(mscp1);
		if(count==0){
			rm.setState("500");
			rm.setMsgInfo("机台："+params.getMemo1()+"\n岗位："+params.getMemo2()+"\n在系统中不存在！");
			throw new Exception("机台："+params.getMemo1()+"\n岗位："+params.getMemo2()+"\n在系统中不存在！");
		}
		this.getMapper(MesShiftConfigPostMapper.class).delete(mscp.getMachine_code(),mscp.getPost_code());
		rm.setState("200");
		rm.setMsgInfo("删除成功！");
		return rm;
	}
	
}
