package com.steering.pss.ydm.pipemanage;

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

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

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

    /**
     * 查询材料库存信息
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryStorage(ArrayList<String> list,String[] arr) {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeStow.queryStorage");
    	sql += CoreConverter.toSqlConditionString("B.STORAGE_NO",arr);
    	sql += "  group by  T.judge_stove_no, T.batch_no, T.batch_groud_no, C.STORAGE_NAME,B.STORAGE_NO,B.LOCATION_NO ORDER BY t.judge_stove_no ASC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询材料位置信息
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryStow(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeStow.queryStow", list.toArray());
    }

    /**
     * 查询倒垛信息
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryStowList(ArrayList<String> list,String[] arr) {
    	String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeStow.queryStowList");
    	sql += CoreConverter.toSqlConditionString("B.STORAGE_NO",arr);
    	sql += "   GROUP BY  T.JUDGE_STOVE_NO,T.BATCH_NO,T.BATCH_GROUD_NO,B.STORAGE_NO,T.LOCATION_NO,T.OLD_LOCATION_NO,T.STOW_TIME ORDER BY T.STOW_TIME DESC";
        return this.getData(sql, list.toArray());
        
    }

    /**
     * 目标料位信息
     * @param list 传递参数
     * @return 平台统一返回对象
     */
    public CoreReturnObject queryLocaTarget(ArrayList<String> list) {
        return this.getDataBySqlID("FrmPipeStow.queryLocaTarget", 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("FrmPipeStow.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;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            int abc = Integer.parseInt(list.get(5));
            int[] piecesList = new int[] { (int) abc }; // 支数
            ArrayList<String> billMats = new ArrayList<String>(); // 保存材料表主键
            String sqlquery = ""; // 根据按炉批行查询MAT_ZC_M主键
            String sqlqueTarget = "";
            String sqlinsert = "";
            String sqlupdate = "";

            for (int i = 0; i < piecesList.length; i++) {
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmPipeStow.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(4, list.get(3));
                psta.setString(4, list.get(4));
                psta.setInt(5, piecesList[i] + 1);
                ResultSet rs = psta.executeQuery();
                while (rs.next()) {
                    billMats.add(rs.getString(1));
                }
                rs.close();
                psta.close();
            }
            Date now = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String systime = format.format(now);
            sqlqueTarget = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeStow.queryTager");// 查询要新增的目标库号信息
            psta = connection.prepareStatement(sqlqueTarget);
            psta.setString(1, list.get(6));
            ResultSet rsTag = psta.executeQuery();
            if (rsTag.next()) {
                for (int i = 0; i < billMats.size(); i++) {
                    sqlquery = CoreXmlSqlParsersFactory
                            .getSql("FrmPipeStow.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));
                        out.setStorage(rs1.getString(39));
                        out.setLocation(rs1.getString(40));
                        out.setLary(rs1.getString(41));
                        out.setLoc(rs1.getString(42));
                        out.setStoattr(rs1.getString(43));
                        out.setStoattrname(rs1.getString(44));
                        out.setStotype(rs1.getString(45));
                        out.setStotypename(rs1.getString(46));
                        out.setManagno(rs1.getString(47));
                        out.setManagname(rs1.getString(48));

                        sqlinsert = CoreXmlSqlParsersFactory
                                .getSql("FrmPipeStow.AddStowList"); // 新增
                        psta = connection.prepareStatement(sqlinsert);
                        psta.setString(1, querySendSql(connection, billMats
                                .get(i)));
                        psta.setString(2, out.getMat());
                        psta.setString(3, out.getLot());
                        psta.setString(4, out.getStove());
                        psta.setString(5, out.getJudge());
                        psta.setString(6, out.getBatch());
                        psta.setString(7, out.getGroud());
                        psta.setString(8, out.getMatno());
                        psta.setString(9, out.getMatname());
                        psta.setString(10, out.getProode());
                        psta.setString(11, out.getProname());
                        psta.setString(12, out.getStdcode());
                        psta.setString(13, out.getStdname());
                        psta.setString(14, out.getGradecode());
                        psta.setString(15, out.getGradename());
                        psta.setString(16, out.getSteelcode());
                        psta.setString(17, out.getSteelname());
                        psta.setString(18, out.getStdstyle());
                        psta.setString(19, out.getStyledesc());
                        psta.setString(20, out.getSpeccode());
                        psta.setString(21, out.getSpecname());
                        psta.setString(22, out.getModcode());
                        psta.setString(23, out.getModdesc());
                        psta.setString(24, out.getProorder());
                        psta.setString(25, out.getPlicode());
                        psta.setString(26, out.getPliname());
                        psta.setString(27, out.getDimater());
                        psta.setString(28, out.getHeight());
                        psta.setString(29, out.getLen());
                        psta.setString(30, out.getLenmin());
                        psta.setString(31, out.getLenmax());
                        psta.setString(32, out.getCount());
                        psta.setString(33, out.getWeight());
                        psta.setString(34, out.getTheoryweight());
                        psta.setString(35, out.getBelcode());
                        psta.setString(36, out.getBelname());
                        if (out.getRpotime() == null) {
                            psta.setString(37, "");
                        }
                        else {
                            psta.setString(37, out.getRpotime()
                                    .substring(0, 19));
                        }
                        psta.setString(38, out.getProbc());
                        psta.setString(39, out.getProbz());
                        psta.setString(40, list.get(7));// 班次
                        psta.setString(41, list.get(8));// 班组
                        psta.setString(42, rsTag.getString(1));
                        psta.setString(43, rsTag.getString(2));
                        psta.setString(44, rsTag.getString(3));
                        psta.setString(45, out.getLocation());
                        psta.setString(46, out.getLary());
                        psta.setString(47, out.getLoc());
                        psta.setString(48, out.getStoattr());
                        psta.setString(49, out.getStoattrname());
                        psta.setString(50, out.getStotype());
                        psta.setString(51, out.getStotypename());
                        psta.setString(52, list.get(10));
                        psta.setString(53, list.get(11));
                        psta.setString(54, list.get(12));
                        psta.setString(55, list.get(13));
                        psta.setString(56, list.get(9));
                        psta.setString(57, systime);
                        psta.setString(58, systime);
                        psta.executeUpdate();
                        psta.close();

                        sqlupdate = "update ydm_zc_buttress set location_no ="
                                + "'" + rsTag.getString(1) + "'"
                                + ", lary_no = " + "'" + rsTag.getString(2)
                                + "'" + ", loc_no= " + "'" + rsTag.getString(3)
                                + "'" + ", update_name = " + "'" + list.get(9)
                                + "'"
                                + ", update_time =SYSDATE where mat_no = '"
                                + billMats.get(i) + "'";// 修改材料位置信息
                        Statement sta = connection.createStatement();
                        sta.executeUpdate(sqlupdate);
                        sta.close();
                    }
                }
            }else
            {
            	cro.setV_errCode(-1);
                cro.setV_errMsg("没有维护该料位,无法进行倒垛!");
                return cro;
            }
            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;
    }
}
