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 com.steering.comm.tools.SqlBuilder;
import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.pss.qcm.model.BaseAddAskObject;

/**
 * 附加要求类
 * @author Administrator 2015-02-05
 */
public class CoreBaseAddAsk extends DBCommon {

    /**
     * 附加要求 保存
     * @param itemData 项目
     * @param subitemData 分项
     * @param standData 标准
     * @return 执行行数
     */
    public CoreReturnObject saveAddAsk(ArrayList<Object> itemData,
            ArrayList<Object> subitemData, ArrayList<Object> standData) {
        ArrayList<String> sqlIdList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreBaseAddAsk.getItemMax");
        int indexPart = 0;
        try {
            ResultSet rs = this.getDao("TgDao").ExceuteQueryForResultSet(sql);
            if (rs.next()) {
                indexPart = Integer.parseInt(rs.getString(1).substring(4, 6));
            }
            rs.close();
        }
        catch (Exception e) {
        }
        HashMap<String, String> itemHap = new HashMap<String, String>();
        for (int i = 0; i < itemData.size(); i++) {
            String itemStr = itemData.get(i).toString();
            JSONObject json = JSONObject.fromObject(itemStr);
            BaseAddAskObject com = (BaseAddAskObject) JSONObject.toBean(json,
                    BaseAddAskObject.class);
            Object[] param = null;
            if (com.getValidflag().equals("")) {
                DecimalFormat df = new DecimalFormat("00");
                String itemCode = "1210" + df.format(indexPart + 1);
                itemHap.put(com.getAskItemNo(), itemCode);
                indexPart += 1;
                com.setAskItemNo(itemCode);
                param = new Object[] { com.getAskItemNo(), com.getAskItemDesc(),
                        com.getStationCode(), com.getStationDesc(),
                        com.getPic(), com.getAskPItemNo(), com.getCreateName(),
                        com.getStdFlag(), com.getIsAdd(), com.getIsAllowEdit()};
                sqlIdList.add("CoreBaseAddAsk.add");
            }
            else {
                param = new Object[] { com.getAskItemNo(), com.getAskItemDesc(),
                        com.getStationCode(), com.getStationDesc(),
                        com.getPic(), com.getAskPItemNo(), com.getCreateName(),
                        com.getStdFlag(), com.getIsAdd(), com.getIsAllowEdit(), com.getAskItemNo() };
                sqlIdList.add("CoreBaseAddAsk.update");
            }
            params.add(param);
        }
        HashMap<String, String> subitemHap = new HashMap<String, String>();
        HashMap<String, Integer> hasC = new HashMap<String, Integer>();
        for (int i = 0; i < subitemData.size(); i++) {
            String subitemStr = subitemData.get(i).toString();
            JSONObject json = JSONObject.fromObject(subitemStr);
            BaseAddAskObject com = (BaseAddAskObject) JSONObject.toBean(json,
                    BaseAddAskObject.class);
            Object[] param = null;
            if (com.getValidflag().equals("")) {
                if (itemHap.containsKey(com.getAskPItemNo())) {
                    com.setAskPItemNo(itemHap.get(com.getAskPItemNo()));
                }
                if (!hasC.containsKey(com.getAskPItemNo())) {
                    int partIndex = 0;
                    Object[] obj = new Object[] { com.getAskPItemNo() };
                    String sqlc = CoreXmlSqlParsersFactory
                            .getSql("CoreBaseAddAsk.getSubitemMax");
                    try {
                        ResultSet rsC = this.getDao("TgDao")
                                .ExceuteQueryForResultSet(sqlc, obj);
                        if (rsC.next()) {
                            partIndex = Integer
                                    .parseInt(rsC.getString(1).substring(6, 8));

                        }
                    }
                    catch (SQLException e) {
                    }
                    hasC.put(com.getAskPItemNo(), partIndex);
                }
                DecimalFormat df = new DecimalFormat("00");
                String askItemNo = com.getAskPItemNo()
                        + df.format(hasC.get(com.getAskPItemNo()) + 1);
                hasC.put(com.getAskPItemNo(),
                        hasC.get(com.getAskPItemNo()) + 1);
                subitemHap.put(com.getAskItemNo(), askItemNo);
                com.setAskItemNo(askItemNo);
                param = new Object[] { com.getAskItemNo(), com.getAskItemDesc(),
                        com.getStationCode(), com.getStationDesc(),
                        com.getPic(), com.getAskPItemNo(), com.getCreateName(),
                        com.getStdFlag(), com.getIsAdd(), com.getIsAllowEdit() };
                sqlIdList.add("CoreBaseAddAsk.add");
            }
            else {
                param = new Object[] { com.getAskItemNo(), com.getAskItemDesc(),
                        com.getStationCode(), com.getStationDesc(),
                        com.getPic(), com.getAskPItemNo(), com.getCreateName(),
                        com.getStdFlag(), com.getIsAdd(), com.getIsAllowEdit(), com.getAskItemNo() };
                sqlIdList.add("CoreBaseAddAsk.update");
            }
            params.add(param);
        }
        HashMap<String, Integer> hasStand = new HashMap<String, Integer>();
        for (int i = 0; i < standData.size(); i++) {
            String standStr = standData.get(i).toString();
            JSONObject json = JSONObject.fromObject(standStr);
            BaseAddAskObject com = (BaseAddAskObject) JSONObject.toBean(json,
                    BaseAddAskObject.class);
            Object[] param = null;
            if (com.getValidflag().equals("")) {

                if (subitemHap.containsKey(com.getAskPItemNo())) {
                    com.setAskPItemNo(subitemHap.get(com.getAskPItemNo()));
                }
                if (!hasStand.containsKey(com.getAskPItemNo())) {
                    int part = 0;
                    Object[] obj = new Object[] { com.getAskPItemNo() };
                    String sqlc = CoreXmlSqlParsersFactory
                            .getSql("CoreBaseAddAsk.getStandMax");
                    try {
                        ResultSet rs = this.getDao("TgDao")
                                .ExceuteQueryForResultSet(sqlc, obj);
                        if (rs.next()) {
                            part = Integer
                                    .parseInt(rs.getString(1).substring(8, 16));
                        }
                        hasStand.put(com.getAskPItemNo(), part);
                    }
                    catch (SQLException e) {
                    }
                }
                DecimalFormat df = new DecimalFormat("00000000");
                String askItemNo = com.getAskPItemNo()
                        + df.format(hasStand.get(com.getAskPItemNo()) + 1);
                hasStand.put(com.getAskPItemNo(),
                        hasStand.get(com.getAskPItemNo()) + 1);
                com.setAskItemNo(askItemNo);
                param = new Object[] { com.getAskItemNo(), com.getAskItemDesc(),
                        com.getStationCode(), com.getStationDesc(),
                        com.getPic(), com.getAskPItemNo(), com.getCreateName(),
                        com.getStdFlag(), com.getIsAdd(), com.getIsAllowEdit() };
                sqlIdList.add("CoreBaseAddAsk.add");
            }
            else {
                param = new Object[] { com.getAskItemNo(), com.getAskItemDesc(),
                        com.getStationCode(), com.getStationDesc(),
                        com.getPic(), com.getAskPItemNo(), com.getCreateName(),
                        com.getStdFlag(), com.getIsAdd(), com.getIsAllowEdit(), com.getAskItemNo() };
                sqlIdList.add("CoreBaseAddAsk.update");
            }
            params.add(param);
        }
        return this.setDataById(sqlIdList, params);
    }

    /**
     * @return 查询项目
     */
    public CoreReturnObject queryAddAskItem(Boolean flag) {
        String isDelete = flag ? "1" : "0";
        Object[] params = new Object[] { isDelete };
        return this.getDataBySqlID("CoreBaseAddAsk.QueryItem", params);
    }

    public CoreReturnObject isLinkStd(String code) {

        Object[] params = new Object[] { code };
        return this.getDataBySqlID("CoreBaseAddAsk.isLinkStd", params);
    }

    public CoreReturnObject isLinkStd1(String code) {

        Object[] params = new Object[] { code };
        return this.getDataBySqlID("CoreBaseAddAsk.isLinkStd1", params);
    }

    public CoreReturnObject isLinkStd2(String code) {

        Object[] params = new Object[] { code };
        return this.getDataBySqlID("CoreBaseAddAsk.isLinkStd2", params);
    }

    /**
     * @return 查询分项
     */
    public CoreReturnObject queryAddAskSub(Boolean flag) {
        String isDelete = flag ? "1" : "0";
        Object[] params = new Object[] { isDelete };
        return this.getDataBySqlID("CoreBaseAddAsk.QuerySub", params);
    }

    /**
     * @param askItemNo 分项编码
     * @return 查询详细说明
     */
    public CoreReturnObject queryAddAskStan(String askItemNo) {
        Object[] params = new Object[] { askItemNo };
        return this.getDataBySqlID("CoreBaseAddAsk.QueryStand", params);
    }

    /**
     * @param itemData 数据集合
     * @param flag 标识
     * @return 删除或者恢复
     */
    public CoreReturnObject deleteOrResume(ArrayList<Object> itemData,
            Boolean flag) {
        ArrayList<String> sqlIdList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        for (int i = 0; i < itemData.size(); i++) {
            String itemStr = itemData.get(i).toString();
            JSONObject json = JSONObject.fromObject(itemStr);
            BaseAddAskObject com = (BaseAddAskObject) JSONObject.toBean(json,
                    BaseAddAskObject.class);
            Object[] param = null;
            if (flag) {
                param = new Object[] { com.getDeleteName(),
                        com.getAskItemNo() };
                sqlIdList.add("CoreBaseAddAsk.delete");
            }
            else {
                param = new Object[] { "", com.getAskItemNo() };
                sqlIdList.add("CoreBaseAddAsk.resume");
            }
            params.add(param);
        }
        return this.setDataById(sqlIdList, params);
    }
    
    /**
     * 获取
     * @param askItemNo
     * @return
     */
    public CoreReturnObject getSlmBaseAddaskByPk(String askItemNo){
    	String sql = CoreXmlSqlParsersFactory.getSql("CoreBaseAddAsk.getSlmBaseAddaskByPk");
    	sql = StringTool.ReplaceAll(sql, "#ASK_ITEM_NO#", askItemNo);
    	return this.getData(sql);
    }

    /**
     * 删除数据
     * @param askItemNo
     * @return
     */
    public CoreReturnObject deleteData(String askItemNo){
        SqlBuilder b = new SqlBuilder("CoreBaseAddAsk.deleteData", askItemNo);

        return this.setData(b.toString());
    }
}
