package com.steering.pss.plan.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.plan.order.model.PlnOrdCraftFileEntity;

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

    /**
     * 查询合同头信息
     * @param orderNo
     * @return
     */
    public CoreReturnObject getOrderHeadInfo(ArrayList<String> params) {
        return this.getDataBySqlID("CoreOrderCraftInfo.getOrderHeadInfo",
                params.toArray());
    }

    /**
     * 查询合同行信息
     * @param params
     * @return
     */
    public CoreReturnObject getOrderLineInfo(ArrayList<String> params) {
        return this.getDataBySqlID("CoreOrderCraftInfo.getOrderLineInfo",
                params.toArray());
    }

    /**
     * 查询监制文件信息
     * @param params
     * @return
     */
    public CoreReturnObject getCraftfileInfo(ArrayList<String> params) {
        return this.getDataBySqlID("CoreOrderCraftInfo.getCraftfileInfo",
                params.toArray());
    }

    /**
     * 查询工艺信息
     * @param orderNo
     * @param orderSeq
     * @return
     */
    public CoreReturnObject getCraftByOrderno(String orderNo, String orderSeq) {
        return this.getDataBySqlID("CoreOrderCraftInfo.getCraftByOrderno",
                new Object[] { orderNo, orderSeq });
    }

    /**
     * 查询监制文件信息
     * @param beginTime
     * @param orderSeq
     * @return
     */
    public CoreReturnObject getCraftfileByTime(String beginTime, String endTime) {
        return this.getDataBySqlID("CoreOrderCraftInfo.getCraftfileByTime",
                new Object[] { beginTime, endTime });
    }

    /**
     * 查询工艺文件类型
     * @return
     */
    public CoreReturnObject getBaseInfo2002() {
        return this.getDataBySqlID("CoreOrderCraftInfo.getBaseInfo2002");
    }

    /**
     * 查询工艺文件状态
     * @return
     */
    public CoreReturnObject getBaseInfo2003() {
        return this.getDataBySqlID("CoreOrderCraftInfo.getBaseInfo2003");
    }

    /**
     * 查询扣型
     * @return
     */
    public CoreReturnObject getBaseModel() {
        return this.getDataBySqlID("CoreOrderCraftInfo.getBaseModel");
    }
    
    /**
     * 查询工艺标准
     * @param craftFileNo
     * @param orderNo
     * @param orderSeq
     * @return
     */
    public CoreReturnObject getOrdCraftStd(String craftFileNo, String orderNo,
            String orderSeq) {
        return this.getDataBySqlID("CoreOrderCraftInfo.getOrdCraftStd",
                new Object[] { craftFileNo, orderNo, orderSeq });
    }

    /**
     * 查询钢级
     * @return
     */
    public CoreReturnObject getBaseSteel() {
        return this.getDataBySqlID("CoreOrderCraftInfo.getBaseSteel");
    }

    /**
     * 查询变径类型
     * @return
     */
    public CoreReturnObject getBjProcessC() {
        return this.getDataBySqlID("CoreOrderCraftInfo.getBjProcessC");
    }

    /**
     * 查询品种
     * @return
     */
    public CoreReturnObject getBaseProduct() {
        return this.getDataBySqlID("CoreOrderCraftInfo.getBaseProduct");
    }

    /**
     * 删除工艺文件
     * @return
     */
    public CoreReturnObject deleteCraftInfo(ArrayList<ArrayList<Object>> list) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CoreOrderCraftInfo.deleteCraftInfo");
            parmlist.add(list.get(i).toArray());

            Object[] param = new Object[] { "", "", list.get(i).get(0),
                    list.get(i).get(1) };
            sqllist.add("CoreOrderCraftInfo.matchCraftfileToPln");
            parmlist.add(param);
        }
        return this.setDataById(sqllist, parmlist);
    }

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

    /**
     * 查询二级工序
     * @param list
     * @return
     */
    public CoreReturnObject getOrderCraftpath(ArrayList<Object> list) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreOrderCraftInfo.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("CoreOrderCraftInfo.getCraftFileByOrdLnPk");
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCraftInfo.addCraftFile");
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderCraftInfo.updateCraftFile");
//            String sqlUpdateCg = CoreXmlSqlParsersFactory
//                    .getSql("CoreOrderCraftInfo.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("CoreOrderCraftInfo.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
     * @return
     */
    public CoreReturnObject setCraftFileFlag(ArrayList<ArrayList<Object>> list) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            sqllist.add("CoreOrderCraftInfo.setCraftFileFlag");
            parmlist.add(list.get(i).toArray());
        }

        return this.setDataById(sqllist, parmlist);
    }

    /**
     * 启用工艺文件
     * @param craftFiles
     * @return
     */
    public CoreReturnObject setCraftFileEnable(
            ArrayList<PlnOrdCraftFileEntity> craftFiles) {
        ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();

        for (PlnOrdCraftFileEntity craftFile : craftFiles) {
            Object[] param = new Object[] { craftFile.getCraftFileFlagCode(),
                    craftFile.getCraftFileFlagNm(),
                    craftFile.getCraftFileTypeCode(),
                    craftFile.getCraftFileTypeNm(),
                    craftFile.getCraftFilePath(), craftFile.getOrderNo(),
                    craftFile.getOrderSeq() };
            sqllist.add("CoreOrderCraftInfo.setCraftFileFlag");
            parmlist.add(param);

            // 更新销售提报表匹配工艺文件
            param = new Object[] { craftFile.getCraftFileNo(),
                    craftFile.getCraftFileMan(), craftFile.getOrderNo(),
                    craftFile.getOrderSeq() };
            sqllist.add("CoreOrderCraftInfo.matchCraftfileToPln");
            parmlist.add(param);
        }
        return this.setDataById(sqllist, parmlist);
    }

}
