/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.MesShiftBaseCsmMapper;
import market.Mes.Shift.mapper.MesShiftConfigCsmMapper;
import market.Mes.Shift.service.MesShiftConfigCsmService;
import market.Mes.Shift.vo.MesShiftBaseCsm;
import market.Mes.Shift.vo.MesShiftConfigCsm;

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="MesShiftConfigCsmService")
public class MesShiftConfigCsmServiceImpl extends BaseServiceImpl<MesShiftConfigCsm,Object> implements MesShiftConfigCsmService {
	
	/**
	 * 查询
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<MesShiftConfigCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(MesShiftConfigCsmMapper.class).doQuery(sum.getObject()));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 新增
	 * @param sum
	 * memo1  登录用户名
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<MesShiftConfigCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			String machine_code=sum.getObject().getMachine_code();
			int icount=0;
			ComBaseMachine cbm=new ComBaseMachine();
			cbm.setMachine_code(machine_code);
			//SELECT count(*) FROM COM_BASE_MACHINE WHERE MACHINE_CODE = '机台代ss码'
			icount=this.getMapper(ComBaseMachineMapper.class).count(cbm);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("机台代码："+cbm.getMachine_code()+"，在系统中不存在！");
				return rm;
			}
			String csm_item_code=sum.getObject().getCsm_item_code();
			MesShiftBaseCsm msbc=new MesShiftBaseCsm();
			msbc.setCsm_item_code(csm_item_code);
			icount=this.getMapper(MesShiftBaseCsmMapper.class).count(msbc);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("消耗项目："+sum.getObject().getCsm_item_name()+"，在系统中不存在！");
				return rm;
			}
			String condition="MACHINE_CODE = '"+machine_code+"' AND CSM_ITEM_CODE = '"+csm_item_code+"'";
			//SELECT MACHINE_CODE,CSM_ITEM_CODE,DISPLAY_ORDER,CREATE_OPR,CREATE_TIME,UPDATE_OPR,UPDATE_TIME FROM MES_SHIFT_CONFIG_CSM WHERE MACHINE_CODE = 'M0004' AND CSM_ITEM_CODE = 'C001'
			List<MesShiftConfigCsm> lmscc=this.getMapper(MesShiftConfigCsmMapper.class).findWithCondition(null, condition, null, 0, 0);
			if(lmscc!=null && lmscc.size()>0){
				rm.setState("500");
				rm.setMsgInfo("机台："+sum.getObject().getMachine_name()+"\n消耗项目："+sum.getObject().getCsm_item_name()+"\n在系统中已存在，不能重复！");
				return rm;
			}
			long display_order=0;
			List<MesShiftConfigCsm> lmsccq=this.getMapper(MesShiftConfigCsmMapper.class).findWithCondition(null, null, " display_order desc ", 0, 1);
			if(lmsccq!=null && lmsccq.size()>0){
				display_order=lmsccq.get(0).getDisplay_order();
			}
			display_order++;
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			sum.getObject().setDisplay_order(display_order);
			sum.getObject().setCreate_opr(userName);
			sum.getObject().setCreate_time(new Date());
			this.getMapper(MesShiftConfigCsmMapper.class).doAdd(sum.getObject());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败！"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 保存显示顺序
	 * @param sum
	 * memo1  登录用户名
	 * @return
	 */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel saveDisplayOrder(CommonPage<MesShiftConfigCsm> sum) throws Exception{
		ResultModel rm=new ResultModel();
		List<MesShiftConfigCsm> lmscc = sum.getObjectList();
		String condition=null;
		if(lmscc!=null && lmscc.size()>0){
			for(MesShiftConfigCsm mscc:lmscc){
				condition="MACHINE_CODE = '"+mscc.getMachine_code()+"' AND CSM_ITEM_CODE = '"+mscc.getCsm_item_code()+"'";
				List<MesShiftConfigCsm> lmsccq=this.getMapper(MesShiftConfigCsmMapper.class).findWithCondition(null, condition, null, 0, 0);
				if(lmsccq!=null && lmsccq.size()==0){
					rm.setState("500");
					rm.setMsgInfo("机台："+mscc.getMachine_name()+"\n消耗项目："+mscc.getCsm_item_name()+"\n在系统中不存在！");
					throw new Exception("机台："+mscc.getMachine_name()+"\n消耗项目："+mscc.getCsm_item_name()+"\n在系统中不存在！");
				}
				String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				this.getMapper(MesShiftConfigCsmMapper.class).doUpdate(mscc,userName);
			}
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 删除
	 * @param sum
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<MesShiftConfigCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			String condition="MACHINE_CODE = '"+sum.getObject().getMachine_code()+"' AND CSM_ITEM_CODE = '"+sum.getObject().getCsm_item_code()+"'";
			//SELECT MACHINE_CODE,CSM_ITEM_CODE,DISPLAY_ORDER,CREATE_OPR,CREATE_TIME,UPDATE_OPR,UPDATE_TIME FROM MES_SHIFT_CONFIG_CSM WHERE MACHINE_CODE = 'M0002' AND CSM_ITEM_CODE = 'C001'
			List<MesShiftConfigCsm> lmsccq=this.getMapper(MesShiftConfigCsmMapper.class).findWithCondition(null, condition, null, 0, 0);
			if(lmsccq!=null && lmsccq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("机台："+sum.getObject().getMachine_name()+"\n消耗项目："+sum.getObject().getCsm_item_name()+"\n在系统中不存在！");
				return rm;
			}
			this.getMapper(MesShiftConfigCsmMapper.class).doDeleteByIds(sum.getObject());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败！"+ex.getMessage());
		}
		return rm;
	}
	
}
