package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.ydm.entity.QcmZgLockLogEntity;

public class FrmManageBlock extends DBCommon {

    /***
     * 查询成品库存
     * @param list
     * @param arr
     * @param startTim
     * @param endTim
     * @return
     */
    public CoreReturnObject queryInListData(String orderNo, String jusNo,
            String storageNo, String[] arr, String strFeng, String strMatLb) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmManageBlock.queryInListData");
        if (!orderNo.equals("")) {
            sql += "  AND T1.ORDER_NO LIKE '%" + orderNo + "%'";
        }
        if (!jusNo.equals("")) {
            sql += "  AND T1.JUDGE_STOVE_NO ='" + jusNo + "'";
        }
        if (!storageNo.equals("")) {
            sql += "  AND S.STORAGE_NO ='" + storageNo + "'";
        }
        if (!strFeng.equals("")) {
            sql += "  AND T1.MNG_LOCK_FLAG ='" + strFeng + "'";
        }
        if (!strMatLb.equals("")) {
            sql += "AND T1.PRODUCT_FLAG = '" + strMatLb + "'";
        }
        sql += CoreConverter.toSqlConditionString("T1.BELONG_CODE", arr);
        sql += "  GROUP BY T1.OLD_ORDER_NO, T1.OLD_ORDER_SEQ, T1.ORDER_NO, T1.ORDER_SEQ, "
                + "T1.ORDER_SEQ_NO,T1.LOCK_ID, T1.JUDGE_STOVE_NO, T1.BATCH_NO, T1.BATCH_GROUD_NO, "
                + "T1.PRODUCNAME, T1.STD_STYLE_DESC, T1.STEELNAME, T1.SPEC_NAME, T1.MODEL_DESC, "
                + "T1.STD_NAME, T1.JUDGE_STD_NAME, T1.GRADENAME, T1.ACT_DIMATER, T1.ACT_HEIGHT, "
                + "T1.ACT_LEN_MIN, T1.ACT_LEN_MAX, T1.BELONG_NAME, D.SALE_AREA_DESC, "
                + "D.CREATE_NAME, T1.PLINE_NAME, S.STORAGE_NAME,S.STORAGE_NO, D.CUSTOMER_NM, G.RECEIV_NM, "
                + "T1.PRODUCT_FLAG, T1.MAT_STATUS, T1.MEMO, T1.PIPMEMO, T1.INSTOCK_TIME, "
                + "T1.MNG_LOCK_MAKER, T1.MNG_LOCK_REMARK, T1.MNG_LOCK_RSN_CODE, T1.MNG_LOCK_FLAG";
        return this.getData(sql);
    }

    /***
     * 查询封锁记录
     * @param list
     * @param arr
     * @param startTim
     * @param endTim
     * @return
     */
    public CoreReturnObject queryLockLog(String jusNo, String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmManageBlock.queryLockLogMat");
        if (!jusNo.equals("")) {
            sql += "  AND JUDGE_STOVE_NO ='" + jusNo + "'";
        }
        sql += CoreConverter.toSqlConditionString("BELONG_CODE", arr);

        return this.getData(sql);
    }

    /**
     * 查询支数对应重量
     * @param list
     * @return
     */
    public CoreReturnObject checkMatWeight(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory.getSql(
                "FrmManageBlock.checkMatWeight").replace("#RUNMBER#",
                list.get(4));
        return this.getData(sql, new Object[] { list.get(0), list.get(1),
                list.get(2), list.get(3) });
    }

    /**
     * 查询封锁原因
     * @param list
     * @return
     */
    public CoreReturnObject lockReason(String lockCode) {
        return this.getDataBySqlID("FrmManageBlock.lockReason",
                new Object[] { lockCode });
    }

    /**
     * 查询封锁日志序号
     * @param conn
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryLockLog(Connection conn) throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyyMMdd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmManageBlock.queryLockLog");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, matter1.format(dt));
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString(1);
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     * 成品封锁
     * @param billNo
     * @return
     */
    @SuppressWarnings( { "unchecked" })
    public CoreReturnObject addBlock(ArrayList<Object> pram)
            throws SQLException {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        String sql = "";
        PreparedStatement psta = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);

            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("FrmManageBlock.updateMatLock"); // 修改材料表封锁标志
            PreparedStatement prsaUpdate = con.prepareStatement(sqlUpdate);

            String sqlInster = CoreXmlSqlParsersFactory
                    .getSql("FrmManageBlock.insterMatLock"); // 新增封锁日志表
            PreparedStatement prsaInster = con.prepareStatement(sqlInster);

            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String jusNo = list.get(1);
                String batch = list.get(2);
                String group = list.get(3);
                String lockCode = list.get(4);
                String name = list.get(6);
                String lockMeno = list.get(5);
                String time = list.get(7);
                String lockFlag = list.get(8);
                int count = Integer.parseInt(list.get(0));

                sql = CoreXmlSqlParsersFactory.getSql(
                        "FrmManageBlock.QUERYMATLOCK").replace("#count#",
                        String.valueOf(count + 1));
                psta = con.prepareStatement(sql);
                psta.setObject(1, jusNo);
                psta.setObject(2, batch);
                psta.setObject(3, group);
                psta.setObject(4, lockFlag);
                ResultSet rs = psta.executeQuery();
                List<QcmZgLockLogEntity> qcmLock = EntityTool.getEntitysByRs(
                        rs, QcmZgLockLogEntity.class);
                rs.close();
                psta.close();

                if (qcmLock == null) {
                    core.setV_errMsg("数据已发生变化,请重新查询！");
                    return core;
                }

                String lockNo = queryLockLog(con);// 获取锁定日志表序号

                prsaUpdate.setObject(1, lockCode);
                prsaUpdate.setObject(2, time);
                prsaUpdate.setObject(3, name);
                prsaUpdate.setObject(4, lockMeno);
                prsaUpdate.setObject(5, lockNo);
                prsaUpdate.setObject(6, jusNo);
                prsaUpdate.setObject(7, batch);
                prsaUpdate.setObject(8, group);
                prsaUpdate.setObject(9, lockFlag);
                prsaUpdate.setObject(10, String.valueOf(count + 1));
                prsaUpdate.addBatch();

                for (QcmZgLockLogEntity qcmZgLock : qcmLock) {

                    prsaInster.setObject(1, lockNo);
                    prsaInster.setObject(2, qcmZgLock.getJudgeStoveNo());
                    prsaInster.setObject(3, qcmZgLock.getBatchNo());
                    prsaInster.setObject(4, qcmZgLock.getBatchGroudNo());
                    prsaInster.setObject(5, qcmZgLock.getMaterialNo());
                    prsaInster.setObject(6, qcmZgLock.getMaterialName());
                    prsaInster.setObject(7, qcmZgLock.getProduccode());
                    prsaInster.setObject(8, qcmZgLock.getProducname());
                    prsaInster.setObject(9, qcmZgLock.getSteelcode());
                    prsaInster.setObject(10, qcmZgLock.getSteelname());
                    prsaInster.setObject(11, qcmZgLock.getSpecCode());
                    prsaInster.setObject(12, qcmZgLock.getSpecName());
                    prsaInster.setObject(13, qcmZgLock.getModelCode());
                    prsaInster.setObject(14, qcmZgLock.getModelDesc());
                    prsaInster.setObject(15, qcmZgLock.getStdStyle());
                    prsaInster.setObject(16, qcmZgLock.getStdStyleDesc());
                    prsaInster.setObject(17, qcmZgLock.getBelongCode());
                    prsaInster.setObject(18, qcmZgLock.getBelongName());
                    prsaInster.setObject(19, qcmZgLock.getStdCode());
                    prsaInster.setObject(20, qcmZgLock.getStdName());
                    prsaInster.setObject(21, qcmZgLock.getActDimater());
                    prsaInster.setObject(22, qcmZgLock.getActHeight());
                    prsaInster.setObject(23, String.valueOf(qcmZgLock
                            .getActLen()));
                    prsaInster.setObject(24, qcmZgLock.getActLenMin());
                    prsaInster.setObject(25, qcmZgLock.getActLenMax());
                    prsaInster.setObject(26, String.valueOf(qcmZgLock
                            .getActCount()));
                    prsaInster.setObject(27, String.valueOf(qcmZgLock
                            .getActWeight()));
                    prsaInster.setObject(28, String.valueOf(qcmZgLock
                            .getActTheoryWeight()));
                    prsaInster.setObject(29, lockFlag);
                    prsaInster.setObject(30, lockCode);
                    prsaInster.setObject(31, time);
                    prsaInster.setObject(32, name);
                    prsaInster.setObject(33, lockMeno);
                    prsaInster.addBatch();
                }
            }
            prsaInster.executeBatch();
            prsaUpdate.executeBatch();
            core.setResult(1);
            core.setV_errMsg("封锁成功!");
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return core;
    }

    /**
     * 成品解锁
     * @param billNo
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject deleteBlock(ArrayList<Object> pram)
            throws SQLException {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        String sql = "";
        PreparedStatement psta = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);

            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("FrmManageBlock.updateMatLock1"); // 修改材料表封锁标志
            PreparedStatement prsaUpdate = con.prepareStatement(sqlUpdate);

            String sqlupdateLog = CoreXmlSqlParsersFactory
                    .getSql("FrmManageBlock.updateMatLockLog"); // 修改封锁日志表解锁信息
            PreparedStatement prsalog = con.prepareStatement(sqlupdateLog);

            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String jusNo = list.get(1);
                String batch = list.get(2);
                String group = list.get(3);
                String memo = list.get(4);
                String name = list.get(5);
                String time = list.get(6);
                String lockFlag = list.get(7);
                String lockId = list.get(8);
                int count = Integer.parseInt(list.get(0));

                sql = CoreXmlSqlParsersFactory.getSql(
                        "FrmManageBlock.QUERYMATLOCK").replace("#count#",
                        String.valueOf(count + 1));
                psta = con.prepareStatement(sql);
                psta.setObject(1, jusNo);
                psta.setObject(2, batch);
                psta.setObject(3, group);
                psta.setObject(4, lockFlag);
                ResultSet rs = psta.executeQuery();
                List<QcmZgLockLogEntity> qcmLock = EntityTool.getEntitysByRs(
                        rs, QcmZgLockLogEntity.class);
                rs.close();
                psta.close();

                if (qcmLock == null) {
                    core.setV_errMsg("数据已发生变化,请重新查询！");
                    return core;
                }

                prsaUpdate.setObject(1, jusNo);
                prsaUpdate.setObject(2, batch);
                prsaUpdate.setObject(3, group);
                prsaUpdate.setObject(4, lockFlag);
                prsaUpdate.setObject(5, lockId);
                prsaUpdate.setObject(6, String.valueOf(count + 1));
                prsaUpdate.addBatch();

                prsalog.setObject(1, time);
                prsalog.setObject(2, name);
                prsalog.setObject(3, memo);
                prsalog.setObject(4, lockId);
                prsalog.addBatch();

            }
            prsalog.executeBatch();
            prsaUpdate.executeBatch();
            core.setResult(1);
            core.setV_errMsg("解锁成功!");
            con.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            e.printStackTrace();
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return core;
    }
}
