/***文档注释***********************************************
 * 作者               :                   朱泽鹏
 * 创建日期      :                   2018.2.5
 * 描述               :        			工艺参数系数接口
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Com.impl;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseCraftMapper;
import market.Com.mapper.ComBaseStationRCraftMapper;
import market.Com.service.ComBaseCraftService;
import market.Com.vo.ComBaseCraft;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBaseStationRCraft;
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="ComBaseCraftService")
public class ComBaseCraftServiceImpl extends BaseServiceImpl<ComBaseCraft,Object> implements ComBaseCraftService {


	/**
	 * 页面初始化编辑区【工艺参数类型】
	 * @param phy
	 * @return
	 */
	public ResultModel doFindCbi() {
		ResultModel rm =new ResultModel();
		try {
			List<ComBaseInfo> cbilist =this.getMapper(ComBaseCraftMapper.class).dofindcsh();
			rm.setData(cbilist);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("初始化失败");
			rm.setState("500");
		}
		return rm;
	}

	/**
	 * 初始化的时候查询表中的有效数据和查询按钮的操作
	 * @param aft
	 * @return
	 */
	public ResultModel doFindAftcsh(CommonPage<ComBaseCraft> craft) {
		ResultModel rm =new ResultModel();
		ComBaseCraft aft =craft.getObject();
		try {
			int pageSize=craft.getPageSize();
			int pageIndex=craft.getPageIndex();
			String s =craft.getMemo1();
			if (s.equals("0")) {
				s="1";
			}else {
				s="";
			}
			PageModel<ComBaseCraft> pm = new PageModel<ComBaseCraft>(pageIndex,pageSize);
			String name=aft.getCraft_param_desc();
			pm.setRows(this.getMapper(ComBaseCraftMapper.class).GexList((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, name,s));
			pm.setTotal(this.getMapper(ComBaseCraftMapper.class).GexListCount(name,s));
			rm.setState("200");
			rm.setData(pm);
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询失败");
		}
		return rm;
	}

	/**
	 * 新增功能处理
	 * @param craft
	 * @return
	 */
	public ResultModel doAddWeb(CommonPage<ComBaseCraft> craft) {
		ResultModel rm =new ResultModel();
		try {
			ComBaseCraft afts =new ComBaseCraft();
			afts.setCraft_param_desc(craft.getObject().getCraft_param_desc());
			List<ComBaseCraft> craList =this.getMapper(ComBaseCraftMapper.class).find(afts, 0, 0);
			if (craList.size()>0) {
				rm.setState("500");
				rm.setMsgInfo("工艺参数名称不能和之前的一样");
			}else {
				ComBaseCraft aft =craft.getObject();
				String id ="C";
				String ls =this.getMapper(ComBaseCraftMapper.class).doFindLs();
				ls=ls.substring(2, 5);
				id=id+ls;
				aft.setCraft_param_code(id);
				aft.setValidflag("1");
				String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				aft.setCreate_name(username);
				aft.setCreate_time(new Date());
				this.getMapper(ComBaseCraftMapper.class).insert(aft);
				rm.setState("200");
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("新增失败");
		}
		return rm;
	}
	/**
	 * 修改功能
	 * @param craft
	 * @return
	 */
	public ResultModel doUpdateWeb(CommonPage<ComBaseCraft> craft) {
		ResultModel rm =new ResultModel();
		ComBaseCraft aft =craft.getObject();
		try {
			//状态为有效才能修改
			ComBaseStationRCraft cf = new ComBaseStationRCraft();
			cf.setCraft_param_code(aft.getCraft_param_code());
			if(this.getMapper(ComBaseStationRCraftMapper.class).count(cf) > 0)
			{
				rm.setState("500");
				rm.setMsgInfo("工序点与工艺参数对应表存在对应的子记录，不能修改！");
				return rm;
			}
			ComBaseCraft afts =new ComBaseCraft();
			String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			aft.setUpdate_name(username);
			aft.setUpdate_time(new Date());
			afts.setCraft_param_desc(aft.getCraft_param_desc());
			List<ComBaseCraft> craList =this.getMapper(ComBaseCraftMapper.class).find(afts, 0, 0);
			if (craList.size()>=2) {
				rm.setState("500");
				rm.setMsgInfo("工艺参数名称不能和之前的一样");
			}else if (craList.size()==1){
				if (aft.getCraft_param_code().equals(craList.get(0).getCraft_param_code())) {
					if(aft.getValidflag().trim().equals("0")) {
						rm.setState("500");
						rm.setMsgInfo("状态无效，不能修改");
					}else {
						this.getMapper(ComBaseCraftMapper.class).update(aft);
						rm.setState("200");
					}
				}else {
					rm.setState("500");
					rm.setMsgInfo("工艺参数名称不能和之前的一样");
				}
			}else {
				if(aft.getValidflag().trim().equals("0")) {
					rm.setState("500");
					rm.setMsgInfo("状态无效，不能修改");
				}else {
					this.getMapper(ComBaseCraftMapper.class).update(aft);
					rm.setState("200");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("修改失败");
		}
		return rm;
	}
	/**
	 * 作废处理
	 * @return
	 */
	public ResultModel doUpdatezf(CommonPage<ComBaseCraft> craft) {
		ResultModel rm =new ResultModel();
		ComBaseCraft aft =craft.getObject();
		try {
			ComBaseStationRCraft cf = new ComBaseStationRCraft();
			cf.setCraft_param_code(aft.getCraft_param_code());
			if(this.getMapper(ComBaseStationRCraftMapper.class).count(cf) > 0)
			{
				rm.setState("500");
				rm.setMsgInfo("工序点与工艺参数对应表存在对应的子记录，不能作废！");
				return rm;
			}
			aft =this.getMapper(ComBaseCraftMapper.class).findById(aft.getCraft_param_code());
			if(aft.getValidflag().equals("1")) {
				aft.setValidflag("0");
				String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				aft.setDelete_name(username);
				aft.setDelete_time(new Date());
				this.getMapper(ComBaseCraftMapper.class).update(aft);
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("数据无效");
			}
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("作废失败");
		}
		return rm;
	}

	/**
	 * 恢复数据
	 * @param craft
	 * @return
	 */
	public ResultModel doUpdatehf(CommonPage<ComBaseCraft> craft) {
		ResultModel rm =new ResultModel();
		ComBaseCraft aft =craft.getObject();
		try {
			ComBaseCraft afts =this.getMapper(ComBaseCraftMapper.class).findById(aft.getCraft_param_code());
			if(afts.getValidflag().equals("0")) {
				this.getMapper(ComBaseCraftMapper.class).doUPdateHf(afts.getCraft_param_code());
				rm.setState("200");
			}else {
				rm.setState("500");
				rm.setMsgInfo("数据有效，无需恢复！");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("恢复失败");
		}
		return rm;
	}
}
