/***文档注释***********************************************
 * 作者            ：欧阳志强
 * 创建日期     ：2018-03-21
 * 描述            ：交接班点检项目基础表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Mes.Shift.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

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.MesShiftBaseCheckService;
import market.Mes.Shift.vo.MesShiftBaseCheck;
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 = "MesShiftBaseCheckService")
public class MesShiftBaseCheckServiceImpl extends
		BaseServiceImpl<MesShiftBaseCheck, Object> implements
		MesShiftBaseCheckService {
	/**
	 * 交接班管理-点检项目信息-查询按钮 
	 * memo1 名称 
	 * memo2 包含无效  未勾选 返回'0',勾选了"1"
	 * memo3 类别
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<MesShiftBaseCheck> sum) {
		ResultModel rm = new ResultModel();
		try {	
			rm.setData(this.getMapper(MesShiftBaseCheckMapper.class)
					.doQueryWeb(sum.getMemo1(),sum.getMemo2(),sum.getMemo3()));
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！" + ex.getMessage());
		}
		return rm;
	}

	/**
	 * 交接班管理-点检项目信息-新增按钮 
	 * sum.check_item_name 项目名称 
	 * sum.check_item_type 类别（编码）
	 * sum.create_opr 登录用户名
	 * 
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddWeb(CommonPage<MesShiftBaseCheck> sum)
			throws Exception {
		ResultModel rm = new ResultModel();
		try {
			MesShiftBaseCheck msbc = sum.getObject();
			MesShiftBaseCheck msbc2 = new MesShiftBaseCheck();
			// 验证前端传入的“项目名称”+“类别（编码）”
			msbc2.setCheck_item_name(msbc.getCheck_item_name());
			msbc2.setCheck_item_type(msbc.getCheck_item_type());
			int msb = this.getMapper(MesShiftBaseCheckMapper.class)
					.count(msbc2);
			if (msb != 0) {
				rm.setState("500");
				rm.setMsgInfo("项目名称：" + msbc.getCheck_item_name()
						+ "，在系统中已存在，不能重复！");
				throw new Exception("项目名称：" + msbc.getCheck_item_name()
						+ "，在系统中已存在，不能重复！");
			}
			// 获取所有的项目代码
			List<MesShiftBaseCheck> ypdm = this.getMapper(
					MesShiftBaseCheckMapper.class).findCode();
			int icount = 1;
			if (ypdm != null && ypdm.size() != 0) {
				boolean[] temp = new boolean[1010];
				Arrays.fill(temp, true);
				for (MesShiftBaseCheck ypd : ypdm) {
					temp[Integer
							.parseInt(ypd.getCheck_item_code().substring(1))] = false;
				}
				for (int i = 1; i < 1010; i++) {
					if (temp[i]) {
						icount = i;
						break;
					}
				}
			}
			// 范围超出显示
			if (icount > 999) {
				rm.setState("500");
				rm.setMsgInfo("新增失败！数据条数[001-999]超出限制！");
				return rm;
			}
			//项目代码生成
			String cd = "D" + String.format("%03d", icount);
			msbc.setCheck_item_code(cd);
			msbc.setValid_flag("1");
			msbc.setCreate_time(new Date());
//			显示顺序 
			long disor=1;
			List<MesShiftBaseCheck> mc=this.getMapper(MesShiftBaseCheckMapper.class).findWithCondition(null,
					"CHECK_ITEM_TYPE = '" + msbc.getCheck_item_type()
							+ "' AND VALID_FLAG = '1' AND DISPLAY_ORDER IS NOT NULL ",
					"DISPLAY_ORDER DESC", 0, 1); 
			if(mc!=null&&mc.size()!=0){
					disor=mc.get(0).getDisplay_order() + 1;}
			//显示顺序
			msbc.setDisplay_order(disor);
//			插入数据
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			msbc.setCreate_opr(userName);
			this.getMapper(MesShiftBaseCheckMapper.class).insert(msbc);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败！" + ex.getMessage());
		}
		return rm;
	}

	/**
	 * 交接班管理-点检项目信息-修改按钮
	 * sum.check_item_code 点检项目代码
	 * sum.check_item_name 点检项目名称
	 * sum.check_item_type 点检项目类别
	 * sum.valid_flag 有效标志（1有效、0无效）
	 * sum.update_opr 更新人
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doUpdateWeb(CommonPage<MesShiftBaseCheck> sum) 
			throws Exception {
		ResultModel rm = new ResultModel();
		try {
			MesShiftBaseCheck msbc = sum.getObject();
			
//			验证前端传入的“项目代码”参数值
			MesShiftBaseCheck msbc1 = new MesShiftBaseCheck();
			msbc1.setCheck_item_code(msbc.getCheck_item_code());
			int msb1 = this.getMapper(MesShiftBaseCheckMapper.class)
					.count(msbc1);
			if (msb1==0) {
				rm.setState("500");
				rm.setMsgInfo("项目代码："+msbc1.getCheck_item_code()+"，\n在系统中不存在！");
				throw new Exception("项目代码："+msbc1.getCheck_item_code()+"，\n在系统中不存在！");
			}
			
			// 验证前端传入的“项目名称”+“项目类别”,"项目代码。
			MesShiftBaseCheck msbc2 = new MesShiftBaseCheck();
//			msbc2.setCheck_item_name(msbc.getCheck_item_name());
//			msbc2.setCheck_item_type(msbc.getCheck_item_type());
			
//			int msb2 = this.getMapper(MesShiftBaseCheckMapper.class)
//					.count(msbc2);
			 String condition="CHECK_ITEM_NAME = '"+msbc.getCheck_item_name()+"' and CHECK_ITEM_TYPE ='"+msbc.getCheck_item_type()+"' AND CHECK_ITEM_CODE <> '"+msbc.getCheck_item_code()+"'";
			int msb2=this.getMapper(MesShiftBaseCheckMapper.class).countWithCondition(msbc2, condition);
			
			if (msb2 != 0) {
				rm.setState("500");
				rm.setMsgInfo("类别代码："+msbc.getCheck_item_type()
						+"\n项目名称："+msbc.getCheck_item_name()+"\n在系统中已存在，不能重复！");
				throw new Exception("类别代码："+msbc.getCheck_item_type()
						+"\n项目名称："+msbc.getCheck_item_name()+"\n在系统中已存在，不能重复！");
			}
			
//			修改记录
//			获取对应项目代码信息
			msbc1=this.getMapper(MesShiftBaseCheckMapper.class).findById(msbc.getCheck_item_code());
			msbc1.setCheck_item_name(msbc.getCheck_item_name());//名称
			msbc1.setCheck_item_type(msbc.getCheck_item_type());//类别
			msbc1.setValid_flag(msbc.getValid_flag());//有效标识
			String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			msbc1.setUpdate_opr(userName);//更新者
			this.getMapper(MesShiftBaseCheckMapper.class).doUpdateWeb(msbc1);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败！" + ex.getMessage());
		}
		return rm;
	}

	/**
	 * 交接班管理-点检项目信息-保存显示顺序按钮
	 * sum.check_item_code 点检项目代码
	 * sum.display_order 显示顺序
	 * sum.update_opr  更新人
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel saveShowOrder(CommonPage<MesShiftBaseCheck> sum) 
			throws Exception {
		ResultModel rm = new ResultModel();
		try {
			List<MesShiftBaseCheck> mslist=sum.getObjectList();
			if(mslist==null||mslist.size()==0){
				rm.setState("500");
				rm.setMsgInfo("请传入数据！");
			}
			for(MesShiftBaseCheck msbc:mslist){
//				验证前端传入的“项目代码”参数值，当前在点检项目基础表存在
				MesShiftBaseCheck mst=this.getMapper(MesShiftBaseCheckMapper.class).findById(msbc.getCheck_item_code());
				if(mst==null){
					rm.setState("500");
					rm.setMsgInfo("项目代码："+msbc.getCheck_item_code()+"\n在系统中不存在！");
					throw new Exception("项目代码："+msbc.getCheck_item_code()+"\n在系统中不存在！");
				}
					
					
//				保存显示顺序按钮
				msbc.setUpdate_time(new Date());
				String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				msbc.setUpdate_opr(userName);
//				if(msbc.getDisplay_order()==null){
//					this.getMapper(MesShiftBaseCheckMapper.class).doUpdateOrder2(msbc,userName);
//				}else{
//					this.getMapper(MesShiftBaseCheckMapper.class).doUpdateOrder(msbc,userName);
//				}
				this.getMapper(MesShiftBaseCheckMapper.class).doUpdateOrder(msbc,userName);
				//this.getMapper(MesShiftBaseCheckMapper.class).update(msbc);
			}
			rm.setState("200");	
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("保存失败！" + ex.getMessage());
		}
		return rm;
	}

	/**
	 * 交接班管理-点检项目信息-删除按钮
	 * sum.check_item_code 点检项目代码
	 * @param sum
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteWeb(CommonPage<MesShiftBaseCheck> sum) 
			throws Exception{
		ResultModel rm = new ResultModel();
		try {
			MesShiftBaseCheck msbc=sum.getObject();
			if(msbc==null||msbc.getCheck_item_code()==null){
				rm.setState("500");
				rm.setMsgInfo("请传入数据！");
			}
//			验证前端传入的“项目代码”参数值，当前在“点检项目基础表”存在
			if(this.getMapper(MesShiftBaseCheckMapper.class).count(msbc)==0){
				rm.setState("500");
				rm.setMsgInfo("项目代码："+msbc.getCheck_item_code()+"\n在系统中不存在！");
				throw new Exception("项目代码："+msbc.getCheck_item_code()+"\n在系统中不存在！");
			}
//			验证前端传入的“项目代码”参数值，当前在“交接班点检项目配置表”中不存在
			MesShiftConfigCheck msconf=new MesShiftConfigCheck();
			msconf.setCheck_item_code(msbc.getCheck_item_code());
			if(this.getMapper(MesShiftConfigCheckMapper.class).count(msconf)!=0){
				rm.setState("500");
				rm.setMsgInfo("项目代码："+msconf.getCheck_item_code()+"\n在系统中已引用，不能删除！");
				throw new Exception("项目代码："+msconf.getCheck_item_code()+"\n在系统中已引用，不能删除！");
			}
			this.getMapper(MesShiftBaseCheckMapper.class).delete(msbc.getCheck_item_code());
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("删除失败！" + ex.getMessage());
		}
		return rm;
	}
}
