package com.steering.pss.qcm;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

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

import CoreFS.SA06.CoreReturnObject;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.comm.tools.StringTool;
import com.steering.comm.tools.TransctionTool;
import com.steering.pss.qcm.model.GenericEntity;
import com.steering.pss.sale.order.craftDesign.server.FTPTool;

@SuppressWarnings("unchecked")
public class JGAndMSC extends DBCommon {

    public CoreReturnObject GetMSC() {
        return this.getDataBySqlID("JGAndMSC.GetMSC"); // 标准描述
    }

    /**
     * Title: doQuery
     * Description: 查询
     * @param flag true有效 false 有效+无效
     * @param msc 冶金规范码
     * @return 平台统一返回对象
     */
    public CoreReturnObject doQuery(Boolean flag,String msc) {
        String deletedFlag = flag ? "0" : "";
        Object[] params = new Object[] { deletedFlag ,msc};
        return this.getDataBySqlID("JGAndMSC.QUERY", params); // 查询
    }
    
    public CoreReturnObject doQuery(String msc, String stockStyle) {
        Object[] params = new Object[] {msc, stockStyle};
        return this.getDataBySqlID("JGAndMSC.queryMscRJg", params); // 查询
    }

    /**
     * Title: AndJG
     * Description: 新增冶金规范码和接箍码关系
     * @param parm  新增的接箍的信息
     * @return   平台统一返回对象
     */
    public CoreReturnObject AndJG(ArrayList<ArrayList<String>> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        TransctionTool transction = new TransctionTool();
        Connection con = transction.getCon();
        try{
	        for (int i = 0; i < parm.size(); i++) {
	        	String sqlGuid = CoreXmlSqlParsersFactory.getSql("JGAndMSC.getGuid");
	        	GenericEntity guid = EntityTool.getEntityBySqlNoClsCon(sqlGuid, con, GenericEntity.class);
	            ArrayList<String> list = parm.get(i);
	            
	            //复制接箍图片
	            SqlBuilder b = new SqlBuilder("JGAndMSC.getBaseJgFile");
	            b.Fill(new String[]{ list.get(2) });
	            GenericEntity baseFilePath = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, GenericEntity.class);
	            List<String> mscJgFiles = Arrays.asList(baseFilePath.getValue().split("#"));
	    		String craftJgFiles = "";
	    		String craftFjPath = "Qcm/Jg/" + guid.getValue() + "/";
	    		for(String mscJgFile : mscJgFiles){
	    			String[] results = FTPTool.CopyFile(mscJgFile, craftFjPath);
	    			if(results[0].equals("False")){
	    				con.close();
	    				cro.setV_errCode(-1);
	    				cro.setV_errMsg("获取接箍螺纹图片失败，请重试！");
	    				return cro;
	    			}
	    			
	    			if(craftJgFiles.equals("")){
	    				craftJgFiles = results[1];
	    			}else{
	    				craftJgFiles += "#" + results[1];
	    			}
	    		}
	            
	            String sqlInserJg = CoreXmlSqlParsersFactory.getSql("JGAndMSC.AndJG");
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#MSC#", list.get(0));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#MSC_DESC#", list.get(1));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#CODE_JG#", list.get(2));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#STOCK_STYLE#", list.get(3));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#CREATE_NAME#", list.get(4));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#CUST_ALPHA_NO#", list.get(5));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#ORDER_SPECIAL_DESC#", list.get(6));
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#KEY_JG#", guid.getValue());
	            sqlInserJg = StringTool.ReplaceAll(sqlInserJg, "#FILE_PATH#", craftJgFiles);
	            if(!transction.ExcuteTransction(sqlInserJg)){
	            	return transction.getReturnObject();
	            }
	            
	            String sqlInsertJgBacklog = CoreXmlSqlParsersFactory.getSql("JGCodeManage.insertJgBacklog");
	            sqlInsertJgBacklog = StringTool.ReplaceAll(sqlInsertJgBacklog, "#KEY_JG#", guid.getValue());
	            sqlInsertJgBacklog = StringTool.ReplaceAll(sqlInsertJgBacklog, "#CREATE_NAME#", list.get(4));
	            if(!transction.ExcuteTransction(sqlInsertJgBacklog)){
	            	return transction.getReturnObject();
	            }
	        }
        }catch(Exception ex){
        	cro = transction.rollBack();
        	cro.setV_errMsg(ex.getMessage());
        	return cro;
        }
        return transction.commit();
    }

    /**
     * Title: AndBL
     * Description:  新增备料
     * @param parm  备料信息参数
     * @return     平台的统一返回对象
     */
    public CoreReturnObject AndBL(ArrayList<ArrayList<String>> parms) {
        CoreReturnObject cro = new CoreReturnObject();
        String blCodes = "'1'";
        for(ArrayList<String> parm : parms){
        	blCodes += ", '"+ parm.get(2) +"'";
        }
        ArrayList<String> sqllist = new ArrayList<String>();
        if(parms.size() > 0){
	        String sqlDelete = CoreXmlSqlParsersFactory.getSql("JGAndMSC.DeleteBL");
	        sqlDelete = StringTool.ReplaceAll(sqlDelete, "#MSC#", parms.get(0).get(0));
	        sqlDelete = sqlDelete.replaceAll("#CODE_JG_S#", blCodes);
	        sqllist.add(sqlDelete);
        }
        
        for(ArrayList<String> parm : parms){
        	String sql = CoreXmlSqlParsersFactory.getSql("JGAndMSC.AndBL");
        	sql = StringTool.ReplaceAll(sql, "#MSC#", parm.get(0));
        	sql = StringTool.ReplaceAll(sql, "#MSC_DESC#", parm.get(1));
        	sql = StringTool.ReplaceAll(sql, "#CODE_JG#", parm.get(2));
        	sql = StringTool.ReplaceAll(sql, "#STOCK_STYLE#", parm.get(3));
        	sql = StringTool.ReplaceAll(sql, "#CREATE_NAME#", parm.get(4));
        	sqllist.add(sql);
        }
        cro = this.getDao("TgDao").ExcuteTransction(sqllist);
        return cro;
    }

    /**
     * Title: doDelete
     * Description:  刪除和恢复
     * @param flag  true 刪除 false 恢复
     * @param msc  冶金规范码
     * @param code  接箍码或者备料码
     * @param name  操作人
     * @return  平台统一返回对象
     */
    public CoreReturnObject doDelete(String keyJg) {
            Object[] params = new Object[] { keyJg};
            return this.setDataBySqlID("JGAndMSC.DELETE", params);
    }
    
    /**
     * 是否重复
     * @param msc
     * @param custAlpha
     * @param codeJg
     * @return
     */
    public CoreReturnObject IsRepeat(String msc, String orderSpecialDesc, String codeJg){
    	String sql = CoreXmlSqlParsersFactory.getSql("JGAndMSC.isRepeat");
    	sql = StringTool.ReplaceAll(sql, "#MSC#", msc);
    	sql = StringTool.ReplaceAll(sql, "#ORDER_SPECIAL_DESC#", orderSpecialDesc);
    	sql = StringTool.ReplaceAll(sql, "#CODE_JG#", codeJg);
    	return this.getData(sql);
    }
}
