package com.steering.pss.ydm.Report;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.CoreOrderInfoUpdate;
import com.steering.pss.ydm.entity.SlmOrderLogoutReviewEntity;

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

public class FrmSingleReview extends DBCommon {
    /**
     * 查询评审单
     * @param strWhere
     * @return
     */
    public CoreReturnObject getDoQueryRev(String stratTime, String endTime,
            String orderNo,String orderNoReal, String orderStrats) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSingleReview.getDoQueryRev");
        if (!stratTime.equals("")) {
            sql += "  AND T.CREATE_TIME BETWEEN TO_DATE('" + stratTime
                    + "', 'YYYY-MM-DD HH24:MI:SS') AND" + " TO_DATE('"
                    + endTime + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        if (!orderNoReal.equals("")) {
            sql += " AND substr(t.ord_ln_pk,0,length(t.ord_ln_pk)-3) in (select h.ord_pk from slm_order_head h where h.order_no like '%" + orderNoReal + "%')";
        }
        if (!orderStrats.equals("")) {
            sql += " AND T.REVIEW_STATUS ='" + orderStrats + "'";
        }
        if (!orderNo.equals("")) {
            sql += " AND T.REVIEW_NO LIKE '%'||'" + orderNo + "'||'%'";
        }
        sql += " GROUP BY T.REVIEW_NO,T.REVIEW_SRC,T.REVIEW_STATUS,T.REVEIW_BDATE,T.REVEIW_EDATE,"
                + "T.REVEIW_MAN,T.REVEIW_CONTENT,T.REVEIW_ASKER,T.REVEIW_ASK_TIME,T.MEMO,T.CREATE_NAME,"
                + " T.CREATE_TIME,T.UPDATE_NAME,T.UPDATE_TIME";
        sql += " ORDER BY T.CREATE_TIME ASC";
        return this.getData(sql);
    }

    /**
     * 查询注销单详情
     * @param strWhere
     * @return
     */
    public CoreReturnObject getDoQueryOrderRev(String reviewNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmSingleReview.getDoQueryOrderRev");
        return this.getData(sql, new Object[] { reviewNo });
    }

    /**
     * 评审通过
     * @param list 传递参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doReviewBy(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlOrdPk = CoreXmlSqlParsersFactory
                    .getSql("FrmSingleReview.UpdateRevNo");// 修改注销单
            PreparedStatement psOrdPk = connection.prepareStatement(sqlOrdPk);
            String sqlOrdLnPk = CoreXmlSqlParsersFactory
                    .getSql("FrmSingleReview.UpdateOrderSeqZx");// 修改合同行有效标志
            PreparedStatement psupdate = connection
                    .prepareStatement(sqlOrdLnPk);
            String updateOrdPk = CoreXmlSqlParsersFactory
                    .getSql("FrmSingleReview.UpdateOrderZx");// 修改合同有效标志
            PreparedStatement pstaupdate = connection
                    .prepareStatement(updateOrdPk);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String revNo = list.get(0);
                String revSrc = list.get(1);
                String user = list.get(2);
                String time = list.get(3);
                // 查询是注销单状态
                ArrayList<SlmOrderLogoutReviewEntity> ordLnPk = new ArrayList<SlmOrderLogoutReviewEntity>();
                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmSingleReview.queryLoagoutRevStart");
                PreparedStatement psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, revNo);
                psta.setObject(2, revSrc);
                ResultSet rs = psta.executeQuery();
                ordLnPk = EntityTool.getEntitysByRs(rs,
                        SlmOrderLogoutReviewEntity.class);

                rs.close();
                psta.close();

                String ordPK="";
                CoreOrderInfoUpdate coreOrderInfoUpdate = new CoreOrderInfoUpdate();
                for (SlmOrderLogoutReviewEntity ordLnPkNo : ordLnPk) {
                    if (!ordLnPkNo.getReviewStatus().equals("W")) {
                        cro.setV_errMsg("您选择的评审单不是待审状态,不能评审！");
                        return cro;
                    }
                    ordPK=ordLnPkNo.getOrdPk();
                    
                    // 是否存在库存
                    int count = 0;
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmSingleReview.QueryOrderMatNo");
                    psta = connection.prepareStatement(sqlquery);
                    psta.setObject(1, ordLnPkNo.getOrdLnPk());
                    rs = psta.executeQuery();
                    if (rs.next()) {
                        count = Integer.parseInt(rs.getString("COUNT"));
                    }
                    rs.close();
                    psta.close();
                    if (count != 0) {
                        cro.setV_errMsg("你选择的评审单存在合同行库存,请先脱单！");
                        return cro;
                    }
                    // 注销合同行
                    psupdate.setObject(1, "0");
                    psupdate.setObject(2, user);
                    psupdate.setObject(3, time);
                    psupdate.setObject(4, ordLnPkNo.getOrdLnPk());
                    psupdate.setObject(5, "0");
                    psupdate.setObject(6, user);
                    psupdate.setObject(7, time);
                    psupdate.setObject(8, ordLnPkNo.getOrdLnPk());
                    psupdate.setObject(9, ordLnPkNo.getOrdLnPk());
                    psupdate.addBatch();
                    coreOrderInfoUpdate.doCrmEvent(connection, "",ordLnPkNo.getOrdLnPk(),"","2");
                }
                psupdate.executeBatch();
                
                int countHt = 0;
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmSingleReview.QueryOrdNo");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, ordPK);
                rs = psta.executeQuery();
                if (rs.next()) {
                    countHt = Integer.parseInt(rs.getString("COUNT"));
                }
                rs.close();
                psta.close();
                if (countHt == 0) {
                    pstaupdate.setObject(1, "0");
                    pstaupdate.setObject(2, user);
                    pstaupdate.setObject(3, time);
                    pstaupdate.setObject(4, ordPK);
                    pstaupdate.addBatch();
                    pstaupdate.executeBatch();
                    coreOrderInfoUpdate.doCrmEvent(connection,  ordPK ,"","","2");
                }
                
                psOrdPk.setObject(1, "S");
                psOrdPk.setObject(2, user);
                psOrdPk.setObject(3, time);
                psOrdPk.setObject(4, revNo);
                psOrdPk.addBatch();

            }

            psOrdPk.executeBatch();

            cro.setV_errMsg("评审通过成功!");
            cro.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     *评审不通过
     * @param list 传递参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doAuditBy(ArrayList<Object> pram) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlOrdPk = CoreXmlSqlParsersFactory
                    .getSql("FrmSingleReview.UpdateRevNo");// 修改注销单
            PreparedStatement psOrdPk = connection.prepareStatement(sqlOrdPk);
            String updateOrdLnPk = CoreXmlSqlParsersFactory
                    .getSql("FrmSingleReview.updateOrdLnPkRevNo");// 修改合同行注销单
            PreparedStatement pstaOrdLnPk = connection
                    .prepareStatement(updateOrdLnPk);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String revNo = list.get(0);
                String revSrc = list.get(1);
                String user = list.get(2);
                String time = list.get(3);
                // 查询是注销单状态
                ArrayList<SlmOrderLogoutReviewEntity> ordLnPk = new ArrayList<SlmOrderLogoutReviewEntity>();
                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmSingleReview.queryLoagoutRevStart");
                PreparedStatement psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, revNo);
                psta.setObject(2, revSrc);
                ResultSet rs = psta.executeQuery();
                ordLnPk = EntityTool.getEntitysByRs(rs,
                        SlmOrderLogoutReviewEntity.class);
                rs.close();
                psta.close();

                for (SlmOrderLogoutReviewEntity ordLnPkNo : ordLnPk) {
                    if (!ordLnPkNo.getReviewStatus().equals("W")) {
                        cro.setV_errMsg("您选择的评审单不是待审状态,不能评审！");
                        return cro;
                    }
                    pstaOrdLnPk.setObject(1, "");
                    pstaOrdLnPk.setObject(2, ordLnPkNo.getOrdLnPk());
                    pstaOrdLnPk.addBatch();
                }
                pstaOrdLnPk.executeBatch();

                psOrdPk.setObject(1, "F");
                psOrdPk.setObject(2, user);
                psOrdPk.setObject(3, time);
                psOrdPk.setObject(4, revNo);
                psOrdPk.addBatch();
            }

            psOrdPk.executeBatch();

            cro.setV_errMsg("评审不通过成功!");
            cro.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }
}
