/***文档注释***********************************************
 * 作者               :                   
 * 创建日期      :                   
 * 描述               :        			质量一级工序
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                  刘学 
 * 修改人员      :                  2017.12.4 
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;

import java.util.Date;

import market.Com.mapper.ComBaseProcessCMapper;
import market.Com.mapper.ComBaseProcessMapper;
import market.Com.service.ComBaseProcessService;
import market.Com.vo.ComBaseProcess;
import market.Com.vo.ComBaseProcessC;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;

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 = "ComBaseProcessService")
public class ComBaseProcessServiceImpl extends BaseServiceImpl<ComBaseProcess, String> implements ComBaseProcessService {

	/**
	 * WEB 一级工序界面：查询 
	 * @param cp 
	 * memo1  传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryProcessWeb(CommonPage<ComBaseProcess> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseProcess> pm = new PageModel<ComBaseProcess>(pageIndex,pageSize);
		try {
			ComBaseProcess cbp = cp.getObject();
			String condition = "1=1";
			if ("0".equals(cp.getMemo1())) {
				condition += " and VALIDFLAG = '1'";
				cbp.setValidflag("1");
			}
			else
			{
			cbp.setValidflag(null);
			}
			if (cbp.getProcessDesc() != null && !"".equals(cbp.getProcessDesc())) {
				condition += " and upper(PROCESS_DESC) like '%" + cbp.getProcessDesc().toUpperCase() + "%'";
			}

			pm.setRows(this.getMapper(ComBaseProcessMapper.class).findWithCondition(null, condition, "process_code",
							(pageIndex - 1) * pageSize, pageSize));
			pm.setTotal(this.getMapper(ComBaseProcessMapper.class).countWithCondition(null,condition));
			rm.setData(pm);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * WEB 一级工序界面：作废 
	 * @param processCode 工序代码(主键)
	 * @return
	 */
	public ResultModel doCancelProcessWeb(CommonPage<ComBaseProcess> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String processCode = cp.getObject().getProcessCode();
		try {
			ComBaseProcessC t = new ComBaseProcessC();
			t.setProcessCdoe(processCode);
			if(this.getMapper(ComBaseProcessCMapper.class).count(t)>0){
				rm.setState("500");
				rm.setMsgInfo("存在对应的二级子目录！");
				return rm;
			}
			
			ComBaseProcess cbp = this.getMapper(ComBaseProcessMapper.class).findById(processCode);
			if (cbp!=null) {
				if (cbp.getValidflag().equals("1")) {
					
					cbp.setProcessCode(processCode);
					cbp.setValidflag("0");
					cbp.setDeleteName(username);
					cbp.setDeleteTime(new Date());
					this.getMapper(ComBaseProcessMapper.class).update(cbp);
					rm.setState("200");
				}else {
					rm.setState("500");
					rm.setMsgInfo("选择的工序的状态不是有效，无法作废!"); 
				}
			}else {
				rm.setState("500");
				rm.setMsgInfo("作废失败,选择的工序在数据库中不存在!"); 
			} 
			
			
			
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("作废失败!");
			ex.printStackTrace(); 
		}
		return rm;
	}

	/**
	 * WEB 一级工序界面：恢复 
	 * @param processCode 工序代码(主键)
	 * @return
	 */
	public ResultModel doRecoverProcessWeb(CommonPage<ComBaseProcess> cp) {
		ResultModel rm = new ResultModel();
		String processCode = cp.getObject().getProcessCode();
		try {
			this.getMapper(ComBaseProcessMapper.class).DoRecoverProcess(processCode);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	/**
	 * WEB 一级工序界面：新增
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertProcessWeb(CommonPage<ComBaseProcess> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		

		
		try {
			ComBaseProcess cb=cp.getObject();
			ComBaseProcess cbp = new ComBaseProcess(); 
			String processCode=GetProcessCode();
			
			if (cb.getProcessDesc() == null || "".equals(cb.getProcessDesc())) {
				rm.setState("500");
				rm.setMsgInfo("工序名称不能为空!");
				return rm;
			}

			String condition = "  Process_Desc = '" + cb.getProcessDesc() + "'";
			int num = this.getMapper(ComBaseProcessMapper.class).countWithCondition(null, condition);

			if (num != 0) {
				rm.setState("500");
				rm.setMsgInfo("存在相同的工序名称、不允许新增!");
				return rm;
			}
			
			
			if ("ERROR".equals(processCode)) {
				rm.setState("500");
				rm.setMsgInfo("工序代码不在A-Z的范围内！");
				return rm;
			}
			cbp.setProcessCode(processCode);//
			cbp.setProcessDesc(cb.getProcessDesc());
			cbp.setStyle("A");//工序类型(A:一级工序,B:二级工序)
			cbp.setProcessId(cb.getProcessId());
			cbp.setCost(cb.getCost());
			cbp.setMinuteProduct(cb.getMinuteProduct());//生产时间-自炼
			cbp.setMinuteWaite(cb.getMinuteWaite());//等待时间-自炼
			cbp.setCreateName(username);
			cbp.setCreateTime(new Date());
			cbp.setValidflag("1");//有效标志(1:有效，0:无效)
			cbp.setMemo(cb.getMemo());
			cbp.setIcount(cb.getIcount());
			cbp.setProcessCodeOld(null);
			cbp.setMinuteProductOut(cb.getMinuteProductOut());//生产时间-外购
			cbp.setMinuteWaiteOut(cb.getMinuteWaiteOut());//等待时间-外购
			cbp.setBatchWaite(cb.getBatchWaite());//批量等待时间-自炼
			cbp.setBatchWaiteOut(cb.getBatchWaiteOut());//批量等待时间-外购
			this.getMapper(ComBaseProcessMapper.class).insert(cbp);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	/**
	 * WEB 一级工序界面：修改
	 * @param cp
	 * @return
	 */
	public ResultModel doUpdateProcessWeb(CommonPage<ComBaseProcess> cp){
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);		 		
		try {
			ComBaseProcess cb=cp.getObject();
			ComBaseProcess cbp = this.getMapper(ComBaseProcessMapper.class).findById(cb.getProcessCode());
			
			ComBaseProcessC t = new ComBaseProcessC();
			t.setProcessCdoe(cb.getProcessCode());
			if(this.getMapper(ComBaseProcessCMapper.class).count(t)>0){
				rm.setState("500");
				rm.setMsgInfo("存在对应的二级子目录！");
				return rm;
			}
			if (cb.getProcessDesc() != null && !"".equals(cb.getProcessDesc())) {
				String condition = "  Process_Desc = '" + cb.getProcessDesc() + "' and Process_Code <> '" + cb.getProcessCode() + "'";
				int num = this.getMapper(ComBaseProcessMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同工序名称、不允许修改!");
					return rm;
				}
			}
			
			if (cbp!=null) {
				if ("1".equals(cbp.getValidflag())) {
					cbp.setProcessCode(cb.getProcessCode());//工序代码(主键)
					cbp.setProcessDesc(cb.getProcessDesc());
					cbp.setStyle("A");//工序类型(A:一级工序,B:二级工序)
					cbp.setProcessId(cb.getProcessId());
					cbp.setCost(cb.getCost());
					cbp.setMinuteProduct(cb.getMinuteProduct());//生产时间-自炼
					cbp.setMinuteWaite(cb.getMinuteWaite());//等待时间-自炼
					cbp.setUpdateName(username);
					cbp.setUpdateTime(new Date());
					cbp.setValidflag("1");//有效标志(1:有效，0:无效)
					cbp.setMemo(cb.getMemo());
					cbp.setIcount(cb.getIcount());
					cbp.setProcessCodeOld(null);
					cbp.setMinuteProductOut(cb.getMinuteProductOut());//生产时间-外购
					cbp.setMinuteWaiteOut(cb.getMinuteWaiteOut());//等待时间-外购
					cbp.setBatchWaite(cb.getBatchWaite());//批量等待时间-自炼
					cbp.setBatchWaiteOut(cb.getBatchWaiteOut());//批量等待时间-外购
					this.getMapper(ComBaseProcessMapper.class).update(cbp);
					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("修改失败!");
			ex.printStackTrace(); 
		}
		return rm;
	}
	
	
	
	
	/**
	 * 自动生成工序代码，代码生成规则：A~Z
	 * @return A-Z，超出范围返回：ERROR
	 */
	public String GetProcessCode(){
		String processCode =null;
		try {
			processCode =this.getMapper(ComBaseProcessMapper.class).findMaxProcessCode();
			if ("".equals(processCode)||processCode==null) {
				processCode = "A";
			}else {
				char[] charArray = processCode.toCharArray();
				if (charArray!=null && charArray.length > 0) {
					int Code =(int)charArray[0];
					if (Code>=65 && Code < 90) { //A~Z(不包括Z)
						Code++;
						processCode=(char)Code+"";
					}else {
						processCode = "ERROR";
					}
				}
			} 
		} catch (Exception e) {
			 
		} 
		return processCode;
	}
	
	
	
	
	
}
