package market.Qms.slmord.impl;

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

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.mapper.ComBaseMachineMapper;
import market.Com.mapper.ComBaseMachineRStationMapper;
import market.Com.mapper.ComPscMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.ComBaseMachineRStation;
import market.Com.vo.ComPsc;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.com.mapper.PscRMscMapper;
import market.Qms.com.service.impl.ComBaseConfigServiceImpl;
import market.Qms.com.vo.PscRMsc;
import market.Qms.common.DataHelper;
import market.Qms.common.ReturnMsg;
import market.Qms.metallurgy.mapper.ComMscMapper;
import market.Qms.metallurgy.mapper.ComPscRMscMapper;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComPscRMsc;
import market.Qms.qcm.mapper.QcmZbsTemplateRMscMapper;
import market.Qms.qcm.vo.QcmZbsTemplateRMsc;
import market.Qms.slmord.mapper.SlmOrdDesignMscDMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlGxPrlMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPcLstMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlPrcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlStiMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscPlStiMcMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscTicItemMapper;
import market.Qms.slmord.mapper.SlmOrdDesignMscTicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignSampleMMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdCicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdCicSmpMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdDicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdPicSmpMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStdSicMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStsTklgLstMapper;
import market.Qms.slmord.mapper.SlmOrdDesignStsTklgMapper;
import market.Qms.slmord.mapper.SlmOrdDesignZbsTemplateMapper;
import market.Qms.slmord.service.DesignOrderReviewService;
import market.Qms.slmord.service.QualityDesignAbilityRequireService;
import market.Qms.slmord.service.QualityDesignAddRequireService;
import market.Qms.slmord.service.QualityDesignInsideStdService;
import market.Qms.slmord.service.QualityDesignMscBaseService;
import market.Qms.slmord.service.QualityDesignMscNService;
import market.Qms.slmord.service.QualityDesignProLinesService;
import market.Qms.slmord.service.QualityDesignProcedureService;
import market.Qms.slmord.service.QualityDesignSteelBrandService;
import market.Qms.slmord.service.SlmOrdDesignStsTklgService;
import market.Qms.slmord.vo.SlmOrdDesignMsc;
import market.Qms.slmord.vo.SlmOrdDesignMscD;
import market.Qms.slmord.vo.SlmOrdDesignMscPl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlGxPrl;
import market.Qms.slmord.vo.SlmOrdDesignMscPlSti;
import market.Qms.slmord.vo.SlmOrdDesignMscPlStiMc;
import market.Qms.slmord.vo.SlmOrdDesignSampleM;
import market.Qms.slmord.vo.SlmOrdDesignStd;
import market.Qms.slmord.vo.SlmOrdDesignStdPic;
import market.Qms.slmord.vo.SlmOrdDesignStdPicSmp;
import market.Qms.slmord.vo.SlmOrdDesignStsTklg;
import market.Qms.slmord.vo.SlmOrdDesignStsTklgLst;
import market.Qms.slmord.vo.SlmOrdDesignZbsTemplate;
import market.Slm.ContractManagement.impl.SlmContractIdenOrgServiceImpl;
import market.Slm.ContractManagement.impl.SlmCustAlphaServiceImpl;
import market.Slm.ContractManagement.vo.SlmContractIdenOrg;
import market.Slm.ContractManagement.vo.SlmContractLineInfo;
import market.Slm.ContractManagement.vo.SlmCustAlpha;
import market.Slm.ContractManagement.vo.SlmCustAlphaChem;
import market.Slm.ContractManagement.vo.SlmCustAlphaDetection;
import market.Slm.ContractManagement.vo.SlmCustAlphaPhy;
import market.Slm.ContractManagement.vo.SlmCustAlphaSpec;
import market.Slm.OrderManagement.impl.SlmProcessPlanServiceImpl;
import market.Slm.OrderManagement.mapper.SlmProcessPlanMapper;
import market.Slm.OrderManagement.vo.SlmContractProcessPlan;
import market.Slm.OrderManagement.vo.SlmProcessPlan;

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 = "DesignOrderReviewService")
public class DesignOrderReviewImpl extends BaseServiceImpl<Object, Object> implements DesignOrderReviewService {

	/**
	 * 单条订单信息 技术评审
	 * 
	 * @param orderLine
	 * @param userName
	 * @return
	 */
	private ResultModel OrderSingleReview(SlmContractProcessPlan orderLine, String userName) {
		// TODO Auto-generated method stub
		ReturnMsg msg = new ReturnMsg();
		msg.setSuccess(false);
		ResultModel returnM = new ResultModel();
		returnM.setState("500");
		// 初步验证
		boolean pass = InfoValidation(msg, orderLine);
		if (!pass) {
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		}
		// 预处理
		String designKey = ReviewPrepare(orderLine, userName);
		if (designKey == null) {
			returnM.setMsgInfo("技术评审预处理失败：生成designKey出错");
			return returnM;
		}

		// 全局质量设计明细mapper
		SlmOrdDesignStsTklgLstMapper tkLstMapper = this.getMapper(SlmOrdDesignStsTklgLstMapper.class);
		// 初始化质量设计明细
		InitDesignDetail(tkLstMapper, orderLine, userName, designKey);

		/*
		 * //===副产品码查询 CommonPage<SlmProcessPlan> com=new CommonPage<>();
		 * SlmProcessPlan plan=new SlmProcessPlan();
		 * plan.setPlan_no(orderLine.getContract_line_no());
		 * com.setObject(plan); ResultModel
		 * pscModel=this.getBean(SlmContractPscServiceImpl.class).
		 * doQueryPscByPlanNo(com ); List<SlmContractPsc> listPsc=new
		 * ArrayList<>();//副产品码 if(pscModel.getState().equals("200")){
		 * listPsc=(List<SlmContractPsc>)pscModel.getData(); for (SlmContractPsc
		 * slmContractPsc : listPsc) {
		 * pass=InsertDesignMscD(orderLine,designKey,slmContractPsc.getPsc(),
		 * returnM, userName); if(!pass){ return returnM; } SlmOrdDesignStd
		 * designStd=new SlmOrdDesignStd(); designStd.setDesign_key(designKey);
		 * designStd.setPsc(slmContractPsc.getPsc());
		 * this.getMapper(SlmOrdDesignStdMapper.class).insert(designStd); }
		 * }else{ returnM.setMsgInfo("技术评审预处理失败："+pscModel.getMsgInfo()); return
		 * returnM; }
		 */
		// ==副产品码查询 end

		// 接下来明细类都利用此设计明细类赋值
		SlmOrdDesignStsTklgLst tklgLst = new SlmOrdDesignStsTklgLst();
		tklgLst.setDesign_key(designKey);
		tklgLst.setCreate_name(userName);
		tklgLst.setCreate_time(new Date());
		tklgLst.setFinal_user(orderLine.getUser_no());
		tklgLst.setOrder_pk(orderLine.getContract_line_no());
		tklgLst.setPsc(orderLine.getPsc());
		tklgLst.setValidflag("1");

		// === Start 验证基础冶金规范

		ComMsc _msc = new ComMsc();
		QualityDesignMscBaseService mscBaseService = this.getService(QualityDesignMscBaseService.class);
		pass = mscBaseService.ValidationMsc(orderLine, tklgLst, msg, _msc);
		_msc.setPsc(orderLine.getPsc());
		_msc.setPsc_desc(orderLine.getPsc_desc());
		tkLstMapper.update(tklgLst);
		if (!pass) {
			returnM.setData("{\"msc\":\"" + _msc.getMsc() + "\"}");
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		}
		// === End 验证基础冶金规范

		// === Start 验证交付标准(成分、材质、公差、探伤)

		pass = mscBaseService.ValidationMscStd(msg, null, _msc, orderLine, tklgLst, tkLstMapper);
		if (!pass) {
			returnM.setMsgInfo(msg.getMessage());
			returnM.setData("{\"msc\":\"" + _msc.getMsc() + "\"}");
			return returnM;
		}
		// 主产品码 写入材质试验主表
		/*
		 * if(!DataHelper.IsNullOrSpace(orderLine.getOrg_code())){ List<String>
		 * codes=DataHelper.GetListString(orderLine.getOrg_code(), ",");
		 * List<String> names=DataHelper.GetListString(orderLine.getOrg_name(),
		 * ","); pass=InsertPhySampleM(codes,names,designKey,orderLine.getPsc(),
		 * returnM); if(!pass){ return returnM; } }
		 */

		// 副产品码 写入材质试验主表
		/*
		 * for (SlmContractPsc slmContractPsc : listPsc) { List<String>
		 * orgCodes=new ArrayList<>();//认证机构编码 List<String> orgNames=new
		 * ArrayList<>();//认证机构名称 for (SlmContractIdenOrg org :
		 * slmContractPsc.getOrg_list()) { orgCodes.add(org.getOrg_code());
		 * orgNames.add(org.getOrg_name());
		 * pass=InsertPhySampleM(orgCodes,orgNames,designKey,slmContractPsc.
		 * getPsc(), returnM); if(!pass){ return returnM; } } }
		 */

		// === End 验证交付标准

		// === Start 验证合同附加要求

		QualityDesignAddRequireService addAskService = this.getService(QualityDesignAddRequireService.class);
		// pass = addAskService.ValidationOrderAsk(msg,
		// orderLine.getAdd_ask_no(), orderLine.getStd_code(), designKey);
		pass = addAskService.ValidationOrderAsk(msg, orderLine, orderLine.getStd_code(), designKey);
		if (!pass) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "合同附加要求", "F", "91", msg.getMessage(), "");
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "合同附加要求", "S", "11",
					DataHelper.IsNullOrSpace(orderLine.getAdd_ask_no()) ? "无附加要求" : "合同附加要求通过验证", "");
			tkLstMapper.update(tklgLst);
		}

		// === End 合同附加要求

		// === Start 验证产品技术要求

		QualityDesignAbilityRequireService abilityReq = this.getService(QualityDesignAbilityRequireService.class);
		pass = abilityReq.checkCustSpecialAsk(msg, _msc, orderLine);
		if (!pass) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "产品技术要求", "F", "91", msg.getMessage(), "");
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "产品技术要求", "S", "11",
					DataHelper.IsNullOrSpace(orderLine.getCustomer_alpha_no()) ? "无产品技术要求" : "产品技术要求通过验证", "");
			tkLstMapper.update(tklgLst);
		}

		// === End 产品技术要求

		// === Start 验证 msc(n)

		if (!DataHelper.IsNullOrSpace(orderLine.getMsc_n())) {
			pass = this.getService(QualityDesignMscNService.class).ValidationMscN(msg, _msc, orderLine, designKey);
			if (!pass) {
				DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "MSC(n)", "F", "91", msg.getMessage(), "");
				tklgLst.setMsc(orderLine.getMsc_n());
				tkLstMapper.update(tklgLst);
				returnM.setMsgInfo(msg.getMessage());
				return returnM;
			} else {
				DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "MSC(n)", "S", "11", "MSC(n) 验证通过", "");
				tklgLst.setMsc(orderLine.getMsc_n());
				tkLstMapper.update(tklgLst);
			}
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "MSC(n)", "S", "00", "MSC(n)无要求", "");
			tklgLst.setMsc(orderLine.getMsc_n());
			tkLstMapper.update(tklgLst);
		}

		// === End MSC(n)

		// === Start 验证制程

		List<SlmOrdDesignMscPl> listOrdMscPl = this.getService(QualityDesignProcedureService.class)
				.ValidationMscProduc(msg, designKey, orderLine);
		if (listOrdMscPl.size() < 1) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "制程", "F", "91", msg.getMessage(), "");
			if (orderLine.getMsc_n() != null) {
				tklgLst.setMsc(orderLine.getMsc_n());
			}
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "制程", "S", "11", "制程验证通过", "");
			if (orderLine.getMsc_n() != null) {
				tklgLst.setMsc(orderLine.getMsc_n());
			}
			tkLstMapper.update(tklgLst);
		}

		// === End 制程

		// === Start 验证炼钢牌号

		QualityDesignSteelBrandService brandService = this.getService(QualityDesignSteelBrandService.class);
		pass = brandService.ValidationSteelBrand(msg, listOrdMscPl, orderLine, designKey);
		if (!pass) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "炼钢牌号", "F", "91", msg.getMessage(), "");
			if (orderLine.getMsc_n() != null) {
				tklgLst.setMsc(orderLine.getMsc_n());
			}
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "炼钢牌号", "S", "11", "炼钢牌号验证通过", "");
			if (orderLine.getMsc_n() != null) {
				tklgLst.setMsc(orderLine.getMsc_n());
			}
			tkLstMapper.update(tklgLst);
		}

		// === End 验证炼钢牌号

		// === Start 制程适用产线
		QualityDesignProLinesService proLineService = this.getService(QualityDesignProLinesService.class);
		pass = proLineService.ValidationProductLine(msg, listOrdMscPl, designKey, orderLine);
		if (!pass) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "适用产线", "F", "91", msg.getMessage(), "");
			if (orderLine.getMsc_n() != null) {
				tklgLst.setMsc(orderLine.getMsc_n());
			}
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "适用产线", "S", "11", "适用产线验证通过", "");
			if (orderLine.getMsc_n() != null) {
				tklgLst.setMsc(orderLine.getMsc_n());
			}
			tkLstMapper.update(tklgLst);
		}

		// === End 制程适用产线

		// ===Start 写入机台信息

		SlmOrdDesignMscPlGxPrl _prl = new SlmOrdDesignMscPlGxPrl();
		_prl.setDesign_key(designKey);
		// 获取适用产线
		List<SlmOrdDesignMscPlGxPrl> listPrl = this.getMapper(SlmOrdDesignMscPlGxPrlMapper.class).find(_prl, 0, 0);
		for (SlmOrdDesignMscPlGxPrl pline : listPrl) {
			// 获取工序点
			SlmOrdDesignMscPlSti _sti = new SlmOrdDesignMscPlSti();
			_sti.setBacklog_seq(pline.getBacklog_seq().longValue());
			_sti.setProcess_code(pline.getProcess_code());
			_sti.setDesign_key(pline.getDesign_key());
			_sti.setMsc_pline(pline.getMsc_pline());
			List<SlmOrdDesignMscPlSti> listSti = this.getMapper(SlmOrdDesignMscPlStiMapper.class).find(_sti, 0, 0);
			// 获取产线下机台信息
			ComBaseMachine _machine = new ComBaseMachine();
			_machine.setPline_code(pline.getPline_code());
			_machine.setValidflag("1");
			List<ComBaseMachine> listMachine = this.getMapper(ComBaseMachineMapper.class).find(_machine, 0, 0);
			ComBaseMachineRStationMapper mRStationMapper = this.getMapper(ComBaseMachineRStationMapper.class);
			SlmOrdDesignMscPlStiMcMapper mcMapper = this.getMapper(SlmOrdDesignMscPlStiMcMapper.class);
			for (SlmOrdDesignMscPlSti sti : listSti) {
				for (ComBaseMachine comBaseMachine : listMachine) {
					// 获取产线下面工序点下的机台信息
					ComBaseMachineRStation _station = new ComBaseMachineRStation();
					_station.setMachine_code(comBaseMachine.getMachine_code());
					_station.setProcess_point_code(sti.getProcess_point_code());
					_station.setValidflag("1");
					List<ComBaseMachineRStation> listStation = mRStationMapper.find(_station, 0, 0);
					for (ComBaseMachineRStation station : listStation) {
						SlmOrdDesignMscPlStiMc _stiMc = new SlmOrdDesignMscPlStiMc();
						_stiMc.setDesign_key(designKey);
						_stiMc.setBacklog_seq(pline.getBacklog_seq().intValue());
						_stiMc.setMachine_code(station.getMachine_code());
						_stiMc.setMachine_name(station.getMachine_name());
						_stiMc.setMsc_pline(pline.getMsc_pline());
						_stiMc.setPline_code(pline.getPline_code());
						_stiMc.setPline_name(pline.getPline_name());
						_stiMc.setProcess_code(sti.getProcess_code());
						_stiMc.setProcess_desc(sti.getProcess_desc());
						_stiMc.setProcess_point_code(sti.getProcess_point_code());
						_stiMc.setProcess_point_desc(sti.getProcess_point_desc());
						mcMapper.insert(_stiMc);
					}
				}
			}

			// 获取工序点下机台信息

		}

		// ===End 机台信息

		// === Start 内控标准

		pass = this.getService(QualityDesignInsideStdService.class).ValidationInsideStd(msg, listOrdMscPl, orderLine,
				designKey);
		if (!pass) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "内控标准", "F", "91", msg.getMessage(), "");
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "内控标准", "S", "11", msg.getMessage(), "");
			tkLstMapper.update(tklgLst);
		}

		// === End 内控标准

		// === Start 工艺标准

		pass = this.getBean(SlmOrdDesignMscTicServiceImpl.class).ValidationCraftStd(msg, listOrdMscPl, orderLine,
				designKey);
		if (!pass) {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "工艺标准", "F", "91", msg.getMessage(), "");
			tkLstMapper.update(tklgLst);
			returnM.setMsgInfo(msg.getMessage());
			return returnM;
		} else {
			DataHelper.SetDesignStsTklgLst(orderLine, tklgLst, "工艺标准", "S", "11", msg.getMessage(), "");
			tkLstMapper.update(tklgLst);

		}

		// === End 工艺标准

		// === Start 质保书模板

		// pass=ValidationZbsTemplate(msg,orderLine.getMsc(),designKey);
		// if (!pass) {
		/*
		 * DataHelper.SetDesignStsTklgLst(orderLine,tklgLst,"质保书模板","F","91",
		 * msg. getMessage(),""); tkLstMapper.update(tklgLst);
		 * returnM.setMsgInfo(msg.getMessage());
		 * returnM.setMsgInfo(msg.getMessage()); return returnM; } else {
		 */
		/*
		 * DataHelper.SetDesignStsTklgLst(orderLine,tklgLst,"质保书模板","S","11",
		 * "质保书模板暂时不启用评审",""); tkLstMapper.update(tklgLst); }
		 */

		// === End 质保书模板

		// 主产品码 写入材质试验主表
		SlmContractIdenOrg org = new SlmContractIdenOrg();
		org.setContract_line_no(orderLine.getContract_line_no());
		org.setPsc(orderLine.getPsc());
		ResultModel rm = this.getBean(SlmContractIdenOrgServiceImpl.class).doQueryIdenOrg(org);
		List<SlmContractIdenOrg> org_codes = (List<SlmContractIdenOrg>) rm.getData();
		if (org_codes != null && org_codes.size() > 0) {
			List<String> codes = new ArrayList<String>();
			List<String> names = new ArrayList<String>();

			for (SlmContractIdenOrg slmContractIdenOrg : org_codes) {
				codes.add(slmContractIdenOrg.getOrg_code());
				names.add(slmContractIdenOrg.getOrg_name());
			}
			/*
			 * List<String> codes =
			 * DataHelper.GetListString(orderLine.getOrg_code(), ",");
			 * List<String> names =
			 * DataHelper.GetListString(orderLine.getOrg_name(), ",");
			 */
			pass = InsertPhySampleM(codes, names, designKey, orderLine.getPsc(), returnM);
			if (!pass) {
				return returnM;
			}
		} else {

			org_codes = new ArrayList<SlmContractIdenOrg>();
			ComBaseInfo info = this.getMapper(ComBaseInfoMapper.class).findById("409601");
			if (info != null) {
				SlmContractIdenOrg orgs = new SlmContractIdenOrg();
				orgs.setOrg_code(info.getBase_code());
				orgs.setOrg_name(info.getBase_name());
				org_codes.add(orgs);

			}
			List<String> codes = new ArrayList<String>();
			List<String> names = new ArrayList<String>();

			for (SlmContractIdenOrg slmContractIdenOrg : org_codes) {
				codes.add(slmContractIdenOrg.getOrg_code());
				names.add(slmContractIdenOrg.getOrg_name());
			}

			/*
			 * List<String> codes =
			 * DataHelper.GetListString(orderLine.getOrg_code(), ",");
			 * List<String> names =
			 * DataHelper.GetListString(orderLine.getOrg_name(), ",");
			 */

			pass = InsertPhySampleM(codes, names, designKey, orderLine.getPsc(), returnM);
			if (!pass) {
				return returnM;
			}

		}

		// 最后一步操作技术评审成功

		SlmOrdDesignStsTklg _tklg = new SlmOrdDesignStsTklg();
		_tklg.setDesign_key(designKey);
		_tklg.setDesign_sts("S");
		_tklg.setUpdate_name(userName);
		_tklg.setUpdate_time(new Date());
		this.getMapper(SlmOrdDesignStsTklgMapper.class).update(_tklg);

		SlmContractLineInfo line = new SlmContractLineInfo();
		line.setContract_line_no(orderLine.getContract_line_no());
		// this.getBean(SlmContractLineInfoServiceImpl.class).doReview(line);
		// //通过
		// UpdateDesignMscResult(orderLine.getContract_line_no(), "1");
		returnM.setMsgInfo("技术评审通过");
		returnM.setState("200");
		return returnM;
	}

	@Override
	public ResultModel OrderPossibleReview(CommonPage<SlmContractProcessPlan> orderLines) {
		ResultModel rm = new ResultModel();
		//String userName ="123";
		String userID = this.getSession().getAttribute(HomeService.SESSION_USER_ID).toString();
		String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
		if (orderLines.getObjectList() == null || orderLines.getObjectList().size() < 1) {
			rm.setState("404");
			rm.setMsgInfo("未新增加工计划");
			return rm;
		}

		boolean  pass=Preprocessing(rm,orderLines.getObjectList());
		
		if (!pass) {
			rm.setState("404");
			return rm;
		}
		// 数据验证
		int count = 0;
		List<String> listOrderNo = new ArrayList<>();// 合同行主键集合
		SlmProcessPlanMapper planMapper = this.getMapper(SlmProcessPlanMapper.class);
		for (SlmContractProcessPlan orderLine : orderLines.getObjectList()) {
			listOrderNo.add(orderLine.getContract_line_no());
			SlmProcessPlan plan = planMapper.findById(orderLine.getContract_line_no());
			try {
				rm = OrderSingleReview(orderLine, userName);
			} catch (Exception e) {
				rm.setState("404");
				rm.setMsgInfo("加工计划"+orderLine.getContract_line_no() + "设计失败");
				rm.setErrorInfo(e.getMessage());
				return rm;
			}
			
			try {
				//修改订单状态为评审中
				// 修改订单状态为评审中
				if ("1".equals(orderLine.getIs_auto())) {
					plan.setReview_man_name("system");
					plan.setReview_man_no("system");
				} else {
					plan.setReview_man_name(userName);
					plan.setReview_man_no(userID);
				}
				plan.setReview_time(new Date());
				this.getBean(SlmProcessPlanServiceImpl.class).doReviewing(plan);
			} catch (Exception e) {
				rm.setState("404");
				rm.setMsgInfo(orderLine.getPlan_no() + "修改订单状态失败," + "总共有：" + count + " 条加工计划通过质量设计");
				rm.setErrorInfo(e.getMessage());
				return rm;
			}

			
			if (rm.getState().equals("200")) {
				count++;
			}
		}

		ResultModel tklg = this.getService(SlmOrdDesignStsTklgService.class).GetOrdDesignStsTklgInfo(listOrderNo);
		rm.setData(tklg.getData());
		rm.setMsgInfo("总共有：" + count + " 条加工计划通过质量设计");
		return rm;
	}

	
	/**
	 * 验证质保书模板
	 * 
	 * @param msg       返回信息
	 * @param msc       冶金规范码
	 * @param designKey
	 * @return
	 */
	private boolean ValidationZbsTemplate(ReturnMsg msg, String msc, String designKey) {
		QcmZbsTemplateRMsc templateMsc = new QcmZbsTemplateRMsc();
		templateMsc.setValidflag("1");
		templateMsc.setMsc(msc);
		QcmZbsTemplateRMscMapper mscMapper = this.getMapper(QcmZbsTemplateRMscMapper.class);
		SlmOrdDesignZbsTemplateMapper stdTemplateMapper = this.getMapper(SlmOrdDesignZbsTemplateMapper.class);
		// 获取与 msc 关联的质保书模板
		List<QcmZbsTemplateRMsc> listRMsc = mscMapper.find(templateMsc, 0, 0);
		if (listRMsc.size() < 1) {
			msg.setMessage("没有找到与冶金规范码‘" + msc + "’关联的质保书模板");
			return false;
		} else {
			SlmOrdDesignZbsTemplate _zbsTemplate = new SlmOrdDesignZbsTemplate();
			try {
				DataHelper.CopyEntity(listRMsc.get(0), _zbsTemplate);
				_zbsTemplate.setDesign_key(designKey);
				stdTemplateMapper.insert(_zbsTemplate);
				return true;
			} catch (Exception ex) {
				msg.setMessage("验证质保书模板出错：" + ex.getMessage());
				return false;
			}
		}
	}

	/**
	 * 获取 所有评审节点
	 * 
	 * @return list<String>
	 */
	private List<String> GetAllReviewNodes() {
		List<String> list = new ArrayList<>();
		list.add("MSC(0)");
		list.add("成分标准");
		list.add("材质标准");
		list.add("公差标准");
		list.add("探伤标准");
		list.add("合同附加要求");
		list.add("产品技术要求");
		list.add("MSC(n)");
		list.add("制程");
		list.add("炼钢牌号");
		list.add("适用产线");
		list.add("内控标准");
		list.add("工艺标准");// list.add("质保书模板");
		return list;
	}

	/**
	 * 初始化设计明细数据
	 * 
	 * @param orderLine 订单信息
	 * @param userName  操作人
	 * @param designKey
	 */
	private void InitDesignDetail(SlmOrdDesignStsTklgLstMapper mapper, SlmContractProcessPlan orderLine,
			String userName, String designKey) {
		SlmOrdDesignStd designStd = new SlmOrdDesignStd();
		designStd.setDesign_key(designKey);
		designStd.setPsc(orderLine.getPsc());
		this.getMapper(SlmOrdDesignStdMapper.class).insert(designStd);
		List<String> reviewNodes = GetAllReviewNodes();
		List<SlmOrdDesignStsTklgLst> listTklg = new ArrayList<>();
		int seq = 0;
		for (String node : reviewNodes) {
			SlmOrdDesignStsTklgLst status = new SlmOrdDesignStsTklgLst();
			status.setDesign_key(designKey);
			status.setOrder_pk(orderLine.getContract_line_no());
			status.setLst_item(node);
			status.setPsc(orderLine.getPsc());
			status.setDesign_sts("W");
			status.setDesign_sts_rs("");
			status.setDesign_sts_desc("");
			status.setFinal_user(orderLine.getUser_no());
			status.setDesign_obj_key("");
			status.setValidflag("1");
			// 暂时找不到此信息
			// status.setUnit_code("");
			// status.setUnit_desc("");
			status.setCreate_name(userName);
			DecimalFormat df = new DecimalFormat("000");
			String index = df.format(seq + 1);
			seq++;
			status.setSeq_index(index);
			listTklg.add(status);
		}
		// 初始化设计主、从明细数据
		mapper.insertBatch(listTklg);

		SlmOrdDesignStsTklg tklg = new SlmOrdDesignStsTklg();
		tklg.setDesign_key(designKey);
		tklg.setDesign_sts("F");
		tklg.setPrdcyc_meet("W");
		tklg.setCreate_name(userName);
		tklg.setCreate_time(new Date());
		tklg.setValidflag("1");
		tklg.setOrd_ln_pk(orderLine.getContract_line_no());
		this.getMapper(SlmOrdDesignStsTklgMapper.class).insert(tklg);

	}

	/**
	 * 新增 质量设计--副产品设计主记录
	 * 
	 * @param orderLine 订单信息
	 * @param designKey
	 * @param pscCode   产品码
	 * @param model     返回信息
	 * @param userName  操作人
	 * @return
	 */
	private boolean InsertDesignMscD(SlmContractProcessPlan orderLine, String designKey, String pscCode,
			ResultModel model, String userName) {
		SlmOrdDesignMscD newMsc = new SlmOrdDesignMscD();
		try {
			ComPsc psc = this.getMapper(ComPscMapper.class).findById(pscCode);
			SlmOrdDesignMscDMapper mapper = this.getMapper(SlmOrdDesignMscDMapper.class);
			String condition = "psc='" + pscCode + "'";
			List<ComPscRMsc> listPsc = this.getMapper(ComPscRMscMapper.class).findWithCondition(null, condition,
					" create_time", 0, 0);
			ComMsc _msc = new ComMsc();
			_msc.setMsc(listPsc.get(0).getMsc());
			ComMsc msc = this.getMapper(ComMscMapper.class).findById(_msc.getMsc());
			newMsc.setDesign_key(designKey);
			newMsc.setContract_line_no(orderLine.getContract_line_no());
			newMsc.setPsc(pscCode);
			newMsc.setPsc_desc(psc.getPsc_desc());
			newMsc.setMsc(msc.getMsc());
			newMsc.setMsc_desc(msc.getMsc_desc());
			newMsc.setProd_code(psc.getProd_code());
			newMsc.setProd_name(psc.getProd_name());
			newMsc.setSteel_code(msc.getSteel_code());
			newMsc.setSteel_name(msc.getSteel_name());
			// 交货状态代码
			newMsc.setDelivery_state_code(orderLine.getDelivery_state_code()); //
			// 交货状态代码
			newMsc.setDelivery_state_desc(orderLine.getDelivery_state_desc());
			newMsc.setStd_code(msc.getStd_code());
			newMsc.setStd_name(msc.getStd_name());
			newMsc.setCreate_name(userName);
			newMsc.setCreate_time(new Date());
			newMsc.setCreate_name(userName);
			newMsc.setValidflag("1");
			mapper.insert(newMsc);
		} catch (Exception e) {
			model.setMsgInfo("写入副产品码质量设计msc出错：" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 新增 质量设计 msc
	 * 
	 * @param mapper
	 * @param orderLine
	 * @param userName
	 * @throws Exception
	 */
	private SlmOrdDesignMsc InsertDesignMsc(SlmOrdDesignMscMapper mapper, SlmContractProcessPlan orderLine,
			String userName) {
		SlmOrdDesignMsc newMsc = new SlmOrdDesignMsc();
		try {
			String designKey = DataHelper.getUUID();
			newMsc.setDesign_key(designKey);
			/*
			 * newMsc.setDesign_seq(orderLine.getContract_line_no().substring(orderLine.
			 * getContract_line_no().length() - 2,
			 * orderLine.getContract_line_no().length()));
			 */
			newMsc.setContract_line_no(orderLine.getContract_line_no());
			newMsc.setPsc(orderLine.getPsc());
			newMsc.setPsc_desc(orderLine.getPsc_desc());
			newMsc.setMsc(orderLine.getMsc());
			newMsc.setMsc_desc(orderLine.getMsc_desc());
			newMsc.setCust_alpha_no(orderLine.getCustomer_alpha_no());
			newMsc.setAlpha_code(orderLine.getUse_code());
			newMsc.setAlpha_name(orderLine.getUse_desc());
			newMsc.setProd_code(orderLine.getProd_code());
			newMsc.setProd_name(orderLine.getProd_name());
			newMsc.setSteel_code(orderLine.getSteel_code());
			newMsc.setSteel_name(orderLine.getSteel_name());
			// 交货状态代码
			newMsc.setDelivery_state_code(orderLine.getDelivery_state_code()); //
			// 交货状态代码
			newMsc.setDelivery_state_desc(orderLine.getDelivery_state_desc());
			newMsc.setStd_code(orderLine.getStd_code());
			newMsc.setStd_name(orderLine.getStd_name());
			newMsc.setThick(orderLine.getThick());
			newMsc.setWidth(orderLine.getWidth());
			newMsc.setLength(orderLine.getLength());
			newMsc.setOrder_unit(orderLine.getDelivery_unit_name());
			// 订货量
			newMsc.setOrder_qty(orderLine.getOrder_quantity());
			// 最终用户代码
			newMsc.setFinal_user_code(orderLine.getUser_no());
			newMsc.setFinal_user_desc(orderLine.getUser_name());
			newMsc.setAdd_ask_no(orderLine.getAdd_ask_no());
			newMsc.setAdd_ask_desc(orderLine.getAdd_ask_desc());
			newMsc.setCreate_name(userName);
			newMsc.setCreate_time(new Date());
			newMsc.setCreate_name(userName);
			newMsc.setCreate_time(new Date());
			newMsc.setDesign_result("0");
			newMsc.setValidflag("1");
			mapper.insert(newMsc);
		} catch (Exception e) {
			return null;
		}
		return newMsc;
	}

	/**
	 * 技术评审预处理
	 * 
	 * @param msg
	 * @param orderLine
	 * @return designKey
	 */
	private String ReviewPrepare(SlmContractProcessPlan orderLine, String userName) {
		SlmOrdDesignMsc queryMsc = new SlmOrdDesignMsc();
		queryMsc.setContract_line_no(orderLine.getContract_line_no());
		queryMsc.setValidflag("1");
		SlmOrdDesignMscMapper mapper = this.getMapper(SlmOrdDesignMscMapper.class);

		/****************************** 修改逻辑 *************************************/
		List<SlmOrdDesignMsc> listIsPass = mapper.findWithCondition(null, "CONTRACT_LINE_NO='"
				+ orderLine.getContract_line_no() + "'and VALIDFLAG='1' and(DESIGN_RESULT='1' or DESIGN_RESULT='2')",
				null, 0, 0);
		if (listIsPass != null && listIsPass.size() > 0) {
			// 将状态改变状态，其他表不做操作
			SlmOrdDesignStsTklg tklg = new SlmOrdDesignStsTklg();
			tklg.setDesign_key(listIsPass.get(0).getDesign_key());
			tklg.setValidflag("0");

			/*
			 * SlmOrdDesignStsTklgLst tklglst=new SlmOrdDesignStsTklgLst();
			 * tklglst.setDesign_key(listIsPass.get(0).getDesign_key());
			 * tklglst.setValidflag("0");
			 */

			this.getMapper(SlmOrdDesignStsTklgMapper.class).update(tklg);
			this.getMapper(SlmOrdDesignStsTklgLstMapper.class).updateKey(listIsPass.get(0).getDesign_key());
			mapper.UpdateInvalid(orderLine.getContract_line_no());
		} else {
			List<SlmOrdDesignMsc> listMsc = mapper.find(queryMsc, 0, 0);
			if (listMsc.size() > 0) {
				String oldKey = listMsc.get(0).getDesign_key();
				// 清理之前的评审结果
				mapper.UpdateInvalid(orderLine.getContract_line_no());
				this.getMapper(SlmOrdDesignZbsTemplateMapper.class).DeleteByDesignKey(oldKey);
				// this.getMapper(SlmOrdDesignMscCftMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlGxPrlMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlPcLstMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlPcMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlPrcMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlStiMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdCicMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdPicMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdPicSmpMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdSicMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdDicMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStdCicSmpMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStsTklgLstMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignStsTklgMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscPlStiMcMapper.class).DelByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignSampleMMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscTicMapper.class).DeleteByDesignKey(oldKey);
				this.getMapper(SlmOrdDesignMscTicItemMapper.class).DeleteByDesignKey(oldKey);

			}

		}

		SlmOrdDesignMsc ordDesignMsc = InsertDesignMsc(mapper, orderLine, userName);
		return ordDesignMsc != null ? ordDesignMsc.getDesign_key() : null;
	}

	/**
	 * 初步验证技术评审所需信息是否合理
	 * 
	 * @param msg       返回结果model
	 * @param orderLine 合同行与合同头信息
	 * @return
	 */
	private boolean InfoValidation(ReturnMsg msg, SlmContractProcessPlan orderLine) {
		if (orderLine == null) {
			msg.setMessage("合同行信息不能为空");
			return false;
		}
		// 验证产品码
		ComPsc psc = this.getMapper(ComPscMapper.class).findById(orderLine.getPsc());
		if (psc == null || psc.getValidflag().equals("0")) {
			msg.setMessage("合同订货产品找不到或已处于无效状态");
			return false;
		}
		
		if (orderLine.getDelivery_state_code()==null) {
			msg.setMessage("订单交货状态不能为空");
			return false;
		}
		
		
		
		return true;
	}

	/**
	 * 写入材质试验表
	 * 
	 * @param orgCodes  认证机构代码
	 * @param orgNames  认证机构名称
	 * @param designKey
	 * @param psc       产品码
	 * @param returnM   返回信息
	 * @return
	 */
	private boolean InsertPhySampleM(List<String> orgCodes, List<String> orgNames, String designKey, String psc,
			ResultModel returnM) {
		try {
			// 将材质数据写入试样信息表 多个认证机构则写多套数据
			if (orgCodes.size() > 0) {
				CommonPage<SlmOrdDesignStd> com = new CommonPage<>();
				SlmOrdDesignStd _designStd = new SlmOrdDesignStd();
				_designStd.setPsc(psc);
				_designStd.setDesign_key(designKey);
				com.setObject(_designStd);
				List<SlmOrdDesignStdPic> listPic = (List<SlmOrdDesignStdPic>) this
						.getBean(QualityDesignStdPicServiceImpl.class).GetStdPhyItem(com).getData();
				List<SlmOrdDesignSampleM> listSample = new ArrayList<>();
				SlmOrdDesignStdPicSmpMapper smpMapper = this.getMapper(SlmOrdDesignStdPicSmpMapper.class);
				int indexSeq=0;
				List<SlmOrdDesignSampleM> listIndex = this.getMapper(SlmOrdDesignSampleMMapper.class).findWithCondition(null, "DESIGN_KEY='"+designKey+"'", "INDEX_SEQ desc", 0, 0);
				if (listIndex!=null && listIndex.size()>0) {
					indexSeq=listIndex.get(0).getIndex_seq();
				}
				indexSeq++;
				
				int index = 1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
				for (SlmOrdDesignStdPic stdPic : listPic) {
					SlmOrdDesignStdPicSmp smp = new SlmOrdDesignStdPicSmp();
					smp.setPic(stdPic.getPic());// 材质索引
					smp.setPhy_code_m(stdPic.getPhy_code_m());// 材质试样组
					smp.setItem_code_d(stdPic.getItem_code_d());
					smp.setItem_code_s(stdPic.getItem_code_s());
					smp.setItem_code_t(stdPic.getItem_code_t());
					smp.setDesign_key(designKey);
					List<SlmOrdDesignStdPicSmp> listSmp = smpMapper.find(smp, 0, 0);
					String[] styles = stdPic.getSampleStyleCode().split(",");
					String[] styleNames = stdPic.getSampleStyleName().split(",");
					int styleSeq = 0;
					for (String style : styles) {// 多个试样类型写多套
						int count = 0;
						for (String orgCode : orgCodes) {// 多个认证机构写多套
							SlmOrdDesignSampleM sampleM = new SlmOrdDesignSampleM();
							DataHelper.CopyEntity(stdPic, sampleM);
							sampleM.setOrg_code(orgCode);
							sampleM.setOrg_name(orgNames.get(count));
							sampleM.setIndex_seq(indexSeq++);
							sampleM.setRate_code(listSmp.get(0).getRate());
							sampleM.setRate_desc(listSmp.get(0).getDesc_rate());
							sampleM.setSample_style_code(style);
							sampleM.setSample_style_name(styleNames[styleSeq]);
							sampleM.setPsc(psc);
							//sampleM.set
							listSample.add(sampleM);
							
							//index++;
							count++;
						}
						styleSeq++;
					}
				}
				if (listSample!=null && listSample.size()>0) {
				this.getMapper(SlmOrdDesignSampleMMapper.class).insertBatch(listSample);	
				}
				
			}
		} catch (Exception ex) {
			returnM.setState("500");
			returnM.setMsgInfo("写入材质试样信息失败：" + ex.getMessage());
			ex.printStackTrace();
			return false;
		}
		return true;

	}

	/**
	 * 修改质量设计结果
	 * 
	 * @param orderLineNo 合同行主键
	 * @param flag        标识 0 评审中，1 评审通过，2 评审失败
	 */
	private void UpdateDesignMscResult(String orderLineNo, String flag) {
		SlmOrdDesignMscMapper mscMapper = this.getMapper(SlmOrdDesignMscMapper.class);
		SlmOrdDesignMsc msc = new SlmOrdDesignMsc();
		msc.setContract_line_no(orderLineNo);
		msc.setValidflag("1");
		List<SlmOrdDesignMsc> listMsc = mscMapper.find(msc, 0, 0);
		listMsc.get(0).setDesign_result(flag);
		// 如果评审通过，给DESIGN_SEQ赋值
		if ("1".equals(flag)) {
			int count = 0;
			int a = mscMapper.countWithCondition(null, "CONTRACT_LINE_NO='" + orderLineNo + "' and DESIGN_SEQ!=null");
			count = a;
			count++;
			listMsc.get(0).setDesign_seq(String.format("%02d", count));
		}

		mscMapper.update(listMsc.get(0));
	}

	/**
	 * 确定订单是否可以评审通过
	 * 
	 * @param orderNo
	 * @return
	 */
	public ResultModel GetOrderStatus(String orderNo) {
		ResultModel rm = new ResultModel("200", "获取成功");
		try {
			SlmOrdDesignStsTklg tklg = new SlmOrdDesignStsTklg();
			tklg.setValidflag("1");
			tklg.setOrd_ln_pk(orderNo);
			tklg.setDesign_sts("S");
			List<SlmOrdDesignStsTklg> _listTklg = this.getMapper(SlmOrdDesignStsTklgMapper.class).find(tklg, 0, 0);
			if (_listTklg.size() < 1) {
				rm.setState("403");
				rm.setMsgInfo("订单未设计通过");
				return rm;
			}
			/*SlmProcessPlan _plan = new SlmProcessPlan();
			_plan.setPlan_no(orderNo);
			_plan.setPlan_flag("1");
			List<SlmProcessPlan> list = this.getMapper(SlmProcessPlanMapper.class).find(_plan, 0, 0);
			if (list.size() < 1) {
				rm.setState("403");
				rm.setMsgInfo("订单不处于评审中");
				return rm;
			}*/
		} catch (Exception ex) {
			rm.setErrorInfo(ex.getMessage());
			rm.setState("500");
		}
		return rm;
	}

	@Override
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel OrdeReviewPass(CommonPage<SlmContractLineInfo> orderLines, boolean flag) throws Exception {
		ResultModel rm = new ResultModel();
		SlmOrdDesignStsTklgMapper tklgMapper = this.getMapper(SlmOrdDesignStsTklgMapper.class);
		SlmOrdDesignStsTklg tklg = new SlmOrdDesignStsTklg();
		String userID = this.getSession().getAttribute(HomeService.SESSION_USER_ID).toString();
		String userName = this.getSession().getAttribute(HomeService.SESSION_USER_NAME).toString();
	
		tklg.setValidflag("1");
		int passCount = 0;
		for (SlmContractLineInfo lineInfo : orderLines.getObjectList()) {
			SlmProcessPlanServiceImpl planService = this.getBean(SlmProcessPlanServiceImpl.class);
			CommonPage<SlmProcessPlan> comPage = new CommonPage<SlmProcessPlan>();
			SlmProcessPlan plan = new SlmProcessPlan();
			plan.setPlan_no(lineInfo.getContract_line_no());
			plan.setReview_man_name(userName);
			plan.setReview_man_no(userID);
			plan.setReview_time(new Date());
			tklg.setOrd_ln_pk(lineInfo.getContract_line_no());
			tklg.setDesign_sts("S");
			if (flag) {// 技术评审通过
				List<SlmOrdDesignStsTklg> _listTklg = tklgMapper.find(tklg, 0, 0);
				if (_listTklg.size() < 1) {
					continue;
				}
				UpdateDesignMscResult(lineInfo.getContract_line_no(), "1");
				//调用销售接口评审通过
				this.getBean(SlmProcessPlanServiceImpl.class).doReviewPass(plan);
			} else {// 技术评审失败
				
				UpdateDesignMscResult(lineInfo.getContract_line_no(), "0");
				// 调用销售接口评审失败
				this.getBean(SlmProcessPlanServiceImpl.class).doReviewNotPass(plan);
			}
			// 调用销售接口设计失败、成功
			// comPage.setObject(plan);
			ResultModel model = planService.doReviewWeb(comPage);
			/*
			 * if (!model.getState().equals("200")) { throw new
			 * Exception(model.getMsgInfo()); }
			 */
			passCount++;
		}
		if (passCount == 0) {
			rm.setMsgInfo(passCount + "条记录评审通过");
			rm.setState("403");
		} else {
			rm.setMsgInfo(passCount + "条记录评审通过");
			rm.setState("200");
		}
		return rm;
	}

	/**
	 * 判断订单质量设计是不是评审中状态
	 */
	public ResultModel judgeStatus(CommonPage<SlmContractProcessPlan> orderLines) {
		ResultModel rm = new ResultModel();

		try {
			List<SlmContractProcessPlan> order = orderLines.getObjectList();

			if (order.size() < 1) {
				rm.setState("500");
				rm.setMsgInfo("订单不存在");
				return rm;

			}

			// 遍历集合

			for (SlmContractProcessPlan slmContractProcessPlan : order) {

				if (!slmContractProcessPlan.getPlan_flag().equals("103413")) {

					rm.setState("500");
					rm.setMsgInfo("订单" + slmContractProcessPlan.getContract_line_no() + "不是评审中状态，不能进行质量设计");
					return rm;

				}

			}

			rm.setData(order);
			rm.setState("200");

		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("判断订单状态失败");
			rm.setErrorInfo(e.getMessage());

		}

		return rm;
	}
	
	
	public  ResultModel   Preprocess(List<SlmContractProcessPlan> objectList) {
		
		return null;
	}
	
	
	
private boolean Preprocessing(ResultModel rm, List<SlmContractProcessPlan> objectList) {
	
	for (SlmContractProcessPlan orderLine : objectList) {
		//验证订单信息是否全面
		if (orderLine == null) {
			rm.setMsgInfo("合同行信息不能为空");
			return false;
		}
		// 验证产品码
		ComPsc psc = this.getMapper(ComPscMapper.class).findById(orderLine.getPsc());
		if (psc == null || psc.getValidflag().equals("0")) {
			rm.setMsgInfo("合同订货产品找不到或已处于无效状态");
			return false;
		}
		
		
		if (orderLine.getPsc_desc()==null || "".equals(orderLine.getPsc_desc())) {
			rm.setMsgInfo("产品码描述不能为空");
			return false;
		}
		
		if (orderLine.getDelivery_state_code()==null || "".equals(orderLine.getDelivery_state_code())) {
			rm.setMsgInfo("订单交货状态代码不能为空");
			return false;
		}
		
		
		if (orderLine.getDelivery_state_desc()==null ||"".equals(orderLine.getDelivery_state_desc())) {
			rm.setMsgInfo("订单交货状态描述不能为空");
			return false;
		}
		
		if (orderLine.getThick()==null) {
			rm.setMsgInfo("订单厚度不能为空");
			return false;
		}
		
		if (orderLine.getWidth()==null) {
			rm.setMsgInfo("订单宽度不能为空");
			return false;
		}
		
		if (orderLine.getProd_code()==null || "".equals(orderLine.getProd_code())) {
			rm.setMsgInfo("产品名称代码不能为空");
			return false;
		}
		
		if (orderLine.getProd_name()==null ||"".equals(orderLine.getProd_name())) {
			rm.setMsgInfo("产品名称描述不能为空");
			return false;
		}
		
		
		if (orderLine.getSteel_code()==null || "".equals(orderLine.getSteel_code())) {
			rm.setMsgInfo("产品牌号代码不能为空");
			return false;
		}
		
		
		if (orderLine.getSteel_name()==null || "".equals(orderLine.getSteel_name())) {
			rm.setMsgInfo("产品牌号描述不能为空");
			return false;
		}
		
		
		if (orderLine.getStd_code()==null || "".equals(orderLine.getStd_code())) {
			rm.setMsgInfo("订货标准代码不能为空");
			return false;
		}
		
		if (orderLine.getStd_name()==null || "".equals(orderLine.getStd_name())) {
			rm.setMsgInfo("订货标准名称不能为空");
			return false;
		}
		
		String config=this.getBean(ComBaseConfigServiceImpl.class).getConfig("SIC0010");
		if ("0".equals(config)) {
			if (orderLine.getH_precisicon_type_code()==null ||"".equals(orderLine.getH_precisicon_type_code())) {
				rm.setMsgInfo("订单精度类型代码不能为空");
				return false;
			}
			
			if (orderLine.getH_precisicon_type_desc()==null || "".equals(orderLine.getH_precisicon_type_desc())) {
				rm.setMsgInfo("订单精度类型描述不能为空");
				return false;
			}
		}
		if (orderLine.getCut_edge_type_code()==null || "".equals(orderLine.getCut_edge_type_code())) {
			rm.setMsgInfo("订单切边方式代码不能为空");
			return false;
		}
		if (orderLine.getCut_edge_type_name()==null || "".equals(orderLine.getCut_edge_type_name())) {
			rm.setMsgInfo("订单切边方式名称不能为空");
			return false;
		}
		
		
		//验证订单有指定的特殊要求
		if (orderLine.getCustomer_alpha_no()!=null) {
			//订单有指定的特殊要求,最终用户不能为空
			if (orderLine.getUser_no()==null || "".equals(orderLine.getUser_no())) {
				rm.setMsgInfo("订单存在特殊要求，但最终用户代码为空");
				return false;
			}
			
			if (orderLine.getUser_name()==null || "".equals(orderLine.getUser_name())) {
				rm.setMsgInfo("订单存在特殊要求，但最终用户名称为空");
				return false;
			}
			
			
			// 调用销售的接口，获取客户的特殊要求
			CommonPage<SlmCustAlpha> coms = new CommonPage<>();
			SlmCustAlpha _alpha = new SlmCustAlpha();
			_alpha.setCust_alpha_no(orderLine.getCustomer_alpha_no());
			coms.setObject(_alpha);
			ResultModel custModel = this.getBean(SlmCustAlphaServiceImpl.class).doQueryWeb(coms);
			SlmCustAlpha custAlpha = (SlmCustAlpha) custModel.getData();

			if (custAlpha==null) {
				rm.setMsgInfo("订单有特殊要求，但未查询到特殊要求信息");
				return false;
			}
			//成分项目验证
			if (custAlpha.getChemList()!=null && custAlpha.getChemList().size()>0) {
				
				for (SlmCustAlphaChem chem : custAlpha.getChemList()) {
					//增加定位记录的信息，确定是第几条
					if (chem.getChem_code()==null || "".equals(chem.getChem_code())) {
						rm.setMsgInfo("有成分要求的订单，化学元素代码不能为空");
						return false;
					}
					
					if (chem.getChem_name()==null ||"".equals(chem.getChem_name())) {
						rm.setMsgInfo("有成分要求的订单，化学元素名称不能为空");
						return false;
					}
					
					if (chem.getStd_type_code()==null || "".equals(chem.getStd_type_code())) {
						rm.setMsgInfo("有成分要求的订单，成分标准类型代码不能为空");
						return false;
					}
					
					if (chem.getStd_type_name()==null ||"".equals(chem.getStd_type_name())) {
						rm.setMsgInfo("有成分要求的订单，成分标准类型名称不能为空");
						return false;
					}
					
					if (chem.getRound_off_digits()==null) {
						rm.setMsgInfo("有成分要求的订单，修约位数不能为空");
						return false;
					}
					
					if (chem.getRound_off_rules_code()==null || "".equals(chem.getRound_off_rules_code())) {
						rm.setMsgInfo("有成分要求的订单，修约规则代码不能为空");
						return false;
					}
					
					if (chem.getRound_off_rules_name()==null ||"".equals(chem.getRound_off_rules_name())) {
						rm.setMsgInfo("有成分要求的订单，修约规则名称不能为空");
						return false;
					}
					
					
					
					
				}
				
				
				
				
			}
			
			//材质项目验证
			if (custAlpha.getPhyList()!=null && custAlpha.getPhyList().size()>0) {
				
				for (SlmCustAlphaPhy phy:custAlpha.getPhyList()) {
					
					if (phy.getPhy_code_l()==null||"".equals(phy.getPhy_code_l())) {
						rm.setMsgInfo("有材质要求的订单，材质检验大项代码不能为空");
						return false;
					}
					
					if (phy.getPhy_name_l()==null || "".equals(phy.getPhy_name_l())) {
						rm.setMsgInfo("有材质要求的订单，材质检验大项名称不能为空");
						return false;
					}
					
					if (phy.getPhy_code_m()==null ||"".equals(phy.getPhy_code_m())) {
						rm.setMsgInfo("有材质要求的订单，试样组代码不能为空");
						return false;
					}
					
					if (phy.getPhy_name_m()==null || "".equals(phy.getPhy_name_m())) {
						rm.setMsgInfo("有材质要求的订单，试样组名称不能为空");
						return false;
					}
					
					if (phy.getPhy_code_s()==null || "".equals(phy.getPhy_code_s())) {
						rm.setMsgInfo("有材质要求的订单，检验项代码不能为空");
						return false;
					}
					
					if (phy.getPhy_name_s()==null||"".equals(phy.getPhy_name_s())) {
						rm.setMsgInfo("有材质要求的订单，检验项名称不能为空");
						return false;
					}

					if (phy.getSampleStyleCode()==null||"".equals(phy.getSampleStyleCode())) {
						rm.setMsgInfo("有材质要求的订单，试样类型代码不能为空");
						return false;
					}
					
					if (phy.getSampleStyleName()==null || "".equals(phy.getSampleStyleName())) {
						rm.setMsgInfo("有材质要求的订单，试样类型名称不能为空");
						return false;
					}
					
					if (phy.getRound_off_digits()==null ) {
						rm.setMsgInfo("有材质要求的订单，俢约位数不能为空");
						return false;
					}
					
					
					if (phy.getRound_off_rules_code()==null || "".equals(phy.getRound_off_rules_code())) {
						rm.setMsgInfo("有材质要求的订单，俢约规则代码不能为空");
						return false;
					}
					
					if (phy.getRound_off_rules_name()==null || "".equals(phy.getRound_off_rules_name())) {
						rm.setMsgInfo("有材质要求的订单，俢约规则名称不能为空");
						return false;
					}
					//判定依据
					if (phy.getJudge_basis()==null || "".equals(phy.getJudge_basis())) {
						rm.setMsgInfo("有材质要求的订单，判定依据不能为空");
						return false;
					}
					
					
					
				}
				
			}
			
			//公差项目验证
			if (custAlpha.getSpecList()!=null && custAlpha.getSpecList().size()>0) {
				
				for (SlmCustAlphaSpec spec : custAlpha.getSpecList()) {
					
					if (spec.getBias_code()==null || "".equals(spec.getBias_code())) {
						rm.setMsgInfo("有公差要求的订单，公差项代码不能为空");
						return false;
					}
					
					if (spec.getBias_name()==null || "".equals(spec.getBias_name())) {
						rm.setMsgInfo("有公差要求的订单，公差项名称不能为空");
						return false;
					}
					
					
					if (orderLine.getH_precisicon_type_code()==null || "".equals(orderLine.getH_precisicon_type_code())) {
						rm.setMsgInfo("有公差要求的订单，精度类型代码不能为空");
						return false;
					}
					
					if (orderLine.getH_precisicon_type_desc()==null || "".equals(orderLine.getH_precisicon_type_desc())) {
						rm.setMsgInfo("有公差要求的订单，精度类型名称不能为空");
						return false;
					}
					
					if (spec.getLv_code()==null || "".equals(spec.getLv_code())) {
						rm.setMsgInfo("有公差要求的订单，精度等级代码不能为空");
						return false;
					}
					
					if (spec.getLv_desc()==null || "".equals(spec.getLv_desc())) {
						rm.setMsgInfo("有公差要求的订单，精度等级名称不能为空");
						return false;
					}
					
					
					
					if ("T0002".equals(spec.getBias_code())) {
						
						/*if (spec.getCut_edge_type_no()==null) {
							rm.setMsgInfo("有宽度公差要求的订单，切边方式代码不能为空");
							return false;
						}
						
						if (spec.getCut_edge_type_name()==null) {
							rm.setMsgInfo("有宽度公差要求的订单，切边方式名称不能为空");
							return false;
						}
						*/
						//这里要考虑配置项
						if (orderLine.getCut_edge_type_code()==null || "".equals(orderLine.getCut_edge_type_code())) {
							rm.setMsgInfo("有宽度公差要求的订单，订单的切边方式代码不能为空");
							return false;
						}
						
						
						if (orderLine.getCut_edge_type_name()==null || "".equals(orderLine.getCut_edge_type_name())) {
							rm.setMsgInfo("有宽度公差要求的订单，订单的切边方式名称不能为空");
							return false;
						}
						
					}
					
					
				}
				
				
				
			}
			
			
			//探伤项目过滤
			if (custAlpha.getDetectionList()!=null && custAlpha.getDetectionList().size()>0) {
				
				for (SlmCustAlphaDetection detection : custAlpha.getDetectionList()) {
					
					if (detection.getPosition_code()==null ||"".equals(detection.getPosition_code())) {
						rm.setMsgInfo("有探伤要求的订单，探伤位置代码不能为空");
						return false;
					}
					
					if (detection.getPosition_desc()==null || "".equals(detection.getPosition_desc())) {
						rm.setMsgInfo("有探伤要求的订单，探伤位置名称不能为空");
						return false;
					}
					
					
					if (detection.getStd_desc()==null || "".equals(detection.getStd_desc())) {
						rm.setMsgInfo("有探伤要求的订单，探伤参考标准不能为空");
						return false;
					}
					
					if (detection.getLv_code()==null || "".equals(detection.getLv_code())) {
						rm.setMsgInfo("有探伤要求的订单，探伤级别代码不能为空");
						return false;
					}
					
					if (detection.getLv_desc()==null || "".equals(detection.getLv_desc())) {
						rm.setMsgInfo("有探伤要求的订单，探伤级别名称不能为空");
						return false;
					}
					
					if (detection.getCoverage()==null ||"".equals(detection.getCoverage())) {
						rm.setMsgInfo("有探伤要求的订单，探伤覆盖率不能为空");
						return false;
					}
					
					if (detection.getRate()==null || "".equals(detection.getRate())) {
						rm.setMsgInfo("有探伤要求的订单，探伤频率代码不能为空");
						return false;
					}
					
					if (detection.getDesc_rate()==null || "".equals(detection.getDesc_rate())) {
						rm.setMsgInfo("有探伤要求的订单，探伤频率描述不能为空");
						return false;
					}
					
				}
				
				
				
			}
			
			
			
		}
		
		//无特殊要求
		//根据psc和有效标识为1查询psc与msc关系表COM_PSC_R_MSC
		PscRMsc pscMsc = new PscRMsc();
		pscMsc.setValidflag("1");
		pscMsc.setPsc(orderLine.getPsc());
		pscMsc.setMsc(orderLine.getMsc());
		List<PscRMsc> listPM = this.getMapper(PscRMscMapper.class).find(pscMsc, 0, 0);
		
		//根据psc和有效标识查询，若没有查询到，该节点评审失败，返回信息结束质量设计
		if (listPM==null || listPM.size() < 1) {	
			rm.setMsgInfo("没有基础制造规范码，待人工确认");
			return false;
		}
		ComMscMapper mscMapper = this.getMapper(ComMscMapper.class);
	    //根据订单的msc查询msc基础表，如果该msc对应的有效标识为D，表示该订单的msc已经无效，结束质量设计，返回信息
		ComMsc msc = mscMapper.findById(orderLine.getMsc());
		if (msc.getValidflag().equals("D")) {
			rm.setMsgInfo("订单制造规范码已无效，请修改订单或联系技术部门进行调整");
			return false;
		}

		//msc有效，判断该msc信息对应的msc_seq字段是否为空。如果不为空，代表该msc是mscn。msc_seq是msc0。
		if (!DataHelper.IsNullOrSpace(msc.getMsc_seq())) {// 如果有引用制造规范码则表示是用的mscN
			
			ComMsc msc0 = mscMapper.findById(msc.getMsc_seq());
			//如果查询到的msc为空，或者有效标识为D，结束质量设计，返回信息。
			if (msc0 == null || msc0.getValidflag().equals("D")) {	
				rm.setMsgInfo("没有基础制造规范码，待人工确认");
				return false;
			}
			
			/*List<ComMscStdSpec> sics=this.getMapper(ComMscStdSpecMapper.class).findWithCondition(null, "psc='"+orderLine.getPsc()+"' and msc='"+msc0.getMsc()+"' and VALIDFLAG!='0' and (STD_TYPE='G' or STD_TYPE='X')", null, 0, 0);
			if (sics!=null && sics.size()>0) {
				if (orderLine.getH_precisicon_type_code()==null ||"".equals(orderLine.getH_precisicon_type_code())) {
					rm.setMsgInfo("制造规范码["+msc0.getMsc()+"]有公差标准，订单精度类型代码不能为空");
					return false;
				}
				
				if (orderLine.getH_precisicon_type_desc()==null || "".equals(orderLine.getH_precisicon_type_desc())) {
					rm.setMsgInfo("制造规范码["+msc0.getMsc()+"]有公差标准，订单精度类型描述不能为空");
					return false;
				}
				
				
				if (orderLine.getCut_edge_type_code()==null || "".equals(orderLine.getCut_edge_type_code())) {
					rm.setMsgInfo("制造规范码["+msc0.getMsc()+"]有公差标准，订单切边方式代码不能为空");
					return false;
				}
				if (orderLine.getCut_edge_type_name()==null || "".equals(orderLine.getCut_edge_type_name())) {
					rm.setMsgInfo("制造规范码["+msc0.getMsc()+"]有公差标准，订单切边方式名称不能为空");
					return false;
				}
				
			
				
			}*/
			
		}
		/*List<ComMscStdSpec> sics=this.getMapper(ComMscStdSpecMapper.class).findWithCondition(null, "psc='"+orderLine.getPsc()+"' and msc='"+msc.getMsc()+"' and VALIDFLAG!='0' and (STD_TYPE='G' or STD_TYPE='X')", null, 0, 0);
		if (sics!=null && sics.size()>0) {
			
			// 区分内控标准项目与认证机构建立关联关系
			List<String> listconfig_id=new ArrayList<String>();
			listconfig_id.add("SIC0001");
			List<ComBaseConfig> conFig = (List<ComBaseConfig>) this.getBean(ComBaseConfigServiceImpl.class)
					.getBaseConfigInfo(listconfig_id).getData();
			
			
			
			if (conFig==null ||conFig.size()<1 ) {
				rm.setMsgInfo("系统配置SIC0001不存在");
				return false;
			}
			
			if ("2".equals(conFig.get(0).getConfig_val())) {
				//调用销售接口,查询指定公差项,如果msc0向下存在公差标准索引号。 
				SlmProcessPlan plan=new SlmProcessPlan();
				plan.setPlan_no(orderLine.getContract_line_no());
				ResultModel r= this.getBean(SlmContractLineLvServiceImpl.class).doQueryByPlanNoWeb(plan);
				List<SlmContractLineLv> lv_list=(List<SlmContractLineLv>) r .getData();
				
				if (lv_list==null || lv_list.size()<1) {
					rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有公差标准，订单没有指定精度等级");
					return false;
					
				}
				
					
				
				if (orderLine.getH_precisicon_type_code()==null || "".equals(orderLine.getH_precisicon_type_code())) {
					rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有公差标准，订单没有精度类型代码");
					return false;
				}
				
				if (orderLine.getH_precisicon_type_desc()==null || "".equals(orderLine.getH_precisicon_type_desc())) {
					rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有公差标准，订单没有精度类型名称");
					return false;
				}
				
				for (SlmContractLineLv pl : lv_list) {
					if (pl.getTol_code()==null ||"".equals(pl.getTol_code())) {
						rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有宽度公差标准，但指定公差项目代码为空");
						return false;
					}
					
					if (pl.getTol_name()==null || "".equals(pl.getTol_name())) {
						rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有宽度公差标准，但指定公差项目名称为空");
						return false;
					}
					
					if (pl.getLv_code()==null || "".equals(pl.getLv_code())) {
						rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有宽度公差标准，但指定公差项目["+pl.getTol_name()+"]的精度等级代码为空");
						return false;
					}
					
					if (pl.getLv_desc()==null || "".equals(pl.getLv_desc())) {
						rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有宽度公差标准，但指定公差项目["+pl.getTol_name()+"]的精度等级名称为空");
						return false;
					}
					
					if ("T0002".equals(pl.getTol_code())) {
						
						if (orderLine.getCut_edge_type_code()==null || "".equals(orderLine.getCut_edge_type_code())) {
							rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有宽度公差标准，但没有切边方式代码");
							return false;
						}
						if (orderLine.getCut_edge_type_name()==null || "".equals(orderLine.getCut_edge_type_name())) {
							rm.setMsgInfo("制造规范码["+msc.getMsc()+"]有宽度公差标准，但没有切边方式名称");
							return false;
						}
						
					}
					
				}
				
				
				
				
			}
			
			
		}*/
	
		
		
	}
	
	
	
	return true;
}

	
	
	
	
	
	
	
	
	
	
}
