package com.steering.pss.sale.order;

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 java.util.TimerTask;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.DesignMscBlEntity;
import com.steering.pss.sale.order.model.DesignMscJgBlEntity;
import com.steering.pss.sale.order.model.DesignMscJgPlEntity;
import com.steering.pss.sale.order.model.DesignMscJgPlStiEntity;
import com.steering.pss.sale.order.model.DesignMscPlEntity;
import com.steering.pss.sale.order.model.DesignMscPlStiEntity;
import com.steering.pss.sale.order.model.SlmOrderLinePrdcrEntity;
import com.steering.pss.sale.order.model.SlmOrderLinePrdcrLstEntity;
import com.steering.pss.sale.order.orderchange.entity.ChgeSlmOrderLineEntity;

/**
 * 监制信息维护任务
 * @author Administrator
 */
public class CoreOrderLinePrdcrTask extends TimerTask {

    public void run() {

        DBCommon db = new DBCommon();
        Connection con = null;
        try {
            con = db.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            // 取得 技术评审通过 需要维护监制 监制编号字段为空的 合同行记录。
            // 再用合同行获取 Designkey 取得这个Key下的制程 写入监制表中。
            // 1.找寻这个Key的接箍码 再找接箍码的DesignKey 再找这个Key的制程
            // 2.接箍码关联的备料码 再找备料码的制程 3.找寻合同行下的备料码 找到备料码的DesignKey 再找这个Key之下的制程
            List<ChgeSlmOrderLineEntity> line = getOrderLineEntity(con);
            if (line != null && line.size() > 0) {
                for (int i = 0; i < line.size(); i++) {
                    ChgeSlmOrderLineEntity sol = line.get(i);
                    if (Integer.parseInt(sol.getOrderLnStatus()) < 12020202
                            || Integer.parseInt(sol.getOrderLnStatus()) > 12020401
                            || !sol.getProducerFl().equals("1")
                            || !sol.getPrdcrNo().equals("")) {
                        continue; // 通过技术评审 需要监制 监制编号为空
                    }
                    String designKey = getOrderLineDesignKey(con,
                            sol.getOrdLnPk());
                    if (designKey.equals("")) {
                        continue;
                    }
                    String prdcrNo = generatePrdcrNo(con, sol.getOrdLnPk()); // 监制编号
                    List<DesignMscPlEntity> dmps = getDesignMscPl(con,
                            designKey); // 制程

                    for (DesignMscPlEntity dmp : dmps) {
                        // 写入监制主表 SlmOrderLinePrdcrEntity
                        String prdcrNoIndex = generatePrdcrNoIndex(con, prdcrNo);
                        insertOrderLinePrdcr(con, designKey, "", "", prdcrNo,
                                prdcrNoIndex, sol, dmp);
                        // 写入监制从表 SLM_ORDER_LINE_PRDCR_LST 先获取工序点数据
                        // DesignMscPlStiEntity
                        List<DesignMscPlStiEntity> plStis = getDesignMscPlStiEntity(
                                con, designKey, dmp.getMscPline());
                        for (DesignMscPlStiEntity plSti : plStis) {
                            insertOrderLinePrdcrLst(con, prdcrNo, prdcrNoIndex,
                                    plSti);
                        }
                    }

                    // 接箍码制程 单独的表结构 所以不能用通用的Insert方法
                    String jgCode = getOrderLineJgCode(con, designKey);
                    if (!jgCode.equals("")) {
                        // 取其制程 维护监制信息，再找其备料码
                        List<DesignMscJgPlEntity> jgpls = getDesignMscJgPl(con,
                                designKey, jgCode); // 接箍码的制程
                        for (DesignMscJgPlEntity jgpl : jgpls) {
                            // 写入监制主表 SlmOrderLinePrdcrEntity
                            String prdcrNoIndex = generatePrdcrNoIndex(con,
                                    prdcrNo);
                            SlmOrderLinePrdcrEntity olp = new SlmOrderLinePrdcrEntity();
                            olp.setPrdcrNo(prdcrNo);
                            olp.setCodeBl("");
                            olp.setCodeJg(jgCode);
                            olp.setPsc(sol.getPsc());
                            olp.setPscDesc(sol.getPscDesc());
                            olp.setStdCode(sol.getStdCode());
                            olp.setStdName(sol.getStdName());
                            olp.setMps(designKey);
                            olp.setFinalUser(sol.getFinalUser());
                            olp.setCreateName("SYSTEM");
                            olp.setMscPline(jgpl.getMscPline());
                            olp.setMscPlineDesc(jgpl.getWholeBacklogDesc());
                            olp.setPrdcrIndex(prdcrNoIndex);
                            String sql = CoreXmlSqlParsersFactory
                                    .getSql("CoreOrderLinePrdcrTask.InsertOrderLinePrdcr");
                            sql = EntityTool.fillSqlByEntity(sql, olp);
                            PreparedStatement ps = con.prepareStatement(sql);
                            ps.executeUpdate();
                            ps.close();
                            // 写入监制从表 SLM_ORDER_LINE_PRDCR_LST 先获取工序点数据
                            // DesignMscJgPlStiEntity
                            List<DesignMscJgPlStiEntity> jgplStis = getDesignMscJgPlStiEntity(
                                    con, designKey, jgpl.getMscPline(), jgCode);
                            for (DesignMscJgPlStiEntity jgplSti : jgplStis) {
                                String sql1 = CoreXmlSqlParsersFactory
                                        .getSql("CoreOrderLinePrdcrTask.InsertOrderLinePrdcrLst");
                                SlmOrderLinePrdcrLstEntity olpl = new SlmOrderLinePrdcrLstEntity();
                                olpl.setPrdcrNo(prdcrNo);
                                olpl.setProcessCode(jgplSti.getProcessCode());
                                olpl.setProcessDesc(jgplSti.getProcessDesc());
                                olpl.setProcessCodeC(jgplSti.getProcessCodeC());
                                olpl.setProcessDescC(jgplSti.getProcessDescC());
                                olpl.setPrdcrIndex(prdcrNoIndex);
                                olpl.setStationCode(jgplSti.getStationCode());
                                olpl.setStationDesc(jgplSti.getStationDesc());
                                sql1 = EntityTool.fillSqlByEntity(sql1, olpl);
                                ps = con.prepareStatement(sql1);
                                ps.executeUpdate();
                                ps.close();
                            }
                        }// 接箍制程 end
                         // 接箍码关联的备料码
                        List<DesignMscJgBlEntity> jgbls = getDesignMscJgRBl(
                                con, designKey, jgCode);
                        for (DesignMscJgBlEntity jgbl : jgbls) { // 存在多个BL码
                            List<DesignMscPlEntity> jgblpls = getDesignMscPl(
                                    con, jgbl.getDesginKeyBl()); // 一个BL码存在多个制程
                            for (DesignMscPlEntity jgblpl : jgblpls) {
                                String prdcrNoIndex = generatePrdcrNoIndex(con,
                                        prdcrNo);
                                insertOrderLinePrdcr(con,
                                        jgbl.getDesginKeyBl(), jgCode,
                                        jgbl.getPscBl(), prdcrNo, prdcrNoIndex,
                                        sol, jgblpl);
                                List<DesignMscPlStiEntity> jgblplStis = getDesignMscPlStiEntity(
                                        con, jgbl.getDesginKeyBl(),
                                        jgblpl.getMscPline());
                                for (DesignMscPlStiEntity jgblplSti : jgblplStis) {
                                    insertOrderLinePrdcrLst(con, prdcrNo,
                                            prdcrNoIndex, jgblplSti);
                                }
                            }// 一个备料码的制程
                        }// end 接箍关联的备料
                    }// end JG不为空

                    // 备料码制程
                    List<DesignMscBlEntity> bls = getDesignMscBlEntity(con,
                            designKey);
                    for (DesignMscBlEntity bl : bls) {
                        List<DesignMscPlEntity> blpls = getDesignMscPl(con,
                                bl.getDesginKeyBl()); // 制程
                        for (DesignMscPlEntity blpl : blpls) {
                            // 写入监制主表 SlmOrderLinePrdcrEntity
                            String prdcrNoIndex = generatePrdcrNoIndex(con,
                                    prdcrNo);
                            insertOrderLinePrdcr(con, bl.getDesginKeyBl(), "",
                                    bl.getPscBl(), prdcrNo, prdcrNoIndex, sol,
                                    blpl);
                            // 写入监制从表 SLM_ORDER_LINE_PRDCR_LST 先获取工序点数据
                            // DesignMscPlStiEntity
                            List<DesignMscPlStiEntity> blplStis = getDesignMscPlStiEntity(
                                    con, bl.getDesginKeyBl(),
                                    blpl.getMscPline());
                            for (DesignMscPlStiEntity blplSti : blplStis) {
                                insertOrderLinePrdcrLst(con, prdcrNo,
                                        prdcrNoIndex, blplSti);
                            }
                        }
                    }
                    // 将监制编号写入到合同行表中。
                    String sql2 = CoreXmlSqlParsersFactory
                            .getSql("CoreOrderLinePrdcrTask.setOrderLinePrdcrNo");
                    PreparedStatement ps = con.prepareStatement(sql2);
                    ps.setObject(1, prdcrNo);
                    ps.setObject(2, sol.getOrdLnPk());
                    ps.executeUpdate();
                    ps.close();
                    con.commit(); // 这里提交是由于此程序后台一直运行，能成功一条是一条
                }// end for 循环 一个和同行处理结束
            } // end if line is not null
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                }
                e.printStackTrace();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取OrderLine实体类
     * @param con
     * @return
     * @throws SQLException
     */
    public List<ChgeSlmOrderLineEntity> getOrderLineEntity(Connection con)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectOrderLineEntity");
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<ChgeSlmOrderLineEntity> list = new ArrayList<ChgeSlmOrderLineEntity>();
        list = EntityTool.getEntitysByRs(rs, ChgeSlmOrderLineEntity.class);
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 获取合同行的DesignKey
     * @param con
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public String getOrderLineDesignKey(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectOrderLineDesignKey");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();
        String designKey = "";
        if (rs.next()) {
            designKey = rs.getString(1);
        }
        rs.close();
        ps.close();
        return designKey;
    }

    /**
     * 获取制程
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public List<DesignMscPlEntity> getDesignMscPl(Connection con,
            String designKey) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectDesignMscPl");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        List<DesignMscPlEntity> list = new ArrayList<DesignMscPlEntity>();
        ResultSet rs = ps.executeQuery();
        list = EntityTool.getEntitysByRs(rs, DesignMscPlEntity.class);
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 合同行生成监制编码 （还要回写合同行中）
     * @param con
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public String generatePrdcrNo(Connection con, String ordLnPk)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.generatePrdcrNo");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ps.setObject(2, ordLnPk);
        ResultSet rs = ps.executeQuery();
        String prdcrNo = "";
        if (rs.next()) {
            prdcrNo = rs.getString(1);
        }
        rs.close();
        ps.close();
        return prdcrNo;
    }

    /**
     * 获取监制编码的序号
     * @param con
     * @param prdcrNo
     * @return
     * @throws SQLException
     */
    public String generatePrdcrNoIndex(Connection con, String prdcrNo)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.generatePrdcrNoIndex");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, prdcrNo);
        ResultSet rs = ps.executeQuery();
        String prdcrNoIndex = "";
        if (rs.next()) {
            prdcrNoIndex = rs.getString(1);
        }
        rs.close();
        ps.close();
        return prdcrNoIndex;
    }

    /**
     * 获取制程下的工序点信息
     * @param con
     * @param designKey
     * @param mscPline
     * @return
     * @throws SQLException
     */
    public List<DesignMscPlStiEntity> getDesignMscPlStiEntity(Connection con,
            String designKey, String mscPline) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectDesignMscPlSti");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, mscPline);
        ResultSet rs = ps.executeQuery();
        List<DesignMscPlStiEntity> list = new ArrayList<DesignMscPlStiEntity>();
        list = EntityTool.getEntitysByRs(rs, DesignMscPlStiEntity.class);
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 获取接箍码的制程的工序点信息
     * @param con
     * @param designKey
     * @param mscPline
     * @return
     * @throws SQLException
     */
    public List<DesignMscJgPlStiEntity> getDesignMscJgPlStiEntity(
            Connection con, String designKey, String mscPline, String jgCode)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectDesignMscJgPlSti");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, jgCode);
        ps.setObject(3, mscPline);
        ResultSet rs = ps.executeQuery();
        List<DesignMscJgPlStiEntity> list = new ArrayList<DesignMscJgPlStiEntity>();
        list = EntityTool.getEntitysByRs(rs, DesignMscJgPlStiEntity.class);
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 写入监制主表信息 如果是合同行的主制程 那么jgCode和blCode的值为空
     * @param con
     * @param designKey
     * @param jgCode
     * @param blCode
     * @param prdcrNo
     * @param prdcrNoIndex
     * @param sol
     * @param dmp
     * @throws SQLException
     */
    public void insertOrderLinePrdcr(Connection con, String designKey,
            String jgCode, String blCode, String prdcrNo, String prdcrNoIndex,
            ChgeSlmOrderLineEntity sol, DesignMscPlEntity dmp)
            throws SQLException {

        SlmOrderLinePrdcrEntity olp = new SlmOrderLinePrdcrEntity();
        olp.setPrdcrNo(prdcrNo);
        olp.setCodeBl(blCode);
        olp.setCodeJg(jgCode);
        olp.setPsc(sol.getPsc());
        olp.setPscDesc(sol.getPscDesc());
        olp.setStdCode(sol.getStdCode());
        olp.setStdName(sol.getStdName());
        olp.setMps(designKey);
        olp.setFinalUser(sol.getFinalUser());
        olp.setCreateName("SYSTEM");
        olp.setMscPline(dmp.getMscPline());
        olp.setMscPlineDesc(dmp.getWholeBacklogDesc());
        olp.setPrdcrIndex(prdcrNoIndex);
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.InsertOrderLinePrdcr");
        sql = EntityTool.fillSqlByEntity(sql, olp);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 写入监制从表信息 用工序点信息赋值
     * @param con
     * @param prdcrNo
     * @param prdcrNoIndex
     * @param plSti
     * @throws SQLException
     */
    public void insertOrderLinePrdcrLst(Connection con, String prdcrNo,
            String prdcrNoIndex, DesignMscPlStiEntity plSti)
            throws SQLException {
        String sql1 = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.InsertOrderLinePrdcrLst");
        SlmOrderLinePrdcrLstEntity olpl = new SlmOrderLinePrdcrLstEntity();
        olpl.setPrdcrNo(prdcrNo);
        olpl.setProcessCode(plSti.getProcessCode());
        olpl.setProcessDesc(plSti.getProcessDesc());
        olpl.setProcessCodeC(plSti.getProcessCodeC());
        olpl.setProcessDescC(plSti.getProcessDescC());
        olpl.setPrdcrIndex(prdcrNoIndex);
        olpl.setStationCode(plSti.getStationCode());
        olpl.setStationDesc(plSti.getStationDesc());
        sql1 = EntityTool.fillSqlByEntity(sql1, olpl);
        PreparedStatement ps = con.prepareStatement(sql1);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取合同行的JGcode
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public String getOrderLineJgCode(Connection con, String designKey)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectOrderLineJgCode");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ResultSet rs = ps.executeQuery();
        String jgCode = "";
        if (rs.next()) {
            jgCode = rs.getString(1);
        }
        return jgCode;
    }

    /**
     * 获取接箍码的制程
     * @param con
     * @param designKey
     * @param jgCode
     * @return
     * @throws SQLException
     */
    public List<DesignMscJgPlEntity> getDesignMscJgPl(Connection con,
            String designKey, String jgCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectDesignMscJgPl");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, jgCode);
        List<DesignMscJgPlEntity> list = new ArrayList<DesignMscJgPlEntity>();
        ResultSet rs = ps.executeQuery();
        list = EntityTool.getEntitysByRs(rs, DesignMscJgPlEntity.class);
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 获取接箍码关联的备料码
     * @param con
     * @param designKey
     * @param jgCode
     * @return
     * @throws SQLException
     */
    public List<DesignMscJgBlEntity> getDesignMscJgRBl(Connection con,
            String designKey, String jgCode) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectOrderLineJgRBlCode");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, jgCode);
        ResultSet rs = ps.executeQuery();
        List<DesignMscJgBlEntity> list = new ArrayList<DesignMscJgBlEntity>();
        list = EntityTool.getEntitysByRs(rs, DesignMscJgBlEntity.class);
        return list;
    }

    /**
     * 获取质量设计备料码实体类
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public List<DesignMscBlEntity> getDesignMscBlEntity(Connection con,
            String designKey) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderLinePrdcrTask.selectDesignMscBlEntity");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ResultSet rs = ps.executeQuery();
        List<DesignMscBlEntity> list = new ArrayList<DesignMscBlEntity>();
        list = EntityTool.getEntitysByRs(rs, DesignMscBlEntity.class);
        rs.close();
        ps.close();
        return list;
    }

}
