/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-07-12
 * 描述            ：工艺参数标准索引表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.qcm.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.mapper.ComBaseMachineRStationMapper;
import market.Com.mapper.ComBaseStationMapper;
import market.Com.mapper.ComBaseStationRCraftMapper;
import market.Com.vo.ComBaseMachineRStation;
import market.Com.vo.ComBaseStation;
import market.Com.vo.ComBaseStationRCraft;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.qcm.mapper.ComBaseStdCraftTicItemMapper;
import market.Qms.qcm.mapper.ComBaseStdCraftTicMapper;
import market.Qms.qcm.service.ComBaseStdCraftTicService;
import market.Qms.qcm.vo.ComBaseStdCraft;
import market.Qms.qcm.vo.ComBaseStdCraftTic;
import market.Qms.qcm.vo.ComBaseStdCraftTicItem;

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

@RemoteService(path="ComBaseStdCraftTicService")
public class ComBaseStdCraftTicServiceImpl extends BaseServiceImpl<ComBaseStdCraftTic,Object> implements ComBaseStdCraftTicService {
	public ResultModel getCraftTicInfo(CommonPage<ComBaseStdCraft> sum){
		ResultModel rm=new ResultModel();
		ComBaseStdCraft cbsc=sum.getObject();
		 List<ComBaseStdCraftTic> craftTicInfo=new ArrayList<>();
		try {
			if (cbsc.getValidflag()!=null&&!cbsc.getValidflag().equals("")) {
				 craftTicInfo = this.getMapper(ComBaseStdCraftTicMapper.class).getInfo(cbsc.getCraft_std_code());
			}else{
	            craftTicInfo = this.getMapper(ComBaseStdCraftTicMapper.class).getCraftTicInfo(cbsc.getCraft_std_code());
			}
			rm.setData(craftTicInfo); 
	      rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm ;
	}
	/**
	 * 新增索引信息的同时，重新获取工序点关联的最新工艺参数项目信息
	 * @param sum
	 * @return
	 */
	public void getStationRCraftForBuild(ComBaseStdCraftTic sum){
		ComBaseStdCraftTic cbscf=sum;
		String userneme=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		int seq=0;
		
		try {
			List<ComBaseStationRCraft> listcbsr = this.getMapper(ComBaseStationRCraftMapper.class).findWithCondition(null, "PROCESS_POINT_CODE ='"+cbscf.getProcess_point_code()+"'", null, 0, 0);
		    for (ComBaseStationRCraft c : listcbsr) {
		    	ComBaseStdCraftTicItem cbsi=new ComBaseStdCraftTicItem();
		    	cbsi.setTic(cbscf.getTic());
		    	cbsi.setCraft_param_code(c.getCraft_param_code());
		    	cbsi.setCraft_param_desc(c.getCraft_param_desc());
		    	cbsi.setCreate_name(userneme);
		    	cbsi.setCreate_time(new Date());
		    	cbsi.setIndex_seq(seq);
		    	seq++;
				this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(cbsi);
			}
		    
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	/**
	 * 修改索引信息的同时，重新获取工序点关联的最新工艺参数项目信息
	 * @param cbsc
	 */
	public void getStationRCraftForRebuild(ComBaseStdCraftTic cbsc){
		boolean flag = false;
		int count = 0;
		int seq=0;
		List<ComBaseStdCraftTicItem> result = new ArrayList<ComBaseStdCraftTicItem>();
		try {
			String userneme=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			//获取COM_BASE_STATION_R_CRAFT表中的项目
			List<ComBaseStationRCraft> listcbsr = this.getMapper(ComBaseStationRCraftMapper.class).findWithCondition(null, "PROCESS_POINT_CODE ='"+cbsc.getProcess_point_code()+"'", null, 0, 0);
			//COM_BASE_STD_CRAFT_TIC_ITEM表中的项目
			List<ComBaseStdCraftTicItem> listcbsct = this.getMapper(ComBaseStdCraftTicItemMapper.class).findWithCondition(null, "tic='"+cbsc.getTic()+"'", null, 0, 0);
			//判断：如果listcbsr比listcbsct多则在COM_BASE_STD_CRAFT_TIC_ITEM中增加项目，否则删除项目
			List<ComBaseStdCraftTicItem> list = this.getMapper(ComBaseStdCraftTicItemMapper.class).findWithCondition(null, null, "INDEX_SEQ desc", 0, 0);
 			seq=list.get(0).getIndex_seq();
			for (ComBaseStationRCraft a : listcbsr) {
				for (ComBaseStdCraftTicItem b : listcbsct) {
					if (a.getCraft_param_code().equals(b.getCraft_param_code())) {
						result.add(b);
						flag = false;
						break;
					}else {
						flag=true;
					}
				}
				if (flag) {
//					new yitiao   b jilu
					ComBaseStdCraftTicItem temp = new ComBaseStdCraftTicItem();
					temp.setCraft_param_code(a.getCraft_param_code());
					temp.setCraft_param_desc(a.getCraft_param_desc());
					temp.setTic(cbsc.getTic());
					temp.setIndex_seq(seq);
					seq++;
					this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(temp);
					result.add(temp);
				}
			}
//			for (ComBaseStdCraftTicItem r : result) {
//				System.out.println(r.getCraft_param_code());
//			}
//			listcbsct.retainAll(listcbsr);//取交集
//			listcbsr.removeAll(listcbsct);//取listcbsr与listcbsct不相同的数据
//			List<ComBaseStdCraftTicItem> list=new ArrayList<>();
//			list.addAll(listcbsct);
//			int seq=list.size();
//			if (listcbsr.size()>0) {
//				List<ComBaseStationRCraft> listft=new ArrayList<ComBaseStationRCraft>(); 
//				listft.addAll(listcbsr);
//				for (ComBaseStationRCraft c : listft) {
//					ComBaseStdCraftTicItem cbsi=new ComBaseStdCraftTicItem();
//			    	cbsi.setTic(cbsc.getTic());
//			    	cbsi.setCraft_param_code(c.getCraft_param_code());
//			    	cbsi.setCraft_param_desc(c.getCraft_param_desc());
//			    	cbsi.setCreate_name(userneme);
//			    	cbsi.setCreate_time(new Date());
//			    	cbsi.setIndex_seq(seq);
//			    	seq++;
//					this.getMapper(ComBaseStdCraftTicItemMapper.class).insert(cbsi);
//				}
//			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	/**
	 * 新增索引信息
	 * @param sum
	 * @return
	 */
	public ResultModel insertCraftTicInfo(CommonPage<ComBaseStdCraftTic> sum){
		ResultModel rm=new ResultModel();
		ComBaseStdCraftTic cbscf=sum.getObject();
		try {
			  int count=0;
				String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
//				获取工艺标准代码最大值
				List<ComBaseStdCraftTic> qrlist=this.getMapper(ComBaseStdCraftTicMapper.class)
						.findWithCondition(null,null, "tic DESC", 0, 1);
				if(qrlist!=null&&qrlist.size()>0){
					String s=qrlist.get(0).getTic();
					count=Integer.parseInt(s.substring(s.length()-6, s.length()));
				}
				count++;
				String tic="TIC"+String.format("%06d",count);
				cbscf.setTic(tic);
				cbscf.setCreate_name(username);
				cbscf.setCreate_time(new Date());
				this.getMapper(ComBaseStdCraftTicMapper.class).insert(cbscf);
				getStationRCraftForBuild(cbscf);
				rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("新增信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 修改索引信息
	 * @param sum
	 * @return
	 */
	public ResultModel upCraftTicInfo(CommonPage<ComBaseStdCraftTic> sum){
		ResultModel rm=new ResultModel();
		ComBaseStdCraftTic cbscf=sum.getObject();
		try {
			  int count=0;
				String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				cbscf.setUpdate_name(username);
				cbscf.setUpdate_time(new Date());
				this.getMapper(ComBaseStdCraftTicMapper.class).upCraftTicInfo(cbscf);
				getStationRCraftForRebuild(cbscf);
				rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("修改信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 作废索引信息
	 * @param sum
	 * @return
	 */
	public ResultModel toVoidCraftTicInfo(CommonPage<ComBaseStdCraftTic> sum){
		ResultModel rm=new ResultModel();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);

		try {
			List<ComBaseStdCraftTic> listcbscf=sum.getObjectList();
			   for (ComBaseStdCraftTic c : listcbscf) {
				   ComBaseStdCraftTic cbscf=new ComBaseStdCraftTic();
				   cbscf.setTic(c.getTic());
				   cbscf.setValidflag("0");
				   cbscf.setDelete_name(username);
				   cbscf.setDelete_time(new Date());
				   this.getMapper(ComBaseStdCraftTicMapper.class).update(cbscf);
			}
			   rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("作废信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 恢复索引信息
	 * @param sum
	 * @return
	 */
	public ResultModel recorveryCraftTicInfo(CommonPage<ComBaseStdCraftTic> sum){
		ResultModel rm=new ResultModel();
		try {
			List<ComBaseStdCraftTic> listcbscf=sum.getObjectList();
			   for (ComBaseStdCraftTic c : listcbscf) {
				   ComBaseStdCraftTic cbscf=new ComBaseStdCraftTic();
				   cbscf.setTic(c.getTic());
				   this.getMapper(ComBaseStdCraftTicMapper.class).TovidSample(cbscf.getTic());;
			}
			   rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("恢复信息失败");
			rm.setErrorInfo(e.getMessage());
		} 
		return rm;
	}
	/**
	 * 获取工序点列表
	 * @return
	 */
	public ResultModel findProcessPoint(CommonPage<ComBaseStdCraft> sum){
		ResultModel rm=new ResultModel();
		ComBaseStdCraft cbsct=sum.getObject();
		try {
			List<ComBaseStation> listr = this.getMapper(ComBaseStationMapper.class).findWithCondition(null, "PROCESS_CODE='"+cbsct.getProcess_code()+"' and VALIDFLAG='1'", null, 0, 0);
		    rm.setData(listr);
		    rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询工序点失败");
			rm.setErrorInfo(e.getMessage());
		}
		
		return rm;
	}
	/**
	 * 查询机台下拉信息
	 * @param sum
	 * @return
	 */
	public ResultModel findMachine(CommonPage<ComBaseStdCraftTic> sum){
		ResultModel rm=new ResultModel();
		ComBaseStdCraftTic cbsct=sum.getObject();
		try {
			List<ComBaseMachineRStation> listm = this.getMapper(ComBaseMachineRStationMapper.class).findWithCondition(null, "PROCESS_POINT_CODE='"+cbsct.getProcess_point_code()+"'", null, 0, 0);
		   rm.setData(listm);
		   rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo("查询机台信息失败");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
}
