package com.steering.pss.plnsaleord.GpOptimize;

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

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.plnsaleord.base.PlnHelper;
import com.steering.pss.plnsaleord.orderManage.entity.SlmOrdDesignBomEntity;
import com.steering.pss.plnsaleord.processOrder.entity.PlnOrderDbkSEntity;
import com.steering.pss.plnsaleord.processOrder.entity.PlnOrderJgxSEntity;
import com.steering.pss.plnsaleord.processOrder.entity.PlnOrderRclSEntity;
import com.steering.pss.plnsaleord.processOrder.entity.PlnOrderZgSEntity;

/**
 * 查询钢种及钢种索引码
 * @author TJG
 * @version v1.1
 * @date 2017.07.11
 */
public class FrmChangeGrade extends DBCommon {

    /**
     * 查询交货行BOM
     * @param ordLnDlyPk
     * @return
     */
    public CoreReturnObject getBomByOrdLnDlyPk(String proPlanId,
            String gxPlanNo, String gradeCodeAll, String dimert,
            String specNameR,String mscPline) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmChangeGrade.getBomByOrdLnDlyPk");
        if (!gradeCodeAll.equals("")) {
            String[] stringArr = gradeCodeAll.split(",");
            sql += CoreConverter.toSqlConditionString("B.GRADENAME", stringArr);
        }
//        sql += " AND S.DIMATER=TO_NUMBER('" + dimert + "')";
//        sql += " AND B.SPEC_NAME_R='" + specNameR + "'";
        return this.getData(sql, new Object[] { proPlanId, gxPlanNo, mscPline});
    }

    /**
     * 查询钢种及钢种索引码
     * @param weight
     * @return
     */
    public CoreReturnObject getQueryGrade(String[] groupCic, String outsBilletfl) {
        CoreReturnObject cro = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmChangeGrade.getQueryGrade");
        // 去除数组中的空格
        List<String> tmp = new ArrayList<String>();
        for (String str : groupCic) {
            if (str != null && str.length() != 0) {
                tmp.add(str.trim());
            }
        }
        groupCic = tmp.toArray(new String[0]);
        if (groupCic == null || groupCic.length == 0) {
            sql += " AND T.CIC =''";
        }
        sql += CoreConverter.toSqlConditionString("T.CIC", groupCic);
        // sql += " AND T1.PURCHASE_STD='" + outsBilletfl + "'";
        cro = this.getData(sql);
        return cro;
    }
    /**
     * 修改钢种及钢种索引码
     * @param bom
     * @param plineCode
     * @param plineName
     * @param specName
     * @param passCode
     * @param dimaterManderl
     * @param proPlanId
     * @param gxPlanNo
     * @return
     */
    public CoreReturnObject changeZgPlan(String bom, String proPlanId,
            String gxPlanNo, String cic, String casTyte) {
        CoreReturnObject core = new CoreReturnObject();
        core.setResult(false);
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            SlmOrdDesignBomEntity bomEntity = PlnHelper.getEntityData(con,
                    new String[] { bom }, "FrmChangeGrade.getBomByBom",
                    SlmOrdDesignBomEntity.class);
            if (bomEntity == null) {
                core.setV_errMsg("数据异常,请刷新数据!");
                return core;
            }
            String wholeBacklog = bomEntity.getWholeBacklog();
            if (wholeBacklog.contains("E")) {
                List<PlnOrderDbkSEntity> dbkList = PlnHelper.getEntityDatas(
                        con, new String[] { proPlanId },
                        "FrmBomConfirm.getPlineEByProPlanId",
                        PlnOrderDbkSEntity.class);
                if (dbkList != null && dbkList.size() > 0) {
                    for (PlnOrderDbkSEntity entity : dbkList) {
                        PlnHelper.deleteOrUpdate(con,
                                "FrmBomConfirm.updateDbkMaterial",
                                new String[] { bomEntity.getEInMaterialNo(),
                                        bomEntity.getEInMaterialDesc(),
                                        bomEntity.getEOutMaterialNo(),
                                        bomEntity.getEOutMaterialDesc(),
                                        bomEntity.getMsc(),
                                        bomEntity.getMscPline(),
                                        bomEntity.getBom(),
                                        bomEntity.getWholeBacklogDesc(),
                                        entity.getProPlanId(),
                                        entity.getGxPlanNo() });
                        // 修改BOM
                        PlnHelper.deleteOrUpdate(con,
                                "FrmBomConfirm.updateDbkBom", new String[] {
                                        bom, entity.getProPlanId() });
                    }
                }
                else {
                    core.setV_errMsg("数据异常,请刷新数据!");
                    con.rollback();
                    return core;
                }
            }

            if (wholeBacklog.contains("F")) {
                List<PlnOrderRclSEntity> rclList = PlnHelper.getEntityDatas(
                        con, new String[] { proPlanId },
                        "FrmBomConfirm.getPlineFByProPlanId",
                        PlnOrderRclSEntity.class);
                if (rclList != null && rclList.size() > 0) {
                    for (PlnOrderRclSEntity entity : rclList) {
                        PlnHelper.deleteOrUpdate(con,
                                "FrmBomConfirm.updateRclMaterial",
                                new String[] { bomEntity.getFInMaterialNo(),
                                        bomEntity.getFInMaterialDesc(),
                                        bomEntity.getFOutMaterialNo(),
                                        bomEntity.getFOutMaterialDesc(),
                                        bomEntity.getMsc(),
                                        bomEntity.getMscPline(),
                                        bomEntity.getBom(),
                                        bomEntity.getWholeBacklogDesc(),
                                        entity.getProPlanId(),
                                        entity.getGxPlanNo() });
                        // 修改BOM
                        PlnHelper.deleteOrUpdate(con,
                                "FrmBomConfirm.updateRclBom", new String[] {
                                        bom, entity.getProPlanId() });
                    }
                }
                else {
                    core.setV_errMsg("数据异常,请刷新数据!");
                    con.rollback();
                    return core;
                }
            }

            if (wholeBacklog.contains("G")) {
                List<PlnOrderJgxSEntity> jgxList = PlnHelper.getEntityDatas(
                        con, new String[] { proPlanId },
                        "FrmBomConfirm.getPlineGByProPlanId",
                        PlnOrderJgxSEntity.class);
                if (jgxList != null && jgxList.size() > 0) {
                    for (PlnOrderJgxSEntity entity : jgxList) {
                        PlnHelper.deleteOrUpdate(con,
                                "FrmBomConfirm.updateJgxMaterial",
                                new String[] { bomEntity.getIInMaterialNo(),
                                        bomEntity.getIInMaterialDesc(),
                                        bomEntity.getIOutMaterialNo(),
                                        bomEntity.getIOutMaterialDesc(),
                                        bomEntity.getMsc(),
                                        bomEntity.getMscPline(),
                                        bomEntity.getBom(),
                                        bomEntity.getWholeBacklogDesc(),
                                        entity.getProPlanId(),
                                        entity.getGxPlanNo() });
                        // 修改BOM
                        PlnHelper.deleteOrUpdate(con,
                                "FrmBomConfirm.updateJgxBom", new String[] {
                                        bom, entity.getProPlanId() });
                    }
                }
                else {
                    core.setV_errMsg("数据异常,请刷新数据!");
                    con.rollback();
                    return core;
                }
            }
            // 修改轧管信息
            List<PlnOrderZgSEntity> zgList = PlnHelper.getEntityDatas(con,
                    new String[] { proPlanId, gxPlanNo },
                    "FrmChangeGrade.getZgPlanInfo",
                    " AND T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO=?",
                    PlnOrderZgSEntity.class);
            if (zgList == null) {
                core.setV_errMsg("数据异常,请刷新数据!");
                con.rollback();
                return core;
            }
            for (PlnOrderZgSEntity zgEntity : zgList) {
                if (!zgEntity.getGpreqId().equals("")) {
                    core.setV_errMsg("选择的轧管订单：" + zgEntity.getProPlanId() + "/"
                            + zgEntity.getGxPlanNo() + ",已生产管坯需求,请撤销管坯需求！");
                    con.rollback();
                    return core;
                }
                // 修改工序合同BOM
                PlnHelper.deleteOrUpdate(con, "FrmBomConfirm.updateZgBom",
                        new String[] { bom, zgEntity.getProPlanId() });
                // 修改排产耗时表BOM
                PlnHelper
                        .deleteOrUpdate(con, "FrmBomConfirm.updateZgSBom",
                                new String[] { bom, bomEntity.getMsc(),
                                        bomEntity.getMscPline(),
                                        bomEntity.getWholeBacklogDesc(),
                                        zgEntity.getProPlanId(),
                                        zgEntity.getGxPlanNo() });

                // if (!casTyte.equals("")) {
                // // 修改轧管排产表自炼,CIC
                // String sqlupdate = CoreXmlSqlParsersFactory
                // .getSql("FrmBomConfirm.updateOutsFl");
                // PreparedStatement psta = con.prepareStatement(sqlupdate);
                // psta.setObject(1, casTyte);
                // psta.setObject(2, zgEntity.getProPlanId());
                // psta.setObject(3, zgEntity.getGxPlanNo());
                // psta.executeUpdate();
                // psta.close();
                // }
                PlnHelper.deleteOrUpdate(con,
                        "FrmChangeGrade.updateZgMaterial", new String[] {
                                bomEntity.getGradecode(),
                                bomEntity.getGradename(),
                                bomEntity.getDInMaterialNo(),
                                bomEntity.getDInMaterialDesc(),
                                bomEntity.getDOutMaterialNo(),
                                bomEntity.getDOutMaterialDesc(), cic,
                                proPlanId, gxPlanNo });
            }
            core.setV_errMsg("修改成功!");
            core.setResult(true);
            con.commit();
        }
        catch (SQLException sqle) {
            try {
                con.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            core.setV_errCode(-1);
            core.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        // catch (Exception e) {
        // e.printStackTrace();
        // try {
        // con.rollback();
        // }
        // catch (SQLException e1) {
        // e1.printStackTrace();
        // }
        // }
        return core;
    }
}