package com.steering.pss.plnsaleord.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.order.model.SlmOrderLenEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 轧管推荐长度申请审批
 * @author tjg 2017.05.25
 */
public class FrmApplyRoval extends DBCommon {

    /**
     * 查询
     * @param orderNo 合同号
     * @param strat 状态
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param args
     * @return
     */
    public CoreReturnObject queryApplyNo(String orderNo, String strat,
                                         String startTime, String endTime, String[] args, String orderName) {
        return queryApplyNo(orderNo,strat,startTime,endTime,args,orderName,"");
    }
    public CoreReturnObject queryApplyNo(String orderNo, String strat,
            String startTime, String endTime, String[] args, String orderName,String supp) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmApplyRoval.queryApplyNo");
        // sql += CoreConverter.toSqlConditionString("T.FOUNDER_NAME",
        // args);
        if (!orderNo.equals("")) {
            sql += " AND T.ORDER_NO LIKE '%" + orderNo + "%'";
        }
        if (!strat.equals("")) {
            sql += " AND T.EXE_STATUS='" + strat + "'";
        }
        if (!startTime.equals("")) {
            sql += " AND T.APPLY_TIME  between TO_DATE('" + startTime
                    + "','YYYY-MM-DD HH24:MI:SS') and TO_DATE('" + endTime
                    + "','YYYY-MM-DD HH24:MI:SS')";
        }
        // sql += " AND T.FOUNDER_NAME='" + orderName + "'";
        if(!StringUtils.isEmpty(supp)){
            sql += "  AND exists (SELECT 1 FROM SLM_ORDER_HEAD WHERE ORDER_NO = T.ORDER_NO AND VALIDFLAG = '1' AND SUPPLY_UNIT = '"+supp+"')";
        }
        cro = this.getData(sql);
        return cro;
    }

    /**
     * 审批通过
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject appThrough(ArrayList<Object> list, String oderName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        String nowTime = df.format(time);
        try {
            conn.setAutoCommit(false);

            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> list1 = (ArrayList<String>) list.get(i);
                String applyNo = list1.get(0);
                String orderLnPk = list1.get(1);
                String lengthDiscN = list1.get(5);// 长度描述
                String orderNo = list1.get(7);
                String orderSeq = list1.get(8);
                String memo = list1.get(10);
                String lenNoN = list1.get(11);// 推荐长度描述
                // 查询是否已审批
                String strat = "";
                String stratText = "";
                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("CorePlnZgMStoGp.PlnSizUpApplyNoStrat");
                psta = conn.prepareStatement(sqlquery);
                psta.setString(1, applyNo);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    strat = rs.getString("EXE_STATUS");
                    stratText = rs.getString("EXE_STATUS_TEXT");
                }
                rs.close();
                psta.close();

                if (!strat.equals("10")) {
                    cro.setV_errMsg("申请单号:" + applyNo + "已," + stratText
                            + ",不允许审批。");
                    return cro;
                }
                SlmOrderLenEntity lenEntity = new SlmOrderLenEntity();
                // 查询推荐长度信息
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderLenPln.QUERY1");
                psta = conn.prepareStatement(sqlquery);
                psta.setString(1, lenNoN);
                rs = psta.executeQuery();
                if (rs.next()) {
                    lenEntity.setLenNo(lenNoN);
                    lenEntity.setLenDesc(lengthDiscN);
                    lenEntity.setLenMax(rs.getString("LEN_MAX"));
                    lenEntity.setLenMin(rs.getString("LEN_MIN"));
                    lenEntity.setLongest(rs.getString("LONGEST"));
                    lenEntity.setLongLenRato(rs.getString("LONG_LEN_RATO"));
                    lenEntity.setAvgLenMin(rs.getString("AVG_LEN_MIN"));
                    lenEntity.setShortest(rs.getString("SHORTEST"));
                    lenEntity.setShrtLenRato(rs.getString("SHRT_LEN_RATO"));
                    lenEntity.setShrtStove(rs.getString("SHRT_STOVE"));
                    lenEntity.setSiglLen(rs.getString("SIGL_LEN"));
                    lenEntity.setDifrncLenMax(rs.getString("DIFRNC_LEN_MAX"));
                    lenEntity.setEndlenSpecstd(rs.getString("ENDLEN_SPECSTD"));

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

                lenEntity.setOrderNo(orderNo);
                lenEntity.setOrderSeq(orderSeq);
                // 修改审批状态
                String sqlupdate = CoreXmlSqlParsersFactory
                        .getSql("FrmApplyRoval.updateApplyNoStrat");
                PreparedStatement pstaupdate = conn.prepareStatement(sqlupdate);
                pstaupdate.setObject(1, 20);
                pstaupdate.setObject(2, memo);
                pstaupdate.setObject(3, nowTime);
                pstaupdate.setObject(4, oderName);
                pstaupdate.setObject(5, "");
                pstaupdate.setObject(6, applyNo);
                pstaupdate.executeUpdate();
                pstaupdate.close();
                // 修改合同行表
                String sqlSlmOrder = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmApplyRoval.updateSlmOrderLen"),
                        lenEntity);
                PreparedStatement pstaSlmOrder = conn
                        .prepareStatement(sqlSlmOrder);
                pstaSlmOrder.setObject(1, orderLnPk);
                pstaSlmOrder.executeUpdate();
                pstaSlmOrder.close();
                // 修改合同提报表
                String sqlPlnSA = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmApplyRoval.updatePlnSarORDERLen"),
                        lenEntity);
                PreparedStatement pstaupdateLen = conn
                        .prepareStatement(sqlPlnSA);
                pstaupdateLen.setObject(1, orderLnPk);
                pstaupdateLen.executeUpdate();
                pstaupdateLen.close();
                // 修改轧管排产耗时表状态
                String sqlupdateS = CoreXmlSqlParsersFactory
                        .getSql("FrmApplyRoval.updatePlnOrderZgS");
                PreparedStatement pstaupdateS = conn
                        .prepareStatement(sqlupdateS);
                pstaupdateS.setObject(1, "20");
                pstaupdateS.setObject(2, memo);
                pstaupdateS.setObject(3, orderNo);
                pstaupdateS.setObject(4, orderSeq);
                pstaupdateS.executeUpdate();
                pstaupdateS.close();
                // 修改计划表长度
                String sqlPln = EntityTool.fillSqlByEntity(
                        CoreXmlSqlParsersFactory
                                .getSql("FrmApplyRoval.updatePln"), lenEntity);
                PreparedStatement pstaPln = conn.prepareStatement(sqlPln);
                pstaPln.executeUpdate();
                pstaPln.close();
            }

            cro.setV_errMsg("审批通过成功!");
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                cro.setV_errMsg(sqle.getLocalizedMessage());
                conn.rollback();
            }
            catch (SQLException e) {

                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 审批不通过
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject appNotThrough(ArrayList<Object> list,
            String oderName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        String nowTime = df.format(time);
        try {
            conn.setAutoCommit(false);
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmApplyRoval.updateApplyNoStrat");
            PreparedStatement pstaupdate = conn.prepareStatement(sqlupdate);
            String sqlupdateS = CoreXmlSqlParsersFactory
                    .getSql("FrmApplyRoval.updatePlnOrderZgS");
            PreparedStatement pstaupdateS = conn.prepareStatement(sqlupdateS);
            String sqlPlnSA = CoreXmlSqlParsersFactory
                    .getSql("FrmApplyRoval.updatePlnSarORDER");
            PreparedStatement pstaupdateSA = conn.prepareStatement(sqlPlnSA);
            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> list1 = (ArrayList<String>) list.get(i);
                String applyNo = list1.get(0);
                String orderLnPk = list1.get(1);
                String orderNo = list1.get(7);
                String orderSeq = list1.get(8);
                String lengthDiscN = list1.get(5);// 长度描述
                String memo = list1.get(10);
                String lenNoN = list1.get(11);
                // 查询是否已审批
                String strat = "";
                String stratText = "";
                String sqlquery = CoreXmlSqlParsersFactory
                        .getSql("CorePlnZgMStoGp.PlnSizUpApplyNoStrat");
                psta = conn.prepareStatement(sqlquery);
                psta.setString(1, applyNo);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    strat = rs.getString("EXE_STATUS");
                    stratText = rs.getString("EXE_STATUS_TEXT");
                }
                rs.close();
                psta.close();

                if (!strat.equals("10")) {
                    cro.setV_errMsg("申请单号:" + applyNo + "已," + stratText
                            + ",不允许审批。");
                    return cro;
                }
                // 修改审批状态
                pstaupdate.setObject(1, "30");
                pstaupdate.setObject(2, memo);
                pstaupdate.setObject(3, nowTime);
                pstaupdate.setObject(4, oderName);
                pstaupdate.setObject(5, lengthDiscN);
                pstaupdate.setObject(6, applyNo);
                pstaupdate.addBatch();
                // 修改排产耗时表
                pstaupdateS.setObject(1, "30");
                pstaupdateS.setObject(2, memo);
                pstaupdateS.setObject(3, orderNo);
                pstaupdateS.setObject(4, orderSeq);
                pstaupdateS.addBatch();
                // 修改合同提报推荐长度
                pstaupdateSA.setObject(1, "");
                pstaupdateSA.setObject(2, "");
                pstaupdateSA.setObject(3, orderLnPk);
                pstaupdateSA.addBatch();
            }
            pstaupdate.executeBatch();
            pstaupdateS.executeBatch();
            pstaupdateSA.executeBatch();
            cro.setV_errMsg("审批不通过成功!");
            conn.commit();
        }
        catch (SQLException sqle) {
            try {
                cro.setV_errMsg(sqle.getLocalizedMessage());
                conn.rollback();
            }
            catch (SQLException e) {

                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }
}