package com.steering.pss.plnsaleord.steelMarkingPlan;

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

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforOrdEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.entity.PlnSteelforfurnaceEntity;

/**
 * 炉计划控件类
 * @author Administrator
 */
public class ControlFurNacePlan extends DBCommon {
    /**
     * 查询所有已组炉的炉次
     * @return CoreReturnObject
     */
    public CoreReturnObject getFurnaceByGroup() {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getFurnace");
        sql += " and  t.status='0' ";
        return this.getData(sql);
    }

    /**
     * 通过铸造订单查询是否组浇
     * @param castno 浇次号
     * @return
     */
    public CoreReturnObject getFurnacePlnOrderProPlanId(String ProPlanId,
            String gxPlanNo) {
        Object[] params = new Object[] { ProPlanId, gxPlanNo };
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder1");
        sql += " and t.PRO_PLAN_ID=? and t.GX_PLAN_NO=?";
        return this.getData(sql, params);
    }

    /**
     * 通过浇次查询炉次
     * @param castno 浇次号
     * @return
     */
    public CoreReturnObject getFurnaceByCastno(String castno) {
        Object[] params = new Object[] { castno };
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getFurnace");
        sql += " and t.cast_no=? order by t.furnaceno_seq";
        return this.getData(sql, params);
    }

    /**
     * 通过浇次查询炉次订单
     * @param furnaceNo 炉次号
     * @return CoreReturnObject
     */
    public CoreReturnObject getSteelOrder(String castno) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder");
        ArrayList<String> params = new ArrayList<String>();
        if (!castno.equals("")) {
            sql += " and t.CAST_NO=? ";
            params.add(castno);
        }
        sql += " order by t.FURNACENO,t.CCM_SEQ";
        return this.getData(sql, params.toArray());
    }

    /**
     * 通过浇次查询炉次
     * @param castno 浇次号
     * @return
     */
    public CoreReturnObject getFurnaceCastno(String[] plineCode,
            String[] status, String stratTime, String castNo,String stoveNo, String judgeNo,String endTime) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getFurnace");
        sql += CoreConverter.toSqlConditionString("t.status", status);
        if (plineCode.length > 0) {
            sql += CoreConverter.toSqlConditionString("T.EF_PLINE_CODE",
                    plineCode);
        }
        if (!stratTime.equals("")) {
            sql += " AND T.PRO_MONTH >= '" + stratTime + "' ";
            sql += " AND T.PRO_MONTH  <='" + endTime + "'";
        }
        if (!castNo.equals("")) {
            sql += " AND  INSTR(T.CAST_NO, '" + castNo + "') > 0  ";
        }
        if (!stoveNo.equals("")) {
            sql += " AND T.STOVE_NO='"+stoveNo+"' ";
        }
        if (!judgeNo.equals("")) {
            sql += " AND T.STOVE_NO=(SELECT O.STOVE_NO FROM PLN_STEELFOR_ORD O WHERE O.JUDGE_STOVE_NO='"+judgeNo+"' AND O.CCM_PLN_TYPE='00')";
        }
        sql += " order by t.FURNACENO_SEQ asc";
        return this.getData(sql);
    }

    /**
     * 通过浇次查询炉次订单
     * @param furnaceNo 炉次号
     * @return CoreReturnObject
     */
    public CoreReturnObject getSteelOrd(String[] plineCode, String[] status,
            String castNo,String stoveNo, String judgeNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder");
        sql += CoreConverter.toSqlConditionString("t.status", status);
        if (plineCode != null) {
            sql += CoreConverter.toSqlConditionString("T.EF_PLINE_CODE",
                    plineCode);
        }
        if (!castNo.equals("")) {
            sql += " AND  INSTR(T.CAST_NO, '" + castNo + "') > 0  ";
        }
        if (!stoveNo.equals("")) {
            sql += " AND T.STOVE_NO='"+stoveNo+"' ";
        }
        if (!judgeNo.equals("")) {
            sql += " AND T.JUDGE_STOVE_NO='"+judgeNo+"'";
        }
        sql += " order by T.CUT_NUM asc ";
        return this.getData(sql);
    }

    /**
     * 通过浇次查询炉次订单
     * @param furnaceNo 炉次号
     * @return CoreReturnObject
     */
    public CoreReturnObject getSteelOrd(String[] CastNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder");
        sql += CoreConverter.toSqlConditionString("T.CAST_NO", CastNo);

        sql += " order by T.CUT_NUM asc ";
        return this.getData(sql);
    }

    /**
     * 通过炉次查询炉次订单分配
     * @param furnaceNo 炉次号
     * @return CoreReturnObject
     */
    public CoreReturnObject getFurnSteelOrder(String furnaceNo) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder");
        // if (!furnaceNo.equals("")) {
        sql += " AND T.FURNACENO='" + furnaceNo + "' ";
        // }
        sql += " order by t.FURNACENO,t.CUT_NUM";
        return this.getData(sql);
    }

    /**
     * 通过炉号查询炉次
     * @param con
     * @param furnaceno
     * @return
     * @throws SQLException
     */
    public static PlnSteelforfurnaceEntity getFurnaceByFurnaceno(
            Connection con, String furnaceno) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getFurnace");
        sql += "AND T.FURNACENO=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnaceno);
        ResultSet rs = ps.executeQuery();
        List<PlnSteelforfurnaceEntity> list = EntityTool.getEntitysByRs(rs,
                PlnSteelforfurnaceEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        else {
            return null;
        }
    }

    /**
     * 通过炉号查询炉次订单
     * @param con
     * @param furnaceno
     * @return
     * @throws SQLException
     */
    public static List<PlnSteelforOrdEntity> getSteelOrdByFurnaceno(
            Connection con, String furnaceno) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder1");
        sql += "AND T.FURNACENO=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnaceno);
        ResultSet rs = ps.executeQuery();
        List<PlnSteelforOrdEntity> list = EntityTool.getEntitysByRs(rs,
                PlnSteelforOrdEntity.class);
        rs.close();
        ps.close();
        if (list != null && list.size() > 0) {
            return list;
        }
        else {
            return null;
        }
    }

    /**
     * 通过炉号与炉内顺序号查询炉次订单
     * @param con
     * @param furnaceno 炉号
     * @param seq 炉内顺序号
     * @return
     * @throws SQLException
     */
    public static PlnSteelforOrdEntity getSteelOrdById(Connection con,
            String furnaceno, String seq) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ControlFurNacePlan.getSteelOrder");
        sql += "AND T.FURNACENO=? AND T.SEQUENCENO=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, furnaceno);
        ps.setObject(2, seq);
        ResultSet rs = ps.executeQuery();
        PlnSteelforOrdEntity entity = EntityTool.getEntityByRs(rs,
                PlnSteelforOrdEntity.class);
        rs.close();
        ps.close();
        return entity;
    }
}
