package com.steering.pss.ydm.pipeleavefactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import net.sf.json.JSONObject;
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.pss.ydm.entity.YdmZcAskMonthEntity;

/**
 * 铁运出厂月计划运号审批
 * @author Administrator 2015-06-16
 */
public class FrmShipmentApp extends DBCommon {

    /**
     * 查询铁运运号
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject doQuery(ArrayList<String> list, String[] salgs) {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmShipmentApp.doQuery");
        sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO", salgs);
        sql += "  ORDER BY T.CREATE_TIME DESC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询出厂计划主表信息
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject querySendPlanM(ArrayList<Object> param,
            String[] salgs) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmShipmentApp.QuerySENDPLANM");
        sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO", salgs);
        sql += " GROUP BY T.RECEIV_NO,T.RECEIV_NM,T.TRANS_TYPE,T.PLAN_YEAR_MONTH,T.STATION_NM,T.STATION_NO,T.SPCL_LN_NM,T.SPCL_LN_NO  ORDER BY T.PLAN_YEAR_MONTH ";
        return this.getData(sql, param.toArray());
    }

    /**
     * 根据到站、专用线、收货单位、计划年月 查询出厂计划号
     * @return obj
     */
    public CoreReturnObject querySendPlanMat(ArrayList<String> list,
            String[] salgs) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmShipmentApp.QuerySendPlanMat");
        sql += CoreConverter.toSqlConditionString("T.MANAGEMENT_NO", salgs);
        sql += " ORDER BY T.SEND_PLAN_NO ASC";
        return this.getData(sql, list.toArray());
    }

    /**
     * 根据主表查询出厂计划子表信息
     * @return obj
     */
    public CoreReturnObject querySendPlanC(String[] salgs) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmShipmentApp.QuerySENDPLANC");
        sql += CoreConverter.toSqlConditionString("T.SEND_PLAN_NO", salgs);
        return this.getData(sql);
    }

    /**
     * 根据到站、专用线、收货单位、计划年月查询是否已有月请车
     * @return obj
     */
    public CoreReturnObject doQueryMonthask(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmShipmentApp.doQueryMonthask");
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询月请车是否做日请车
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject doQueryDayFalg(ArrayList<String> list) {
        return this.getDataBySqlID("FrmShipmentApp.doQueryDayFalg", list
                .toArray());
    }

    /**
     * 查询批复状态
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject doQueryFalg(ArrayList<String> list) {
        return this
                .getDataBySqlID("FrmShipmentApp.doQueryFalg", list.toArray());
    }

    /**
     * 查询月请车批号
     * @param list 查询参数
     * @return obj
     */
    public CoreReturnObject doQueryAskMonth(String strTime) {
        return this.getDataBySqlID("FrmShipmentApp.doQueryAskMonth",
                new Object[] { strTime });
    }

    /**
     * 修改车皮运号，批复数
     * @param list 参数
     * @return obj
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doUpdate(ArrayList<Object> list) {

        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            for (int i = 0; i < list.size(); i++) {
                ArrayList<String> list1 = (ArrayList<String>) list.get(i);
                String sqlquery = "";
                String sqlupdate = "";
                String askMonthPk = list1.get(4);
                String velMonth = "";// 请车数
                // String bathMonth = "";// 批复数
                String flag = "";// 批复状态
                // String strAskMonth = "";

                // 查询请车信息进行判断
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmShipmentApp.queryAskMonthFlag");
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, askMonthPk);
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
                    velMonth = rs.getString(1);
                    // bathMonth = rs.getString(2);
                    flag = rs.getString(3);
                }
                rs.close();
                psta.close();

                if (Double.parseDouble(velMonth) < Double.parseDouble(list1
                        .get(1))) {
                    cro.setV_errMsg("批复数不能大于请车数,请重新输入！");
                    cro.setV_errCode(-1);
                    return cro;
                }
                if (flag.equals("1")) {
                    cro.setV_errMsg("该记录已审批,请重新选择！");
                    cro.setV_errCode(-1);
                    return cro;
                }
                sqlupdate = (CoreXmlSqlParsersFactory
                        .getSql("FrmShipmentApp.doUpdate"));
                psta = connection.prepareStatement(sqlupdate);
                psta.setObject(1, list1.get(0));
                psta.setObject(2, list1.get(1));
                psta.setObject(3, list1.get(2));
                psta.setObject(4, list1.get(3));
                psta.setObject(5, askMonthPk);
                psta.executeUpdate();
                psta.close();

            }
            cro.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            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 doApp(ArrayList<ArrayList<String>> list) {
        // sql语句
        ArrayList<String> sqllist = new ArrayList<String>();
        // 字段集合
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist
                    .add(CoreXmlSqlParsersFactory
                            .getSql("FrmShipmentApp.doApp"));
            parmlist.add(list.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist);
    }

    /**
     *新增请车
     * @param list 参数
     * @return obj
     */
    public CoreReturnObject doAdd(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sqlInster = "";
        String sqlquery = "";
        String sqlupdate = "";
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
//            String strAskMonth = "";

            for (int i = 0; i < list.size(); i++) {
//                double vehicle = 0;
//                double batch = 0;
                String appFlag = "";
                String askMonth = "";
//                String wagons = "";
                // 将JSON字符串转化成对象
                String baseStr = list.get(i).toString();
                JSONObject json = JSONObject.fromObject(baseStr);
                YdmZcAskMonthEntity com = (YdmZcAskMonthEntity) JSONObject
                        .toBean(json, YdmZcAskMonthEntity.class);
//                strAskMonth = com.getWagonsPlanNo();

                String spclNo = "N";
                if (!com.getSpclLnNo().equals("")) {
                    spclNo = com.getSpclLnNo();
                }
                sqlquery = CoreXmlSqlParsersFactory
                        .getSql("FrmShipmentApp.queryAskMonth"); // 查询到月度请车批车表
                psta = connection.prepareStatement(sqlquery);
                psta.setString(1, com.getSendPlanNo());// 计划年月
                psta.setString(2, com.getStationNo());
                psta.setString(3, spclNo);
                psta.setString(4, com.getReceivNo());
                psta.setString(5, com.getAskMonthNo());
                ResultSet rs = psta.executeQuery();
                if (rs.next()) {
//                    vehicle = Double.parseDouble(rs.getString(1));
//                    batch = Double.parseDouble(rs.getString(2));
                    appFlag = rs.getString(3);
                    askMonth = rs.getString(4);
//                    wagons = rs.getString(5);
                }
                rs.close();
                psta.close();

                if (appFlag.equals("1")) {
                    cro.setV_errMsg("您选择的记录已请车审批,请重新选择！");
                    cro.setV_errCode(-1);
                    return cro;
                }
                if (askMonth.equals(com.getAskMonthNo())) {    
                    sqlupdate = "UPDATE YDM_ZC_ASK_MONTH SET VEHICLE_MONTH_NUM=TO_NUMBER(VEHICLE_MONTH_NUM)+TO_NUMBER('"
                            + com.getVehicleMonthNum()
                            + "') WHERE ASK_MONTH_NO='"
                            + askMonth
                            + "' AND PLAN_YEAR_MONTH='"
                            + com.getSendPlanNo()
                            + "' AND STATION_NO='"
                            + com.getStationNo()
                            + "' AND DECODE(SPCL_LN_NO,'','N',SPCL_LN_NO)='"
                            + spclNo
                            + "' AND RECEIV_NO='"
                            + com.getReceivNo()
                            + "'";
                    Statement sta = connection.createStatement();
                    sta.executeUpdate(sqlupdate);
                    sta.close();
                    cro.setV_errMsg("修改请车成功！");
                }
                else {
                    sqlInster = CoreXmlSqlParsersFactory
                            .getSql("FrmShipmentApp.AddAskMonth"); // 新增到月度请车批车表
                    psta = connection.prepareStatement(sqlInster);
                    psta.setString(1, com.getAskMonthNo());
                    psta.setString(2, com.getVehicleMonthNum());
                    psta.setString(3, com.getWagonsFlag());
                    psta.setString(4, com.getStationNo());
                    psta.setString(5, com.getStationNm());
                    psta.setString(6, com.getSpclLnNo());
                    psta.setString(7, com.getSpclLnNm());
                    psta.setString(8, com.getReceivNo());
                    psta.setString(9, com.getReceivNm());
                    psta.setString(10, com.getCreateName());
                    psta.setString(11, com.getCreateTime());
                    psta.setString(12, com.getDepartmentCode());
                    psta.setString(13, com.getDepartmentDesc());
                    psta.setString(14, com.getManagementNo());
                    psta.setString(15, com.getManagementName());
                    psta.setString(16, com.getCreateName());
                    psta.setString(17, com.getCreateTime());
                    psta.setString(18, com.getSendPlanNo());// 计划年月
                    psta.executeUpdate();
                    psta.close();
                    cro.setV_errMsg("新增请车成功！");
                }
            }
            connection.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            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 doDeleteApp(ArrayList<ArrayList<String>> list) {
        // sql语句
        ArrayList<String> sqllist = new ArrayList<String>();
        // 字段集合
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add(CoreXmlSqlParsersFactory
                    .getSql("FrmShipmentApp.doDeleteApp"));
            parmlist.add(list.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist);
    }

    /**
     * 取消请车
     * @param list
     * @return
     */
    public CoreReturnObject doDeleteAdd(ArrayList<ArrayList<String>> list) {
        // sql语句
        ArrayList<String> sqllist = new ArrayList<String>();
        // 字段集合
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            sqllist.add(CoreXmlSqlParsersFactory
                    .getSql("FrmShipmentApp.doDeleteAdd"));
            parmlist.add(list.get(i).toArray());
        }
        return this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
                sqllist, parmlist);
    }
}
