/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-12-10
 * 描述            ：基础制程引用表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.com.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.ComMscQuoteBacklogMapper;
import market.Qms.com.mapper.ComMscQuoteProcessMapper;
import market.Qms.com.mapper.ComMscQuoteStationMapper;
import market.Qms.com.service.ComMscQuoteBacklogService;
import market.Qms.com.vo.ComMscQuoteBacklog;
import market.Qms.com.vo.ComMscQuoteProcess;
import market.Qms.com.vo.ComMscQuoteStation;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.impl.ComMscServiceImpl;
import market.Qms.metallurgy.vo.ComMscBacklog;
import market.Qms.metallurgy.vo.ComMscProcess;
import market.Qms.metallurgy.vo.ComMscStation;
import market.Qms.metallurgy.vo.Process;
import market.Qms.metallurgy.vo.ProcessQuote;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="ComMscQuoteBacklogService")
public class ComMscQuoteBacklogServiceImpl extends BaseServiceImpl<ComMscQuoteBacklog,Object> implements ComMscQuoteBacklogService {


	/**
	 * 添加基础制程ProcessQuote
	 */
	
	/**
	 * 制程-新增
	 */

	public ResultModel doAddZc(ProcessQuote ps) {
		ResultModel rm = new ResultModel();
		// 写入冶金规范产线表（COM_MSC_BACKLOG）
		ComMscQuoteBacklog log = ps.getObject1();
		// 获取全程产线号
		String mscpline = this.getMapper(ComMscQuoteBacklogMapper.class).GetMaxNo();
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		log.setMsc_pline(mscpline);
		log.setValidflag("1");
		// 获取当前登录人
		log.setCreate_name(username);
		log.setCreate_time(new Date());
		this.getMapper(ComMscQuoteBacklogMapper.class).insert(log);
		// 写入冶金规范工序表（COM_MSC_PROCESS）
		List<ComMscQuoteProcess> cessList = ps.getObject2();

		if (cessList.size() > 0) {
			for (ComMscQuoteProcess cess : cessList) {
				cess.setMsc_pline(mscpline);
				cess.setValidflag("1");
				cess.setGuid(DataHelper.getUUID());
				cess.setCreate_name(username);
				cess.setCreate_time(new Date());
				this.getMapper(ComMscQuoteProcessMapper.class).insert(cess);
			}
		}
		// 写入冶金规范工序点表(制程)（COM_MSC_STATION）
		List<ComMscQuoteStation> stationList = ps.getObject3();
		if (stationList.size() > 0) {
			for (ComMscQuoteStation station : stationList) {
				station.setMsc_pline(mscpline);
				station.setValidflag("1");
				long seq = 1L;
				station.setGuid(DataHelper.getUUID());
				station.setProcess_seq(seq);
				station.setCreate_name(username);
				station.setCreate_time(new Date());
				this.getMapper(ComMscQuoteStationMapper.class).insert(station);
			}
		}
		rm.setState("200");
		return rm;

	}
	
	
	
	/**
	 * 查询制程关联
	 */
	
	/**
	 * 制程-查询 冶金规范产线表COM_MSC_QUOTE_BACKLOG
	 */
	public ResultModel doFindBackLog(CommonPage<ComMscQuoteBacklog> sm) {
		ResultModel rm = new ResultModel();
		try {	
			ReturnMsg<List<ComMscQuoteBacklog>> msg = new ReturnMsg<>();
			ComMscQuoteBacklog log=sm.getObject();
			Integer pageSize= sm.getPageSize()*(sm.getPageIndex()-1);
			Integer pageIndex=sm.getPageSize()*(sm.getPageIndex()-1)+sm.getPageSize();
			List<ComMscQuoteBacklog> back = this.getMapper(ComMscQuoteBacklogMapper.class).findByPage(log.getValidflag(),pageSize,pageIndex);
			
			for (ComMscQuoteBacklog comMscQuoteBacklog : back) {	
				comMscQuoteBacklog.setRoute(doQueryStationWeb(comMscQuoteBacklog.getMsc_pline()));
			}
			
			
			msg.setResult(back);
			msg.setPageIndex(pageIndex);
			msg.setPageSize(pageSize);
			msg.setSuccess(true);
			msg.setTotal(this.getMapper(ComMscQuoteBacklogMapper.class).count(log));
			rm.setData(msg);
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			e.printStackTrace();
		}
		return rm;
	}

	
	
	/**
	 * 修改制程
	 */

	/**
	 * 制程-修改
	 */
	public ResultModel doUpdateZc(CommonPage<ProcessQuote> sm) {
		ResultModel rm = new ResultModel();

		List<ProcessQuote> pList = sm.getObjectList();
		for (ProcessQuote ps : pList) {
			// 写入冶金规范产线表（COM_MSC_BACKLOG）
			ComMscQuoteBacklog log = ps.getObject1();
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			log.setUpdate_name(username);
			log.setUpdate_time(new Date());
			this.getMapper(ComMscQuoteBacklogMapper.class).update(log);
			if (log.getDelivery_state_code()==null || "".equals(log.getDelivery_state_code())) {
				this.getMapper(ComMscQuoteBacklogMapper.class).UpdateDelivery(log.getMsc_pline());
			}
			String mscpline = log.getMsc_pline();
			// 写入冶金规范工序表（COM_MSC_PROCESS）
			this.getMapper(ComMscQuoteProcessMapper.class).deletemsc(log.getMsc_pline());
			List<ComMscQuoteProcess> cessList = ps.getObject2();
			if (cessList != null && cessList.size() > 0) {
				for (ComMscQuoteProcess comMscProcess : cessList) {
					this.getMapper(ComMscQuoteProcessMapper.class).deleteByPline(comMscProcess.getMsc_pline());
							
				}
				for (ComMscQuoteProcess cess : cessList) {
					cess.setGuid(DataHelper.getUUID());
					cess.setMsc_pline(mscpline);
					// Long
					// seq=this.getMapper(ComMscProcessMapper.class).GetMaxNo(cess.getProcess_code(),cess.getProcess_code_c());
					cess.setValidflag("1");
					cess.setCreate_name(username);
					cess.setCreate_time(new Date());
					this.getMapper(ComMscQuoteProcessMapper.class).insert(cess);
				}
			}
			// 写入冶金规范工序点表(制程)（COM_MSC_STATION）
			List<ComMscQuoteStation> stationList = ps.getObject3();
			if (stationList != null && stationList.size() > 0) {
				for (ComMscQuoteStation comMscStation : stationList) {
					//ComMscQuoteStation sta = new ComMscQuoteStation();
					//sta.setMsc(comMscStation.getMsc());
					//sta.setMsc_pline(comMscStation.getMsc_pline());
					if (comMscStation.getMsc_pline()!=null && !"".equals(comMscStation.getMsc_pline())) {
						this.getMapper(ComMscQuoteStationMapper.class).deleteByPline(comMscStation.getMsc_pline());
					}
					
				}
				for (ComMscQuoteStation station : stationList) {
					station.setGuid(DataHelper.getUUID());
					station.setMsc_pline(mscpline);
					station.setValidflag("1");
					long seq = 1L;
					station.setProcess_seq(seq);
					station.setCreate_name(username);
					station.setCreate_time(new Date());
					this.getMapper(ComMscQuoteStationMapper.class).insert(station);
				}
			}else {
				
				if (mscpline!=null && !"".equals(mscpline)) {
					this.getMapper(ComMscQuoteStationMapper.class).deleteByPline(mscpline);
				}
				
			}
		}
		rm.setState("200");
		return rm;
	}
	
	
	
	/**
	 * 作废制程
	 */

	/**
	 * 制程-作废
	 */
	public ResultModel dozfZc(CommonPage<ComMscQuoteBacklog> sm) {
		ResultModel rm = new ResultModel();
		try {
			List<ComMscQuoteBacklog> logList = sm.getObjectList();
			for (ComMscQuoteBacklog log : logList) {
				String msc = log.getMsc();
				String msc_pline = log.getMsc_pline();
				// 涉及的表：COM_MSC_BACKLOG--冶金规范产线表
				this.getMapper(ComMscQuoteBacklogMapper.class).UpdateZf(msc_pline);
				// COM_MSC_PROCESS--冶金规范工序表
				this.getMapper(ComMscQuoteProcessMapper.class).UpdateZf(msc_pline);
				// COM_MSC_STATION--冶金规范工序点表(制程)
				this.getMapper(ComMscQuoteStationMapper.class).UpdateZf(msc_pline);
				// COM_MSC_GRADE--全程产线号与钢种对应表
				//this.getMapper(ComMscGradeMapper.class).doUpdateZf(msc, msc_pline);
				// COM_MSC_GRADE_R_SPEC--制程钢种与成品规格对应表将涉及的表中满足条件的记录状态更新为无效（VALIDFLAG=‘0’）
				//this.getMapper(ComMscStationMapper.class).UpdateZfs(msc, msc_pline);
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			e.printStackTrace();
		}
		return rm;
	}

	
	/**
	 * 制程-恢复
	 */
	public ResultModel doHfZc(CommonPage<ComMscQuoteBacklog> sm) {
		ResultModel rm = new ResultModel();
		try {
			List<ComMscQuoteBacklog> logList = sm.getObjectList();
			for (ComMscQuoteBacklog log : logList) {
				//String msc = log.getMsc();
				String msc_pline = log.getMsc_pline();
				// 涉及的表：COM_MSC_BACKLOG--冶金规范产线表
				this.getMapper(ComMscQuoteBacklogMapper.class).UpdateHf(msc_pline);
				// COM_MSC_PROCESS--冶金规范工序表
				this.getMapper(ComMscQuoteProcessMapper.class).UpdateHf(msc_pline);
				// COM_MSC_STATION--冶金规范工序点表(制程)
				this.getMapper(ComMscQuoteStationMapper.class).UpdateHf(msc_pline);
				// COM_MSC_GRADE--全程产线号与钢种对应表
				//this.getMapper(ComMscGradeMapper.class).doUpdatehf(msc, msc_pline);
				// COM_MSC_GRADE_R_SPEC--制程钢种与成品规格对应表将涉及的表中满足条件的记录状态更新为无效（VALIDFLAG=‘0’）
				//this.getMapper(ComMscStationMapper.class).UpdateHfs(msc, msc_pline);
			}
			rm.setState("200");
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("恢复失败");
		}
		return rm;
	}
	
	
	
	/**
	 * 工序信息查询COM_MSC_PROCESS
	 */
	public ResultModel doFindProcess(CommonPage<ComMscQuoteProcess> cs) {
		ResultModel rm = new ResultModel();
		try {
			ComMscQuoteProcess process = cs.getObject();
			rm.setData(
					this.getMapper(ComMscQuoteProcessMapper.class).findWithCondition(null, "MSC_PLINE='"+process.getMsc_pline()+"'", "BACKLOG_SEQ", 0, 0));
			rm.setState("200");

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询制程-工序信息出错：" + e.getMessage());
		}
		return rm;
	}
	
	
	/****
	 * 通过工序代码按字母排序后依次根据全程工序顺序号对应的工序点名称进行拼接形成  工序路径
	 * @param cp
	 * @return
	 */
	public String doQueryStationWeb(String msc_pline){
		
	
		String aa=null;
		if(msc_pline!=null&&!"".equals(msc_pline)){
			 aa = this.getMapper(ComMscQuoteStationMapper.class).doSelectStation(msc_pline);
		}
			
		
		return aa;
	}
	
	
	
	/**
	 * 根据工序以及顺序号查询工序点
	 */
	/**
	 * 工序信息查询COM_MSC_PROCESS
	 */
	public ResultModel doFindStation(CommonPage<ComMscQuoteStation> cs) {
		ResultModel rm = new ResultModel();
		try {
			ComMscQuoteStation process = cs.getObject();
			rm.setData(
					this.getMapper(ComMscQuoteStationMapper.class).findWithCondition(null, "MSC_PLINE='"+process.getMsc_pline()+"' and BACKLOG_SEQ='"+process.getBacklog_seq()+"' and PROCESS_CODE='"+process.getProcess_code()
					+"' and PROCESS_CODE_C='"+process.getProcess_code_c()+"'", "BACKLOG_SEQ,PROCESS_POINT_SEQ", 0, 0));
			rm.setState("200");

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询制程-工序信息出错：" + e.getMessage());
		}
		return rm;
	}
	
	
	
	/**
	 * 引用制程确认接口
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel sureZc(CommonPage<ComMscQuoteBacklog> sm) throws Exception {
		ResultModel rm=new ResultModel();
		
		if (sm==null) {
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
		}
		String msc=sm.getMemo1();
		List<ComMscQuoteBacklog>  logs=sm.getObjectList();
		try {
			
			if (msc==null || "".equals(msc)) {
				rm.setState("500");
				rm.setMsgInfo("请传入msc");
				return rm;
			}
			
			if (logs==null || logs.size()<1) {
				rm.setState("500");
				rm.setMsgInfo("请传入制程集合");
				return rm;
			}
			
			String whole_backlog = null;
			String whole_backlog_desc=null;
			Long backlog_seq=1l;
			List<ComMscQuoteProcess> TarProcess=new ArrayList<>();
			List<ComMscQuoteStation> Tstation=new ArrayList<>();
			
			for (ComMscQuoteBacklog log : logs) {
				if (whole_backlog==null && whole_backlog_desc==null) {
					whole_backlog=log.getWhole_backlog();
					whole_backlog_desc=log.getWhole_backlog_desc();
				}else {
					whole_backlog=whole_backlog+"-"+log.getWhole_backlog();
					whole_backlog_desc=whole_backlog_desc+"-"+log.getWhole_backlog_desc();
				}
				//查询出工序
				String[] Process_codes=log.getWhole_backlog().split("-");
				Long count=1l;
				
				for (String code : Process_codes) {
					
					List<ComMscQuoteProcess> process=this.getMapper(ComMscQuoteProcessMapper.class).findWithCondition(null, "MSC_PLINE='"+log.getMsc_pline()
					+"' and BACKLOG_SEQ='"+count+"' and  PROCESS_CODE='"+code+"' and VALIDFLAG='1'", null, 0, 0);
					ComMscQuoteProcess Tprocess=process.get(0);
					Tprocess.setBacklog_seq(backlog_seq);
					Tprocess.setMsc(msc);
					TarProcess.add(Tprocess);
					
					//查询工序点
					List<ComMscQuoteStation> station=this.getMapper(ComMscQuoteStationMapper.class).findWithCondition(null, "MSC_PLINE='"+log.getMsc_pline()+
							"' and BACKLOG_SEQ='"+count+"' and  PROCESS_CODE='"+code+"' and VALIDFLAG='1'", "PROCESS_POINT_SEQ", 0, 0);
					Long station_count=1l;
					for (ComMscQuoteStation p : station) {
						p.setMsc(msc);
						p.setBacklog_seq(backlog_seq);
						p.setProcess_point_seq(station_count);
						Tstation.add(p);
						station_count++;
					}
					
					
					backlog_seq++;
					count++;
				}
					
				
			}
			//需添加的制程
			ComMscBacklog tLog=new ComMscBacklog();
			tLog.setMsc(msc);
			tLog.setMsc_pline(null);
			tLog.setWhole_backlog(whole_backlog);
			tLog.setWhole_backlog_desc(whole_backlog_desc);
			
			List<ComMscProcess> TProcess=new ArrayList<>();
			for (ComMscQuoteProcess comMscProcess : TarProcess) {
				ComMscProcess process=new ComMscProcess();
				DataHelper.CopyEntity(comMscProcess, process);
				process.setMsc_pline(null);
				TProcess.add(process);
			}
			
			List<ComMscStation> TStation=new ArrayList<>();
			for (ComMscQuoteStation comMscStation : Tstation) {
				ComMscStation st=new ComMscStation();
				DataHelper.CopyEntity(comMscStation, st);
				st.setMsc_pline(null);
				TStation.add(st);
			}
			
			
			Process ps=new Process();
			ps.setObject1(tLog);
			ps.setObject2(TProcess);
			ps.setObject3(TStation);
			this.getBean(ComMscServiceImpl.class).doAddZc(ps);
			
			
			
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("生成制程失败");
			throw new Exception(rm.getMsgInfo());
			
		}
		
		
		
		
		return rm;
		
		
		
	}
	

}
