package com.steering.pss.plnsaleord.order;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;

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

import com.steering.pss.plnsaleord.order.model.PlnOrdCraftFileEntity;

/**
 * 工艺文件维护
 * @author Administrator
 */
public class CoreOrderCraftInfo extends DBCommon {

    /**
     * 查询提报合同
     * @param orderNo
     * @param orderSeq
     * @return
     */
    public CoreReturnObject getSaleOrderStatus(String orderNo, String orderSeq) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrdCraftInfo.getSaleOrderStatus",
                new Object[] { orderNo, orderSeq });
        return cro;
    }

    /**
     * 查询二级工序
     * @param list
     * @return
     */
    public CoreReturnObject getOrderCraftpath(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrdCraftInfo.getOrderCraftpath", list
                .toArray());
        return cro;
    }

    /**
     * 导入工艺文件信息
     * @return
     */
    public CoreReturnObject saveCraftInfo(
            ArrayList<PlnOrdCraftFileEntity> craftFiles, Integer startRow) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrdCraftInfo.getCraftFileByOrdLnPk");
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("CoreOrdCraftInfo.addCraftFile");
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("CoreOrdCraftInfo.updateCraftFile");
//            String sqlUpdateCg = CoreXmlSqlParsersFactory
//                    .getSql("CoreOrdCraftInfo.updateCraftFileChange");
            for (PlnOrdCraftFileEntity craftFile : craftFiles) {
                ps = conn.prepareStatement(sql);
                ps.setObject(1, craftFile.getOrderNo());
                ps.setObject(2, craftFile.getOrderSeq());
                ps.setObject(3, craftFile.getCraftFileNo());
                ResultSet rs = ps.executeQuery();
                boolean flag = false;
                if (rs.next()) {
                    flag = true;
                }
                rs.close();
                ps.close();

                // 判断新增还是更新
                int result = 0;
                if (flag) {
                    // 更新
                    String sqlU = "";
                    craftFile.setUpdateName(craftFile.getCreateName());
                    sqlU = EntityTool.fillSqlByEntity(sqlUpdate, craftFile);
//                    if (craftFile.getVersionNo().equals("")) {
//                        // 工艺无变动
//                        sqlU = EntityTool.fillSqlByEntity(sqlUpdate, craftFile);
//                    }
//                    else {
//                        // 工艺有变动
//                        sqlU = EntityTool.fillSqlByEntity(sqlUpdateCg,
//                                craftFile);
//                    }
                    ps = conn.prepareStatement(sqlU);
                    result = ps.executeUpdate();
                    ps.close();
                    if (result <= 0) {
                        cro.setV_errMsg("第" + startRow + "行，工艺文件"
                                + craftFile.getOrderNo() + "/"
                                + craftFile.getOrderSeq() + "导入失败！");
                        return cro;
                    }
                }
                else {
                    String sqlA = EntityTool.fillSqlByEntity(sqlAdd, craftFile);
                    String sqlSync = CoreXmlSqlParsersFactory
                            .getSql("CoreOrdCraftInfo.matchCraftfileToPln");
                    ps = conn.prepareStatement(sqlA);
                    result = ps.executeUpdate();
                    ps.close();
                    if (result <= 0) {
                        cro.setV_errMsg("第" + startRow + "行，工艺文件"
                                + craftFile.getOrderNo() + "/"
                                + craftFile.getOrderSeq() + "导入失败！");
                        return cro;
                    }

                    // 同步工艺到合同提报表
                    ps = conn.prepareStatement(sqlSync);
                    ps.setString(1, craftFile.getCraftFileNo());
                    ps.setString(2, craftFile.getCraftFileMan());
                    ps.setString(3, craftFile.getOrderNo());
                    ps.setString(4, craftFile.getOrderSeq());
                    ps.executeUpdate();
                    ps.close();
                }
                startRow++;
                ps.close();
            }
            conn.commit();
            cro.setV_errMsg("导入成功！");
        }
        catch (SQLException sqle) {
            try {
                cro.setV_errMsg("第" + startRow + "行导入失败，"
                        + sqle.getLocalizedMessage());
                conn.rollback();
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询合同
     * @param list
     * @param notCraft
     * @param isImportOrd
     * @return
     */
    public CoreReturnObject queryExportSaleOrder(ArrayList<String> list,
            String strFinish, Boolean notCraft, Boolean isImportOrd) {
        CoreReturnObject cro = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrdCraftInfo.queryExportOutImport");
        if (notCraft) {
            sql = sql + " AND M.CRAFT_NO IS NULL";
        }
        if (strFinish.equals("0")) {
            sql = sql + " AND (T2.PLANSTATUS IN ('1', '2', '3') OR "
                    + "(T2.PLANSTATUS IN ('4', '5') AND "
                    + "TO_DATE(T2.ROLL_MONTH, 'YYYY-MM') = "
                    + "TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM'), 'YYYY-MM')))";
        }
        if (!isImportOrd) {
            sql = sql + " AND LENGTH(T.ORDER_SEQ) != 2";
        }
        cro = this.getData(sql, list.toArray());
        return cro;
    }

}
