package com.steering.pss.sale.order.qualityDesign.Dao.idao;

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.SA08.CoreXmlSqlParsersFactory;

import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlPcEntity;
import com.steering.pss.sale.order.model.DesignMscPlPcLstEntity;
import com.steering.pss.sale.order.model.OrderLineDeliveryEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.ProCycleDaoImp;

/**
 * 生产周期
 * @author Administrator
 */
public class IProCycleDao implements ProCycleDaoImp {

    /**
     * 查询质量设计验证通过的制程
     * @param con 连接字符串
     * @param designKey 质量主键
     * @return List<DesignMscPlEntity>
     * @throws SQLException
     */
    public List<DesignMscPlEntity> getValidZC(Connection con, String designKey)
            throws SQLException {
        List<DesignMscPlEntity> list = new ArrayList<DesignMscPlEntity>();
        String sql = CoreXmlSqlParsersFactory.getSql("IProCycleDao.getValidZC");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            DesignMscPlEntity mscPl = new DesignMscPlEntity();
            mscPl.setDesginKey(rs.getString("DESGIN_KEY"));
            mscPl.setMscPline(rs.getString("MSC_PLINE"));
            mscPl.setSetFl(rs.getString("SET_FL"));
            mscPl.setWholeBacklog(rs.getString("WHOLE_BACKLOG"));
            mscPl.setWholeBacklogDesc(rs.getString("WHOLE_BACKLOG_DESC"));
            list.add(mscPl);
        }
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 查询验证通过的接箍码
     * @param con 连接字符串
     * @param designKey 质量主键
     * @return List<String>
     * @throws SQLException
     */
    public List<String> getValidJG(Connection con, String designKey)
            throws SQLException {
        List<String> list = new ArrayList<String>();
        String sql = CoreXmlSqlParsersFactory.getSql("IProCycleDao.getValidJG");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String jgCode = rs.getString("CODE_JG");
            list.add(jgCode);
        }
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 查询产品码
     * @param con 连接字符串
     * @param proDucCode 品名代码
     * @param stdCode 标准代码
     * @param steelCode 钢级代码
     * @param stdStyle 标准类别
     * @return String
     * @throws SQLException
     */
    public String getPscByCode(Connection con, String proDucCode,
            String stdCode, String steelCode, String stdStyle)
            throws SQLException {
        String psc = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.getPscByCode");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, proDucCode);
        ps.setObject(2, stdCode);
        ps.setObject(3, steelCode);
        ps.setObject(4, stdStyle);
        ps.setObject(5, stdStyle);
        ps.setObject(6, stdStyle);
        ps.setObject(7, stdStyle);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            psc = rs.getString("PSC");
        }
        rs.close();
        ps.close();
        return psc;
    }

    /**
     * 查询冶金规范码
     * @param con 连接字符串
     * @param psc 产品码
     * @param useCode 用途码
     * @return String
     * @throws SQLException
     */
    public String getMscByPsc(Connection con, String psc, String useCode)
            throws SQLException {
        String msc = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.getMscByPsc");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, psc);
        ps.setObject(2, useCode);
        ps.setObject(3, useCode);
        ps.setObject(4, useCode);
        ps.setObject(5, useCode);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            msc = rs.getString("MSC");
        }
        rs.close();
        ps.close();
        return msc;
    }

    /**
     * 查询交货行信息
     * @param con
     * @param ordLnPk
     * @param form 等于2 来自合同变更请求
     * @return
     * @throws SQLException
     */
    public List<OrderLineDeliveryEntity> getLineDelivery(Connection con,
            String ordLnPk, String form) throws SQLException {
        List<OrderLineDeliveryEntity> list = new ArrayList<OrderLineDeliveryEntity>();
        String sql = "";
        if (form.equals("2")) {
            sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderChangeManage.getLineDelivery");
        }
        else {
            sql = CoreXmlSqlParsersFactory
                    .getSql("IProCycleDao.getLineDelivery");
        }
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            OrderLineDeliveryEntity lineDelivery = new OrderLineDeliveryEntity();
            lineDelivery.setOrdPk(rs.getString("ORD_PK"));
            lineDelivery.setOrdLnPk(rs.getString("ORD_LN_PK"));
            lineDelivery.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
            lineDelivery.setDeliveryNo(rs.getString("DELIVERY_NO"));
            lineDelivery.setDelvryBdate(rs.getString("DELVRY_BDATE"));
            lineDelivery.setDelvryEdate(rs.getString("DELVRY_EDATE"));
            lineDelivery.setDelvryQty(rs.getString("DELVRY_QTY"));
            lineDelivery.setOrderUnit(rs.getString("ORDER_UNIT"));
            lineDelivery.setWgtEstmt(rs.getString("WGT_ESTMT"));
            lineDelivery.setEnsuTotlLen(rs.getString("ENSU_TOTL_LEN"));
            lineDelivery.setReceivNo(rs.getString("RECEIV_NO"));
            lineDelivery.setReceivNm(rs.getString("RECEIV_NM"));
            lineDelivery.setDelvryAddr(rs.getString("DELVRY_ADDR"));
            lineDelivery.setAgencyFl(rs.getString("AGENCY_FL"));
            lineDelivery.setReceivAddr(rs.getString("RECEIV_ADDR"));
            lineDelivery.setTransitPlan(rs.getString("TRANSIT_PLAN"));
            lineDelivery.setTransitMny(rs.getString("TRANSIT_MNY"));
            lineDelivery.setDeliveryStatus(rs.getString("DELIVERY_STATUS"));
            QualityDesignTool.nullConverNullString(lineDelivery);
            list.add(lineDelivery);
        }
        return list;
    }

    /**
     * 保存生存周期主表
     * @param con
     * @param mscPlPcEntity
     * @throws SQLException
     */
    public void saveMSCPLPC(Connection con, DesignMscPlPcEntity mscPlPcEntity)
            throws SQLException {
        if (mscPlPcEntity == null) {
            return;
        }
        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.saveMSCPLPC");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, mscPlPcEntity.getDesginKey());
        ps.setObject(2, mscPlPcEntity.getOrdLnDlyPk());
        ps.setObject(3, mscPlPcEntity.getMscPline());
        ps.setObject(4, mscPlPcEntity.getDelvryEdate());
        ps.setObject(5, mscPlPcEntity.getDelvryQty());
        ps.setObject(6, mscPlPcEntity.getOrderUnit());
        ps.setObject(7, mscPlPcEntity.getReceivNo());
        ps.setObject(8, mscPlPcEntity.getReceivNm());
        ps.setObject(9, mscPlPcEntity.getRcmdDelvryEdate());
        ps.setObject(10, mscPlPcEntity.getPrdCyc());
        ps.setObject(11, mscPlPcEntity.getNMaterialFl());
        ps.setObject(12, mscPlPcEntity.getCycEdit());
        ps.setObject(13, mscPlPcEntity.getCreateName());
        ps.setObject(14, mscPlPcEntity.getPrdCycBtch());
        ps.executeUpdate();
        ps.close();
    }

    public void updateMSCPLPC(Connection con, DesignMscPlPcEntity mscPlPcEntity)
            throws SQLException {
        if (mscPlPcEntity == null) {
            return;
        }
        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.saveMSCPLPC");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, mscPlPcEntity.getDesginKey());
        ps.setObject(2, mscPlPcEntity.getOrdLnDlyPk());
        ps.setObject(3, mscPlPcEntity.getMscPline());
        ps.setObject(4, mscPlPcEntity.getDelvryEdate());
        ps.setObject(5, mscPlPcEntity.getDelvryQty());
        ps.setObject(6, mscPlPcEntity.getOrderUnit());
        ps.setObject(7, mscPlPcEntity.getReceivNo());
        ps.setObject(8, mscPlPcEntity.getReceivNm());
        ps.setObject(9, mscPlPcEntity.getRcmdDelvryEdate());
        ps.setObject(10, mscPlPcEntity.getPrdCyc());
        ps.setObject(11, mscPlPcEntity.getNMaterialFl());
        ps.setObject(12, mscPlPcEntity.getCycEdit());
        ps.setObject(13, mscPlPcEntity.getCreateName());
        ps.setObject(14, mscPlPcEntity.getPrdCycBtch());
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 保存生产周期从表
     * @param con
     * @param mscPlPcLstEntity
     * @throws SQLException
     */
    public void saveMSCPLPCLST(Connection con,
            DesignMscPlPcLstEntity mscPlPcLstEntity) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.saveMSCPLPCLST");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, mscPlPcLstEntity.getDesginKey());
        ps.setObject(2, mscPlPcLstEntity.getOrdLnDlyPk());
        ps.setObject(3, mscPlPcLstEntity.getMscPline());
        ps.setObject(4, mscPlPcLstEntity.getBacklogSeq());
        ps.setObject(5, mscPlPcLstEntity.getProcessCode());
        ps.setObject(6, mscPlPcLstEntity.getProcessDesc());
        ps.setObject(7, mscPlPcLstEntity.getReviewAsk());
        ps.setObject(8, mscPlPcLstEntity.getLstPrdDate());
        ps.setObject(9, mscPlPcLstEntity.getPrdCyc());
        ps.setObject(10, mscPlPcLstEntity.getPrdCycBtch());
        ps.executeUpdate();
        ps.close();
    }

    public void deleteMscPLPC(Connection con, String ordDlyLnPk)
            throws SQLException {

        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.deleteMSCPLPC");
        PreparedStatement ps = con.prepareStatement(sql);

        ps.setObject(1, ordDlyLnPk);
        ps.executeUpdate();
        ps.close();
    }

    public void deleteMscPLPCLST(Connection con, String ordDlyLnPk)
            throws SQLException {

        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.deleteMSCPLPCLST");
        PreparedStatement ps = con.prepareStatement(sql);

        ps.setObject(1, ordDlyLnPk);
        ps.executeUpdate();
        ps.close();

    }

    /**
     * 查询根据交货行号交货行信息
     * @param con
     * @param oldLnDlyPk 交货行号
     * @return OrderLineDeliveryEntity
     * @throws SQLException
     */
    public OrderLineDeliveryEntity getLineDeliveryByPk(Connection con,
            String oldLnDlyPk) throws SQLException {
        OrderLineDeliveryEntity lineDelivery = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("IProCycleDao.getLineDeliveryByPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, oldLnDlyPk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            lineDelivery = new OrderLineDeliveryEntity();
            lineDelivery.setOrdPk(rs.getString("ORD_PK"));
            lineDelivery.setOrdLnPk(rs.getString("ORD_LN_PK"));
            lineDelivery.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
            lineDelivery.setDeliveryNo(rs.getString("DELIVERY_NO"));
            lineDelivery.setDelvryBdate(rs.getString("DELVRY_BDATE"));
            lineDelivery.setDelvryEdate(rs.getString("DELVRY_EDATE"));
            lineDelivery.setDelvryQty(rs.getString("DELVRY_QTY"));
            lineDelivery.setOrderUnit(rs.getString("ORDER_UNIT"));
            lineDelivery.setWgtEstmt(rs.getString("WGT_ESTMT"));
            lineDelivery.setEnsuTotlLen(rs.getString("ENSU_TOTL_LEN"));
            lineDelivery.setReceivNo(rs.getString("RECEIV_NO"));
            lineDelivery.setReceivNm(rs.getString("RECEIV_NM"));
            lineDelivery.setDelvryAddr(rs.getString("DELVRY_ADDR"));
            lineDelivery.setAgencyFl(rs.getString("AGENCY_FL"));
            lineDelivery.setReceivAddr(rs.getString("RECEIV_ADDR"));
            lineDelivery.setTransitPlan(rs.getString("TRANSIT_PLAN"));
            lineDelivery.setTransitMny(rs.getString("TRANSIT_MNY"));
            lineDelivery.setDeliveryStatus(rs.getString("DELIVERY_STATUS"));
            QualityDesignTool.nullConverNullString(lineDelivery);

        }
        return lineDelivery;
    }
}
