package com.steering.pss.qcm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import CoreFS.SA01.CoreIComponent;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;

/**
 * 平台的使用类都必须继承自平台的CoreIComponent类
 * @author 2014/09/24
 * @version v1.0
 * @modify 1392
 */
public class CoreProductCHEMBIAS extends DBCommon {

    public CoreReturnObject doQuery(Boolean includeDeleted, String cic) {
        String deletedFlag = includeDeleted ? "0" : "";
        Object[] params = new Object[] { deletedFlag, cic };
        return this.getDataBySqlID("CoreProductCHEMBIAS.QUERY_P", params);
    }

    public CoreReturnObject doQueryC(Boolean includeDeleted, String cic) {
        String deletedFlag = includeDeleted ? "0" : "";
        Object[] params = new Object[] { deletedFlag, deletedFlag, cic };
        return this.getDataBySqlID("CoreProductCHEMBIAS.QUERY_C", params);
    }

    @SuppressWarnings("unchecked")
    public CoreReturnObject doSave(ArrayList<Object> parmPA,
            ArrayList<Object> parmPU, ArrayList<Object> parmCA,
            ArrayList<Object> parmCU, ArrayList<Object> parmSteel,
            String deleteCic) {

        ArrayList<String> sqlIDlist = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        HashMap<String, String> map = new HashMap<String, String>();
        String cic = "";
        for (int i = 0; i < parmPA.size(); i++) {
            sqlIDlist.add("CoreProductCHEMBIAS.ADD_P");
            String strSql = CoreXmlSqlParsersFactory
                    .getSql("CoreProductCHEMBIAS.MaxCIC");
            ArrayList<Object> list = (ArrayList<Object>) parmPA.get(i);
            map.put(list.get(0).toString(), cic);
            try {
                long stdCodePart = 0;
                ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(
                        strSql, null); // 取得对应标准类型的最大编码
                if (rs.next()) {
                    String cicp = rs.getString(1);
                    if (cicp.compareTo(cic) < 0) // 小于
                    {
                        stdCodePart = Integer.parseInt(cic.substring(3, 9));
                        DecimalFormat df = new DecimalFormat("000000");
                        String stdCode = "CIC" + df.format(stdCodePart + 1);
                        cic = stdCode;
                        map.put(list.get(0).toString(), cic);
                        list.set(0, stdCode);
                    }
                    else// 大于或等于
                    {
                        stdCodePart = Integer.parseInt(cicp.substring(3, 9));
                        DecimalFormat df = new DecimalFormat("000000");
                        String stdCode = "CIC" + df.format(stdCodePart + 1);
                        cic = stdCode;
                        map.put(list.get(0).toString(), cic);
                        list.set(0, stdCode);
                    }
                }
                else {
                    if (cic == "") {
                        DecimalFormat df = new DecimalFormat("000000");
                        String stdCode = "CIC" + df.format(1);
                        map.put(list.get(0).toString(), stdCode);
                        cic = stdCode;
                        list.set(0, stdCode);
                    }
                    else {
                        stdCodePart = Integer.parseInt(cic.substring(3, 9));
                        DecimalFormat df = new DecimalFormat("000000");
                        String stdCode = "CIC" + df.format(stdCodePart + 1);
                        map.put(list.get(0).toString(), stdCode);
                        cic = stdCode;
                        list.set(0, stdCode);
                    }
                }
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }
            params.add(list.toArray());
        }
        for (int i = 0; i < parmPU.size(); i++) {
            sqlIDlist.add("CoreProductCHEMBIAS.Update_P");
            ArrayList<Object> list = (ArrayList<Object>) parmPU.get(i);
            list.add(list.get(0).toString());
            params.add(list.toArray());

        }
        HashMap<String, Integer> mapc = new HashMap<String, Integer>();
        int index = 0; // 序号
        for (int i = 0; i < parmCA.size(); i++) {
            sqlIDlist.add("CoreProductCHEMBIAS.ADD_C");
            ArrayList<Object> clist = (ArrayList<Object>) parmCA.get(i);
            String cicc_s = clist.get(0).toString(); // 未改之前的码
            String cicc_d = ""; // 改之后的码
            if (map.containsKey(cicc_s)) {
                cicc_d = map.get(cicc_s);
            }
            else {
                cicc_d = cicc_s;
            }
            if (!mapc.containsKey(cicc_d)) {
                mapc.put(cicc_d, 0); // 产品规范码对应序号
            }
            String str = CoreXmlSqlParsersFactory
                    .getSql("CoreProductCHEMBIAS.MaxIndex");
            Object[] obj = new Object[] { cicc_d };
            try {
                ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(
                        str, obj);
                if (rs.next()) {
                    int indexseq = rs.getInt(1);
                    if (indexseq > mapc.get(cicc_d)) {
                        indexseq += 1;
                        index = indexseq;
                        mapc.put(cicc_d, index);
                        clist.set(0, cicc_d);
                        clist.set(1, index);
                    }
                    else {
                        index = mapc.get(cicc_d);
                        index += 1;
                        mapc.put(cicc_d, index);
                        clist.set(0, cicc_d);
                        clist.set(1, index);
                    }
                }
                else { // 数据库不存在序号
                    index = mapc.get(cicc_d);
                    index += 1;
                    mapc.put(cicc_d, index);
                    clist.set(0, cicc_d);
                    clist.set(1, index);
                }
            }
            catch (SQLException e) {
                CoreIComponent.logAdapter.filelogger.logError(e.getMessage());// 将服务端的错误信息写入日志文件
            }
            params.add(clist.toArray());
        }
        for (int i = 0; i < parmCU.size(); i++) {
            sqlIDlist.add("CoreProductCHEMBIAS.Update_C");
            ArrayList<Object> list = (ArrayList<Object>) parmCU.get(i);
            list.add(list.get(0).toString());
            list.add(list.get(1).toString());
            params.add(list.toArray());

        }
        if (map.containsKey(deleteCic))
            deleteCic = map.get(deleteCic);
        sqlIDlist.add("CoreProductCHEMBIAS.Delete_Steel");
        params.add(new Object[] { deleteCic });

        for (int i = 0; i < parmSteel.size(); i++) {
            sqlIDlist.add("CoreProductCHEMBIAS.Insert_Steel");
            params.add(((ArrayList<Object>) parmSteel.get(i)).toArray());
        }

        return this.setDataById(sqlIDlist, params);

    }

    @SuppressWarnings("unchecked")
    public CoreReturnObject doDelete(ArrayList<Object> parms,
            ArrayList<Object> parmsC, Boolean isDelete) {
        ArrayList<String> sqlIDlist = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        if (isDelete) {
            for (int i = 0; i < parms.size(); i++) {
                sqlIDlist.add("CoreProductCHEMBIAS.DELETE_P");
                sqlIDlist.add("CoreProductCHEMBIAS.DELETE_C1");
                params.add(((ArrayList<Object>) parms.get(i)).toArray());
                params.add(((ArrayList<Object>) parms.get(i)).toArray());
            }

            for (int i = 0; i < parmsC.size(); i++) {
                sqlIDlist.add("CoreProductCHEMBIAS.DELETE_C");
                params.add(((ArrayList<Object>) parmsC.get(i)).toArray());
            }

        }
        else {
            for (int i = 0; i < parms.size(); i++) {
                sqlIDlist.add("CoreProductCHEMBIAS.RESUME_P");
                Object[] obj = new Object[] { parms.get(i).toString() };
                params.add(obj);
            }

            for (int i = 0; i < parmsC.size(); i++) {
                sqlIDlist.add("CoreProductCHEMBIAS.RESUME_C");
                params.add(((ArrayList<Object>) parmsC.get(i)).toArray());
            }

        }
        return this.setDataById(sqlIDlist, params);
    }

    public CoreReturnObject getSteel(String cic) {
        Object[] obj = new Object[] { cic, cic };
        return this.getDataBySqlID("CoreProductCHEMBIAS.getSteel", obj);
    }

}
