/***文档注释***********************************************
 * 作者               :                   朱泽鹏
 * 创建日期      :                   2018.2.5
 * 描述               :        			工序点基础表
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.List;

import market.Com.mapper.ComBaseStationMapper;
import market.Com.mapper.ComBaseStationRCraftMapper;
import market.Com.service.ComBaseStationService;
import market.Com.vo.ComBaseStation;
import market.Com.vo.ComBaseStationRCraft;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.metallurgy.mapper.ComBaseAskRStationMapper;
import market.Qms.metallurgy.mapper.ComBaseProcessRStationMapper;
import market.Qms.metallurgy.vo.ComBaseAskRStation;
import market.Qms.metallurgy.vo.ComBaseProcessRStation;

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

@RemoteService(path = "ComBaseStationService")
public class ComBaseStationServiceImpl extends BaseServiceImpl<ComBaseStation,Object> implements ComBaseStationService {
	/**
	 * WEB 工序点界面：查询 
	 * @param cp 
	 * memo1  传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryStationWeb(CommonPage<ComBaseStation> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseStation> pm = new PageModel<ComBaseStation>(pageIndex,pageSize);
		try {
			ComBaseStation cbs = cp.getObject();
			String condition = "1=1";
			if ("0".equals(cp.getMemo1())) {
				condition += " and VALIDFLAG = '1'";
				cbs.setValidflag("1");
			}
			if (cbs.getProcess_point_desc() != null && !"".equals(cbs.getProcess_point_desc())) {
				condition += " and upper(PROCESS_POINT_DESC) like '%" + cbs.getProcess_point_desc().toUpperCase() + "%'";
			}

			pm.setRows(this.getMapper(ComBaseStationMapper.class).findWithCondition(null, condition, "process_point_code",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseStationMapper.class).countWithCondition(null, condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * WEB 工序点界面：新增
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertStationWeb(CommonPage<ComBaseStation> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseStation cbs = cp.getObject();
			//名称首尾去空格处理
			cbs.setProcess_point_desc(cbs.getProcess_point_desc().trim());
			if (cbs.getProcess_point_desc() == null || "".equals(cbs.getProcess_point_desc())) {
				rm.setState("403");
				rm.setMsgInfo("工序点名称不能为空");
				return rm;
			}
			
			String condition = " PROCESS_POINT_DESC = '" + cbs.getProcess_point_desc() + "'";
			int num = this.getMapper(ComBaseStationMapper.class).countWithCondition(null, condition);
			//提示用户不能新增的原因
			if (num!=0) {
				rm.setState("403");
				rm.setMsgInfo("工序点名称重复，不能新增");
				return rm;
			}
			List<HashMap> codes=this.getMapper(ComBaseStationMapper.class).GetMaxPointCode(cbs.getProcess_code());

			if(codes.get(0)==null){
				cbs.setProcess_point_code(cbs.getProcess_code()+"0001");
				cbs.setProcess_point_seq(1);
			}else{
				cbs.setProcess_point_seq(Integer.valueOf(codes.get(0).get("MAXSEQ").toString())+1);
				cbs.setProcess_point_code(DataHelper.InitSerial(codes.get(0).get("MAXCODE").toString(), cbs.getProcess_code(), 5));
			}
			cbs.setCreate_name(username);
			cbs.setCreate_time(new Date());
			cbs.setValidflag("1");//有效标志(1:有效，0:无效)
			this.getMapper(ComBaseStationMapper.class).insert(cbs);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败");
			rm.setErrorInfo(ex.getMessage());
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	/**
	 * WEB 工序点界面：修改
	 * @param cp
	 * @return
	 */
	public ResultModel doUpdateStationWeb(CommonPage<ComBaseStation> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);		 		
		try {
			ComBaseStation cb=cp.getObject();
			String code = cb.getProcess_point_code();
			ComBaseProcessRStation c1 = new ComBaseProcessRStation();
			c1.setProcess_point_code(code);
			/*if(this.getMapper(ComBaseProcessRStationMapper.class).count(c1) > 0){
				rm.setState("500");
				rm.setMsgInfo("存在二级工序与工序点对应关系表对应子记录");
				return rm;
			}
			ComBaseAskRStation c2 = new ComBaseAskRStation();
			c2.setProcess_point_code(code);
			if(this.getMapper(ComBaseAskRStationMapper.class).count(c2)>0){
				rm.setState("500");
				rm.setMsgInfo("存在附加要求项与工序点对应关系表对应子记录");
				return rm;
			}
			
			if(validate(cb.getProcess_code(), cb.getProcess_point_code())>0){
				rm.setState("500");
				rm.setMsgInfo("存在工序点与工艺参数对应子记录");
				return rm;
			}*/
			cb.setProcess_point_code(cb.getProcess_point_code().trim());
			cb.setProcess_point_desc(cb.getProcess_point_desc().trim());
			
			if (cb.getProcess_point_desc() == null || "".equals(cb.getProcess_point_desc())) {
				rm.setState("403");
				rm.setMsgInfo("工序点名称不能为空");
				return rm;
			}
			
			//工序点唯一性验证
			String condition = " PROCESS_POINT_DESC = '" + cb.getProcess_point_desc() + "' and PROCESS_POINT_CODE <> '" + cb.getProcess_point_code() + "'";
			int num = this.getMapper(ComBaseStationMapper.class).countWithCondition(null, condition);
			if (num!=0) {
				rm.setState("403");
				rm.setMsgInfo("工序点名称重复，无法修改");
				return rm;
			}//
			ComBaseStation cbs = this.getMapper(ComBaseStationMapper.class).findById(cb.getProcess_point_code());
			if (cbs!=null) {
				if ("1".equals(cbs.getValidflag())) {
					
					cb.setUpdate_name(username);
					cb.setUpdate_time(new Date());
					this.getMapper(ComBaseStationMapper.class).update(cb);
					rm.setState("200");
				}else {
					rm.setState("500");
					rm.setMsgInfo("待修改操作的数据状态不是有效！");
					return rm;
				} 
			}else {
				rm.setState("500");
				rm.setMsgInfo("待修改操作的数据在数据库中不存在！");
				return rm;
			} 
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败");
			rm.setErrorInfo(ex.getMessage());
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	/**
	 * WEB 工序点界面：作废 
	 * @param process_point_code 工序点代码(主键)
	 * @return
	 */
	public ResultModel doCancelStationWeb(CommonPage<ComBaseStation> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			ComBaseStation cb = cp.getObject();
			String code = cb.getProcess_point_code();
			ComBaseProcessRStation c1 = new ComBaseProcessRStation();
			c1.setProcess_point_code(code);
			if(this.getMapper(ComBaseProcessRStationMapper.class).count(c1) > 0){
				rm.setState("500");
				rm.setMsgInfo("存在二级工序与工序点对应关系表对应子记录");
				return rm;
			}
			ComBaseAskRStation c2 = new ComBaseAskRStation();
			c2.setProcess_point_code(code);
			if(this.getMapper(ComBaseAskRStationMapper.class).count(c2)>0){
				rm.setState("500");
				rm.setMsgInfo("存在附加要求项与工序点对应关系表对应子记录");
				return rm;
			}
			if(validate(cb.getProcess_code(), cb.getProcess_point_code())>0){
				rm.setState("500");
				rm.setMsgInfo("存在工序点与工艺参数对应子记录");
				return rm;
			}
			ComBaseStation cbs = this.getMapper(ComBaseStationMapper.class).findById(cb.getProcess_point_code());
			if(cbs!=null){
				if("1".equals(cbs.getValidflag())){
					cbs.setValidflag("0");
					cbs.setDelete_name(username);
					cbs.setDelete_time(new Date());
					this.getMapper(ComBaseStationMapper.class).update(cbs);
					rm.setState("200");
				}else {
					rm.setState("500");
					rm.setMsgInfo("选择的工序点的状态不是有效，无法作废");
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("作废失败，选择的工序点在数据库中不存在！");
			}
			
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("作废失败");
			rm.setErrorInfo(ex.getMessage());
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	
	/**
	 * WEB 工序点界面：恢复 
	 * @param processCode 工序代码(主键)
	 * @return
	 */
	public ResultModel doRecoverStationWeb(CommonPage<ComBaseStation> cp) {
		ResultModel rm = new ResultModel();
		try {
			ComBaseStation cb = cp.getObject();
			ComBaseStation cbs = this.getMapper(ComBaseStationMapper.class).findById(cb.getProcess_point_code());
			if(cbs!=null){
				if("0".equals(cbs.getValidflag())){
					this.getMapper(ComBaseStationMapper.class).DoRecoverStation(cb.getProcess_point_code());
					rm.setState("200");
				}else {
					rm.setState("403");
					rm.setMsgInfo("选择的工序点状态不是无效，无法恢复");
				}
			}else {
				rm.setState("403");
				rm.setMsgInfo("恢复失败，选择的工序点在数据库中不存在");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败");
			rm.setErrorInfo(ex.getMessage());
			ex.printStackTrace(); 
		}
		return rm;
	}

	public int validate(String param1,String param2){
		ComBaseStationRCraft c = new ComBaseStationRCraft();
		c.setProcess_code(param1);
		c.setProcess_point_code(param2);
		int count = this.getMapper(ComBaseStationRCraftMapper.class).count(c);
		return count;
	}
	
}
