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.StringTool;
import com.steering.pss.sale.order.model.CoreOrderStatus;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CoreOrderReviewLaw extends DBCommon {
    /**
     * 合同评审界面数据查询.
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param appstatus 审批状态
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderReviewLawQuery(String appdate,
                                                    String order, String appstatus, String[] arr) {
        return CoreOrderReviewLawQuery(appdate,order,appstatus,arr,"");
    }
    public CoreReturnObject CoreOrderReviewLawQuery(String appdate,
            String order, String appstatus, String[] arr,String supp) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.CoreOrderReviewLawQuery");
        // Object[] params = new Object[] { appdate, order, appstatus };
        if (!StringTool.isNullOrEmpty(appdate)) {
            sql += appdate;
        }
        if (!StringTool.isNullOrEmpty(order)) {
            sql += " and B.ORDER_NO LIKE '%" + order + "%'";
        }
        if (!StringTool.isNullOrEmpty(appstatus)) {
            sql += " and B.ORDER_STS = '" + appstatus + "'";
        }
        if (!StringUtils.isEmpty(supp) && "120504".equals(supp)) {
            sql += " AND B.SUPPLY_UNIT = '120504' AND B.FST_DRAWEE_UNIT = '120607'";
        }
        String condition = CoreConverter.toSqlConditionString("CREATE_DEPT",
                arr);
        sql += condition;
        sql += " order by ORDER_NO";
        return this.getData(sql);
    }

    /**
     * 改变商务条款状态
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject UpdateOrderModelSts(String modelSts, String ordPk) {
        Object[] obj = new Object[] { modelSts, ordPk };
        return this.setDataBySqlID("CoreOrderReviewLaw.UpdateOrderModelSts",
                obj);
    }

    /**
     * 评审状态的绑定
     * @return CoreReturnObject 状态编码数据
     */
    public CoreReturnObject appStatus() {
        return this.getDataBySqlID("CoreOrderReviewLaw.appStatus");
    }

    /**
     * 根据合同范本查询合同条目
     * @param ordermodel 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject ordermodelQuery(String ordermodel) {
        Object[] obj = new Object[] { ordermodel };
        return this.getDataBySqlID("CoreOrderReviewLaw.ordermodelQuery", obj);
    }

    /**
     * 合同法务审批业务.
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject appWorkAdd(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkAdd");
        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 appWorkAdd2(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkAdd2");
        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 appWorkAdd3(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkAdd3");
        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 appWorkAdd4(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkAdd4");
        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
     *         appWorkUpdate(ArrayList<Object> parm) { CoreReturnObject cro =
     *         new CoreReturnObject(); String strSql = CoreXmlSqlParsersFactory
     *         .getSql("CoreOrderReviewLaw.appWorkUpdate"); 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 appWorkUpdate(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkUpdate");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 合同法务审批不通过业务.
     * @param order 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject appWorkUpdate2(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkUpdate2");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 合同法务审批不通过业务.
     * @param order 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject appWorkUpdate3(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkUpdate3");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 合同法务审批不通过业务.
     * @param order 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject appWorkUpdate4(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.appWorkUpdate4");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 根据客户名称查询出客户内容名称
     * @param customer 客户
     * @return CoreReturnObject
     */
    public CoreReturnObject customerdbQuery(String customer) {
        Object[] obj = new Object[] { customer };
        return this.getDataBySqlID("CoreOrderReviewLaw.customerdbQuery", obj);
    }

    // ----------------------------------2016-06-20---------------------------------------合同状态--------

    /**
     * 法务评审通过
     * @param ordPk
     * @param name
     * @param dept
     * @param reviewMemo
     * @return
     */
    public CoreReturnObject lawReviewPass(ArrayList<String> parm, String name,
            String dept, String reviewMemo) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            java.sql.Date date = new java.sql.Date(
                    new java.util.Date().getTime());
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();
            for (int i = 0; i < parm.size(); i++) {
                String ordPk = parm.get(i);
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                if (cos.getOrderSts().equals("12020232") || cos.getOrderSts().equals("12020222")) { // 价格评审通过 国贸主管经理通过
                    String reviewNo = cou.getOrderReviewNo(con, ordPk);
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            cos.getOrderSts(), "12020242", "法务评审通过", name,
                            date.toString());
                    cou.updateOrderLineStatus(con, ordPk, cos.getOrderSts(),
                            "12020242");
                    cou.updateOrderDeliveryLineStatus(con, ordPk, cos.getOrderSts(),
                            "12020242");
//                    cou.doCrmEvent(con, ordPk,"","","1");
                    // 评审表
                    String obj = dept + "-" + name + "在[合同法务评审]界面,点击[评审通过]";
                    CoreOrderStatusCommFun.updateOrderReviewItemList(con,
                            reviewNo, "F", "H", ordPk, "S", "1", name, dept,
                            reviewMemo, obj);
                    CoreOrderStatusCommFun.updateOrderReviewItemEDate(con,
                            reviewNo, "F", "S");
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            "12020242", "12020251", "评审通过", name,
                            date.toString());
                    cou.updateOrderLineStatus(con, ordPk, "12020242",
                            "12020251");
                    cou.updateOrderDeliveryLineStatus(con, ordPk, "12020242",
                            "12020251");
                    String time = StringTool.ReplaceAll(date.toString(), "-",
                            "");
                    addOrderLawRecord(con, ordPk, time, name);
                    // }
                    // else {
                    // String stsT = cou.getOrderReviewItemSatus(con, reviewNo,
                    // "T");
                    // String stsD = cou.getOrderReviewItemSatus(con, reviewNo,
                    // "D");
                    // String stsP = cou.getOrderReviewItemSatus(con, reviewNo,
                    // "P");
                    // String stsF = cou.getOrderReviewItemSatus(con, reviewNo,
                    // "F");
                    // if (stsT.equals("S") && stsD.equals("S")
                    // && stsP.equals("S") && stsF.equals("S")) {
                    // CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                    // "12020242", "12020251", "评审通过", name,
                    // date.toString());
                    // cou.updateOrderLineStatus(con, ordPk, "12020242",
                    // "12020251");
                    // cou.updateOrderDeliveryLineStatus(con, ordPk, "12020242",
                    // "12020251");
                    // }
                    // else {
                    // List<SlmOrderReviewItemListEntity> list =
                    // CoreOrderStatusCommFun
                    // .selectOrderReviewItemListInfo(con, reviewNo,
                    // "F", "H", ordPk);
                    // CoreOrderStatusCommFun.setOrderReviewItemListValue(con,
                    // reviewNo, "F", "H", ordPk);
                    // if (list != null && list.size() > 0) {
                    // SlmOrderReviewItemListEntity slm = list.get(0);
                    // slm.setCreateName(name);
                    // slm.setReviewDept(dept);
                    // slm.setReviewEdate("");
                    // slm.setReviewBdate("");
                    // CoreOrderStatusCommFun.addOrderReviewItemListInfo(
                    // con, slm);
                    // }
                    // }
                    // }
                }
                else {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同状态不是‘价格评审通过或主管审批’，不能进行审批通过操作！");
                    return cro;
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        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 e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 法务评审不通过
     * @param ordPk
     * @param name
     * @param dept
     * @param reviewMemo
     * @return
     */
    public CoreReturnObject lawReviewBack(ArrayList<String> parm, String name,
            String dept, String reviewMemo) {
        CoreReturnObject cro = new CoreReturnObject();
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            CoreOrderInfoUpdate cou = new CoreOrderInfoUpdate();

            for (int i = 0; i < parm.size(); i++) {
                String ordPk = parm.get(i);
                CoreOrderStatus cos = cou.getOrderStatus(con, ordPk);
                if (cos == null) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("所选合同已被作废，尝试刷新一下数据！");
                    return cro;
                }
                if (cos.getOrderSts().equals("12020232")) { // 价格评审通过
                    String reviewNo = cou.getOrderReviewNo(con, ordPk);
                    // cou.updateOrderHeadStatus(con, ordPk, "12020232",
                    // "12020241",
                    // "法务评审失败");
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            "12020232", "12020241", "法务评审失败", name,
                            date.toString());
                    cou.updateOrderLineStatus(con, ordPk, "12020232",
                            "12020241");
                    cou.updateOrderDeliveryLineStatus(con, ordPk, "12020232",
                            "12020241");
//                    cou.doCrmEvent(con, ordPk,"","","1");
                    // 评审表
                    String obj = dept + "-" + name + "在[合同法务评审]界面,点击[评审不通过]";
                    CoreOrderStatusCommFun.updateOrderReviewItemList(con,
                            reviewNo, "F", "H", ordPk, "F", "1", name, dept,
                            reviewMemo, obj);
                    CoreOrderStatusCommFun.updateOrderReviewItemNoDate(con,
                            reviewNo, "F", "F");

                    // 2015-10-28合同退回修改价格评审状态
                    CoreOrderStatusCommFun.setOrderPriceStatus(con, ordPk);
                }
                else if (cos.getOrderSts().equals("12020242")
                        || cos.getOrderSts().equals("12020251")) {// 评审通过
                                                                  // ---在界面显示
                                                                  // 法务评审通过
                    String reviewNo = cou.getOrderReviewNo(con, ordPk);
                    // cou.updateOrderHeadStatus(con, ordPk, "12020232",
                    // "12020241",
                    // "法务评审失败");
                    CoreOrderStatusCommFun.updateOrderHeadStatus(con, ordPk,
                            "12020242", "12020241", "法务评审失败", name,
                            date.toString());
                    cou.updateOrderLineStatus(con, ordPk, "12020242",
                            "12020241");
                    cou.updateOrderDeliveryLineStatus(con, ordPk, "12020242",
                            "12020241");
//                    cou.doCrmEvent(con, ordPk,"","","1");
                    // 评审表
                    String obj = dept + "-" + name + "在[合同法务评审]界面,点击[评审不通过]";
                    CoreOrderStatusCommFun.updateOrderReviewItemList(con,
                            reviewNo, "F", "H", ordPk, "F", "1", name, dept,
                            reviewMemo, obj);
                    CoreOrderStatusCommFun.updateOrderReviewItemNoDate(con,
                            reviewNo, "F", "F");
                    // 2015-10-28合同退回修改价格评审状态
                    CoreOrderStatusCommFun.setOrderPriceStatus(con, ordPk);
                    // 2015-11-05
                    clearOrderLawRecord(con, ordPk);
                }
                else {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("合同状态不是‘价格评审通过’或‘法务评审通过’，不能进行审批不通过操作！");
                    return cro;
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setResult(e.getMessage());
                e.printStackTrace();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }

        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    public CoreReturnObject queryLineAPrice(String ordPk) {
        Object[] params = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderReviewLaw.queryLineAPrice",
                params);
    }

    public CoreReturnObject queryPriceInfo(String ordPk) {
        Object[] params = new Object[] { ordPk };
        return this.getDataBySqlID("CoreOrderReviewLaw.queryPriceInfo", params);
    }
        
    /**
     * 新增法务评审记录
     * @param con
     * @param ordPk
     * @param date
     * @param name
     * @param isSeal 是否盖章
     * @throws SQLException
     */
    public void addOrderLawRecord(Connection con, String ordPk, String date,
            String name) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.addLawRecord");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, date);
        ps.setObject(2, date);
        ps.setObject(3, ordPk);
        ps.setObject(4, ordPk);
        ps.setObject(5, ordPk);
        ps.setObject(6, ordPk);
        ps.setObject(7, ordPk);
        ps.setObject(8, ordPk);
        ps.setObject(9, ordPk);
        ps.setObject(10, ordPk);
        ps.setObject(11, name);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 清除法务评审记录
     * @param con
     * @param ordPk
     * @throws SQLException
     */
    public static void clearOrderLawRecord(Connection con, String ordPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.clearLawRecord");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 法务评审单记录查询
     * @param orderNo
     * @return
     */
    public CoreReturnObject queryLawRecord(String orderNo, String queryFlag) {
        Object[] obj = new Object[] { orderNo };
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderReviewLaw.queryLawRecore");
        if (queryFlag.equals("2")) {
            // 查询全部 不必添加条件
        }
        else if (queryFlag.equals("1")) {
            sql += "AND T.IS_SEAL = '1'";
        }
        else if (queryFlag.equals("0")) {
            sql += " AND T.IS_SEAL = '0'";
        }
        return this.getData(sql, obj);
    }

    /**
     * 确认盖章
     * @param parm
     * @param name
     * @return
     */
    public CoreReturnObject confirmSeal(ArrayList<Object> parm, String name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderReviewLaw.queryRecordLaw");
            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderReviewLaw.confirmLawRecord");
            PreparedStatement ps = null;
            ResultSet rs = null;
            for (int i = 0; i < parm.size(); i++) {
                String recordId = parm.get(i).toString();
                ps = con.prepareStatement(sql);
                ps.setObject(1, recordId);
                rs = ps.executeQuery();
                String status = "";
                if (rs.next()) {
                    status = rs.getString(1);
                }
                rs.close();
                ps.close();
                if (status == null || status.equals("")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("您选择的记录已不存在！");
                    return cro;
                }
                else if (status.equals("1")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("您选择的记录已经确认盖章！");
                    return cro;
                }
                else if (status.equals("0")) {
                    ps = con.prepareStatement(sql1);
                    ps.setObject(1, name);
                    ps.setObject(2, recordId);
                    ps.executeUpdate();
                    ps.close();
                }
            }
            cro.setV_errCode(0);
            con.commit();
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }
}
