package com.steering.pss.ydm.acasleavefactory;

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

import java.util.List;

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.comm.tools.StringTool;

import com.steering.pss.ydm.entity.YdmZcOutlistEntity1;
import com.steering.pss.ydm.entity.YmdGpOutlistEntity;

/**
 * 坯子装车实绩
 * @author Administrator TJG 2015-03-21
 */
public class FrmCasLoad extends DBCommon {

    /***
     * 查询提单是否过磅
     * @param areaNo
     * @return
     */
    public CoreReturnObject queryBillCFlag(String billNo) {
        return this.getDataBySqlID("FrmCasLoad.queryBillCFlag",
                new Object[] { billNo });
    }

    /**
     * 修改发货实绩表信息确认
     * @param list 传递参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject updateDelConfirm(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlquery = "";
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updateMatZcNo");// 修改材料表过磅重量
            PreparedStatement psupdate = connection.prepareStatement(sqlupdate);
            String sqlupdateload = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updateDelConfirm");// 修改装车主表确认状态
            PreparedStatement psLoad = connection
                    .prepareStatement(sqlupdateload);
            String sqlupdateloadMAT = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updateMatConfirm");// 修改装车材料表状态
            PreparedStatement psLoadMat = connection
                    .prepareStatement(sqlupdateloadMAT);
            String insertOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.insertOutList"); // 新增到出库表
            PreparedStatement pstaOut = connection.prepareStatement(insertOut);
            String deleteButtress = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.deleteButtress"); // 删除位置表
            PreparedStatement pstaButtress = connection
                    .prepareStatement(deleteButtress);
            String updatProOrd = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updatProOrd"); // 回写进程表
            PreparedStatement pstaPro = connection
                    .prepareStatement(updatProOrd);

            List<YmdGpOutlistEntity> ydmOut = new ArrayList<YmdGpOutlistEntity>();
            for (int i = 0; i < list.size(); i++) {
                ydmOut.clear();
                ArrayList<String> list1 = (ArrayList<String>) list.get(i);
                String loadNo = list1.get(0);
                String billNo = list1.get(1);
                String tallNo = list1.get(2);
                String velNo = list1.get(3);
                String name = list1.get(4);
                String strtime = list1.get(5);
                String bc = list1.get(6);
                String bz = list1.get(7);

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasLoad.queryLoadMatNo");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, loadNo);
                psta.setObject(2, billNo);
                psta.setObject(3, tallNo);
                psta.setObject(4, velNo);
                ResultSet rs = psta.executeQuery();
                ydmOut = EntityTool
                        .getEntitysByRs(rs, YmdGpOutlistEntity.class);
                rs.close();
                psta.close();

                String sendOut = FrmCasOutAutomatic.querySendOut(connection,
                        "800805");
                // 获取出库流水号
                String butessMatNo = FrmCasOutAutomatic.querySendOutList(
                        connection, sendOut, "800805");
                int outNo = Integer.parseInt(butessMatNo.substring(butessMatNo
                        .length() - 4, butessMatNo.length()));

                for (YmdGpOutlistEntity lvMat : ydmOut) {
                    double count = Double.parseDouble(lvMat.getActCount());
                    double wt = 0;

                    String profalg = "";
                    // 判断是否过磅
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmCasLoad.queryBillCFlag");
                    psta = connection.prepareStatement(sqlquery);
                    psta.setObject(1, billNo);
                    psta.setObject(2, lvMat.getOrdLnDlyPk());
                    rs = psta.executeQuery();
                    if (rs.next()) {
                        profalg = rs.getString("PRT_FLAG");
                    }
                    rs.close();
                    psta.close();
                    if (profalg.equals("1")) {
                        if (lvMat.getPonderNo().equals("")) {
                            cro.setV_errMsg("请输入过磅单号！");
                            return cro;
                        }
                        if (lvMat.getPonderGrossWt().equals("")) {
                            cro.setV_errMsg("请输入过磅重量！");
                            return cro;
                        }
                        wt = Double.parseDouble(lvMat.getPonderGrossWt());
                    }
                    else {
                        wt = Double.parseDouble(lvMat.getActWeight());
                    }

                    // 新增出库表
                    pstaOut.setString(1, sendOut
                            + StringTool.formatDecimal(outNo, "0000"));
                    pstaOut.setString(2, sendOut);
                    pstaOut.setString(3, "800805");
                    pstaOut.setString(4, "销售出库");
                    pstaOut.setString(5, lvMat.getVehicleNo());
                    pstaOut.setString(6, lvMat.getPonderNo());
                    pstaOut.setString(7, String.valueOf(lvMat
                            .getPonderGrossWt()));
                    pstaOut.setString(8, lvMat.getOrderNo());
                    pstaOut.setString(9, lvMat.getOrderSeq());
                    pstaOut.setString(10, lvMat.getOrderSeqNo());
                    pstaOut.setString(11, lvMat.getBelongCode());
                    pstaOut.setString(12, lvMat.getBelongName());
                    pstaOut.setString(13, lvMat.getStorageNo());
                    pstaOut.setString(14, lvMat.getLocationNo());
                    pstaOut.setString(15, lvMat.getLaryNo());
                    pstaOut.setString(16, lvMat.getLocNo());
                    pstaOut.setString(17, lvMat.getDepartmentCode());
                    pstaOut.setString(18, lvMat.getDepartmentDesc());
                    pstaOut.setString(19, lvMat.getManagementNo());
                    pstaOut.setString(20, lvMat.getManagementName());
                    pstaOut.setString(21, lvMat.getVstel());
                    pstaOut.setString(22, lvMat.getVstelDesc());
                    pstaOut.setString(23, strtime);
                    pstaOut.setString(24, bc);
                    pstaOut.setString(25, bz);
                    pstaOut.setString(26, lvMat.getStorageAttr());
                    pstaOut.setString(27, lvMat.getStorageAttrName());
                    pstaOut.setString(28, lvMat.getStorageTypeNo());
                    pstaOut.setString(29, lvMat.getStorageTypeName());
                    pstaOut.setString(30, name);
                    pstaOut.setString(31, strtime);
                    pstaOut.setString(32, String.valueOf(lvMat.getSendNum()));
                    pstaOut.setString(33, lvMat.getMatNo());
                    pstaOut.addBatch();
                    outNo++;

                    psupdate.setObject(1, lvMat.getPonderNo());
                    psupdate.setObject(2, lvMat.getPonderGrossWt());
                    psupdate.setObject(3, lvMat.getMatNo());
                    psupdate.addBatch();

                    pstaPro.setObject(1, count);
                    pstaPro.setObject(2, wt);
                    pstaPro.setObject(3, lvMat.getOrdLnDlyPk());
                    pstaPro.addBatch();

                }
                pstaOut.executeBatch();
                pstaPro.executeBatch();

                pstaButtress.setObject(1, billNo);
                pstaButtress.setObject(2, loadNo);
                pstaButtress.setObject(3, tallNo);
                pstaButtress.setObject(4, velNo);
                pstaButtress.addBatch();

                psLoad.setObject(1, loadNo);
                psLoad.addBatch();

                psLoadMat.setObject(1, loadNo);
                psLoadMat.addBatch();
            }
            psupdate.executeBatch();
            pstaButtress.executeBatch();
            psLoad.executeBatch();
            psLoadMat.executeBatch();
            psupdate.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
     */
    public CoreReturnObject updateDelCancel(ArrayList<ArrayList<String>> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlquery = "";
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updateMatZcNo1");// 修改材料表过磅重量
            PreparedStatement psupdate = connection.prepareStatement(sqlupdate);
            String sqlupdateload = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updateDelCancel");// 修改装车主表确认状态
            PreparedStatement psLoad = connection
                    .prepareStatement(sqlupdateload);
            String sqlupdateloadMAT = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.updateMatCancel");// 修改装车材料表状态
            PreparedStatement psLoadMat = connection
                    .prepareStatement(sqlupdateloadMAT);
            String insterButtr = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.AddButtress"); // 新增位置表
            PreparedStatement pstaButtr = connection
                    .prepareStatement(insterButtr);
            String deleteOut = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.deleteOut"); // 删除出库表销售出库对应记录
            PreparedStatement pstaOut = connection.prepareStatement(deleteOut);
            String deleteProOrd = CoreXmlSqlParsersFactory
                    .getSql("FrmCasLoad.deleteProOrd"); // 回写进程表
            PreparedStatement pstaPro = connection
                    .prepareStatement(deleteProOrd);

            List<YdmZcOutlistEntity1> ydmOut = new ArrayList<YdmZcOutlistEntity1>();
            for (int i = 0; i < list.size(); i++) {
                ydmOut.clear();
                ArrayList<String> list1 = (ArrayList<String>) list.get(i);
                String loadNo = list1.get(0);
                String billNo = list1.get(1);
                String tallNo = list1.get(2);
                String velNo = list1.get(3);
                String name = list1.get(4);
                String strTime = list1.get(5);

                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmCasLoad.queryLoadMatNo");
                psta = connection.prepareStatement(sqlquery);
                psta.setObject(1, loadNo);
                psta.setObject(2, billNo);
                psta.setObject(3, tallNo);
                psta.setObject(4, velNo);
                ResultSet rs = psta.executeQuery();
                ydmOut = EntityTool.getEntitysByRs(rs,
                        YdmZcOutlistEntity1.class);
                rs.close();
                psta.close();

                for (YdmZcOutlistEntity1 com : ydmOut) {
                    double count = Double.parseDouble(com.getActCount());
                    double wt = 0;

                    String profalg = "";
                    // 判断是否过磅
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmCasLoad.queryBillCFlag");
                    psta = connection.prepareStatement(sqlquery);
                    psta.setObject(1, billNo);
                    psta.setObject(2, com.getOrdLnDlyPk());
                    rs = psta.executeQuery();
                    if (rs.next()) {
                        profalg = rs.getString("PRT_FLAG");
                    }
                    rs.close();
                    psta.close();
                    if (profalg.equals("1")) {

                        wt = Double.parseDouble(com.getPonderGrossWt());
                    }
                    else {
                        wt = Double.parseDouble(com.getActWeight());
                    }
                    // 新增位置表
                    pstaButtr.setString(1, com.getMatNo());
                    pstaButtr.setString(2, com.getJudgeStoveNo());
                    pstaButtr.setString(3, com.getBatchNo());
                    pstaButtr.setString(4, com.getBatchGroudNo());
                    pstaButtr.setString(5, com.getStorageNo());
                    pstaButtr.setString(6, com.getLocationNo());
                    pstaButtr.setObject(7, com.getLaryNo());
                    pstaButtr.setObject(8, com.getLocNo());
                    pstaButtr.setString(9, com.getStorageAttr());
                    pstaButtr.setString(10, com.getStorageAttrName());
                    pstaButtr.setString(11, com.getStorageTypeNo());
                    pstaButtr.setString(12, com.getStorageTypeName());
                    pstaButtr.setString(13, com.getManagementNo());
                    pstaButtr.setString(14, com.getManagementName());
                    pstaButtr.setString(15, com.getDepartmentCode());
                    pstaButtr.setString(16, com.getDepartmentDesc());
                    pstaButtr.setString(17, name);
                    pstaButtr.setString(18, strTime);
                    pstaButtr.addBatch();

                    psupdate.setObject(1, "");
                    psupdate.setObject(2, "");
                    psupdate.setObject(3, com.getMatNo());
                    psupdate.addBatch();

                    pstaPro.setObject(1, count);
                    pstaPro.setObject(2, wt);
                    pstaPro.setObject(3, com.getOrdLnDlyPk());
                    pstaPro.addBatch();

                }
                pstaPro.executeBatch();

                pstaOut.setObject(1, billNo);
                pstaOut.setObject(2, loadNo);
                pstaOut.setObject(3, tallNo);
                pstaOut.setObject(4, velNo);
                pstaOut.addBatch();

                psLoad.setObject(1, loadNo);
                psLoad.addBatch();

                psLoadMat.setObject(1, loadNo);
                psLoadMat.addBatch();
            }
            pstaButtr.executeBatch();
            pstaOut.executeBatch();
            psupdate.executeBatch();
            psLoad.executeBatch();
            psLoadMat.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
     */
    public CoreReturnObject queryAppFlag(String list) {
        return this.getDataBySqlID("FrmCasLoad.queryAppFlag",
                new Object[] { list });
    }

    /**
     * 查询销售结算标志
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryVelFalg(String list) {
        return this.getDataBySqlID("FrmCasLoad.queryVelFalg",
                new Object[] { list });
    }

    /**
     *查询提单表状态
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryBillType(String billType) {
        return this.getDataBySqlID("FrmCasLoad.QueryBillType",
                new Object[] { billType });
    }

    /**
     *确认是查询是否输入过磅单号，过磅重量
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallPond(String pond) {
        return this.getDataBySqlID("FrmCasLoad.queryTallPond",
                new Object[] { pond });
    }

    /**
     *确认是查询是否输入过磅单号，过磅重量
     * @param list 传递参数
     * @return obj
     */
    public CoreReturnObject queryTallPond1(ArrayList<String> list) {
        return this.getDataBySqlID("FrmCasLoad.queryTallPond1", list.toArray());
    }
}
