/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-20
 * 描述            ：产品规范与冶金规范对应表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Qms.metallurgy.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Api.Slm.impl.SlmApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Qms.common.DataHelper;
import market.Qms.metallurgy.mapper.ComMscStdCicMapper;
import market.Qms.metallurgy.mapper.ComMscStdDetectionMapper;
import market.Qms.metallurgy.mapper.ComMscStdPicMapper;
import market.Qms.metallurgy.mapper.ComMscStdSpecMapper;
import market.Qms.metallurgy.mapper.ComPscRMscMapper;
import market.Qms.metallurgy.service.ComPscRMscService;
import market.Qms.metallurgy.vo.ComMsc;
import market.Qms.metallurgy.vo.ComMscStdCic;
import market.Qms.metallurgy.vo.ComMscStdDetection;
import market.Qms.metallurgy.vo.ComMscStdPic;
import market.Qms.metallurgy.vo.ComMscStdSpec;
import market.Qms.metallurgy.vo.ComPscRMsc;
import market.Slm.ContractManagement.impl.SlmCustAlphaServiceImpl;
import market.Slm.ContractManagement.mapper.SlmCustAlphaChemMapper;
import market.Slm.ContractManagement.mapper.SlmCustAlphaDetectionMapper;
import market.Slm.ContractManagement.mapper.SlmCustAlphaMapper;
import market.Slm.ContractManagement.mapper.SlmCustAlphaPhyMapper;
import market.Slm.ContractManagement.mapper.SlmCustAlphaSpecMapper;
import market.Slm.ContractManagement.mapper.SlmCustAlphaUseMapper;
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.ContractManagement.vo.SlmCustAlphaUse;

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;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="ComPscRMscService")
public class ComPscRMscServiceImpl extends BaseServiceImpl<ComPscRMsc,Object> implements ComPscRMscService {
	/**
	 * 查询技术标准要求
	 * @param psc 销售订单PSC
	 * @return
	 */
	public ResultModel getMscSample(String psc) {
		ResultModel rm = new ResultModel();
		try {
          List<ComMsc> mscSample = this.getMapper(ComPscRMscMapper.class).getMscSample(psc);
          rm.setData(mscSample);
          rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 查询特殊要求
	 * @param chemAlphaNo 适用客户（合同行最终用户）
	 * @return
	 */
	public ResultModel getAlphaSample(String chemAlphaNo){
		ResultModel rm=new ResultModel();
		try {
			List<SlmCustAlpha> alphaSample = this.getMapper(SlmCustAlphaMapper.class).getAlphaSample(chemAlphaNo);
		    rm.setData(alphaSample);
		    rm.setState("200"); 
		} catch (Exception e) {
			// TODO: handle exception
			 rm.setState("500");
			 rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 查询四大标准    msc和alno只会存在一个
	 * @param msc
	 * @param alNo
	 * @return
	 */
	public List<ResultModel> getStandardSample(String msc,String alNo,Double thick,Double width,Double length){
		ResultModel rm1=new ResultModel();
		ResultModel rm2=new ResultModel();
		List<ResultModel> listrm=new ArrayList<>();
		//点击G02区域查询标准
		SlmCustAlpha sml=new SlmCustAlpha();
		//点击G01区域查询标准
		ComMsc com=new ComMsc();
		List<SlmCustAlphaChem> chemList = new ArrayList<SlmCustAlphaChem>();
		List<SlmCustAlphaDetection> detectionList = new ArrayList<SlmCustAlphaDetection>();
		List<SlmCustAlphaPhy> phyList = new ArrayList<SlmCustAlphaPhy>();
		List<SlmCustAlphaSpec> specList = new ArrayList<SlmCustAlphaSpec>();
		try {
			//点击G02区域查询标准（特殊要求）
			if (alNo!=null&&!alNo.equals("")) {
				sml.setCust_alpha_no(alNo);
				chemList = this.getMapper(SlmCustAlphaChemMapper.class).findWithCondition(null, "cust_alpha_no='"+alNo+"'", null, 0, 0);
				detectionList = this.getMapper(SlmCustAlphaDetectionMapper.class).findWithCondition(null, "cust_alpha_no='"+alNo+"'", null, 0, 0);
				phyList = this.getMapper(SlmCustAlphaPhyMapper.class).findWithCondition(null, "cust_alpha_no='"+alNo+"'", null, 0, 0);
				specList = this.getMapper(SlmCustAlphaSpecMapper.class).findWithCondition(null, "cust_alpha_no='"+alNo+"'", null, 0, 0);
				//过滤限制规格
				chemList=this.getBean(SlmCustAlphaServiceImpl.class).FilterChemAl(chemList, thick, width, length);
				detectionList=this.getBean(SlmCustAlphaServiceImpl.class).FilterdetectionAl(detectionList, thick, width, length);
				phyList=this.getBean(SlmCustAlphaServiceImpl.class).FilterphyAl(phyList, thick, width, length);
				specList=this.getBean(SlmCustAlphaServiceImpl.class).FilterSpecCAl(specList, thick, width, length);
				sml.setChemList(chemList);
				sml.setPhyList(phyList);
				sml.setDetectionList(detectionList);
				sml.setSpecList(specList);
				rm1.setData(sml);
				rm1.setState("200");
			}
			//点击G01区域查询标准（技术标准）
			if (msc!=null&&!msc.equals("")) {
				com.setMsc(msc);
				chemList = this.getMapper(SlmCustAlphaChemMapper.class).doQueryStd(
						msc);
				detectionList = this.getMapper(SlmCustAlphaDetectionMapper.class)
						.doQueryStd(msc);
				phyList = this.getMapper(SlmCustAlphaPhyMapper.class).doQueryStd(
						msc);
				specList = this.getMapper(SlmCustAlphaSpecMapper.class).doQueryStd(
						msc);
				//过滤限制规格
				chemList=this.getBean(SlmCustAlphaServiceImpl.class).FilterChemAl(chemList, thick, width, length);
				detectionList=this.getBean(SlmCustAlphaServiceImpl.class).FilterdetectionAl(detectionList, thick, width, length);
				phyList=this.getBean(SlmCustAlphaServiceImpl.class).FilterphyAl(phyList, thick, width, length);
				specList=this.getBean(SlmCustAlphaServiceImpl.class).FilterSpecCAl(specList, thick, width, length);
				com.setChemList(chemList);
				com.setPhyList(phyList);
				com.setDetectionList(detectionList);
				com.setSpecList(specList);
				rm2.setData(com);
				rm2.setState("200");
			}
			listrm.add(rm1);
			listrm.add(rm2);
		} catch (Exception e) {
			// TODO: handle exception
			rm1.setState("500");
			rm2.setState("500");
		} 
		return listrm;
	}
	/**
	 *  新增特殊要求
	 * @param alNo
	 * @param chemList
	 * @param detectionList
	 * @param phyList
	 * @param specList
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel saveSpecialRequirements(String alNo,String alName,List<SlmCustAlphaChem> chemList,List<SlmCustAlphaDetection> detectionList,List<SlmCustAlphaPhy> phyList,List<SlmCustAlphaSpec> specList){
		ResultModel rm=new ResultModel();
		String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		try {
			//获取特殊要求编号
//			int count=0;
//			SimpleDateFormat sdf=new SimpleDateFormat("yyMM");
//			String date=sdf.format(new Date());
//			List<SlmCustAlphaUse> listal = this.getMapper(SlmCustAlphaUseMapper.class).findWithCondition(null, "CHEM_ALPHA_NO ='"+alNo+"' and SUBSTR(cust_alpha_no,13,4)='"+date+"'", "cust_alpha_no desc", 0, 1);
//            if (listal.size()>0&&listal.get(0).getChem_alpha_no()!=null) {
//            	String s=listal.get(0).getCust_alpha_no();
//				count=Integer.parseInt(s.substring(s.length()-4, s.length()));
//			}   
//			count++;
			String custno="";//alNo+sdf.format(new Date())+String.format("%04d",count);
			int count=0;
			List<SlmCustAlphaUse> listal = this.getMapper(SlmCustAlphaUseMapper.class).findWithCondition(null, "CHEM_ALPHA_NO ='"+alNo+"'", "cust_alpha_no desc", 0, 1);
			SimpleDateFormat sdf=new SimpleDateFormat("yyMM");
		    String date=sdf.format(new Date());
		    if (listal.size()>0&&listal.get(0).getChem_alpha_no()!=null) {
		    	String s=listal.get(0).getCust_alpha_no();
		    	if (date.equals(s.substring(12, 16))) {
		    		count=Integer.parseInt(s.substring(s.length()-4, s.length()));
		    		count++;
		    		custno=s.substring(0, s.length()-4)+String.format("%04d",count);
				}else {
					count++;
					custno=alNo+sdf.format(new Date())+String.format("%04d",count);
				}
			}else {
				count++;
				custno=alNo+sdf.format(new Date())+String.format("%04d",count);
			}
		    //将信息插入适用客户主表
		    SlmCustAlpha sca=new SlmCustAlpha();
		    sca.setCust_alpha_no(custno);
		    sca.setCust_alpha_desc(alName);
		    sca.setCust_alpha_sts("W");
		    sca.setCreate_name(username);
		    sca.setCreate_time(new Date());
		    this.getMapper(SlmCustAlphaMapper.class).insert(sca);
			//将信息插入客户成分要求表
			if (chemList.size()>0) {
				int c=0;
				for (SlmCustAlphaChem cic : chemList) {
					cic.setCust_alpha_no(custno);
					cic.setUpdate_time(new Date());
					cic.setUpdate_name(username);
					cic.setIndex_seq(++c);
					this.getMapper(SlmCustAlphaChemMapper.class).insert(cic);
				}
			}
			//将信息插入客户探伤要求表
			if (detectionList.size()>0) {
				int c=0;
				for (SlmCustAlphaDetection dic : detectionList) {
					dic.setCust_alpha_no(custno);
					dic.setUpdate_time(new Date());
					dic.setUpdate_name(username);
					dic.setIndex_seq(++c);
					this.getMapper(SlmCustAlphaDetectionMapper.class).insert(dic);
				}
			}
			//将信息插入客户材质要求表
			if (phyList.size()>0) {
				int c=0;
				for (SlmCustAlphaPhy pic : phyList) {
					pic.setCust_alpha_no(custno);
					pic.setUpdate_time(new Date());
					pic.setUpdate_name(username);
					pic.setIndex_seq(++c);
					this.getMapper(SlmCustAlphaPhyMapper.class).insert(pic);
				}
			}
			//将信息插入客户公差要求表
			if (specList.size()>0) {
				int c=0;
				for (SlmCustAlphaSpec sic : specList) {
					sic.setCust_alpha_no(custno);
					sic.setUpdate_time(new Date());
					sic.setUpdate_name(username);
					sic.setIndex_seq(++c);
					this.getMapper(SlmCustAlphaSpecMapper.class).insert(sic);
				}
			}
			//将信息客户Alpha要求适用客户表
			SlmCustAlphaUse scau=new SlmCustAlphaUse();
			scau.setCust_alpha_no(custno);
			scau.setChem_alpha_no(alNo);
			scau.setCreate_name(username);
			scau.setCreate_time(new Date());
			this.getMapper(SlmCustAlphaUseMapper.class).insert(scau);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 作废信息
	 * @param custno
	 * @return
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel TovoidAlphaSample(String custno){
		ResultModel rm=new ResultModel();
		try {
			//调用销售模块接口判断是否存在订单关联信息
			List<String> listdd = this.getBean(SlmApiServiceImpl.class).doJudgeCustomerAlphaNo(custno);
			if (listdd.size()>0) {
				rm.setState("403");
				rm.setMsgInfo("当前特殊要求存在订单关联引用，不能作废！");
				return rm;
			}
			this.getMapper(SlmCustAlphaUseMapper.class).toVoidSample(custno);
			SlmCustAlpha sca=new SlmCustAlpha();
			sca.setValidflag("0");
			sca.setCust_alpha_no(custno);
			this.getMapper(SlmCustAlphaMapper.class).update(sca);
			this.getMapper(SlmCustAlphaChemMapper.class).toVoidSample(custno);
			this.getMapper(SlmCustAlphaDetectionMapper.class).toVoidSample(custno);
			this.getMapper(SlmCustAlphaPhyMapper.class).toVoidSample(custno);
			this.getMapper(SlmCustAlphaSpecMapper.class).toVoidSample(custno);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	/**
	 * 特殊要求确认 (调用销售模块接口)
	 * @param custno
	 * @param lineno
	 * @return
	 */
	public ResultModel doConfirm(String custno,String lineno){
		ResultModel rm=new ResultModel();
		try {
			SlmContractLineInfo scl=new SlmContractLineInfo();
			scl.setCustomer_alpha_no(custno);
			scl.setContract_line_no(lineno);
			CommonPage<SlmContractLineInfo> com=new CommonPage<>();
			com.setObject(scl);
			this.getBean(SlmApiServiceImpl.class).updateCustomerAlphaNoByContractLineNo(com);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
		}
		return rm;
	}
	/**
	 * 根据MSC生成最大流水号
	 * @param msc
	 * @return
	 */
	public ResultModel getFlowNumber(String msc,String cf,String cz,String gc,String ts){
		ResultModel rm=new ResultModel();
		List<String> list=new ArrayList<>();
		try {
			if (!DataHelper.IsNullOrSpace(cf)) {
			//成分
			int countcic=0;
			List<ComMscStdCic> listcic = this.getMapper(ComMscStdCicMapper.class).findWithCondition(null, "cic like '%G%'","cic desc", 0, 0);
		     if (listcic.size()>0) {
				String s=listcic.get(0).getCic();
			     countcic=Integer.parseInt(s.substring(s.length()-6,s.length()));
			}
		     countcic++;
		     String cic="CICG"+String.format("%06d", countcic);
//		     ComMscStdCic ccic=new ComMscStdCic();
//		     ccic.setMsc(msc);
//		     ccic.setCic(cic);
//		     this.getMapper(ComMscStdCicMapper.class).insert(ccic);
		     list.add(cic);
			}else {
				list.add(null);
			}
			if (!DataHelper.IsNullOrSpace(cz)) {
			//材质
			int countpic=0;
			List<ComMscStdPic> listpic = this.getMapper(ComMscStdPicMapper.class).findWithCondition(null, "pic like '%G%'","pic desc", 0, 0);
		     if (listpic.size()>0) {
				String s=listpic.get(0).getPic();
			     countpic=Integer.parseInt(s.substring(s.length()-6,s.length()));
			}
		     countpic++;
		     String pic="PICG"+String.format("%06d", countpic);
//		     ComMscStdPic cpic=new ComMscStdPic();
//		     cpic.setMsc(msc);
//		     cpic.setPic(pic);
//		     cpic.setStdType("G");
//		     this.getMapper(ComMscStdPicMapper.class).insert(cpic);
		     list.add(pic);
			}else {
				list.add(null);
			}
			if (!DataHelper.IsNullOrSpace(gc)) {
		   //公差
				int countsic=0;
				List<ComMscStdSpec> listsic = this.getMapper(ComMscStdSpecMapper.class).findWithCondition(null, "sic like '%G%'","sic desc", 0, 0);
			     if (listsic.size()>0) {
					String s=listsic.get(0).getSic();
				     countsic=Integer.parseInt(s.substring(s.length()-6,s.length()));
				}
			     countsic++;
			     String sic="SICG"+String.format("%06d", countsic);
//			     ComMscStdSpec csic=new ComMscStdSpec();
//			     csic.setMsc(msc);
//			     csic.setSic(sic);
//			     csic.setStdType("G");
//			     this.getMapper(ComMscStdSpecMapper.class).insert(csic);
			     list.add(sic);
			}else {
				list.add(null);
			}
			if (!DataHelper.IsNullOrSpace(ts)) {
			     //探伤
					int countdic=0;
					List<ComMscStdDetection> listdic = this.getMapper(ComMscStdDetectionMapper.class).findWithCondition(null, "dic like '%G%'","dic desc", 0, 0);
				     if (listdic.size()>0) {
						String s=listdic.get(0).getDic();
					     countdic=Integer.parseInt(s.substring(s.length()-6,s.length()));
					}
				     countdic++;
				     String dic="DICG"+String.format("%06d", countdic);
//				     ComMscStdDetection cdic=new ComMscStdDetection();
//				     cdic.setMsc(msc);
//				     cdic.setDic(dic);;
//				     cdic.setStdType("G");
//				     this.getMapper(ComMscStdDetectionMapper.class).insert(cdic);
				     list.add(dic);
			}else {
				list.add(null);
			}
			rm.setData(list);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
	public ResultModel getFlowNumberByNo(String cust_no,String cf,String cz,String gc,String ts){
		ResultModel rm=new ResultModel();
		List<String> list=new ArrayList<>();
		try {
			//成分
			if (!DataHelper.IsNullOrSpace(cf)) {
				int countcic=0;
				List<SlmCustAlphaChem> listcic = this.getMapper(SlmCustAlphaChemMapper.class).findWithCondition(null, "cic like '%G%'", " CIC DESC", 0, 0);
				if (listcic.size()>0) {
					String s=listcic.get(0).getCic();
				     countcic=Integer.parseInt(s.substring(s.length()-6,s.length()));
				}
			     countcic++;
			     String cic="CICG"+String.format("%06d", countcic);
//			     SlmCustAlphaChem ccic=new SlmCustAlphaChem();
//			     ccic.setCust_alpha_no(cust_no);
//			     ccic.setCic(cic);
//			     ccic.setIndex_seq(1);
//			     this.getMapper(SlmCustAlphaChemMapper.class).insert(ccic);
			     list.add(cic);
				}else {
					list.add(null);
				}
			
			//材质
			if (!DataHelper.IsNullOrSpace(cz)) {
				//材质
				int countpic=0;
				List<SlmCustAlphaPhy> listpic = this.getMapper(SlmCustAlphaPhyMapper.class).findWithCondition(null, "pic like '%G%'","pic desc", 0, 0);
			     if (listpic.size()>0) {
					String s=listpic.get(0).getPic();
				     countpic=Integer.parseInt(s.substring(s.length()-6,s.length()));
				}
			     countpic++;
			     String pic="PICG"+String.format("%06d", countpic);
//			     SlmCustAlphaPhy cpic=new SlmCustAlphaPhy();
//			     cpic.setCust_alpha_no(cust_no);
//			     cpic.setPic(pic);
//			     cpic.setIndex_seq(1);
//			     this.getMapper(SlmCustAlphaPhyMapper.class).insert(cpic);
			     list.add(pic);
				}else {
					list.add(null);
				}
			if (!DataHelper.IsNullOrSpace(gc)) {
				   //公差
						int countsic=0;
						List<SlmCustAlphaSpec> listsic = this.getMapper(SlmCustAlphaSpecMapper.class).findWithCondition(null, "sic like '%G%'","sic desc", 0, 0);
					     if (listsic.size()>0) {
							String s=listsic.get(0).getSic();
						     countsic=Integer.parseInt(s.substring(s.length()-6,s.length()));
						}
					     countsic++;
					     String sic="SICG"+String.format("%06d", countsic);
//					     SlmCustAlphaSpec csic=new SlmCustAlphaSpec();
//					     csic.setCust_alpha_no(cust_no);
//					     csic.setSic(sic);
//					     csic.setIndex_seq(1);
//					     this.getMapper(SlmCustAlphaSpecMapper.class).insert(csic);
					     list.add(sic);
					}else {
						list.add(null);
					}
			if (!DataHelper.IsNullOrSpace(ts)) {
			     //探伤
					int countdic=0;
					List<SlmCustAlphaDetection> listdic = this.getMapper(SlmCustAlphaDetectionMapper.class).findWithCondition(null, "dic like '%G%'","dic desc", 0, 0);
				     if (listdic.size()>0) {
						String s=listdic.get(0).getDic();
					     countdic=Integer.parseInt(s.substring(s.length()-6,s.length()));
					}
				     countdic++;
				     String dic="DICG"+String.format("%06d", countdic);
//				     SlmCustAlphaDetection cdic=new SlmCustAlphaDetection();
//				     cdic.setCust_alpha_no(cust_no);
//				     cdic.setDic(dic);;
//				     cdic.setIndex_seq(1);
//				     this.getMapper(SlmCustAlphaDetectionMapper.class).insert(cdic);
				     list.add(dic);
			}else {
				list.add(null);
			}
			rm.setData(list);
			rm.setState("200");
		} catch (Exception e) {
			// TODO: handle exception
			rm.setState("500");
			rm.setErrorInfo(e.getMessage());
		}
		return rm;
	}
}
