package com.steering.mes.mcp.CollarLibrary;

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.converter.EntityTool;
import com.steering.comm.converter.QualityDesignTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.CouplingMatMEntity;

/**
 * 接箍倒库
 * @author siy 2015/05/28
 * @version v1.0
 */
public class FrmCollarOutBound extends DBCommon {

    /**
     * 查询接箍库存信息
     * @param list
     * @return
     */
    public CoreReturnObject doQueryInfoResult(ArrayList<String> list,
            String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCollarOutBound.queryInventory");
        // sql += " AND T1.KEP_NO LIKE '%' || '" + list.get(1) + "' || '%'";
        if (!list.get(0).equals("")) {
            sql += " AND T1.STORAGE_NO ='" + list.get(0) + "'";
        }
        else {
            sql += CoreConverter.toSqlConditionString("T1.STORAGE_NO", arr);
        }
        if (!list.get(2).equals("")) {
        	//sql += " AND T.JUDGE_STOVE_NO = '" + list.get(2) + "'";
        	sql += CoreConverter.toSqlConditionString("T.JUDGE_STOVE_NO", list.get(2).replace("，",",").split(","));

        }
        sql += " AND DECODE('" + list.get(3)
                + "', NULL, '1', T.create_time) BETWEEN";
        sql += " DECODE('" + list.get(4) + "', NULL, '1', to_date('"
                + list.get(5) + "', 'YYYY-MM-DD HH24:MI:SS'))";
        sql += " AND DECODE('" + list.get(6) + "', NULL, '1', to_date('"
                + list.get(7) + "', 'YYYY-MM-DD HH24:MI:SS'))";
        sql += " ORDER BY T.create_time desc";
        return this.getData(sql);
    }

    /**
     * 查询交易表倒库实绩
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryOutPerfo(ArrayList<String> list, String[] arr) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCollarOutBound.queryOutPerfo");
        // sql += "  AND KEP_NO LIKE '%' || '" + list.get(1) + "' || '%' ";
//        if (!list.get(0).equals("")) {
//            sql += " AND STORAGE_NO ='" + list.get(0) + "'";
//        }else {
//            sql += CoreConverter.toSqlConditionString("STORAGE_NO", arr);
//        }
        if (!list.get(2).equals("")) {
        	//sql += " AND T.JUDGE_STOVE_NO = '" + list.get(2) + "'";
        	sql += CoreConverter.toSqlConditionString("T.JUDGE_STOVE_NO", list.get(2).replace("，",",").split(","));

        }
        //sql += " AND judge_stove_no LIKE '%' || '" + list.get(2) + "' || '%'";
        sql += " AND DECODE('" + list.get(3)
                + "', NULL, '1', create_time) BETWEEN DECODE('" + list.get(4)
                + "', NULL, '1', to_date('" + list.get(5)
                + "', 'YYYY-MM-DD HH24:MI:SS'))";
        sql += " AND DECODE('" + list.get(6) + "', NULL, '1', to_date('"
                + list.get(7) + "', 'YYYY-MM-DD HH24:MI:SS')) ";
        sql += " AND DEAL_TYPE_CODE ='" + list.get(8) + "'";
        sql += " ORDER BY CREATE_TIME DESC";
        return this.getData(sql);
    }

    /**
     * 查询计划信息
     * @param con
     * @param sendPlanNo
     * @return
     * @throws SQLException
     */
    public static CouplingMatMEntity getSendPlanCInfo(Connection con,
            String matNo, String matNoXh) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCollarOutBound.QUERYMatButterResult");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, matNoXh);
        ps.setObject(2, matNo);
        ResultSet rs = ps.executeQuery();
        List<CouplingMatMEntity> sendPlanCEntitys = EntityTool.getEntitysByRs(
                rs, CouplingMatMEntity.class);
        rs.close();
        ps.close();
        if (sendPlanCEntitys == null || sendPlanCEntitys.size() == 0) {
            return null;
        }
        else {
            for (CouplingMatMEntity sendPlanCEntity : sendPlanCEntitys) {
                QualityDesignTool.nullConverNullString(sendPlanCEntity);
            }
            return sendPlanCEntitys.get(0);
        }
    }

    /***
     * 交易单号
     * @param plineCode
     * @param zygNo
     * @param connection
     * @return
     */
    public static String doAddDealOrde(String lx, String woId,
            Connection connection) {
        String resultNo = "";
        PreparedStatement psta = null;
        String sqlloading = CoreXmlSqlParsersFactory
                .getSql("FrmCollarOutBound.queryMaxDealOrde");// 查询交易单号
        Date now = new Date();
        SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
        String systime = date.format(now);
        try {
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, lx);
            psta.setObject(2, systime);
            psta.setObject(3, date1.format(now));
            psta.setObject(4, woId);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("DEAL_ORDE");
            }
            rs1.close();
            psta.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return resultNo;
    }


    public CoreReturnObject doAddDealOrde1(String lx, String woId) {
        Date now = new Date();
        SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd");
        String systime = date.format(now);
        return this.getDataBySqlID("FrmCollarOutBound.queryMaxDealOrde",new Object[]{lx,systime,date1.format(now),woId});
    }

    public CoreReturnObject getOutDoc(String storageno) {
        return this.getDataBySqlID("FrmCollarOutBound.getOutDoc",new Object[]{storageno,storageno});
    }


    /***
     * 最大材料序号
     * @param matNo
     * @param connection
     * @return
     */
    public static String doAddButterMax(String matNo, Connection connection) {
        String resultNo = "";
        PreparedStatement psta = null;
        String sqlloading = CoreXmlSqlParsersFactory
                .getSql("FrmCollarOutBound.queryMaxButter");// 查询最大材料序号
        try {
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, matNo);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("MAT_NO_XH");
            }
            rs1.close();
            psta.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return resultNo;
    }

    /**
     * 新增
     * @param list1 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject addOutLibResult(ArrayList<ArrayList<String>> list1) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAddDeal = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarOutBound.AddDealResult");// 新增交易信息
            PreparedStatement patAddDeal = connection
                    .prepareStatement(sqlAddDeal);
            String sqlAddButter = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarOutBound.AddButterResult");// 新增位置表
            PreparedStatement patAddButter = connection
                    .prepareStatement(sqlAddButter);
            String sqlupdateButter = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarOutBound.updateButter");// 修改位置表
            PreparedStatement patupdateButter = connection
                    .prepareStatement(sqlupdateButter);

            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String systime = dateFormat.format(now);

            for (int i = 0; i < list1.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) list1.get(i);
                String relutNo = list.get(2);
                String planNo = list.get(3);
//                String storage = list.get(4);
//                String kenNo = list.get(5);
                String mbStorage = list.get(6);
                String name = list.get(9);
                String count = list.get(10); //目标个
                String strOutNo = list.get(11);
                String strOutname = list.get(12);
                String mBkenNo = list.get(13);
                String matNo = list.get(15);
                String matNoXh = list.get(16);
                String weghit = list.get(17);

                // 获取交易单号
                String dealOrde = doAddDealOrde("OC", "800803", connection);
                // 获取材料实体了
                CouplingMatMEntity pln = getSendPlanCInfo(connection, matNo,
                        matNoXh);
            	if(pln==null){
					cro.setV_errMsg("未找到库存信息,请确认！");
					return cro;
				}
                int abc = Integer.parseInt(count);
                int butCOUNT = Integer.parseInt(pln.getProNum()) - abc;

                String storageAtte = "";
                String storageAtteName = "";
                String storageType = "";
                String storageTypeName = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmInLibrary.queryStorageType");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, mbStorage);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    storageAtte = rs.getString("STORAGE_ATTR");
                    storageAtteName = rs.getString("STORAGE_ATTR_NAME");
                    storageType = rs.getString("STORAGE_TYPE_NO");
                    storageTypeName = rs.getString("STORAGE_TYPE_NAME");
                }
                rs.close();
                psta.close();
                // 新增交易表
                patAddDeal.setObject(1, pln.getMachNo());
                patAddDeal.setObject(2, pln.getMachNm());
                patAddDeal.setObject(3, count);// 交易个数
                patAddDeal.setObject(4, weghit);// 交易重量
                patAddDeal.setObject(5, systime);
                patAddDeal.setObject(6, pln.getStorageNo());
                patAddDeal.setObject(7, pln.getLocationNo());
                patAddDeal.setObject(8, pln.getStorageAttr());
                patAddDeal.setObject(9, pln.getStorageAttrName());
                patAddDeal.setObject(10, pln.getStorageTypeNo());
                patAddDeal.setObject(11, pln.getStorageTypeName());
                patAddDeal.setObject(12, mbStorage);
                patAddDeal.setObject(13, mbStorage + "10101");
                patAddDeal.setObject(14, storageAtte);
                patAddDeal.setObject(15, storageAtteName);
                patAddDeal.setObject(16, storageType);
                patAddDeal.setObject(17, storageTypeName);
                patAddDeal.setObject(18, "0");
                patAddDeal.setObject(19, "0");
                patAddDeal.setObject(20, pln.getDemandCopper());
                patAddDeal.setObject(21, pln.getDemandDetection());
                patAddDeal.setObject(22, pln.getDemandBlast());
                patAddDeal.setObject(23, pln.getDemandBonderizing());
                patAddDeal.setObject(24, pln.getCustAlphaNo());
                patAddDeal.setObject(25, pln.getOrderSpecialDesc());
                patAddDeal.setObject(26, pln.getAlphaCode());
                patAddDeal.setObject(27, pln.getColOperator());              
                patAddDeal.setObject(28, "");// 备注
                patAddDeal.setObject(29, name);
                patAddDeal.setObject(30, systime);
                patAddDeal.setObject(31, strOutNo);
                patAddDeal.setObject(32, strOutname);
                patAddDeal.setObject(33, pln.getManagementNo());
                patAddDeal.setObject(34, pln.getManagementName());
                patAddDeal.setObject(35, "");// 交易受理方
                patAddDeal.setObject(36, "");
                patAddDeal.setObject(37, pln.getKepNo());
                patAddDeal.setObject(38, pln.getMatNo());
                patAddDeal.setObject(39, relutNo);
                patAddDeal.setObject(40, planNo);
                patAddDeal.setObject(41, pln.getProPlanId());
                patAddDeal.setObject(42, pln.getPlineCode());
                patAddDeal.setObject(43, pln.getPlineName());
                patAddDeal.setObject(44, pln.getJudgeStoveNo());
                patAddDeal.setObject(45, pln.getBatchNo());
                patAddDeal.setObject(46, pln.getBlWlId());
                patAddDeal.setObject(47, pln.getBlWlDesc());
                patAddDeal.setObject(48, pln.getMinD());
                patAddDeal.setObject(49, pln.getMinH());
                patAddDeal.setObject(50, pln.getCodeJg());
                patAddDeal.setObject(51, pln.getGradecode());
                patAddDeal.setObject(52, pln.getGradename());
                patAddDeal.setObject(53, pln.getSteelcode());
                patAddDeal.setObject(54, pln.getSteelname());
                patAddDeal.setObject(55, pln.getModelCode());
                patAddDeal.setObject(56, pln.getModelDesc());
                patAddDeal.setObject(57, pln.getOutdiameter());
                patAddDeal.setObject(58, pln.getWallthick());
                patAddDeal.setObject(59, pln.getSpecJg());
                patAddDeal.setObject(60, pln.getSpecJgDesc());
                patAddDeal.setObject(61, pln.getStyleJg());
                patAddDeal.setObject(62, pln.getDiameter());
                patAddDeal.setObject(63, pln.getCheckJVal());
                patAddDeal.setObject(64, pln.getAimlength());
                patAddDeal.setObject(65, pln.getSingleweight());
                patAddDeal.setObject(66, pln.getMonitordemand());
                patAddDeal.setObject(67, pln.getIfMonitorJg());
                patAddDeal.setObject(68, pln.getPrdcrNo());
                patAddDeal.setObject(69, pln.getProducer());
                patAddDeal.setObject(70, pln.getProducerTime());
                patAddDeal.setObject(71, mBkenNo);
                patAddDeal.setObject(72, list.get(14));
                patAddDeal.setObject(73, dealOrde);
        		patAddDeal.setObject(74, pln.getProductFlag());
				patAddDeal.setObject(75, pln.getMatStatus());
				patAddDeal.setObject(76, pln.getDeterStatus());
				patAddDeal.setObject(77, pln.getCodeJgBl());
				patAddDeal.setObject(78, pln.getCraftFileNo());
				patAddDeal.setObject(79, pln.getCraftSeq());
				patAddDeal.setObject(80, pln.getBelongCode());
				patAddDeal.setObject(81, pln.getBelongName());
				patAddDeal.setObject(82, pln.getCoatType());
				patAddDeal.setObject(83, pln.getStoveNo());
				patAddDeal.setObject(84, pln.getFlagStoveNo());
                patAddDeal.addBatch();
                patAddDeal.executeBatch();
                patAddDeal.close();
                // 判断位置表是否全部倒库
                if (butCOUNT == 0) {
                    patupdateButter.setObject(1, mbStorage);
                    patupdateButter.setObject(2, mbStorage + "10101");
                    patupdateButter.setObject(3, storageAtte);
                    patupdateButter.setObject(4, storageAtteName);
                    patupdateButter.setObject(5, storageType);
                    patupdateButter.setObject(6, storageTypeName);
                    patupdateButter.setObject(7, "");
                    patupdateButter.setObject(8, "");
                    patupdateButter.setObject(9, name);
                    patupdateButter.setObject(10, systime);
                    patupdateButter.setObject(11, matNo);
                    patupdateButter.setObject(12, matNoXh);
                    patupdateButter.addBatch();
                    patupdateButter.executeBatch();
                    patupdateButter.close();
                }
                else {
                    String matNoXNo = doAddButterMax(matNo, connection);
                    patAddButter.setObject(1, count);
                    patAddButter.setObject(2, weghit);
                    patAddButter.setObject(3, mbStorage);
                    patAddButter.setObject(4, mbStorage + "10101");
                    patAddButter.setObject(5, storageAtte);
                    patAddButter.setObject(6, storageAtteName);
                    patAddButter.setObject(7, storageType);
                    patAddButter.setObject(8, storageTypeName);
                    patAddButter.setObject(9, "");
                    patAddButter.setObject(10, "");
                    patAddButter.setObject(11, name);
                    patAddButter.setObject(12, systime);
                    patAddButter.setObject(13, matNoXNo);
                    patAddButter.setObject(14, matNo);
                    patAddButter.setObject(15, matNoXh);
                    patAddButter.setObject(16, count);
                    patAddButter.setObject(17, weghit);
                    patAddButter.setObject(18, name);
                    patAddButter.setObject(19, systime);
                    patAddButter.setObject(20, matNoXh);
                    patAddButter.setObject(21, matNo);
                    patAddButter.addBatch();
                    patAddButter.executeBatch();
                    patAddButter.close();
                }
            }

            cro.setV_errMsg("倒库出库成功！");
            connection.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            cro.setV_errMsg(e.getMessage());
            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
     */
    public CoreReturnObject deleteOutLibResult(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAddDeal = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarOutBound.deleteDealResult");// 删除交易信息
            PreparedStatement patAddDeal = connection
                    .prepareStatement(sqlAddDeal);
            String sqlAddButter = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarOutBound.deleteButterResult");// 删除位置表
            PreparedStatement patAddButter = connection
                    .prepareStatement(sqlAddButter);
            String sqlupdateButter = CoreXmlSqlParsersFactory
                    .getSql("FrmCollarOutBound.updateButter1");// 修改位置表
            PreparedStatement patupdateButter = connection
                    .prepareStatement(sqlupdateButter);

            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String systime = dateFormat.format(now);
            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> pram = (ArrayList<String>) list.get(i);
//                String dealType = pram.get(0);
//                String relutNo = pram.get(1);
//                String planNo = pram.get(2);
                String matNo = pram.get(3);
//                String kenNo = pram.get(4);
                String storage = pram.get(5);
//                String mBkenNo = pram.get(6);
                String mbStorage = pram.get(7);
                String count = pram.get(8);
                String weghit = pram.get(9);
                String name = pram.get(10);
                String dealNo = pram.get(11);

                String storageAtte = "";
                String storageAtteName = "";
                String storageType = "";
                String storageTypeName = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmInLibrary.queryStorageType");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, storage);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    storageAtte = rs.getString("STORAGE_ATTR");
                    storageAtteName = rs.getString("STORAGE_ATTR_NAME");
                    storageType = rs.getString("STORAGE_TYPE_NO");
                    storageTypeName = rs.getString("STORAGE_TYPE_NAME");
                }
                rs.close();
                psta.close();

                String recFalg = "";
                sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarOutBound.queryRecFalg");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, dealNo);
                rs = psta.executeQuery();
                if (rs.next()) {
                    recFalg = rs.getString("TAR_REC_FLAG");
                }
                rs.close();
                psta.close();

                if (recFalg.equals("1")) {
                    cro.setV_errMsg("该倒库出库记录已接收,不允许此操作！");
                    return cro;
                }
                // 删除交易信息
                patAddDeal.setObject(1, dealNo);
                patAddDeal.addBatch();
                patAddDeal.executeBatch();
                patAddDeal.close();
                int mbcount = 0;

                // 查询位置表该数据存在几条
                sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarOutBound.queryDealCount");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, matNo);
                rs = psta.executeQuery();
                if (rs.next()) {
                    mbcount = Integer.parseInt(rs.getString("COUNT"));
                }
                rs.close();
                psta.close();
                if (mbcount == 0) {
                    cro.setV_errMsg("无库存不允许此操作！");
                    return cro;
                }
                else if (mbcount == 1) {
                    // patupdateButter.setObject(1, mBkenNo);
                    // patupdateButter.setObject(2, mbStorage);
                    // patupdateButter.setObject(3, matNo);
                    patupdateButter.setObject(1, storage);
                    patupdateButter.setObject(2, storage + "10101");
                    patupdateButter.setObject(3, storageAtte);
                    patupdateButter.setObject(4, storageAtteName);
                    patupdateButter.setObject(5, storageType);
                    patupdateButter.setObject(6, storageTypeName);
                    patupdateButter.setObject(7, "");
                    patupdateButter.setObject(8, "");
                    patupdateButter.setObject(9, name);
                    patupdateButter.setObject(10, systime);
                    patupdateButter.setObject(11, mbStorage);
                    patupdateButter.setObject(12, matNo);
                    patupdateButter.addBatch();
                    patupdateButter.executeBatch();
                    patupdateButter.close();
                }
                else {
                    patAddButter.setObject(1, mbStorage);
                    patAddButter.setObject(2, matNo);
                    patAddButter.setObject(3, count);
                    patAddButter.setObject(4, weghit);
                    patAddButter.setObject(5, name);
                    patAddButter.setObject(6, systime);
                    patAddButter.setObject(7, storage);
                    patAddButter.setObject(8, matNo);
                    patAddButter.addBatch();
                    patAddButter.executeBatch();
                    patAddButter.close();
                }
            }
            cro.setV_errMsg("撤销成功！");
            connection.commit();

        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            cro.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
    }

    /**
     *获取仓库权限
     * @param validDataPurviewIds
     * @return
     */
    public CoreReturnObject getWarehousePermissionsStore(
            String[] validDataPurviewIds) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmCollarOutBound.getWarehousePermissionsStore");
        sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO",
                validDataPurviewIds);
        sql += " ORDER BY T.STORAGE_NAME ASC";
        return this.getData(sql);
    }


    /**
     * 根据科室ID查询对应的部门信息
     * @param section 科室
     * @return obj
     */
    public CoreReturnObject getDeptBySection(String section){
        Object[] params = new Object[]{section};
        return this.getDataBySqlID("FrmCollarOutBound.getDepartmentBySectionID", params);
    }


    /***
     * 查询单号下是否有未接收数据
     * @param dealOrde
     * @return
     */
    public CoreReturnObject doQueryCount(String dealOrde)
    {
        return this.getDataBySqlID("FrmCollarOutBound.doQueryCount", new Object[]{dealOrde});
    }

    public String queryStoreArea(String storageno, String tarStorageNo, Connection con) throws SQLException {
        String areaFlag = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        String storageNoArea = "";//出库仓库所属区域
        String tarStorageArea = "";//收料仓库所属区域
        String sql = CoreXmlSqlParsersFactory.getSql("FrmFilpOutStorage.queryStoreArea");
        ps = con.prepareStatement(sql);
        ps.setString(1,storageno);
        rs = ps.executeQuery();
        if(rs.next())
        {
            storageNoArea = rs.getString("SEAM_AREA");
        }
        rs.close();
        ps.close();


        String sql1 = CoreXmlSqlParsersFactory.getSql("FrmFilpOutStorage.queryStoreArea");
        ps = con.prepareStatement(sql);
        ps.setString(1,tarStorageNo);
        rs = ps.executeQuery();
        if(rs.next())
        {
            tarStorageArea = rs.getString("SEAM_AREA");
        }
        rs.close();
        ps.close();

        if(storageNoArea.equals("0") && tarStorageArea.equals("0"))
        {
            areaFlag = "1";
        }else
        {
            areaFlag = "0";
        }
        return  areaFlag;
    }


    /***
     * 写卡倒库出库
     * @param parmList
     * @return
     */
    public CoreReturnObject doAdd(ArrayList parmList,ArrayList<String> list,String[] storageArr,String outdoc)
    {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        //  TZH 24.4.24 注销物流接口
//        Connection tmsCon = this.getDao("TmsDao").getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        //String bathYear = df.format(now);
        String bathYear = com.steering.common.StringTool.GetBalYear(now,"120501"); //改25号结转
        int aCount = 0;
        String inDoc = "";//入库单号
        String superName = "";
        String superCall = "";
        int tmsCount = 0;
        String endAreaCode = "";//卸点区域
        String msg = "";//错误消息
        String isCarUser = "";//是否限制了一车一卡
        String isCarUserBill = "";
        String tmsWNo = "";//运单号
        String arriveArea = "";//到达点区域

        String isLoading = "";//装点是否需要吊车
        String typeFlag = "";//判断是否和物流对接
        try {
            connection.setAutoCommit(false);
            //  TZH 24.4.24 注销物流接口
//            tmsCon.setAutoCommit(false);
            ArrayList<String> sqllist = new ArrayList<String>();
            //region  验证
//                isCarUserBill = rs.getString("IS_CAR_TAKING_BILL");

//            sql = CoreXmlSqlParsersFactory.getSql("FrmCollarOutBound.SelectStorageIsCarUser");
//            psta = connection.prepareStatement(sql);
//            psta.setString(1,parmList.get(6));
//            rs = psta.executeQuery();
//            if(rs.next())
//            {
//                isCarUser = rs.getString("IS_CAR_TAKING");
//            }
//            rs.close();
//            psta.close();

//            typeFlag = queryStoreArea(parmList.get(6),list.get(0),connection);
//
//            if(typeFlag.equals("1"))
//            {
//                if(!list.get(0).equals("LDC2") )
//                {
//                    //判断司机是否接单并且扫码到达
//                    if((outType.equals("0") && isCarUser.equals("1")) || (outType.equals("1") && isCarUserBill.equals("1")))//写卡倒运/票据倒运
//                    {
//
//                        int m = 0;
//                        for(String strC : parmList)
//                        {
//                            CouplingEntity strCouplingEntity = com.steering.common.EntityTool.getEntityByJSONStr(strC,
//                                    CouplingEntity.class);
//
//
////							if((strMatBcEntity.getStorageNo().equals("LXW1")||strMatBcEntity.getStorageNo().equals("LGM4"))&&(list.get(0).equals("LBC1") || list.get(0).equals("LTF1") || list.get(0).equals("LAL1")))
////							{
////
////							}else
////							{
//
//
//
//                            sql = CoreXmlSqlParsersFactory.getSql("FrmCollarOutBound.selectWayBill");
//                            psta = tmsCon.prepareStatement(sql);
//                            psta.setString(1,strCouplingEntity.getJudgeStoveNo());
//                            psta.setString(2,strCouplingEntity.getStorageNo());
//                            psta.setString(3,list.get(8));
//                            rs = psta.executeQuery();
//                            if(rs.next())
//                            {
//                                tmsCount = Integer.parseInt(rs.getString("ACOUNT"));
//                                endAreaCode = rs.getString("END_AREA_CODE");
//                                tmsWNo = rs.getString("WAYBILL_NO");
//
//                            }
//                            rs.close();
//                            psta.close();
//                            if(tmsCount == 0)
//                            {
//                                cro.setV_errMsg("该车辆"+ list.get(8)+ "不存在已接单并且扫到达二维码的运单或者派单判定炉号与实际装车判定炉号不符，不允许倒运!");
//                                return  cro;
//                            }else
//                            {
//                                if(!endAreaCode.equals(list.get(0)))
//                                {
//                                    cro.setV_errMsg("倒运目标仓库与运输任务分派去向不一致,不允许倒运!");
//                                    return  cro;
//                                }
//                            }
//
//                            //判断当前库存点是否需要吊车扫码
//                            sql = CoreXmlSqlParsersFactory.getSql("FrmCollarOutBound.selectLoadingFlag");
//                            sql = com.steering.common.EntityTool.fillSqlByEntity(sql,strCouplingEntity);
//                            psta = connection.prepareStatement(sql);
//                            rs = psta.executeQuery();
//                            if(rs.next())
//                            {
//                                isLoading = rs.getString("IS_CRANE_LOADING");
//                            }
//                            rs.close();
//                            psta.close();
//                            //判断是否吊车已扫码
//                            if(isLoading.equals("1"))
//                            {
//                                sql = CoreXmlSqlParsersFactory.getSql("FrmCollarOutBound.selectTmsRelation");
//                                psta = tmsCon.prepareStatement(sql);
//                                psta.setString(1,tmsWNo);
//                                psta.setString(2,strCouplingEntity.getStorageNo());
//                                rs = psta.executeQuery();
//                                if(rs.next())
//                                {
//                                    aCount = Integer.parseInt(rs.getString("ACOUNT"));
//                                }
//                                rs.close();
//                                psta.close();
//                                if(aCount == 0)
//                                {
//                                    cro.setV_errMsg(strCouplingEntity.getStorageName() + "打卡前需吊车扫拖车司机运单二维码!");
//                                    return  cro;
//                                }
//                            }
//
//                            //}
////							if((strMatBcEntity.getStorageNo().equals("LXW1")||strMatBcEntity.getStorageNo().equals("LGM4"))&&(list.get(0).equals("LBC1") || list.get(0).equals("LTF1") || list.get(0).equals("LAL1")))
////							{
////
////							}else
////							{
//                            sql = CoreXmlSqlParsersFactory.getSql("FrmCollarOutBound.selectTraceMUnload");
//                            psta = tmsCon.prepareStatement(sql);
//                            psta.setString(1,tmsWNo);
//                            rs = psta.executeQuery();
//                            if(rs.next())
//                            {
//                                arriveArea = rs.getString("UNLOADING_AREA_CODE");
//                            }
//                            rs.close();
//                            psta.close();
//                            if(!arriveArea.equals(strCouplingEntity.getStorageNo()))
//                            {
//                                cro.setV_errMsg("装点区域与司机到达点区域不一致,不允许倒运!");
//                                return  cro;
//                            }
//                            //}
//                        }
//                    }
//                }
//            }

//            for (String str : parmList) {
//                CouplingEntity couplingEntity = com.steering.common.EntityTool.getEntityByJSONStr(str,
//                        CouplingEntity.class);
            //endregion

                String sqlAddDeal = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarOutBound.AddDealResult");// 新增交易信息
                PreparedStatement patAddDeal = connection
                        .prepareStatement(sqlAddDeal);
                String sqlAddButter = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarOutBound.AddButterResult2");// 新增位置表
                PreparedStatement patAddButter = connection
                        .prepareStatement(sqlAddButter);
                String sqlupdateButter = CoreXmlSqlParsersFactory
                        .getSql("FrmCollarOutBound.updateButter");// 修改位置表
                PreparedStatement patupdateButter = connection
                        .prepareStatement(sqlupdateButter);

                // 获取材料实体类
            for(int i=0;i<parmList.size();i++) {
                ArrayList<String> parm = (ArrayList) parmList.get(i);

                CouplingMatMEntity pln = getSendPlanCInfo(connection, parm.get(0), parm.get(7));

                String resultno = doAddDealOrde("OC", "800803",connection);


                if (pln == null) {
                    cro.setV_errMsg("未找到库存信息,请确认！");
                    return cro;
                }
                int butCOUNT = Integer.parseInt(parm.get(8))-Integer.parseInt(parm.get(3));
                int count = Integer.parseInt(parm.get(3));
                String storageAtte = "";
                String storageAtteName = "";
                String storageType = "";
                String storageTypeName = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmInLibrary.queryStorageType");
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, list.get(0));
                ResultSet rs1 = psta.executeQuery();
                if (rs1.next()) {
                    storageAtte = rs1.getString("STORAGE_ATTR");
                    storageAtteName = rs1.getString("STORAGE_ATTR_NAME");
                    storageType = rs1.getString("STORAGE_TYPE_NO");
                    storageTypeName = rs1.getString("STORAGE_TYPE_NAME");
                }
                rs1.close();
                psta.close();
                // 新增交易表
                patAddDeal.setObject(1, pln.getMachNo());
                patAddDeal.setObject(2, pln.getMachNm());
                patAddDeal.setObject(3, count);// 交易个数
                patAddDeal.setObject(4, parm.get(4));// 交易重量
                patAddDeal.setObject(5, systime);
                patAddDeal.setObject(6, pln.getStorageNo());
                patAddDeal.setObject(7, pln.getLocationNo());
                patAddDeal.setObject(8, pln.getStorageAttr());
                patAddDeal.setObject(9, pln.getStorageAttrName());
                patAddDeal.setObject(10, pln.getStorageTypeNo());
                patAddDeal.setObject(11, pln.getStorageTypeName());
                patAddDeal.setObject(12, list.get(0));
                patAddDeal.setObject(13, list.get(0) + "10101");
                patAddDeal.setObject(14, storageAtte);
                patAddDeal.setObject(15, storageAtteName);
                patAddDeal.setObject(16, storageType);
                patAddDeal.setObject(17, storageTypeName);
                patAddDeal.setObject(18, "0");
                patAddDeal.setObject(19, "0");
                patAddDeal.setObject(20, pln.getDemandCopper());
                patAddDeal.setObject(21, pln.getDemandDetection());
                patAddDeal.setObject(22, pln.getDemandBlast());
                patAddDeal.setObject(23, pln.getDemandBonderizing());
                patAddDeal.setObject(24, pln.getCustAlphaNo());
                patAddDeal.setObject(25, pln.getOrderSpecialDesc());
                patAddDeal.setObject(26, pln.getAlphaCode());
                patAddDeal.setObject(27, pln.getColOperator());
                patAddDeal.setObject(28, "");// 备注
                patAddDeal.setObject(29, list.get(1));
                patAddDeal.setObject(30, systime);
                patAddDeal.setObject(31, list.get(8));
                patAddDeal.setObject(32, list.get(9));
                patAddDeal.setObject(33, pln.getManagementNo());
                patAddDeal.setObject(34, pln.getManagementName());
                patAddDeal.setObject(35, "");// 交易受理方
                patAddDeal.setObject(36, "");
                patAddDeal.setObject(37, pln.getKepNo());
                patAddDeal.setObject(38, pln.getMatNo());
                patAddDeal.setObject(39, parm.get(1));
                patAddDeal.setObject(40, parm.get(2));
                patAddDeal.setObject(41, pln.getProPlanId());
                patAddDeal.setObject(42, pln.getPlineCode());
                patAddDeal.setObject(43, pln.getPlineName());
                patAddDeal.setObject(44, pln.getJudgeStoveNo());
                patAddDeal.setObject(45, pln.getBatchNo());
                patAddDeal.setObject(46, pln.getBlWlId());
                patAddDeal.setObject(47, pln.getBlWlDesc());
                patAddDeal.setObject(48, pln.getMinD());
                patAddDeal.setObject(49, pln.getMinH());
                patAddDeal.setObject(50, pln.getCodeJg());
                patAddDeal.setObject(51, pln.getGradecode());
                patAddDeal.setObject(52, pln.getGradename());
                patAddDeal.setObject(53, pln.getSteelcode());
                patAddDeal.setObject(54, pln.getSteelname());
                patAddDeal.setObject(55, pln.getModelCode());
                patAddDeal.setObject(56, pln.getModelDesc());
                patAddDeal.setObject(57, pln.getOutdiameter());
                patAddDeal.setObject(58, pln.getWallthick());
                patAddDeal.setObject(59, pln.getSpecJg());
                patAddDeal.setObject(60, pln.getSpecJgDesc());
                patAddDeal.setObject(61, pln.getStyleJg());
                patAddDeal.setObject(62, pln.getDiameter());
                patAddDeal.setObject(63, pln.getCheckJVal());
                patAddDeal.setObject(64, pln.getAimlength());
                patAddDeal.setObject(65, pln.getSingleweight());
                patAddDeal.setObject(66, pln.getMonitordemand());
                patAddDeal.setObject(67, pln.getIfMonitorJg());
                patAddDeal.setObject(68, pln.getPrdcrNo());
                patAddDeal.setObject(69, pln.getProducer());
                patAddDeal.setObject(70, pln.getProducerTime());
                patAddDeal.setObject(71, list.get(10));
                patAddDeal.setObject(72, parm.get(5));
                patAddDeal.setObject(73, resultno);
                patAddDeal.setObject(74, pln.getProductFlag());
                patAddDeal.setObject(75, pln.getMatStatus());
                patAddDeal.setObject(76, pln.getDeterStatus());
                patAddDeal.setObject(77, pln.getCodeJgBl());
                patAddDeal.setObject(78, pln.getCraftFileNo());
                patAddDeal.setObject(79, pln.getCraftSeq());
                patAddDeal.setObject(80, pln.getBelongCode());
                patAddDeal.setObject(81, pln.getBelongName());
                patAddDeal.setObject(82, pln.getCoatType());
                patAddDeal.setObject(83, pln.getStoveNo());
                patAddDeal.setObject(84, pln.getFlagStoveNo());
                patAddDeal.setObject(85, outdoc);
                patAddDeal.addBatch();
                patAddDeal.executeBatch();

                // 判断位置表是否全部倒库
                if (butCOUNT == 0) {
                    patupdateButter.setObject(1, list.get(0));
                    patupdateButter.setObject(2, list.get(0) + "10101");
                    patupdateButter.setObject(3, storageAtte);
                    patupdateButter.setObject(4, storageAtteName);
                    patupdateButter.setObject(5, storageType);
                    patupdateButter.setObject(6, storageTypeName);
                    patupdateButter.setObject(7, "");
                    patupdateButter.setObject(8, "");
                    patupdateButter.setObject(9, list.get(1));
                    patupdateButter.setObject(10, systime);
                    patupdateButter.setObject(11, parm.get(0));
                    patupdateButter.setObject(12, parm.get(7));
                    patupdateButter.addBatch();
                    patupdateButter.executeBatch();
                }
                else {
                    String matNoXNo = doAddButterMax(parm.get(0), connection);
                    patAddButter.setObject(1, count);
                    patAddButter.setObject(2, parm.get(4));
                    patAddButter.setObject(3, list.get(0));
                    patAddButter.setObject(4, list.get(0) + "10101");
                    patAddButter.setObject(5, storageAtte);
                    patAddButter.setObject(6, storageAtteName);
                    patAddButter.setObject(7, storageType);
                    patAddButter.setObject(8, storageTypeName);
                    patAddButter.setObject(9, "");
                    patAddButter.setObject(10, "");
                    patAddButter.setObject(11, list.get(1));
                    patAddButter.setObject(12, systime);
                    patAddButter.setObject(13, matNoXNo);
                    patAddButter.setObject(14, parm.get(0));
                    patAddButter.setObject(15, parm.get(7));
                    
                    patAddButter.addBatch();
                    patAddButter.executeBatch();

                }
            }
            patupdateButter.close();
            patAddButter.close();
            patAddDeal.close();

            if(msg.equals(""))
            {
                connection.commit();
                //  TZH 24.4.24 注销物流接口
//                tmsCon.commit();
                cro.setV_errMsg("倒库成功!");
            }else
            {
                cro.setV_errMsg(msg);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            //  TZH 24.4.24 注销物流接口
//            if (connection != null && tmsCon !=null) 
            	
            	 if (connection != null)
            {
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
//                    tmsCon.rollback();
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
//                if (tmsCon != null && !tmsCon.isClosed()) {
//                    tmsCon.close();
//                }
            } catch (Exception e2) {
            }
        }
        return cro;
    }

}
