package com.steering.pss.sale.order.craftDesign.server;

import com.steering.pss.sale.order.craftDesign.dao.PretreatmentDao;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscEntity;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignMscJgEntity;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 工艺评审预处理
 * 
 * @author Administrator
 * 
 */
public class Pretreatment {
	private static PretreatmentDao pretreatmentDao = new PretreatmentDao();

	// 处理流程:
	// 1.分析合同行的数据
	// 2.处理原数据
	// 3.生成评审记录
	// 4.
	public String[] pretreatment(Connection con, CraftOrdDesignMscEntity craftOrdDesign,
			String designKey, String operateType, String ordLnPk)
			throws SQLException {
		// String designKey = pretreatmentDao.getDesignKey(con,
		// craftOrdDesign.getOrdLnPk());
		if (designKey.length() > 0) {
			pretreatmentDao.deleteCraftDesignMsc(con, designKey);
			//删除短接附件图片
			String mscFile = "Qcm/Msc/Appendix/" + designKey + "/";
			String deleteMscFileMsg = FTPTool.DeletePath(mscFile);
			if(!deleteMscFileMsg.equals("")){
				return new String[]{deleteMscFileMsg, ""};
			}
			
			//删除合同附加要求贴图
			String ordAddAsk = "sale/ordAsk/" + designKey + "/";
			String ordAddAskMsg = FTPTool.DeletePath(ordAddAsk);
			if(!ordAddAskMsg.equals("")){
				return new String[]{ordAddAskMsg, ""};
			}
			
			pretreatmentDao.deleteCraftDesignStdTklg(con, designKey);
			pretreatmentDao.deleteCraftDesignStdTklgLst(con, designKey);
			pretreatmentDao.deleteCraftDesignStd(con, designKey);
			pretreatmentDao.deleteCraftDesignStdCic(con, designKey);
			pretreatmentDao.deleteCraftDesignStdPic(con, designKey);
			pretreatmentDao.deleteCraftDesignStdPicSmp(con, designKey);
			pretreatmentDao.deleteCraftDesignStdSic(con, designKey);
			pretreatmentDao.deleteCraftDesignStdDic(con, designKey);
			pretreatmentDao.deleteCraftDesignStdWic(con, designKey);
			pretreatmentDao.deleteCraftDesignStdGic(con, designKey);
			pretreatmentDao.deleteCraftDesignMscBilt(con, designKey);
			pretreatmentDao.deleteCraftDesignMscBiltC(con, designKey);
			pretreatmentDao.deleteCraftDesignMscBiltCPl(con, designKey);
			pretreatmentDao.deleteCraftDesignMscBiltPl(con, designKey);
			pretreatmentDao.deleteCraftDesignMscBL(con, designKey);
			pretreatmentDao.deleteCraftDesignMscGzGg(con, designKey);
			pretreatmentDao.deleteCraftDesignMscGzGgPl(con, designKey);
			pretreatmentDao.deleteCraftDesignMscJG(con, designKey);
			//删除接箍文件
			String jgFile = "Qcm/Jg/" + designKey + "/";
			String deleteJgFileMsg = FTPTool.DeletePath(jgFile);
			if(!deleteJgFileMsg.equals("")){
				return new String[]{deleteJgFileMsg, ""};
			}
			pretreatmentDao.deleteCraftDesignMscJGBL(con, designKey);
			pretreatmentDao.deleteCraftDesignMscJGPl(con, designKey);
			pretreatmentDao.deleteCraftDesignMscPlGxPrl(con, designKey);
			pretreatmentDao.deleteCraftDesignMscPlGz(con, designKey);
			pretreatmentDao.deleteCraftDesignMscPlPrc(con, designKey);
			//删除工艺贴图
			String prcFile = "Qcm/Msc/ProcessFile/" + designKey + "/";
			String deletePrcFileMsg = FTPTool.DeletePath(prcFile);
			if(!deletePrcFileMsg.equals("")){
				return new String[]{deletePrcFileMsg, ""};
			}
			pretreatmentDao.deleteCraftDesignMscPlSti(con, designKey);
			pretreatmentDao.deleteCraftOrdDesignMscPl(con, designKey);
			pretreatmentDao.deleteCraftDesignAddAsk(con, designKey);
		}
		String designKeyA = String.valueOf(java.util.UUID.randomUUID())
				.replaceAll("-", "");
		craftOrdDesign.setDesginKey(designKeyA);
		
		pretreatmentDao.addCraftDesignMsc(con, craftOrdDesign);
		// String designKeyA = pretreatmentDao.getDesignKey(con,
		// craftOrdDesign.getOrdLnPk());
		// 针对设定制程/设定接箍
		if (designKey.length() > 0) {
			pretreatmentDao.updateCraftOrdDesignMscPl(con, designKeyA,
					designKey, craftOrdDesign.getMsc());
			pretreatmentDao.updateCraftDesignMscJG(con, designKeyA, designKey);
		}else if(operateType.equals("1")){
			pretreatmentDao.updateCraftOrdDesignMscPl2(con, designKeyA,
					ordLnPk);
			pretreatmentDao.updateCraftDesignMscJG2(con, designKeyA, ordLnPk);
		}
		return new String[]{"", designKeyA};
	}

	/**
	 * 验证传入的合同行数据是否符合标准
	 * 
	 * @param craftLine
	 *            合同行
	 * @return String
	 * @throws SQLException
	 */
	public String checkcraftLine(Connection con,
			CraftOrdDesignMscEntity craftLine) throws SQLException {
		String errMessage = "";
		if (craftLine.getProduccode().equals("")
				|| craftLine.getProducname().equals("")) {
			return "合同行品名代码/描述不能为空!";
		}
		if (craftLine.getMsc().equals("")) {
			return "合同行冶金规范码不能为空!";
		}
		if (craftLine.getStdCode().equals("")
				|| craftLine.getStdName().equals("")) {
			return "合同行标准代码/名称不能为空!";
		}
		if (craftLine.getSteelcode().equals("")
				|| craftLine.getSteelname().equals("")) {
			return "合同行钢级(牌号)代码/名称不能为空!";
		}
		if (craftLine.getSpecCode().equals("")
				|| craftLine.getSpecName().equals("")) {
			return "合同行产品规格代码/描述不能为空!";
		}
		if (craftLine.getDimater().equals("")
				|| craftLine.getHeight().equals("")) {
			return "合同行外径/壁厚不能为空!";
		}
		if (craftLine.getFinalUser().equals("")
				|| craftLine.getFinalUserDesc().equals("")) {
			return "合同行最终客户不能为空!";
		}
		if (craftLine.getAsselFl().length() != 1) {
			return "合同行ASSEL只能为0、1!";
		}
		String specStyle = pretreatmentDao.getSpecCodeStyle(con,
				craftLine.getSpecCode());
		if ((specStyle.equals("B") || specStyle.equals("D"))
				&& (craftLine.getMscStyle().equals("B")
						|| craftLine.getMscStyle().equals("C") || craftLine
						.getMscStyle().equals("D"))) {
			return "合同产品规格( " + craftLine.getSpecName()
					+ " )属于钢锭/管坯产品规格,不需要接箍,而冶金规范码:  " + craftLine.getMscDesc()
					+ "  对接箍码有要求!";
		}
		if ((specStyle.equals("B") || specStyle.equals("D"))
				&& craftLine.getAsselFl().equals("1")) {
			return "合同产品规格( " + craftLine.getSpecName()
					+ " )属于钢锭/管坯产品规格,不能对Assel有要求,但合同选择了Assel,请确定合同维护的合理性";
		}
		return errMessage;
	}
	
	public static String CheckFjImg(CraftOrdDesignMscEntity craftOrdDesign,
			Connection con) throws SQLException{
		//  Qcm/Msc/Appendix/designKey/aaa.jpg
		String setFjImgs = pretreatmentDao.getSetFjImg(con, craftOrdDesign.getOrdLnPk());
		String craftFjPath = "Qcm/Msc/Appendix/" + craftOrdDesign.getDesginKey() + "/";
		List<String> mscFjFiles = Arrays.asList(craftOrdDesign.getFileJpeg().split("#"));
		String craftFjFiles = "";
		
		//处理设定附件图片
		List<String> arySetFjImgs = new ArrayList<String>();
		if(!setFjImgs.equals("")){
			arySetFjImgs = new ArrayList(Arrays.asList(setFjImgs.split("#")));
			for(int i = arySetFjImgs.size() - 1; i >= 0; i--){
				boolean contain = false;
				for(String mscFjFile : mscFjFiles){
					if(arySetFjImgs.get(i).equals(mscFjFile)){
						contain = true;
					}
				}
				if(!contain){
					arySetFjImgs.remove(i);
				}
			}
			if(arySetFjImgs.size() > 0){
				mscFjFiles = arySetFjImgs;
			}
		}
		if(arySetFjImgs.size() == 0 && mscFjFiles.size() > 1){
			String fjMsg = "系统找到多张附件图片，请设定附件图片！";
			return fjMsg;
		}
		
		for(String mscFjFile : mscFjFiles){
			String[] results = FTPTool.CopyFile(mscFjFile, craftFjPath);
			if(results[0].equals("False")){
				String fjMsg = "附件图片获取失败，请重新工艺制定！";
				return fjMsg;
			}
			if(craftFjFiles.equals("")){
				craftFjFiles = results[1];
			}else{
				craftFjFiles += "#" + results[1];
			}
		}
		craftOrdDesign.setFileJpeg(craftFjFiles);
		pretreatmentDao.updateMscFjImg(con, craftOrdDesign);
		return "";
	}
	
	/**
	 * 复制接箍螺纹图片
	 * @param jgEntity
	 * @return
	 */
	public static boolean CopyJgFile(CraftOrdDesignMscJgEntity jgEntity){
		List<String> mscJgFiles = Arrays.asList(jgEntity.getFilePath().split("#"));
		String craftJgFiles = "";
		String craftFjPath = "Qcm/Jg/" + jgEntity.getDesginKey() + "/";
		for(String mscJgFile : mscJgFiles){
			String[] results = FTPTool.CopyFile(mscJgFile, craftFjPath);
			if(results[0].equals("False")){
				return false;
			}
			if(craftJgFiles.equals("")){
				craftJgFiles = results[1];
			}else{
				craftJgFiles += "#" + results[1];
			}
		}
		jgEntity.setFilePath(craftJgFiles);
		return true;
	}
}
