package com.steering.pss.ydm.pipemanage;

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

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

/**
 * @author TJG 2015-04-13
 */
public class FrmPipePayIn extends DBCommon {

    /**
     * 查询库存信息
     * @param list 传递参数
     * @param list1 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryStorage(ArrayList<String> list,
            ArrayList<String> list1) {

        String strparm = "";
        for (int i = 0; i < list1.size(); i++) {
            strparm += "'" + list1.get(i) + "',";
        }
        strparm = strparm.substring(0, strparm.length() - 1);
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipePayIn.queryStorage");
        String str = " and T.judge_stove_no LIKE '%'||" + "'"
                + list.get(0)
                + "'"
                + "||'%' AND DECODE("
                + "'"
                + list.get(1)
                + "'"
                + ", NULL, '1',T.create_time) BETWEEN DECODE("
                + "'"
                + list.get(2)
                + "'"
                + ", NULL, '1',to_date("
                + "'"
                + list.get(3)
                + "'"
                + ",'YYYY/MM/DD HH24:MI:SS')) AND DECODE("
                + "'"
                + list.get(4)
                + "'"
                + ", NULL, '1',to_date("
                + "'"
                + list.get(5)
                + "'"
                + ",'YYYY/MM/DD HH24:MI:SS')) AND T.CREATE_NAME="
                + "'"
                + list.get(6)
                + "'"
                + " AND T.PLINE_CODE in("
                + strparm
                + ") group by T.judge_stove_no, T.batch_no,t.PLINE_CODE ORDER BY t.judge_stove_no ASC";
        sql = sql + str;
        return this.getData(sql);
    }

    /**
     * 查询入库信息
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryInList(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipePayIn.queryInList", list.toArray());
    }

    /**
     * 每支材料对应的倒垛流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSql(Connection conn, String strlist)
            throws Exception {
        int seq = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmPipePayIn.querySqeList");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strlist + "0001";
            }
            else {
                seq = Integer.parseInt(res.substring(12, 16));
            }
        }
        rs.close();
        psta.close();
        return strlist + StringTool.formatDecimal(++seq, "0000");
    }

    /**
     * 保存
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject addPipeSwtow(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        PreparedStatement pstaXs = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            int abc = Integer.parseInt(list.get(4));
            int[] piecesList = new int[] { (int) abc }; // 支数
            ArrayList<String> billMats = new ArrayList<String>(); // 保存材料表主键
            String sqlquery = ""; // 根据按炉批行查询MAT_ZC_M主键
            String sqlqueTarget = "";
            String sqlinsert = "";
            String sqlinsertXs = "";
            String sqlupdate = "";

            for (int i = 0; i < piecesList.length; i++) {
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipePayIn.queryLibrary");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(0));
                psta.setString(2, list.get(1));
                // psta.setString(3, list.get(2));
                psta.setString(3, list.get(3));
                psta.setInt(4, piecesList[i] + 1);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    billMats.add(rs.getString(1));
                }
                rs.close();
                psta.close();
            }
            sqlqueTarget = CoreXmlSqlParsersFactory
                    .getSql("FrmPipePayIn.queryTager");// 查询要新增的目标库号信息
            psta = connection.prepareStatement(sqlqueTarget);
            psta.setString(1, list.get(6));
            psta.setString(2, list.get(5));
            ResultSet rsTag = psta.executeQuery();
            if (rsTag.next()) {
                for (int i = 0; i < billMats.size(); i++) {
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmPipePayIn.QueryAdd"); // 查詢出要新增的数据
                    psta = connection.prepareStatement(sqlquery);
                    psta.setString(1, billMats.get(i));
                    ResultSet rs1 = psta.executeQuery();
                    FrmPipeMoveOutMat out = new FrmPipeMoveOutMat();
                    if (rs1.next()) {
                        out.setMat(rs1.getString(1));
                        out.setLot(rs1.getString(2));
                        out.setStove(rs1.getString(3));
                        out.setJudge(rs1.getString(4));
                        out.setBatch(rs1.getString(5));
                        out.setGroud(rs1.getString(6));
                        out.setMatno(rs1.getString(7));
                        out.setMatname(rs1.getString(8));
                        out.setProode(rs1.getString(9));
                        out.setProname(rs1.getString(10));
                        out.setSteelcode(rs1.getString(11));
                        out.setSteelname(rs1.getString(12));
                        out.setGradecode(rs1.getString(13));
                        out.setGradename(rs1.getString(14));
                        out.setSpeccode(rs1.getString(15));
                        out.setSpecname(rs1.getString(16));
                        out.setModcode(rs1.getString(17));
                        out.setModdesc(rs1.getString(18));
                        out.setStdcode(rs1.getString(19));
                        out.setStdname(rs1.getString(20));
                        out.setStdstyle(rs1.getString(21));
                        out.setStyledesc(rs1.getString(22));
                        out.setProorder(rs1.getString(23));
                        out.setPlicode(rs1.getString(24));
                        out.setPliname(rs1.getString(25));
                        out.setDimater(rs1.getString(26));
                        out.setHeight(rs1.getString(27));
                        out.setLen(rs1.getString(28));
                        out.setLenmin(rs1.getString(29));
                        out.setLenmax(rs1.getString(30));
                        out.setCount(rs1.getString(31));
                        out.setWeight(rs1.getString(32));
                        out.setTheoryweight(rs1.getString(33));
                        out.setBelcode(rs1.getString(34));
                        out.setBelname(rs1.getString(35));
                        out.setProbc(rs1.getString(36));
                        out.setProbz(rs1.getString(37));
                        out.setRpotime(rs1.getString(38));

                        sqlinsert = CoreXmlSqlParsersFactory
                                .getSql("FrmPipePayIn.AddSInList"); // 新增到入库实绩表
                        psta = connection.prepareStatement(sqlinsert);
                        sqlinsertXs = CoreXmlSqlParsersFactory
                                .getSql("FrmPipePayInXs.AddSInList"); // 新增到销售入库实绩表
                        pstaXs = connection.prepareStatement(sqlinsertXs);
                        
                        psta
                                .setString(1, querySendSql(connection, list
                                        .get(9)));
                        psta.setString(2, list.get(9));
                        psta.setString(3, list.get(7));
                        psta.setString(4, list.get(8));
                        psta.setString(5, out.getMat());
                        psta.setString(6, out.getLot());
                        psta.setString(7, out.getStove());
                        psta.setString(8, out.getJudge());
                        psta.setString(9, out.getBatch());
                        psta.setString(10, out.getGroud());
                        psta.setString(11, out.getMatno());
                        psta.setString(12, out.getMatname());
                        psta.setString(13, out.getProode());
                        psta.setString(14, out.getProname());
                        psta.setString(15, out.getStdcode());
                        psta.setString(16, out.getStdname());
                        psta.setString(17, out.getGradecode());
                        psta.setString(18, out.getGradename());
                        psta.setString(19, out.getSteelcode());
                        psta.setString(20, out.getSteelname());
                        psta.setString(21, out.getStdstyle());
                        psta.setString(22, out.getStyledesc());
                        psta.setString(23, out.getSpeccode());
                        psta.setString(24, out.getSpecname());
                        psta.setString(25, out.getModcode());
                        psta.setString(26, out.getModdesc());
                        psta.setString(27, out.getProorder());
                        psta.setString(28, out.getPlicode());
                        psta.setString(29, out.getPliname());
                        psta.setString(30, out.getDimater());
                        psta.setString(31, out.getHeight());
                        psta.setString(32, out.getLen());
                        psta.setString(33, out.getLenmin());
                        psta.setString(34, out.getLenmax());
                        psta.setString(35, out.getCount());
                        psta.setString(36, out.getWeight());
                        psta.setString(37, out.getTheoryweight());
                        psta.setString(38, out.getBelcode());
                        psta.setString(39, out.getBelname());
                        psta.setString(40, list.get(6));
                        psta.setString(41, list.get(5));
                        psta.setString(42, rsTag.getString(1));
                        psta.setString(43, rsTag.getString(2));
                        psta.setString(44, rsTag.getString(3));
                        psta.setString(45, rsTag.getString(4));
                        psta.setString(46, rsTag.getString(5));
                        psta.setString(47, rsTag.getString(6));
                        psta.setString(48, rsTag.getString(7));
                        psta.setString(49, rsTag.getString(8));
                        
                        pstaXs
                        .setString(1, querySendSql(connection, list
                                .get(9)));
                pstaXs.setString(2, list.get(9));
                pstaXs.setString(3, list.get(7));
                pstaXs.setString(4, list.get(8));
                pstaXs.setString(5, out.getMat());
                pstaXs.setString(6, out.getLot());
                pstaXs.setString(7, out.getStove());
                pstaXs.setString(8, out.getJudge());
                pstaXs.setString(9, out.getBatch());
                pstaXs.setString(10, out.getGroud());
                pstaXs.setString(11, out.getMatno());
                pstaXs.setString(12, out.getMatname());
                pstaXs.setString(13, out.getProode());
                pstaXs.setString(14, out.getProname());
                pstaXs.setString(15, out.getStdcode());
                pstaXs.setString(16, out.getStdname());
                pstaXs.setString(17, out.getGradecode());
                pstaXs.setString(18, out.getGradename());
                pstaXs.setString(19, out.getSteelcode());
                pstaXs.setString(20, out.getSteelname());
                pstaXs.setString(21, out.getStdstyle());
                pstaXs.setString(22, out.getStyledesc());
                pstaXs.setString(23, out.getSpeccode());
                pstaXs.setString(24, out.getSpecname());
                pstaXs.setString(25, out.getModcode());
                pstaXs.setString(26, out.getModdesc());
                pstaXs.setString(27, out.getProorder());
                pstaXs.setString(28, out.getPlicode());
                pstaXs.setString(29, out.getPliname());
                pstaXs.setString(30, out.getDimater());
                pstaXs.setString(31, out.getHeight());
                pstaXs.setString(32, out.getLen());
                pstaXs.setString(33, out.getLenmin());
                pstaXs.setString(34, out.getLenmax());
                pstaXs.setString(35, out.getCount());
                pstaXs.setString(36, out.getWeight());
                pstaXs.setString(37, out.getTheoryweight());
                pstaXs.setString(38, out.getBelcode());
                pstaXs.setString(39, out.getBelname());
                pstaXs.setString(40, list.get(6));
                pstaXs.setString(41, list.get(5));
                pstaXs.setString(42, rsTag.getString(1));
                pstaXs.setString(43, rsTag.getString(2));
                pstaXs.setString(44, rsTag.getString(3));
                pstaXs.setString(45, rsTag.getString(4));
                pstaXs.setString(46, rsTag.getString(5));
                pstaXs.setString(47, rsTag.getString(6));
                pstaXs.setString(48, rsTag.getString(7));
                pstaXs.setString(49, rsTag.getString(8));
                
                        if (out.getRpotime() == null) {
                            psta.setString(50, "");
                            pstaXs.setString(50, "");

                        }
                        else {
                            psta.setString(50, out.getRpotime()
                                    .substring(0, 19));
                            pstaXs.setString(50, out.getRpotime()
                                    .substring(0, 19));
                        }
                        psta.setString(51, out.getProbc());
                        psta.setString(52, out.getProbz());
                        psta.setString(53, list.get(10));// 班次
                        psta.setString(54, list.get(11));// 班组
                        psta.setString(55, list.get(12));
                        psta.executeUpdate();
                        psta.close();
                        
                        pstaXs.setString(51, out.getProbc());
                        pstaXs.setString(52, out.getProbz());
                        pstaXs.setString(53, list.get(10));// 班次
                        pstaXs.setString(54, list.get(11));// 班组
                        pstaXs.setString(55, list.get(12));
                        pstaXs.executeUpdate();
                        pstaXs.close();

                        sqlinsert = CoreXmlSqlParsersFactory
                                .getSql("FrmPipePayIn.AddButtList"); // 新增到库存实绩表
                        psta = connection.prepareStatement(sqlinsert);
                        psta.setString(1, billMats.get(i));
                        psta.setString(2, list.get(0));
                        psta.setString(3, list.get(1));
                        psta.setString(4, list.get(2));
                        psta.setString(5, list.get(6));
                        psta.setString(6, list.get(5));
                        psta.setString(7, rsTag.getString(1));
                        psta.setString(8, rsTag.getString(2));
                        psta.setString(9, rsTag.getString(3));
                        psta.setString(10, rsTag.getString(4));
                        psta.setString(11, rsTag.getString(5));
                        psta.setString(12, rsTag.getString(6));
                        psta.setString(13, rsTag.getString(7));
                        psta.setString(14, rsTag.getString(8));
                        psta.setString(15, list.get(12));
                        psta.executeUpdate();
                        psta.close();

                        sqlupdate = "update MAT_ZC_M set INSTOCK_FLAG='1', UPDATE_NAME="
                                + "'"
                                + list.get(12)
                                + "'"
                                + ", UPDATE_TIME=SYSDATE where MAT_NO="
                                + "'"
                                + billMats.get(i) + "'" + "";// 修改材料位置信息
                        Statement sta = connection.createStatement();
                        sta.executeUpdate(sqlupdate);
                        sta.close();

                        cro.setResult(billMats.size());
                        connection.commit();
                    }
                }
            }
        }
        catch (Exception e) {
            System.out.println(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 平台统一返回对象
     */
    public CoreReturnObject deletePipeSwtow(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            int abc = Integer.parseInt(list.get(0));
            int[] piecesList = new int[] { (int) abc }; // 支数
            ArrayList<String> billMats = new ArrayList<String>(); // 保存入库表材料编号
            ArrayList<String> billInSqe = new ArrayList<String>(); // 保存入库变主键
            String sqlquery = ""; // 根据按炉批行查询MAT_ZC_M主键
            String sqlupdate = "";
            String sqldelete = "";

            for (int i = 0; i < piecesList.length; i++) {
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipePayIn.queryInMat");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, list.get(1));
                psta.setString(2, list.get(2));
                psta.setString(3, list.get(3));
                psta.setString(4, list.get(4));
                psta.setInt(5, piecesList[i] + 1);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    billInSqe.add(rs.getString(1));
                    billMats.add(rs.getString(2));
                }
                rs.close();
                psta.close();
            }
            String args = ""; // 入库表主键
            for (int i = 0; i < billInSqe.size(); i++) {
                args += "'" + billInSqe.get(i).toString() + "',";
            }
            args = args.substring(0, args.length() - 1);

            sqlupdate = "update ydm_zc_inlist set validflag = '0',delete_name ="
                    + "'"
                    + list.get(5)
                    + "'"
                    + ",delete_time = SYSDATE where instock_sqe in ("
                    + args
                    + ")"; // 修改入库表有效标志
            Statement sta = connection.createStatement();
            sta.executeUpdate(sqlupdate);
            sta.close();

            String args1 = ""; // 入库表主键
            for (int i = 0; i < billMats.size(); i++) {
                args1 += "'" + billMats.get(i).toString() + "',";
            }
            args1 = args1.substring(0, args1.length() - 1);

            sqlupdate = "update MAT_ZC_M set INSTOCK_FLAG = '0',UPDATE_NAME ="
                    + "'" + list.get(5) + "'"
                    + ",UPDATE_TIME = SYSDATE where MAT_NO in (" + args1 + ")"; // 修改材料表入库标志
            Statement sta1 = connection.createStatement();
            int coun = sta1.executeUpdate(sqlupdate);
            sta1.close();

            sqldelete = "delete ydm_zc_buttress where mat_no  in (" + args1
                    + ")";// 删除材料库存表信息
            Statement stadel = connection.createStatement();
            stadel.executeUpdate(sqldelete);
            stadel.close();

            connection.commit();
            cro.setResult(coun);
        }
        catch (Exception e) {
            System.out.println(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;
    }

}
