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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.steering.comm.file.FileBean;
import com.steering.comm.file.FtpStorage;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.sale.order.craftDesign.model.CraftOrdDesignAddAskEntity;
import com.steering.pss.sale.order.craftDesign.server.FTPTool;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

public class OrderAddAskDao {
	
	public static String SaveOrderAddAsk(String designKey, String ordLnPk, Connection con) throws SQLException{
		String sqlGetOrderAddAsk = CoreXmlSqlParsersFactory.getSql("OrderAddAskDao.QueryOrderAddAskAll");
		sqlGetOrderAddAsk = StringTool.ReplaceAll(sqlGetOrderAddAsk, "#ORD_LN_PK#", ordLnPk);
		List<CraftOrdDesignAddAskEntity> orderAddAsks = EntityTool.getEntitysBySqlNoClsCon(
				sqlGetOrderAddAsk, con, CraftOrdDesignAddAskEntity.class);
		
		String descriptionAll = "";
		FtpStorage ftp = new FtpStorage();
		for(CraftOrdDesignAddAskEntity orderAddAsk : orderAddAsks){
			if(descriptionAll.equals("")){
				descriptionAll += orderAddAsk.getAskSubItmeDesc() + ":" + orderAddAsk.getAskSubItmeValDesc();
			}else{
				descriptionAll += "\r\n" + orderAddAsk.getAskSubItmeDesc() + ":" + orderAddAsk.getAskSubItmeValDesc();
			}

			//处理合同附加要求贴图
			if(orderAddAsk.getPic().equals("")) continue;
			CoreReturnObject cro = ftp.download(orderAddAsk.getPic());
			List<FileBean> listFile = (List<FileBean>)cro.getResult();
			if(listFile != null && listFile.size() > 0){
				String pic = "";
				String path = "sale/ordAsk/" + designKey + "/" + orderAddAsk.getAskSubItme() + "/";
				for(FileBean file : listFile){
					String srcFile = file.getPathName() + "/" + file.getFileName();
					String[] copyResult = FTPTool.CopyFile(srcFile, path);
					if(copyResult[0].equals("False")){
						return "获取合同附加要求贴图失败，请重新工艺制定！";
					}
					if(pic.equals("")){
						pic = copyResult[1];
					}else{
						pic += "#" + copyResult[1];
					}
				}
				orderAddAsk.setPic(pic);
			}
		}
		
		String descriptionDiff = "";
		String sqlGetAddAskDiff = CoreXmlSqlParsersFactory.getSql("OrderAddAskDao.QueryOrderAddAskDiff");
		sqlGetAddAskDiff = StringTool.ReplaceAll(sqlGetAddAskDiff, "#ORD_LN_PK#", ordLnPk);
		List<CraftOrdDesignAddAskEntity> orderAddAskDiffs = EntityTool.getEntitysBySqlNoClsCon(
				sqlGetAddAskDiff, con, CraftOrdDesignAddAskEntity.class);
		for(CraftOrdDesignAddAskEntity orderAddAsk : orderAddAskDiffs){
			if(descriptionDiff.equals("")){
				descriptionDiff += orderAddAsk.getAskSubItmeDesc() + ":" + orderAddAsk.getAskSubItmeValDesc();
			}else{
				descriptionDiff += "\r\n" + orderAddAsk.getAskSubItmeDesc() + ":" + orderAddAsk.getAskSubItmeValDesc();
			}
		}
		
		for(CraftOrdDesignAddAskEntity orderAddAsk : orderAddAsks){
			String sqlInsert = CoreXmlSqlParsersFactory.getSql("OrderAddAskDao.Insert");
			sqlInsert = StringTool.ReplaceAll(sqlInsert, "#DESGIN_KEY#", designKey);
			sqlInsert = StringTool.ReplaceAll(sqlInsert, "#DESCRIPTION_ALL#", descriptionAll);
			sqlInsert = StringTool.ReplaceAll(sqlInsert, "#DESCRIPTION_DIFF#", descriptionDiff);
			sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, orderAddAsk);
			PreparedStatement ps = con.prepareStatement(sqlInsert);
			ps.executeUpdate();
			ps.close();
		}
		
		return "";
	}
}
