/***文档注释***********************************************
 * 作者            ：宋燕辉
 * 创建日期     ：2018-03-21
 * 描述            ：交接班消耗项目基础表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Shift.impl;
import java.util.Date;
import java.util.List;

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.MesShiftBaseCsmService;
import market.Mes.Shift.vo.MesShiftBaseCsm;
import market.Mes.Shift.vo.MesShiftConfigCsm;

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

@RemoteService(path="MesShiftBaseCsmService")
public class MesShiftBaseCsmServiceImpl extends BaseServiceImpl<MesShiftBaseCsm,Object> implements MesShiftBaseCsmService {
	/**
	 * 查询
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<MesShiftBaseCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			rm.setData(this.getMapper(MesShiftBaseCsmMapper.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<MesShiftBaseCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			String csm_item_name = sum.getObject().getCsm_item_name();
			//SELECT CSM_ITEM_CODE,CSM_ITEM_NAME,CSM_ITEM_UNIT,CREATE_OPR,CREATE_TIME,UPDATE_OPR,UPDATE_TIME FROM MES_SHIFT_BASE_CSM WHERE csm_item_name = '消耗项目名称1'
			List<MesShiftBaseCsm> lmsbcq=this.getMapper(MesShiftBaseCsmMapper.class).findWithCondition(null, "csm_item_name = '"+csm_item_name+"'", null, 0, 0);
			if(lmsbcq!=null && lmsbcq.size()>0){
				rm.setState("500");
				rm.setMsgInfo("项目名称："+csm_item_name+"，在系统中已存在，不能重复！");
				return rm;
			}
			List<MesShiftBaseCsm> lmsbc=this.getMapper(MesShiftBaseCsmMapper.class).findWithCondition(null, null, " csm_item_code ", 0, 0);
			if(lmsbc==null||lmsbc.size()==0){
				sum.getObject().setCsm_item_code("C001");
			}else{
				int i=1;
				for(MesShiftBaseCsm msbcc:lmsbc){
					int m=Integer.parseInt(msbcc.getCsm_item_code().trim().substring(1));
					if(i!=m){
						sum.getObject().setCsm_item_code("C"+String.format("%03d", i));
						break;
					}
					i++;
					if(i>999){
						rm.setState("500");
						rm.setMsgInfo("数据条数[001-999]超出范围!");
						return rm;
					}
				}
				if(i>lmsbc.size()){
					sum.getObject().setCsm_item_code("C"+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(MesShiftBaseCsmMapper.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<MesShiftBaseCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			String csm_item_code = sum.getObject().getCsm_item_code();
			List<MesShiftBaseCsm> lmsbcq=this.getMapper(MesShiftBaseCsmMapper.class).findWithCondition(null, "csm_item_code = '"+csm_item_code+"'", null, 0, 0);
			if(lmsbcq!=null && lmsbcq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("项目代码："+csm_item_code+"，在系统中不存在！");
				return rm;
			}
			String csm_item_name = sum.getObject().getCsm_item_name();
			//SELECT CSM_ITEM_CODE,CSM_ITEM_NAME,CSM_ITEM_UNIT,CREATE_OPR,CREATE_TIME,UPDATE_OPR,UPDATE_TIME FROM MES_SHIFT_BASE_CSM WHERE CSM_ITEM_NAME = '消耗项目名称111' AND CSM_ITEM_CODE <> 'C001'
			String condition="CSM_ITEM_NAME = '"+csm_item_name+"' AND CSM_ITEM_CODE <> '"+csm_item_code+"'";
			List<MesShiftBaseCsm> lmsbc=this.getMapper(MesShiftBaseCsmMapper.class).findWithCondition(null, condition, null, 0, 0);
			if(lmsbc!=null && lmsbc.size()>0){
				rm.setState("500");
				rm.setMsgInfo("项目名称："+csm_item_name+"，在系统中已存在，不能重复！");
				return rm;	
			}
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			this.getMapper(MesShiftBaseCsmMapper.class).doUpdate(sum.getObject(),userName);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败！"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 删除
	 * @param sum
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<MesShiftBaseCsm> sum){
		ResultModel rm=new ResultModel();
		try{
			String csm_item_code = sum.getObject().getCsm_item_code();
			List<MesShiftBaseCsm> lmsbcq=this.getMapper(MesShiftBaseCsmMapper.class).findWithCondition(null, "csm_item_code = '"+csm_item_code+"'", null, 0, 0);
			if(lmsbcq!=null && lmsbcq.size()==0){
				rm.setState("500");
				rm.setMsgInfo("项目代码："+csm_item_code+"，在系统中不存在！");
				return rm;
			}
			List<MesShiftConfigCsm> lmscc=this.getMapper(MesShiftConfigCsmMapper.class).findWithCondition(null, "csm_item_code = '"+csm_item_code+"'", null, 0, 0);
			if(lmscc!=null && lmscc.size()>0){
				rm.setState("500");
				rm.setMsgInfo("项目代码："+csm_item_code+"，在系统中已引用，不能删除！");
				return rm;
			}
			this.getMapper(MesShiftBaseCsmMapper.class).deleteById(sum.getObject().getCsm_item_code());
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败！"+ex.getMessage());
		}
		return rm;
	}
	
}
