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.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.DesignBasePlineEntity;
import com.steering.pss.sale.order.model.DesignLengthEntity;
import com.steering.pss.sale.order.model.DesignMscGzGgPlEntity;
import com.steering.pss.sale.order.model.DesignStdSicEntity;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.LengthCalculateDaoImp;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignBomEntity;
import com.steering.pss.sale.order.qualityDesign.model.SlmOrdDesignMscPlGxPrlEntity;

/**
 * 长度基础数据
 * @author Administrator
 */
public class ILengthCalculateDao implements LengthCalculateDaoImp {

    /**
     * 查询公差数据
     * @param con
     * @param designKey
     * @return DesignStdSicEntity
     * @throws SQLException
     */
    public DesignStdSicEntity getSpec(Connection con, String designKey,
            String biseCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getSpec");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, biseCode);
        ResultSet rs = ps.executeQuery();
        List<DesignStdSicEntity> list = EntityTool.getEntitysByRs(rs,
                DesignStdSicEntity.class);
        if (list != null && list.size() > 0) {
            QualityDesignTool.nullConverNullString(list.get(0));
            return list.get(0);
        }
        else {
            return null;
        }
    }

    /**
     * 查询基础产线
     * @param con 连接字符串
     * @param plineCode 产线代码
     * @return DesignBasePlineEntity
     * @throws SQLException
     */
    public DesignBasePlineEntity getBasePline(Connection con, String plineCode)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getPline");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ResultSet rs = ps.executeQuery();
        DesignBasePlineEntity entity = EntityTool.getEntityByRs(rs,
                DesignBasePlineEntity.class);
        rs.close();
        ps.close();
        return entity;
    }

    /**
     * 保存长度
     * @param con 连接字符串
     * @param lengthInfo 长度类
     * @throws SQLException
     */
    public void saveLength(Connection con, DesignLengthEntity lengthInfo)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.saveLength");
        sql = EntityTool.fillSqlByEntity(sql, lengthInfo);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 查询长度
     * @param con 连接字符串
     * @param ordLnPk 合同行号
     * @return List<DesignLengthEntity>
     * @throws SQLException
     */
    public List<DesignLengthEntity> getLength(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getLength");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();
        List<DesignLengthEntity> list = EntityTool.getEntitysByRs(rs,
                DesignLengthEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    /**
     * 查询轧制产线
     * @param con 连接字符串
     * @param designKey 质量主键
     * @param mscPline 全程产线号
     * @return List<DesignMscGzGgPlEntity>
     * @throws SQLException
     */
    public List<DesignMscGzGgPlEntity> getRollSpecPline(Connection con,
            String designKey, String mscPline) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getRollSpecPline");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, mscPline);
        ResultSet rs = ps.executeQuery();
        List<DesignMscGzGgPlEntity> list = EntityTool.getEntitysByRs(rs,
                DesignMscGzGgPlEntity.class);
        rs.close();
        ps.close();
        if (list == null || list.size() == 0) {

            return null;
        }
        return list;
    }

    /**
     * 删除长度
     * @param con 连接字符串
     * @param ordLnPk 合同行号
     * @throws SQLException
     */
    public void deleteLength(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.deleteLength");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ps.executeUpdate();
    }

    /**
     * 修改轧制产线
     * @param con 连接字符串
     * @param rollSpecPline 轧制产线
     * @throws SQLException
     */
    public void updateRollSpecPline(Connection con,
            DesignMscGzGgPlEntity rollSpecPline) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.updateRollSpecLine");
        sql = EntityTool.fillSqlByEntity(sql, rollSpecPline);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 通过规格找外径、壁厚
     */
    public String[] getSpecDH(Connection con, String specCode)
            throws SQLException {
        String[] strarry = new String[2];
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getSpecDH");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, specCode);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            strarry[0] = rs.getString("DIMATER");
            strarry[1] = rs.getString("HEIGHT");
        }
        ps.close();
        rs.close();
        return strarry;
    }

    /**
     * 查询接箍测量Ｊ值
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public String getJgKeyValue(Connection con, String designKey)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getJgKeyValue");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ResultSet rs = ps.executeQuery();
        String checkValue = "";
        if (rs.next()) {
            checkValue = rs.getString("CHECK_J_VAL");
        }
        ps.close();
        rs.close();
        return checkValue;
    }

    /**
     * 查询镦拔扩产线
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public List<SlmOrdDesignMscPlGxPrlEntity> getPlineE(Connection con,
            String designKey, String mscPline, String processCode)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getPlineE");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, processCode);
        ResultSet rs = ps.executeQuery();
        List<SlmOrdDesignMscPlGxPrlEntity> list = EntityTool.getEntitysByRs(rs,
                SlmOrdDesignMscPlGxPrlEntity.class);
        ps.close();
        rs.close();
        return list;
    }

    /**
     * 查询镦粗切头尾
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public String[] getCutHeadtailE(Connection con, String inMaterialNo,
            String outMaterialNo, String plineCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getCutHeadtailE");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, inMaterialNo);
        ps.setObject(2, outMaterialNo);
        ps.setObject(3, plineCode);
        ResultSet rs = ps.executeQuery();
        String[] strArry = new String[] { "", "", "" };
        if (rs.next()) {
            if (rs.getString("CROP_HEAD") != null) {
                strArry[0] = rs.getString("CROP_HEAD");
            }
            if (rs.getString("CROP_END") != null) {
                strArry[1] = rs.getString("CROP_END");
            }
            if (rs.getString("UPSETTING_LENGTH") != null) {
                strArry[2] = rs.getString("UPSETTING_LENGTH");
            }
        }
        ps.close();
        rs.close();
        return strArry;
    }

    /**
     * 查询轧管切头尾
     * @param con
     * @param inMaterialNo
     * @param outMaterialNo
     * @param plineCode
     * @return
     * @throws SQLException
     */
    public String[] getCutHeadtailD(Connection con, String inMaterialNo,
            String outMaterialNo, String plineCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getCutHeadtailD");
        String[] cut = new String[] { "", "", "", "" };
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, inMaterialNo);
        ps.setObject(2, outMaterialNo);
        ps.setObject(3, plineCode);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("crop_head") != null) {
                cut[0] = rs.getString("crop_head");
            }
            if (rs.getString("crop_end") != null) {
                cut[1] = rs.getString("crop_end");
            }
            if (rs.getString("TUBE_END_RATIO") != null) {
                cut[2] = rs.getString("TUBE_END_RATIO");
            }
            if (rs.getString("DBL_LEN_MAX") != null) {
                cut[3] = rs.getString("DBL_LEN_MAX");
            }
        }
        ps.close();
        rs.close();
        return cut;
    }

    /**
     * 查询钢种
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public List<String[]> getGradeBySpecCode(Connection con, String designKey,
            String mscPline, String specCode) throws SQLException {
        List<String[]> list = new ArrayList<String[]>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getGradeBySpecCode");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, specCode);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("GRADECODE") != null
                    && !rs.getString("GRADECODE").equals("")) {
                String[] strArr = new String[2];
                strArr[0] = rs.getString("GRADECODE");
                strArr[1] = rs.getString("GRADENAME");
                list.add(strArr);
            }
        }
        ps.close();
        rs.close();
        return list;
    }

    /**
     * 查询成品物料
     * @param con
     * @param designKey 质量主键
     * @param mscPline 制程
     * @param gradeCode 钢种
     * @param specCodeD 轧管规格
     * @param specCode 断面规格
     * @param flag 标识
     * @return
     * @throws SQLException
     */
    public SlmOrdDesignBomEntity getSlmOrdDesignBomByB(Connection con,
            String designKey, String mscPline, String gradeCode,
            String specCodeD, String specCode, String flag) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getSlmOrdDesignBom");
        if (flag.equals("B")) {
            sql += " and t.SPEC_CODE_Z=?";
        }
        else {
            sql += " and t.SPEC_CODE_D=?";
        }
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, gradeCode);
        ps.setObject(4, specCodeD);
        ps.setObject(5, specCode);
        ResultSet rs = ps.executeQuery();
        SlmOrdDesignBomEntity entity = EntityTool.getEntityByRs(rs,
                SlmOrdDesignBomEntity.class);
        rs.close();
        ps.close();
        return entity;
    }

    /**
     * 查询钢种
     */
    public List<String[]> getGradeByMscPline(Connection con, String desginKey,
            String mscPline) throws SQLException {
        List<String[]> list = new ArrayList<String[]>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("LengthCalculateDao.getGradeByMscPline");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, desginKey);
        ps.setObject(2, mscPline);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            if (rs.getString("GRADECODE") != null
                    && !rs.getString("GRADECODE").equals("")) {
                String[] strArr = new String[2];
                strArr[0] = rs.getString("GRADECODE");
                strArr[1] = rs.getString("GRADENAME");
                list.add(strArr);
            }
        }
        ps.close();
        rs.close();
        return list;
    }

}
