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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseProcessCMapper;
import market.Com.mapper.ComBaseProcessRStationMapper;
import market.Com.service.ComBaseProcessCService;
import market.Com.vo.ComBaseProcessC;
import market.Com.vo.ComBaseProcessRStation;
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 = "ComBaseProcessCService")
public class ComBaseProcessCServiceImpl extends BaseServiceImpl<ComBaseProcessC, Object>
		implements ComBaseProcessCService {

	/**
	 * WEB 二级工序界面：查询
	 * 
	 * @param cp
	 *            memo1 传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryProcessCWeb(CommonPage<ComBaseProcessC> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBaseProcessC> pm = new PageModel<ComBaseProcessC>(pageIndex, pageSize);
		try {
			ComBaseProcessC 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.getProcessDescC() != null && !"".equals(cbp.getProcessDescC())) {
				//condition += " and PROCESS_DESC like '%" + cbp.getProcessDescC() + "%'";
				condition += " and upper(PROCESS_DESC_C) like '%" + cbp.getProcessDescC().toUpperCase() + "%'";
			}

			List<ComBaseProcessC> pro =this.getMapper(ComBaseProcessCMapper.class).findWithCondition(null, condition,
					"process_cdoe_c", (pageIndex - 1) * pageSize, pageSize);
			pm.setRows(pro);
			pm.setTotal(this.getMapper(ComBaseProcessCMapper.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
	 *            memo1 传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryProcessCWebByP(CommonPage<ComBaseProcessC> cp) {
		ResultModel rm = new ResultModel();
		
		try {
			ComBaseProcessC cbp = cp.getObject();
			cbp.setValidflag("1");
			List<ComBaseProcessC> pro = new ArrayList<ComBaseProcessC>();
			if(cbp.getProcessCdoe()!=null&&!"".equals(cbp.getProcessCdoe()))
			{
			
			 pro =this.getMapper(ComBaseProcessCMapper.class).find(cbp, 0, 0);
			}
			
			rm.setData(pro);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	
	/**
	 * WEB 二级工序界面：作废
	 * 
	 * @param processCode
	 *            二级工序代码(主键)
	 * @return
	 */
	public ResultModel doCancelProcessCWeb(CommonPage<ComBaseProcessC> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String processCode = cp.getObject().getProcessCdoeC();
		try {
			if(validate(processCode)>0){
				rm.setMsgInfo("二级工序与工序点对应子记录！");
				rm.setState("500");
				return rm;
			}
			ComBaseProcessC cbp = this.getMapper(ComBaseProcessCMapper.class).findById(processCode);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbp.setProcessCdoeC(processCode);
					cbp.setValidflag("0");
					cbp.setDeleteName(username);
					cbp.setDeleteTime(new Date());
					this.getMapper(ComBaseProcessCMapper.class).update(cbp);
					rm.setState("200");
				} else {
					rm.setState("500");
					rm.setMsgInfo("选择的二级工序的状态不是有效，无法作废!");
				}
			} else {
				rm.setState("500");
				rm.setMsgInfo("作废失败,选择的二级工序在数据库中不存在!");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("作废失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * WEB 二级工序界面：恢复
	 * 
	 * @param processCode
	 *            二级工序代码(主键)
	 * @return
	 */
	public ResultModel doRecoverProcessCWeb(CommonPage<ComBaseProcessC> cp) {
		ResultModel rm = new ResultModel();
		String processCode = cp.getObject().getProcessCdoeC();
		try {
			ComBaseProcessC cbp = this.getMapper(ComBaseProcessCMapper.class).findById(processCode);
			if (cbp != null) {
				if (cbp.getValidflag().equals("0")) {
					this.getMapper(ComBaseProcessCMapper.class).DoRecoverProcessC(processCode);
					rm.setState("200");
				} else {
					rm.setState("500");
					rm.setMsgInfo("选择的二级工序的状态不是无效，无法恢复!");
				}
			} else {
				rm.setState("500");
				rm.setMsgInfo("恢复失败,选择的二级工序在数据库中不存在!");
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * WEB 二级工序界面：修改
	 * 
	 * @param processCode
	 *            二级工序代码(主键)
	 * @return
	 */
	public ResultModel doUpdateProCWeb(CommonPage<ComBaseProcessC> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getProcessCdoeC();
		try {
			if(validate(Phy)>0){
				rm.setMsgInfo("二级工序与工序点对应子记录！");
				rm.setState("500");
				return rm;
			}
			ComBaseProcessC cbpc = new ComBaseProcessC();
			cbpc = cp.getObject();
			
			
			if (cbpc.getProcessDescC() != null && !"".equals(cbpc.getProcessDescC())) {
				String condition = " and Process_Desc_C = '" + cbpc.getProcessDescC() + "' and Process_Cdoe_C <> '" + cbpc.getProcessCdoeC() + "'";
				int num = this.getMapper(ComBaseProcessCMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同的工序名称、不允许修改!");
					return rm;
				}
			}

			ComBaseProcessC cbp = this.getMapper(ComBaseProcessCMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbpc.setUpdateName(username);
					cbpc.setUpdateTime(new Date());
					cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)

					this.getMapper(ComBaseProcessCMapper.class).update(cbpc);
					rm.setState("200");
				} else {
					rm.setState("500");
					rm.setMsgInfo("选择的工序的状态不是有效，无法修改!");
				}
			} else {
				rm.setState("500");
				rm.setMsgInfo("修改失败,选择的工序在数据库中不存在!");
			}
			// cbpc.setItem_code(Phid);

		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * WEB 二级工序界面：新增
	 * 
	 * @param cp
	 * @return
	 */
	public ResultModel doInsertProcessCWeb(CommonPage<ComBaseProcessC> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {

			String ProC = this.getMapper(ComBaseProcessCMapper.class).findMaxProcessCode();
			ComBaseProcessC cbpc = new ComBaseProcessC();
			

			cbpc = cp.getObject();
			
			
			if (cbpc.getProcessDescC() == null || "".equals(cbpc.getProcessDescC())) {
				rm.setState("500");
				rm.setMsgInfo("工序名称不能为空!");
				return rm;
			} 

				String condition = "   Process_Desc_C = '" + cbpc.getProcessDescC() + "'";
				int num = this.getMapper(ComBaseProcessCMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同的工序名称、不允许新增!");
					return rm;
				}
				
			cbpc.setProcessCdoeC(ProC);//

			cbpc.setCreateName(username);
			cbpc.setCreateTime(new Date());
			cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)

			cbpc.setProcessCdoeCOld(null);

			this.getMapper(ComBaseProcessCMapper.class).insert(cbpc);
			rm.setState("200");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("新增失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	public int validate(String code){
		ComBaseProcessRStation c = new ComBaseProcessRStation();
		c.setProcess_code_c(code);
		int count = this.getMapper(ComBaseProcessRStationMapper.class).count(c);
     return count;		
	}
	
}
