package com.steering.pss.qcm;

import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
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.pss.qcm.model.ComBaseSampleEntity;

public class CoreBaseSample extends DBCommon {

    public CoreReturnObject GetPhyLength() {
        return this.getDataBySqlID("CoreBaseSample.Init4010"); // 标准描述4010
    }

    public CoreReturnObject GetPhyRate() {
        return this.getDataBySqlID("CoreBaseSample.Init4011"); // 标准描述4011
    }

    public CoreReturnObject GetPhyPosition() {
        return this.getDataBySqlID("CoreBaseSample.Init4012"); // 标准描述4012
    }

    public CoreReturnObject Query(String phycode, String phyMinName, String validflag) {
        Object[] obj = new Object[] { phycode, phycode, phyMinName, phyMinName, validflag};
        return this.getDataBySqlID("CoreBaseSample.QUERY", obj); // 查询
    }

    public CoreReturnObject doSave(ArrayList<String> parms) {

        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqllist = new ArrayList<String>(); // SQL语句集合
        for (int i = 0; i < parms.size(); i++) {
            ComBaseSampleEntity baseSampeEntity = EntityTool.getEntityByJSONStr(parms.get(i), ComBaseSampleEntity.class);
            
            if(baseSampeEntity.getValidflag().equals("")){
                String sqlInsert = CoreXmlSqlParsersFactory.getSql("CoreBaseSample.ADD");
                sqlInsert = EntityTool.fillSqlByEntity(sqlInsert, baseSampeEntity);
                sqllist.add(sqlInsert);
            }else{
            	String sqlUpdate = CoreXmlSqlParsersFactory.getSql("CoreBaseSample.UPDATE");
            	sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, baseSampeEntity);
            	sqllist.add(sqlUpdate);
            }
            
            //取样码和钢级关系
            String sqlDelBaseSampleRSteel = getDelBaseSampleRSteel(baseSampeEntity.getSic());
            sqllist.add(sqlDelBaseSampleRSteel);
            if(!baseSampeEntity.getSteelCode().equals("")){
	            String[] steelCodes = baseSampeEntity.getSteelCode().split(";");
	            String[] steelNames = baseSampeEntity.getSteelName().split(";");
	        	for(int j = 0; j < steelCodes.length; j++){
	        		String sqlInsertBaseSampleRSteel = getInsertBaseSampleRSteel(
	        				baseSampeEntity.getSic(), steelCodes[j], steelNames[j], baseSampeEntity.getCreateName());
	        		sqllist.add(sqlInsertBaseSampleRSteel);
	        	}
            }
            
            //试样组适用标准
            String sqlDelBaseSampleRStd = getDelBaseSampleRStd(baseSampeEntity.getSic());
            sqllist.add(sqlDelBaseSampleRStd);
            if(!baseSampeEntity.getStdCode().equals("")){
            	String[] stdCodes = baseSampeEntity.getStdCode().split(";");
	            String[] stdNames = baseSampeEntity.getStdName().split(";");
	        	for(int j = 0; j < stdCodes.length; j++){
	        		String sqlInsertBaseSampleRStd = getInsertBaseSampleRStd(
	        				baseSampeEntity.getSic(), stdCodes[j], stdNames[j], baseSampeEntity.getCreateName());
	        		sqllist.add(sqlInsertBaseSampleRStd);
	        	}
            }
            
            //试样组与品名关系
            sqllist.add(getDelBaseSampleRProduct(baseSampeEntity.getSic()));
            if(!baseSampeEntity.getProduccode().equals("")){
            	String[] produccodes = baseSampeEntity.getProduccode().split(";");
	        	for(int j = 0; j < produccodes.length; j++){
	        		String sqlInsertBaseSampleRProduct = getInsertBaseSampleRProduct(baseSampeEntity.getSic(),
	        				produccodes[j]);
	        		sqllist.add(sqlInsertBaseSampleRProduct);
	        	}
            }
        }
        cro = this.getDao("TgDao").ExcuteTransction(sqllist);
        return cro;
    }

    public CoreReturnObject DeleteOrResume(ArrayList<Object> list,
            Boolean flag, String name) {
        ArrayList<String> sqlIDList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            if (flag) {
                sqlIDList.add("CoreBaseSample.DELETE");
                params.add(new Object[] { name, list.get(i).toString() });
            }
            else {
                sqlIDList.add("CoreBaseSample.Resume");
                params.add(new Object[] { list.get(i).toString() });
            }
        }
        return this.setDataById(sqlIDList, params);
    }
    
    public CoreReturnObject loadPhySmpCnt(String phySmlCode){
    	return this.getDataBySqlID("CoreBaseSample.loadPhySmpCnt", new Object[]{phySmlCode});
    }
    
    /**
     * 获取新增取样码和钢级关系
     * @param sic
     * @param steelCode
     * @param steelName
     * @param createName
     * @return
     */
    public String getInsertBaseSampleRSteel(String sic, String steelCode, String steelName, String createName){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseSample.insertBaseSampleRSteel");
    	sql = StringTool.ReplaceAll(sql, "#SIC#", sic);
    	sql = StringTool.ReplaceAll(sql, "#STEELCODE#", steelCode);
    	sql = StringTool.ReplaceAll(sql, "#STEELNAME#", steelName);
    	sql = StringTool.ReplaceAll(sql, "#CREATE_NAME#", createName);
    	return sql;
    }
    
    /**
     * 获取新增取样码和标准关系
     * @param sic
     * @param steelCode
     * @param steelName
     * @param createName
     * @return
     */
    public String getInsertBaseSampleRStd(String sic, String stdCode, String stdName, String createName){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseSample.insertBaseSampleRStd");
    	sql = StringTool.ReplaceAll(sql, "#SIC#", sic);
    	sql = StringTool.ReplaceAll(sql, "#STD_CODE#", stdCode);
    	sql = StringTool.ReplaceAll(sql, "#STD_NAME#", stdName);
    	sql = StringTool.ReplaceAll(sql, "#CREATE_NAME#", createName);
    	return sql;
    }
    
    public String getInsertBaseSampleRProduct(String sic, String produccode){
    	SqlBuilder b = new SqlBuilder("CoreBaseSample.insertBaseSampleRProduct");
    	b.Fill(new String[]{sic, produccode});
    	return b.toString();
    }
    
    /**
     * 删除取样码和钢级关系
     * @param sic
     * @param steelCode
     * @param steelName
     * @param createName
     * @return
     */
    public String getDelBaseSampleRSteel(String sic){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseSample.delBaseSampleRSteel");
    	sql = StringTool.ReplaceAll(sql, "#SIC#", sic);
    	return sql;
    }
    
    /**
     * 删除取样码和标准关系
     * @param sic
     * @param steelCode
     * @param steelName
     * @param createName
     * @return
     */
    public String getDelBaseSampleRStd(String sic){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseSample.delBaseSampleRStd");
    	sql = StringTool.ReplaceAll(sql, "#SIC#", sic);
    	return sql;
    }
    
    /**
     * 删除取样码和品名关系
     * @param sic
     * @return
     */
    public String getDelBaseSampleRProduct(String sic){
    	SqlBuilder b = new SqlBuilder("CoreBaseSample.delBaseSampleRProduct");
    	b.Fill(new String[]{sic});
    	return b.toString();
    }
}
