package com.steering.pss.sale.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONObject;

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

import com.steering.comm.db.DBCommon;
import com.steering.pss.sale.order.model.SlmOrderPriceEntity;

/**
 * @author WenHB 导入Excel
 */
public class CoreExcelToGrid extends DBCommon {

    @SuppressWarnings("unchecked")
    public CoreReturnObject confirmDataToDB(HashMap<String, String> map,
            String parm, HashMap<String, ArrayList<Object>> map1) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        JSONObject json = JSONObject.fromObject(parm);
        SlmOrderPriceEntity sop = (SlmOrderPriceEntity) JSONObject.toBean(json,
                SlmOrderPriceEntity.class);
        CorePriceConfirm cpc = new CorePriceConfirm();
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 解析map获取合同号 生成价格编号以及价格主表数据
            Iterator<Map.Entry<String, String>> iter = map.entrySet()
                    .iterator();
            while (iter.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
                        .next();
                String orderNo = entry.getKey();
                String priceBase = entry.getValue();
                // 根据合同号查找价格编号 存在状态通过的 不再导入
                // 生成价格编号 插入主表记录 明细表记录 写入合同里面
                String ordPk = queryOrderPk(con, orderNo);
                String saleOrg = querySaleOrg(con, ordPk);
                if (ordPk == null || ordPk.equals("")) { // 合同号不存在系统的不导入系统
                    continue;
                }
                else { // 系统存在该合同号
                    if (cpc.isExistsPriceNo(con, ordPk)) { // 已存在价格评审单号
                        String priceNo = cpc.getOrderPriceNo(con, ordPk);
                        String status = cpc.getPriceNoStatus(con, priceNo);
                        if (status.equals("S")) {
                            continue;
                        }
                        else {
                            // 修改主表
                            String sql = CoreXmlSqlParsersFactory
                                    .getSql("CoreExcelToGrid.UpdateOrderPrice");
                            PreparedStatement ps = con.prepareStatement(sql);
                            ps.setObject(1, priceBase);
                            ps.setObject(2, sop.getReportName());
                            ps.setObject(3, sop.getReportDeptcode());
                            ps.setObject(4, sop.getReportDeptdesc());
                            ps.setObject(5, sop.getReportUnitcode());
                            ps.setObject(6, sop.getReportUnitdesc());
                            ps.setObject(7, sop.getReportName());
                            ps.setObject(8, sop.getDealName());
                            ps.setObject(9, sop.getDealDeptcode());
                            ps.setObject(10, sop.getDealDeptdesc());
                            ps.setObject(11, sop.getDealUnitcode());
                            ps.setObject(12, sop.getDealUnitdesc());
                            ps.setObject(13, priceNo);
                            ps.executeUpdate();
                            ps.close();
                            // 修改从表
                            ArrayList<Object> arrList = map1.get(orderNo);
                            for (int i = 0; i < arrList.size(); i++) {
                                String sql1 = CoreXmlSqlParsersFactory
                                        .getSql("CoreExcelToGrid.UpdateOrderPriceList");
                                ArrayList<Object> list = (ArrayList<Object>) arrList
                                        .get(i);
                                ps = con.prepareStatement(sql1);
                                ps.setObject(1, list.get(2));
                                ps.setObject(2, list.get(3));
                                ps.setObject(3, list.get(4));
                                ps.setObject(4, list.get(5));
                                ps.setObject(5, sop.getReportName());
                                ps.setObject(6, priceNo);
                                ps.setObject(7, ordPk);
                                ps.setObject(8, list.get(1));
                                ps.executeUpdate();
                                ps.close();
                            }
                            // 将价格保存到合同行
                            cpc.savePriceToLine(con, priceNo);
                            cpc.updateOrderConfirmTime(con, ordPk);
                        }
                    }
                    else { // 不存在价格评审单号
                        String code = cpc.generatePriceNo(con, saleOrg);
                        if (code.equals("")) {
                            cro.setV_errCode(-1);
                            cro.setV_errMsg("价格评审单号生成错误！");
                            con.rollback();
                            return cro;
                        }
                        // 插入主表
                        String sql2 = CoreXmlSqlParsersFactory
                                .getSql("CoreExcelToGrid.InsertOrderPrice");
                        PreparedStatement ps = con.prepareStatement(sql2);
                        ps.setObject(1, code);
                        ps.setObject(2, priceBase);
                        ps.setObject(3, sop.getReportName());
                        ps.setObject(4, sop.getReportDeptcode());
                        ps.setObject(5, sop.getReportDeptdesc());
                        ps.setObject(6, sop.getReportUnitcode());
                        ps.setObject(7, sop.getReportUnitdesc());
                        ps.setObject(8, sop.getReportName());
                        ps.setObject(9, sop.getDealName());
                        ps.setObject(10, sop.getDealDeptcode());
                        ps.setObject(11, sop.getDealDeptdesc());
                        ps.setObject(12, sop.getDealUnitcode());
                        ps.setObject(13, sop.getDealUnitdesc());
                        ps.setObject(14, ordPk);
                        ps.executeUpdate();
                        ps.close();
                        // 插入从表
                        ArrayList<Object> arrList = map1.get(orderNo);
                        for (int i = 0; i < arrList.size(); i++) {
                            String sql1 = CoreXmlSqlParsersFactory
                                    .getSql("CoreExcelToGrid.InsertOrderPriceList");
                            ArrayList<Object> list = (ArrayList<Object>) arrList
                                    .get(i);
                            ps = con.prepareStatement(sql1);
                            ps.setObject(1, code);
                            ps.setObject(2, ordPk);
                            ps.setObject(3, list.get(1));
                            ps.setObject(4, list.get(2));
                            ps.setObject(5, list.get(3));
                            ps.setObject(6, list.get(4));
                            ps.setObject(7, list.get(5));
                            ps.setObject(8, sop.getReportName());
                            ps.executeUpdate();
                            ps.close();
                        }
                        // 编号写入合同头
                        String sqlH = CoreXmlSqlParsersFactory
                                .getSql("CoreExcelToGrid.UpdateOrderHeadPriceNo");
                        ps = con.prepareStatement(sqlH);
                        ps.setObject(1, code);
                        ps.setObject(2, ordPk);
                        ps.executeUpdate();
                        ps.close();
                        // 将价格保存到合同行
                        cpc.savePriceToLine(con, code);
                        cpc.updateOrderConfirmTime(con, ordPk);
                    }
                }

            } // 整个循环结束
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                }
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
                e.printStackTrace();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    /**
     * 获取合同的ORD_PK值 用于判断是否存在该合同
     * @param con
     * @param orderNo 合同号
     * @return
     * @throws SQLException
     */
    public String queryOrderPk(Connection con, String orderNo)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreExcelToGrid.queryOrderPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, orderNo);
        ResultSet rs = ps.executeQuery();
        String ordPk = "";
        if (rs.next()) {
            ordPk = rs.getString(1);
        }
        rs.close();
        ps.close();
        return ordPk;
    }

    /**
     * 获取合同的销售组织
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public String querySaleOrg(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreExcelToGrid.queryOrderSaleOrg");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        String saleOrg = "";
        if (rs.next()) {
            saleOrg = rs.getString(1);
        }
        rs.close();
        ps.close();
        return saleOrg;
    }
}
