package com.steering.pss.sale.base;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

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

import com.steering.comm.db.DBCommon;

public class CoreBasePrdcrDept extends DBCommon {
    /**
     * 监制信息管理界面主数据查询
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject baseDeptMainQuery() {
        return this.getDataBySqlID("CoreBasePrdcrDept.baseDeptMainQuery");
    }

    /**
     * 监制信息管理界面从数据查询
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject baseDeptTailQuery() {
        return this.getDataBySqlID("CoreBasePrdcrDept.baseDeptTailQuery");
    }

    /**
     * 监制信息管理界面从数据查询ID
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject baseDeptMainGetPetNo() {
        return this.getDataBySqlID("CoreBasePrdcrDept.baseDeptMainGetPetNo");
    }

    /**
     * 监制信息管理界面主数据保存
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject baseDeptMailSure(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();

        java.sql.Connection conn = null;
        try {

            conn = this.getDao("TgDao").getConnection();
            conn.setAutoCommit(false);
            String sql1 = "select PDEPT_NO  from  SLM_BASE_PRDCR_DEPT WHERE PDEPT_NAME = '"
                    + parm.get(0).toString() + "'";
            java.sql.PreparedStatement psta1 = conn.prepareStatement(sql1);
            java.sql.ResultSet rs1 = psta1.executeQuery();

            if (!rs1.next()) {
                String strSql = CoreXmlSqlParsersFactory
                        .getSql("CoreBasePrdcrDept.baseDeptMailAdd");
                try {
                    cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
                            parm.toArray());
                }
                catch (SQLException e) {
                    cro = cro.SetErrInfo(e.getMessage());
                }

                rs1.close();
                psta1.close();
                return cro;
            }
            else {
                String strSql = CoreXmlSqlParsersFactory
                        .getSql("CoreBasePrdcrDept.baseDeptMailUpdate");
                try {
                    cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
                            parm.toArray());
                }
                catch (SQLException e) {
                    cro = cro.SetErrInfo(e.getMessage());
                }
                Object[] parm1 = new Object[] { parm.get(0), parm.get(4) };
                String strSql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreBasePrdcrDept.baseDeptTailUpdateName");
                try {
                    cro = this.getDao("TgDao").ExcuteNonQuery(strSql1, parm1);
                }
                catch (SQLException e) {
                    cro = cro.SetErrInfo(e.getMessage());
                }

            }

            /**
             * String sql =
             * "select count(*) c1 from SLM_BASE_PRDCR_DEPT where pdept_no = '"+
             * parm.get(0) +"'";// CoreXmlSqlParsersFactory.getSql("sqlid");
             * java.sql.PreparedStatement psta = conn.prepareStatement(sql);
             * java.sql.ResultSet rs = psta.executeQuery(); int flag = 0; while
             * (rs.next()) { flag = rs.getInt(1); if(flag == 1)//进行update操作 {
             * String strSql = CoreXmlSqlParsersFactory
             * .getSql("CoreBasePrdcrDept.baseDeptMailUpdate"); try { cro =
             * this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray()); }
             * catch (SQLException e) { cro = cro.SetErrInfo(e.getMessage()); }
             * Object[] param1 = new Object[] { parm.get(0) }; String strSql1 =
             * CoreXmlSqlParsersFactory
             * .getSql("CoreBasePrdcrDept.baseDeptMailUpdate"); try { cro =
             * this.getDao("TgDao").ExcuteNonQuery(strSql1, parm1.toArray()); }
             * catch (SQLException e) { cro = cro.SetErrInfo(e.getMessage()); }
             * } else//进行新增的操作 { String strSql = CoreXmlSqlParsersFactory
             * .getSql("CoreBasePrdcrDept.baseDeptMailAdd"); try { cro =
             * this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray()); }
             * catch (SQLException e) { cro = cro.SetErrInfo(e.getMessage()); }
             * return cro; } }
             **/
            conn.commit();
        }
        catch (SQLException sqlex) {
            System.out.println(sqlex.getMessage());
            try {
                if (conn != null) {
                    conn.rollback();
                }
            }
            catch (Exception ex1) {

            }
        }
        return cro;
    }

    /**
     * 监制信息界面进行主表的删除操作
     * @param name
     * @return
     */
    public CoreReturnObject baseDeptMainDelete(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreBasePrdcrDept.baseDeptMainDelete");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 监制信息管理界面从数据保存
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject baseDeptTailSure(ArrayList<Object> parm,
            String sysGuid) {
        CoreReturnObject cro = new CoreReturnObject();

        if (!sysGuid.equals("")) {
            String strSql = CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.baseDeptailUpdate");
            try {
                String no = parm.get(0).toString();
                parm.remove(0);
                parm.add(no);
                parm.add(sysGuid);
                cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
                        parm.toArray());
            }
            catch (SQLException e) {
                cro = cro.SetErrInfo(e.getMessage());
            }
            return cro;
        }
        else// 进行新增的操作
        {
            String strSql = CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.baseDeptailAdd");
            try {
                cro = this.getDao("TgDao").ExcuteNonQuery(strSql,
                        parm.toArray());
            }
            catch (SQLException e) {
                cro = cro.SetErrInfo(e.getMessage());
            }
            return cro;
        }

    }

    /**
     * 监制信息界面进行从表的删除操作
     * @param name
     * @return
     */
    public CoreReturnObject baseDeptTainDelete(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreBasePrdcrDept.baseDeptTainDelete");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /***
     * 保存基础监制信息（废除之前的方法）
     * @param add
     * @param addc
     * @param update
     * @param updatec
     * @param name
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject save(ArrayList<Object> add, ArrayList<Object> addc,
            ArrayList<Object> update, ArrayList<Object> updatec, String name) {

        CoreReturnObject cro = new CoreReturnObject();

        ArrayList<String> sqlList = new ArrayList<String>();
        ArrayList<Object[]> param = new ArrayList<Object[]>();
        HashMap<String, String> map = new HashMap<String, String>();
        int num = 0;
        for (int i = 0; i < add.size(); i++) {
            String deptNo = "";
            ArrayList<Object> list = (ArrayList<Object>) add.get(i);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.baseDeptMainGetPetNo");
            try {
                ResultSet rs = this.getDao(CoreConstantData.sysbeanstr)
                        .ExceuteQueryForResultSet(sql);
                if (rs.next()) {
                    deptNo = rs.getString(1);
                }
                int n = Integer.parseInt(deptNo.substring(1, 5));
                if (num < n) {
                    num = n;
                }
                else {
                    num = num + 1;
                }
                DecimalFormat df = new DecimalFormat("0000");
                String dept = "D" + df.format(num);
                map.put(list.get(0).toString(), dept);
                list.set(0, dept);
                list.add(name);
                sqlList.add(CoreXmlSqlParsersFactory
                        .getSql("CoreBasePrdcrDept.baseAdd"));
                param.add(list.toArray());
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; i < addc.size(); i++) {
            ArrayList<Object> list = (ArrayList<Object>) addc.get(i);
            String no = "";
            if (map.containsKey(list.get(0).toString())) {
                no = map.get(list.get(0).toString());
            }
            else {
                no = list.get(0).toString();
            }

            list.set(0, no);
            list.add(name);
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.childAdd"));
            param.add(list.toArray());
        }

        for (int i = 0; i < update.size(); i++) {
            ArrayList<Object> list = (ArrayList<Object>) update.get(i);
            String no = list.get(0).toString();
            list.remove(0);
            list.add(name);
            list.add(no);
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.baseUpdate"));
            param.add(list.toArray());
        }

        for (int i = 0; i < updatec.size(); i++) {
            ArrayList<Object> list = (ArrayList<Object>) updatec.get(i);
            String no = list.get(0).toString();
            list.remove(0);
            list.add(name);
            list.add(no);
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.childUpdate"));
            param.add(list.toArray());
        }

        cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqlList, param);

        return cro;
    }

    @SuppressWarnings("unchecked")
    public CoreReturnObject delete(ArrayList<Object> parent,
            ArrayList<Object> child) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqlList = new ArrayList<String>();
        ArrayList<Object[]> param = new ArrayList<Object[]>();

        for (int i = 0; i < parent.size(); i++) {
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.deleteParent"));
            param.add(new Object[] { parent.get(i) });
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.deleteChildAll"));
            param.add(new Object[] { parent.get(i) });
        }

        for (int i = 0; i < child.size(); i++) {
            ArrayList<Object> list = (ArrayList<Object>) child.get(i);
            sqlList.add(CoreXmlSqlParsersFactory
                    .getSql("CoreBasePrdcrDept.deleteChildOnly"));
            param.add(list.toArray());
        }
        cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqlList, param);
        return cro;

    }
}
