package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.SqlBuilder;
import com.steering.pss.plan.order.model.SlmOrderHeadCopy;
import com.steering.pss.sale.order.model.CoreOrderStatus;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CoreOrderReviewManager extends DBCommon {
    /**
     * 合同评审界面数据查询.
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param appstatus 审批状态
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderReviewManagerHeadQuery(String orderNo,
            String appstatus, String beginTime, String endTime, String[] arr) {
        return CoreOrderReviewManagerHeadQuery(orderNo, appstatus,beginTime,endTime,arr,"");
    }

    public CoreReturnObject CoreOrderReviewManagerHeadQuery(String orderNo,String appstatus, String beginTime,
                                                            String endTime, String[] arr,String supp) {
        Object[] params = new Object[] { orderNo, appstatus, appstatus,
                appstatus, beginTime, endTime };
        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",
                arr);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewManager.CoreOrderReviewManagerHeadQuery");
        sql += condition;
        if (!StringUtils.isEmpty(supp)) {
            sql += " and SUPPLY_UNIT = '"+supp+"'";
        }
        return this.getData(sql, params);
    }

    /**
     * 根据合同号查询合同行信息
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderReviewManagerSqeQuery(String ordermodel) {
        Object[] obj = new Object[] { ordermodel };
        return this.getDataBySqlID(
                "CoreOrderReviewManager.CoreOrderReviewManagerSqeQuery", obj);
    }

    /**
     * 提报评审
     * @param parm
     * @param odertyp
     * @param name
     * @param dept
     * @param saleOrg
     * @return
     */
    public CoreReturnObject coreOrderReviewManagerReport(
            ArrayList<Object> parm, String odertyp, String name, String dept,
            String saleOrg) {
        CoreReturnObject cro = new CoreReturnObject();
        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        String ordPk = parm.get(1).toString(); // 合同PK值
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
            if (cos.getOrderSts().equals("12020102")) {
                java.sql.Date date = new java.sql.Date(
                        new java.util.Date().getTime());
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                int year = c.get(Calendar.YEAR);
                int mon = c.get(Calendar.MONTH) + 1;
                int day = c.get(Calendar.DATE);
                ArrayList<Object> list = new ArrayList<Object>();
                list.add(parm.get(0));
                list.add(year);
                list.add(mon);
                list.add(day);
                list.add(parm.get(0));
                list.add(year);
                list.add(mon);
                list.add(day);

                String reviewNo = CoreOrderStatusCommFun.generateOrderReviewNo(
                        con, list); // 生成这个OrdPk的ReviewNo

                ArrayList<Object> list1 = new ArrayList<Object>();
                list1.add(reviewNo);
                list1.add(parm.get(0));
                list1.add(parm.get(1)); // ordPk
                list1.add("W");
                list1.add(parm.get(2));
                list1.add(parm.get(2));
                // 新增主数据
                CoreOrderStatusCommFun.insertOrderReview(con, list1);

                // 评审项目参数（不同类型合同所建立的项目不同）
                ArrayList<Object> item1 = new ArrayList<Object>();
                item1.add(reviewNo);
                item1.add("T");
                item1.add("技术评审");
                item1.add("W");
                item1.add(parm.get(2));
                ArrayList<Object> item2 = new ArrayList<Object>();
                item2.add(reviewNo);
                item2.add("D");
                item2.add("交货期评审");
                item2.add("W");
                item2.add(parm.get(2));
                ArrayList<Object> item3 = new ArrayList<Object>();
                item3.add(reviewNo);
                item3.add("P");
                item3.add("价格评审");
                item3.add("W");
                item3.add(parm.get(2));
                ArrayList<Object> item4 = new ArrayList<Object>();
                item4.add(reviewNo);
                item4.add("F");
                item4.add("法务评审");
                item4.add("W");
                item4.add(parm.get(2));

                // 新增评审项目数据
                if (saleOrg.equals("100102")) {// 国贸公司

                    // 查询客户合同
                    SqlBuilder b = new SqlBuilder("CoreOrderCopy.queryHead");
                    b.Fill(new String[]{ordPk});
                    SlmOrderHeadCopy orderHead = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderHeadCopy.class);
                    CoreOrderCopy orderCopy = new CoreOrderCopy();
                    String custmOrderNo = orderCopy.getOrderByCustm(con,orderHead.getOrderNo());

                    // 现货合同，不用做4项评审，但是需要经过审批提报和审批提报（业务主管）
                     if (odertyp.equals("A")) {

                        CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                ordPk, "12020102", "12020220", "待审批", name,
                                date.toString());
                        cou.updateOrderLineStatus(con, ordPk, "12020102",
                                "12020220");
                        cou.updateOrderDeliveryLineStatus(con, ordPk,
                                "12020102", "12020220");
                        CoreOrderStatusCommFun.insertOrderReviewItem(con, item4);
                        cou.insertOrderReviewItemList(con, reviewNo, "F", "H",
                                 ordPk, "W", parm.get(2).toString(), "");
                    // 天淮成品合同的母合同  跳过技术，交货期评审
                     } else if (!StringUtils.isEmpty(custmOrderNo) && custmOrderNo.startsWith("TH")) {
                             CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                     ordPk, "12020102", "12020213", "待提报审批", name,
                                     date.toString());
                             cou.updateOrderLineStatus(con, ordPk, "12020102","12020213");
                             cou.updateOrderDeliveryLineStatus(con, ordPk,"12020102", "12020213");
                             CoreOrderStatusCommFun.insertOrderReviewItem(con, item4);
                             cou.insertOrderReviewItemList(con, reviewNo, "F", "H",
                                 ordPk, "W", parm.get(2).toString(), "");
                     }
                    else // 非现货合同 需要走 技术 交货期 交货期通过 审批提报
                         // 审批提报(业务主管)-----(跳过价格评审)----法务评审 直接到评审通过
                    {

                        CoreOrderStatusCommFun
                                .insertOrderReviewItem(con, item1);
                        CoreOrderStatusCommFun
                                .insertOrderReviewItem(con, item2);
                        CoreOrderStatusCommFun
                                .insertOrderReviewItem(con, item4);

                        CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                ordPk, "12020102", "12020201", "待技术评审", name,
                                date.toString());
                        cou.updateOrderLineStatus(con, ordPk, "12020102",
                                "12020201");
                        cou.updateOrderDeliveryLineStatus(con, ordPk,
                                "12020102", "12020201");

                        // 新增项目评审明细数据
                        List<String> lnPkList = CoreOrderStatusCommFun
                                .getOrderLnPkValue(con, ordPk);

                        for (int i = 0; i < lnPkList.size(); i++) {
                            cou.insertOrderReviewItemList(con, reviewNo, "T",
                                    "L", lnPkList.get(i), "W", parm.get(2)
                                            .toString(), "");
                        }
                        cou.insertOrderReviewItemList(con, reviewNo, "D", "H",
                                ordPk, "W", parm.get(2).toString(), "");
                        cou.insertOrderReviewItemList(con, reviewNo, "F", "H",
                                ordPk, "W", parm.get(2).toString(), "");
                        updateReviewItemListStatus(con, ordPk, name, dept);
                    }
                }
                else {// 销售公司，钢贸公司。
                    if (odertyp.equals("A")) { // 现货合同
                        SqlBuilder b = new SqlBuilder("CoreOrderCopy.queryHead");
                        b.Fill(new String[]{ordPk});
                        SlmOrderHeadCopy orderHead = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderHeadCopy.class);

                        // 天淮现货直接跳过评审，等待生效
                        if(orderHead != null && orderHead.getOrderNo().startsWith("TH")){
                            CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                    ordPk, "12020102", "12020251", "评审通过", name,
                                    date.toString());
                            cou.updateOrderLineStatus(con, ordPk, "12020102",
                                    "12020251");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020102", "12020251");
                        } else {
                            CoreOrderStatusCommFun
                                    .insertOrderReviewItem(con, item3);
                            CoreOrderStatusCommFun
                                    .insertOrderReviewItem(con, item4);

                            CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                    ordPk, "12020102", "12020212", "交货期评审通过", name,
                                    date.toString());
                            cou.updateOrderLineStatus(con, ordPk, "12020102",
                                    "12020212");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020102", "12020212");

                            cou.insertOrderReviewItemList(con, reviewNo, "P", "H",
                                    ordPk, "W", parm.get(2).toString(), "");
                            cou.insertOrderReviewItemList(con, reviewNo, "F", "H",
                                    ordPk, "W", parm.get(2).toString(), "");
                        }
                    }
                    else { // 非现货合同
                        String ordType = CoreOrderStatusCommFun.getOrderType(
                                con, ordPk);
                        if (ordType.equals("120103")) { // 研发合同

                            CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                    item1);
                            CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                    ordPk, "12020102", "12020201", "待技术评审",
                                    name, date.toString());
                            cou.updateOrderLineStatus(con, ordPk, "12020102",
                                    "12020201");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020102", "12020201");

                            // 新增项目评审明细数据
                            List<String> lnPkList = CoreOrderStatusCommFun
                                    .getOrderLnPkValue(con, ordPk);
                            for (int i = 0; i < lnPkList.size(); i++) {
                                cou.insertOrderReviewItemList(con, reviewNo,
                                        "T", "L", lnPkList.get(i), "W", parm
                                                .get(2).toString(), "");
                            }
                            updateReviewItemListStatus(con, ordPk, name, dept);
                        }
                        else if (ordType.equals("120104")) { // 预排合同

                            CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                    item1);
                            CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                    item2);
                            
                            CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                    ordPk, "12020102", "12020201", "待技术评审",
                                    name, date.toString());
                            cou.updateOrderLineStatus(con, ordPk, "12020102",
                                    "12020201");
                            cou.updateOrderDeliveryLineStatus(con, ordPk,
                                    "12020102", "12020201");

                            // 新增项目评审明细数据
                            List<String> lnPkList = CoreOrderStatusCommFun
                                    .getOrderLnPkValue(con, ordPk);
                            for (int i = 0; i < lnPkList.size(); i++) {
                                cou.insertOrderReviewItemList(con, reviewNo,
                                        "T", "L", lnPkList.get(i), "W", parm
                                                .get(2).toString(), "");
                            }
                            cou.insertOrderReviewItemList(con, reviewNo, "D",
                                    "H", ordPk, "W", parm.get(2).toString(), "");
                            updateReviewItemListStatus(con, ordPk, name, dept);
                        }
                        else {
                            // 查询客户合同
                            SqlBuilder b = new SqlBuilder("CoreOrderCopy.queryHead");
                            b.Fill(new String[]{ordPk});
                            SlmOrderHeadCopy orderHead = EntityTool.getEntityBySqlNoClsCon(b.toString(), con, SlmOrderHeadCopy.class);
                            CoreOrderCopy orderCopy = new CoreOrderCopy();
                            String order = orderCopy.getOrderByOrderNo(con, orderHead.getCustmOrderNo());
                            String custmOrderNo = orderCopy.getOrderByCustm(con,orderHead.getOrderNo());
                            if(orderHead != null && orderHead.getOrderNo().startsWith("TH") && saleOrg.equals("100105") && !StringUtils.isEmpty(order)){ // 天淮成品合同 (跳过价格评审和法务评审)直接到评审通过
                                CoreOrderStatusCommFun
                                        .insertOrderReviewItem(con, item1);
                                CoreOrderStatusCommFun
                                        .insertOrderReviewItem(con, item2);

                                CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                        ordPk, "12020102", "12020201", "待技术评审", name,
                                        date.toString());
                                cou.updateOrderLineStatus(con, ordPk, "12020102",
                                        "12020201");
                                cou.updateOrderDeliveryLineStatus(con, ordPk,
                                        "12020102", "12020201");

                                // 新增项目评审明细数据
                                List<String> lnPkList = CoreOrderStatusCommFun
                                        .getOrderLnPkValue(con, ordPk);

                                for (int i = 0; i < lnPkList.size(); i++) {
                                    cou.insertOrderReviewItemList(con, reviewNo, "T",
                                            "L", lnPkList.get(i), "W", parm.get(2)
                                                    .toString(), "");
                                }
                                cou.insertOrderReviewItemList(con, reviewNo, "D", "H",
                                        ordPk, "W", parm.get(2).toString(), "");
                                updateReviewItemListStatus(con, ordPk, name, dept);
                            } else if(!StringUtils.isEmpty(custmOrderNo)){ // 天淮成品合同对应的天管合同 (不做技术，交货期)
                                CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                        item3);
                                CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                        item4);

                                CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                        ordPk, "12020102", "12020213", "待提报审批", name,
                                        date.toString());
                                cou.updateOrderLineStatus(con, ordPk, "12020102",
                                        "12020213");
                                cou.updateOrderDeliveryLineStatus(con, ordPk,
                                        "12020102", "12020213");

                                // 新增项目评审明细数据
                                List<String> lnPkList = CoreOrderStatusCommFun
                                        .getOrderLnPkValue(con, ordPk);

                                cou.insertOrderReviewItemList(con, reviewNo, "P", "H",
                                        ordPk, "W", parm.get(2).toString(), "");
                                cou.insertOrderReviewItemList(con, reviewNo, "F",
                                        "H", ordPk, "W", parm.get(2).toString(), "");
                                updateReviewItemListStatus(con, ordPk, name, dept);

                            } else {
                                CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                        item1);
                                CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                        item2);
                                CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                        item3);
                                CoreOrderStatusCommFun.insertOrderReviewItem(con,
                                        item4);

                                CoreOrderStatusCommFun.updateOrderHeadStatus(con,
                                        ordPk, "12020102", "12020201", "技术评审中",
                                        name, date.toString());
                                cou.updateOrderLineStatus(con, ordPk, "12020102",
                                        "12020201");
                                cou.updateOrderDeliveryLineStatus(con, ordPk,
                                        "12020102", "12020201");

                                // 新增项目评审明细数据
                                List<String> lnPkList = CoreOrderStatusCommFun
                                        .getOrderLnPkValue(con, ordPk);
                                for (int i = 0; i < lnPkList.size(); i++) {
                                    cou.insertOrderReviewItemList(con, reviewNo,
                                            "T", "L", lnPkList.get(i), "W", parm
                                                    .get(2).toString(), "");
                                }
                                cou.insertOrderReviewItemList(con, reviewNo, "D",
                                        "H", ordPk, "W", parm.get(2).toString(), "");
                                cou.insertOrderReviewItemList(con, reviewNo, "P",
                                        "H", ordPk, "W", parm.get(2).toString(), "");
                                cou.insertOrderReviewItemList(con, reviewNo, "F",
                                        "H", ordPk, "W", parm.get(2).toString(), "");
                                updateReviewItemListStatus(con, ordPk, name, dept);
                            }
                        }
                    }
                }
//                cou.doCrmEvent(con, ordPk,"","","1");
                con.commit();
                cro.setV_errCode(0);
            }
            else {
                cro.setV_errCode(-1);
                cro.setResult("非‘待提报评审’状态的合同，不允许提报评审！");
            }
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            finally {
                try {
                    if (con != null && !con.isClosed()) {
                        con.close();
                    }
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return cro;
    }

    /**
     * 更新评审记录的状态
     * @param con
     * @param ordPk
     * @param name
     * @param deptname
     * @throws SQLException
     */
    public void updateReviewItemListStatus(Connection con, String ordPk,
            String name, String deptname) throws SQLException {

        CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
        String reviewNo = cou.getOrderReviewNo(con, ordPk);

        ArrayList<Object> parm = new ArrayList<Object>();
        parm.add("D");
        parm.add(""); // 这里不再写入 操作人 部门 在最后一步操作写入。
        parm.add("");
        parm.add(reviewNo);
        parm.add("T");
        parm.add("L");

        CoreOrderStatusCommFun.updateOrderReviewItem(con, reviewNo, "T", "D");
        CoreOrderStatusCommFun.updateOrderReviewItemList(con, parm);

    }

    /**
     * 根据合同号查询合同行信息
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderReviewManagerReviewQuery(String ordermodel) {
        Object[] obj = new Object[] { ordermodel };
        return this
                .getDataBySqlID(
                        "CoreOrderReviewManager.CoreOrderReviewManagerReviewQuery",
                        obj);
    }

    /**
     * 查询合同的DesignKey
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderDesignKey(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderReviewManager.getOrderDesignKey",
                obj);
    }
}
