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

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

import market.Com.mapper.ComBasePhyBMapper;
import market.Com.mapper.ComBasePlineMapper;
import market.Com.service.ComBasePlineService;
import market.Com.vo.ComBasePline;
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 = "ComBasePlineService")
public class ComBasePlineServiceImpl extends BaseServiceImpl<ComBasePline, Object> implements ComBasePlineService {

	/**
	 * WEB 产线管理：查询
	 * 
	 * @param cp
	 *            memo1 传入包含无效是否打钩。不打钩："0";打钩："1"
	 * @return
	 */
	public ResultModel doQueryPlinWeb(CommonPage<ComBasePline> cp) {
		ResultModel rm = new ResultModel();
		int pageIndex = cp.getPageIndex();
		int pageSize = cp.getPageSize();
		PageModel<ComBasePline> pm = new PageModel<ComBasePline>(pageIndex, pageSize);
		try {
			ComBasePline 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.getPline_name() != null && !"".equals(cbp.getPline_name())) {
				condition += " and upper(PLINE_name) like '%" + cbp.getPline_name().toUpperCase() + "%'";
			}

			List<ComBasePline> plin = this.getMapper(ComBasePlineMapper.class).findWithCondition(null, condition,
					"pline_code", (pageIndex - 1) * pageSize, pageSize);
			pm.setRows(plin);
			pm.setTotal(this.getMapper(ComBasePlineMapper.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 doInsertPlinWeb(CommonPage<ComBasePline> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {

			ComBasePline cbpc = new ComBasePline();
			cbpc = cp.getObject();
			if (cbpc.getPline_name() == null || "".equals(cbpc.getPline_name()) || cbpc.getProcess_code() == null
					|| "".equals(cbpc.getProcess_code())) {
				rm.setState("500");
				rm.setMsgInfo("产线名称、工序不能为空!");
				return rm;
			}

			
			String condition = "  Pline_name = '" + cbpc.getPline_name() + "'";
			int num = this.getMapper(ComBasePlineMapper.class).countWithCondition(null, condition);

			if (num != 0) {
				rm.setState("500");
				rm.setMsgInfo("存在相同产线名称、不允许新增!");
				return rm;
			} 
			
				String Phid = this.getMapper(ComBasePhyBMapper.class).findMaxPHyCodePlin();

				cbpc.setPline_code(Phid);
				cbpc.setCreate_name(username);
				cbpc.setCreate_time(new Date());
				cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)

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

	/**
	 * WEB 产线管理界面：修改
	 * 
	 * @param cp
	 * @return
	 */
	public ResultModel doUpdatePlinWeb(CommonPage<ComBasePline> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPline_code();
		try {

			ComBasePline cbpc = new ComBasePline();
			cbpc = cp.getObject();
			if(this.getMapper(ComBasePlineMapper.class).validateproduct(cbpc.getPline_code())>0){
				rm.setState("500");
				rm.setMsgInfo("存在关联的产线适用品名关联关系表信息，不能修改！!");
				return rm;
			}
			if(this.getMapper(ComBasePlineMapper.class).validatespec(cbpc.getPline_code())>0){
				rm.setState("500");
				rm.setMsgInfo("存在关联的生产产线适用规格表信息，不能修改！!");
				return rm;
			}if(this.getMapper(ComBasePlineMapper.class).validatesteel(cbpc.getPline_code())>0){
				rm.setState("500");
				rm.setMsgInfo("存在关联的产线适用规格与适用牌号对应关系表信息，不能修改！!");
				return rm;
			}
			if (cbpc.getPline_name() != null && !"".equals(cbpc.getPline_name())) {
				String condition = "  Pline_name = '" + cbpc.getPline_name() + "' and Pline_code <> '" + cbpc.getPline_code() + "'";
				int num = this.getMapper(ComBasePlineMapper.class).countWithCondition(null, condition);

				if (num != 0) {
					rm.setState("500");
					rm.setMsgInfo("存在相同产线名称、不允许修改!");
					return rm;
				}
			}
			
			
			
			ComBasePline cbp = this.getMapper(ComBasePlineMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					cbpc.setUpdate_name(username);
					cbpc.setUpdate_time(new Date());
					cbpc.setValidflag("1");// 有效标志(1:有效，0:无效)

					this.getMapper(ComBasePlineMapper.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 Plinode
	 *            产线管理代码(主键)
	 * @return
	 */
	public ResultModel doCancelPlinWeb(CommonPage<ComBasePline> cp) {
		ResultModel rm = new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String Phy = cp.getObject().getPline_code();
		try {
			ComBasePline cbp = this.getMapper(ComBasePlineMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("1")) {
					if(this.getMapper(ComBasePlineMapper.class).validateproduct(cbp.getPline_code())>0){
						rm.setState("500");
						rm.setMsgInfo("存在关联的产线适用品名关联关系表信息，不能作废!");
						return rm;
					}
					if(this.getMapper(ComBasePlineMapper.class).validatespec(cbp.getPline_code())>0){
						rm.setState("500");
						rm.setMsgInfo("存在关联的生产产线适用规格表信息，不能作废!");
						return rm;
					}if(this.getMapper(ComBasePlineMapper.class).validatesteel(cbp.getPline_code())>0){
						rm.setState("500");
						rm.setMsgInfo("存在关联的产线适用规格与适用牌号对应关系表信息，不能作废!");
						return rm;
					}
					cbp.setPline_code(Phy);
					cbp.setValidflag("0");
					cbp.setDelete_name(username);
					cbp.setDelete_time(new Date());
					this.getMapper(ComBasePlineMapper.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 Plinode
	 *            产线管理代码(主键)
	 * @return
	 */
	public ResultModel doRecoverPlinWeb(CommonPage<ComBasePline> cp) {
		ResultModel rm = new ResultModel();

		String Phy = cp.getObject().getPline_code();
		try {
			ComBasePline cbp = this.getMapper(ComBasePlineMapper.class).findById(Phy);
			if (cbp != null) {
				if (cbp.getValidflag().equals("0")) {
					this.getMapper(ComBasePhyBMapper.class).DoRecoverPlin(Phy);
					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;
	}

/*	public int validate(String process){
		ComBaseStation c = new ComBaseStation();
		c.setProcess_code(process);
		int count = this.getMapper(ComBaseStationMapper.class).count(c);
		return count;
	}
	*/
	
}
