/***文档注释***********************************************
 * 作者            ：欧阳志强
 * 创建日期     ：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.MesShiftBaseCheckMapper;
import market.Mes.Shift.mapper.MesShiftConfigCheckMapper;
import market.Mes.Shift.service.MesShiftConfigCheckService;
import market.Mes.Shift.vo.MesShiftConfigCheck;

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 = "MesShiftConfigCheckService")
public class MesShiftConfigCheckServiceImpl extends
		BaseServiceImpl<MesShiftConfigCheck, Object> implements
		MesShiftConfigCheckService {
	/**
	 * 交接班管理-点检项目配置-查询按钮 
	 * memo1 机台 
	 * memo2 类别 
	 * memo3 项目名称
	 * 
	 * */
	public ResultModel doQueryWeb(CommonPage<MesShiftConfigCheck> sum) {
		ResultModel rm = new ResultModel();
		try {
			rm.setData(this.getMapper(MesShiftConfigCheckMapper.class)
					.doQueryWeb(sum.getMemo1(), sum.getMemo2(), sum.getMemo3()));
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！" + ex.getMessage());
		}
		
		return rm;
	}

	/**
	 * 交接班管理-点检项目配置-新增按钮 
	 * sum.machine_code 机台代码 
	 * sum.check_item_code 点检项目代码
	 * sum.create_opr 登录用户名
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<MesShiftConfigCheck> sum) 
	throws Exception{
		ResultModel rm = new ResultModel();
		try {
			MesShiftConfigCheck mscc = sum.getObject();
			
			MesShiftConfigCheck msc = new MesShiftConfigCheck();
			msc.setCheck_item_code(mscc.getCheck_item_code());
			msc.setMachine_code(mscc.getMachine_code());
			ComBaseMachine cbm = new ComBaseMachine();
			cbm.setMachine_code(mscc.getMachine_code());
			// 验证前端传入的“机台（代码）”，当前在机台基础信息表中存在
			if (this.getMapper(ComBaseMachineMapper.class).count(cbm) == 0) {
				String check_item_name = this.getMapper(MesShiftBaseCheckMapper.class).findById(msc.getCheck_item_code()).getCheck_item_name(); 
				rm.setState("500");
				rm.setMsgInfo("点检项目："+check_item_name+"，在系统中不存在！");
				throw new Exception("点检项目："+check_item_name+"，在系统中不存在！");
			}
			cbm = this.getMapper(ComBaseMachineMapper.class).doFindById(
					cbm.getMachine_code());
			// 验证前端传入的“点检项目（代码）”，当前在“交接班点检项目基础表”中不存在	
			if (this.getMapper(MesShiftConfigCheckMapper.class).count(msc) != 0) {
				String check_item_name = this.getMapper(MesShiftBaseCheckMapper.class).findById(msc.getCheck_item_code()).getCheck_item_name();
				rm.setState("500");
				rm.setMsgInfo("机台："
						+ cbm.getMachine_name()
						+ "\n点检项目："
						+ check_item_name + "\n在系统中已存在，不能重复！");
				throw new Exception("机台："
						+ cbm.getMachine_name()
						+ "\n点检项目："
						+ check_item_name + "\n在系统中已存在，不能重复！");
			}
			// 显示顺序
			long disor = 1;
			List<MesShiftConfigCheck> mc = this.getMapper(
					MesShiftConfigCheckMapper.class).findWithCondition(null,
					null, "DISPLAY_ORDER DESC", 0, 1);
			if (mc != null && mc.size() != 0) {
				disor = mc.get(0).getDisplay_order() + 1;
			}
			// 创建时间
			mscc.setCreate_time(new Date());
			mscc.setDisplay_order(disor);
			
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			mscc.setCreate_opr(userName);
			this.getMapper(MesShiftConfigCheckMapper.class).insert(mscc);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败！" + ex.getMessage());
		}
	
		return rm;
	}

	/**
	 * 交接班管理-点检项目配置-保存显示顺序按钮 
	 * sum.machine_code 机台代码 
	 * sum.check_item_code 点检项目代码
	 * sum.display_order 显示顺序
	 * sum.update_opr 登录用户名
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel saveShowOrder(CommonPage<MesShiftConfigCheck> sum) 
	throws Exception{
		ResultModel rm = new ResultModel();
		try {
			List<MesShiftConfigCheck> mclist=sum.getObjectList();
			if(mclist==null||mclist.size()==0){
				rm.setState("500");
				rm.setMsgInfo("请传入数据！");
			}
			for(MesShiftConfigCheck mc:mclist){
//				逐一验证前端传入的“机台（代码）”+“点检项目（代码）”，当前在“交接班点检项目配置表”中存在
				MesShiftConfigCheck mscc=new MesShiftConfigCheck();
				mscc.setCheck_item_code(mc.getCheck_item_code());
				mscc.setMachine_code(mc.getMachine_code());
				
				if(this.getMapper(MesShiftConfigCheckMapper.class).count(mscc)==0){
//				获取机台名称
				ComBaseMachine cbm = new ComBaseMachine();
				cbm.setMachine_code(mc.getMachine_code());
				cbm = this.getMapper(ComBaseMachineMapper.class).findById(
						cbm.getMachine_code());
//				抛出异常信息
				String check_item_name = this.getMapper(MesShiftBaseCheckMapper.class).findById(mc.getCheck_item_code()).getCheck_item_name() ;
				rm.setState("500");
				rm.setMsgInfo("机台："
						+ cbm.getMachine_name()
						+ "\n点检项目："
						+check_item_name + "\n在系统中不存在！");
					throw new Exception("机台："
						+ cbm.getMachine_name()
						+ "\n点检项目："
						+ check_item_name + "\n在系统中不存在！");
				}
//				修改记录
				mc.setUpdate_time(new Date());
				String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				mc.setUpdate_opr(userName);
				this.getMapper(MesShiftConfigCheckMapper.class).update(mc);
			}
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("保存失败！" + ex.getMessage());
		}
		
		return rm;
	}

	/**
	 * 交接班管理-点检项目配置-删除按钮 
	 * sum.machine_code 机台代码 
	 * sum.check_item_code 点检项目代码
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteWeb(CommonPage<MesShiftConfigCheck> sum) 
			throws Exception{
		ResultModel rm = new ResultModel();
		try {
			MesShiftConfigCheck mc=sum.getObject();
//			逐一验证前端传入的“机台（代码）”+“点检项目（代码）”，当前在“交接班点检项目配置表”中存在
			MesShiftConfigCheck mscc=new MesShiftConfigCheck();
			mscc.setCheck_item_code(mc.getCheck_item_code());
			mscc.setMachine_code(mc.getMachine_code());

			if(this.getMapper(MesShiftConfigCheckMapper.class).count(mscc)==0){
//			获取机台名称
			ComBaseMachine cbm = new ComBaseMachine();
			cbm.setMachine_code(mc.getMachine_code());
			cbm = this.getMapper(ComBaseMachineMapper.class).findById(
					cbm.getMachine_code());
//			抛出异常信息
			String check_item_name = this.getMapper(MesShiftBaseCheckMapper.class).findById(mc.getCheck_item_code()).getCheck_item_name(); 
			rm.setState("500");
			rm.setMsgInfo("机台："
					+ cbm.getMachine_name()
					+ "\n点检项目："
					+check_item_name+ "\n在系统中不存在！");
				throw new Exception("机台："
					+ cbm.getMachine_name()
					+ "\n点检项目："
					+check_item_name+ "\n在系统中不存在！");
			}
//			删除数据
			this.getMapper(MesShiftConfigCheckMapper.class).doDeleteWeb(mc);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("删除失败！" + ex.getMessage());
		}
		
		return rm;
	}
}
