/***文档注释***********************************************
 * 作者            ：宋燕辉
 * 创建日期     ：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.MesShiftBaseEmpMapper;
import market.Mes.Shift.mapper.MesShiftBasePostMapper;
import market.Mes.Shift.mapper.MesShiftConfigPostMapper;
import market.Mes.Shift.service.MesShiftBasePostService;
import market.Mes.Shift.vo.MesShiftBaseEmp;
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="MesShiftBasePostService")
public class MesShiftBasePostServiceImpl extends BaseServiceImpl<MesShiftBasePost,Object> implements MesShiftBasePostService {
	/**
	 * 查询
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<MesShiftBasePost> sum){
		ResultModel rm=new ResultModel();
		try{
			MesShiftBasePost msbp=sum.getObject()==null?new MesShiftBasePost():sum.getObject();
			rm.setData(this.getMapper(MesShiftBasePostMapper.class).doQuery(sum.getObject()));
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("查询失败！"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 新增
	 * @param sum
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<MesShiftBasePost> sum){
		ResultModel rm=new ResultModel();
		try{
			if(sum==null||sum.getObject()==null||sum.getObject().getPost_desc()==null||"".equals(sum.getObject().getPost_desc())){
				rm.setState("500");
				rm.setMsgInfo("请传入正确的参数!");
				return rm;
			}
			
			MesShiftBasePost msbpq=new MesShiftBasePost();
			msbpq.setPost_desc(sum.getObject().getPost_desc().trim());
			int icount=this.getMapper(MesShiftBasePostMapper.class).count(msbpq);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("岗位名称："+sum.getObject().getPost_desc()+"，在系统中已存在，不能重复！");
				return rm;
			}
			
			
			List<MesShiftBasePost> lmsbp=this.getMapper(MesShiftBasePostMapper.class).findWithCondition(null, null, " post_code ", 0, 0);
			if(lmsbp==null||lmsbp.size()==0){
				sum.getObject().setPost_code("P01");
			}else{
				int i=1;
				for(MesShiftBasePost msbp:lmsbp){
					int m=Integer.parseInt(msbp.getPost_code().trim().substring(1));
					if(i!=m){
						sum.getObject().setPost_code("P"+String.format("%02d", i));
						break;
					}
					i++;
					if(i>99){
						rm.setState("500");
						rm.setMsgInfo("岗位数据条数[01-99]超出范围!");
						return rm;
					}
				}
				if(i>lmsbp.size()){
					sum.getObject().setPost_code("P"+String.format("%02d", i));
				}
			}                                              //生成岗位代码
			
			Long iMax=this.getMapper(MesShiftBasePostMapper.class).doQueryMax();
			if(iMax==null){
				iMax = (long) 0;
			}
			sum.getObject().setDisplay_order(iMax+1);
			
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			sum.getObject().setCreate_opr(username);
			sum.getObject().setCreate_time(new Date());
			this.getMapper(MesShiftBasePostMapper.class).insert(sum.getObject());
			rm.setState("200");
			rm.setMsgInfo("新增成功");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败！"+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 修改
	 * @param sum
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<MesShiftBasePost> sum){
		ResultModel rm=new ResultModel();
		try{
			MesShiftBasePost msp=new MesShiftBasePost();
			msp.setPost_code(sum.getObject().getPost_code().trim());
			int icount=this.getMapper(MesShiftBasePostMapper.class).count(msp);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("岗位代码："+msp.getPost_code()+"，在系统中不存在！");
				return rm;
			}
			msp.setPost_code(null);
			msp.setPost_desc(sum.getObject().getPost_desc().trim());
			icount=this.getMapper(MesShiftBasePostMapper.class).countWithCondition(msp, " post_code<>'"+sum.getObject().getPost_code().trim()+"'");
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("岗位名称："+msp.getPost_desc()+"，在系统中已存在，不能重复！");
				return rm;
			}
			
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			sum.getObject().setUpdate_opr(username);
			sum.getObject().setUpdate_time(new Date());
			this.getMapper(MesShiftBasePostMapper.class).update(sum.getObject());
			rm.setState("200");
			rm.setMsgInfo("修改成功");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("修改失败！"+ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 调整顺序
	 * @param sum
	 * @return
	 */
	@Transactional(value="tm" ,rollbackFor=Exception.class)
	public ResultModel doUpdateOrderWeb(CommonPage<MesShiftBasePost> sum){
		ResultModel rm=new ResultModel();
		
		MesShiftBasePost msp=new MesShiftBasePost();
		for(MesShiftBasePost msbp:sum.getObjectList()){
			msp.setPost_code(msbp.getPost_code().trim());
			int icount=this.getMapper(MesShiftBasePostMapper.class).count(msp);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("岗位代码："+msp.getPost_code()+"，在系统中不存在！");
				return rm;
			}
		}
		
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		for(MesShiftBasePost msbp:sum.getObjectList()){
			MesShiftBasePost msbpu=new MesShiftBasePost();
			msbpu.setPost_code(msbp.getPost_code());
			msbpu.setDisplay_order(msbp.getDisplay_order());
			msbpu.setUpdate_opr(username);
			msbpu.setUpdate_time(new Date());
			this.getMapper(MesShiftBasePostMapper.class).doUpdateOrd(msbpu);
			//this.getMapper(MesShiftBasePostMapper.class).update(msbpu);
		}
        rm.setMsgInfo("修改成功！");
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 删除
	 * @param sum
	 * @return
	 */
	public ResultModel doDeleteWeb(CommonPage<MesShiftBasePost> sum){
		ResultModel rm=new ResultModel();
		try{
			MesShiftBasePost msp=new MesShiftBasePost();
			msp.setPost_code(sum.getObject().getPost_code());
			int icount=this.getMapper(MesShiftBasePostMapper.class).count(msp);
			if(icount==0){
				rm.setState("500");
				rm.setMsgInfo("岗位代码："+msp.getPost_code()+"，在系统中不存在！");
				return rm;
			}
			
			MesShiftConfigPost mscp=new MesShiftConfigPost();
			mscp.setPost_code(sum.getObject().getPost_code());
			icount=this.getMapper(MesShiftConfigPostMapper.class).count(mscp);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("岗位代码："+msp.getPost_code()+"，在系统中已引用，不能删除！");
				return rm;
			}
			
			
			MesShiftBaseEmp msbe=new MesShiftBaseEmp();
			msbe.setPost_code(sum.getObject().getPost_code());
			icount=this.getMapper(MesShiftBaseEmpMapper.class).count(msbe);
			if(icount>0){
				rm.setState("500");
				rm.setMsgInfo("岗位代码："+msp.getPost_code()+"，在系统中已引用，不能删除！");
				return rm;
			}
			
			this.getMapper(MesShiftBasePostMapper.class).doDelete(msp);
			rm.setState("200");	
			rm.setMsgInfo("删除成功!");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("删除失败！"+ex.getMessage());
		}
		return rm;
	}
}
