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 com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.PlnOrdCraftFileEntity;

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

/**
 * 合同综合信息
 * @author Administrator
 */
public class CoreOrderColligateInfomation extends DBCommon {

    /**
     * 查询合同头信息
     * @param orderNo
     * @return
     */
    public CoreReturnObject getOrderHeadInfo(ArrayList<String> params,
            String[] dataPurviews, Integer num) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderColligateInfomation.getOrderHeadInfo");
        if (num == 0) {
            sql = sql.replaceAll("##NUM##", "=0");
        }
        else {
            sql = sql.replaceAll("##NUM##", ">=0");
        }

        sql += CoreConverter.toSqlConditionString("T.CREATE_DEPT",
                dataPurviews);
        sql += "  ORDER BY T.SALE_AREA_DESC,T.SALE_ORG_DESC,T.ORDER_NO,T.CREATE_NAME,T.CREATE_TIME ";
        return this.getData(sql, params.toArray());
    }

    /**
     * 查询合同行信息
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderLineInfo(String ordPk) {
        Object[] params = new Object[] { ordPk };
        return this.getDataBySqlID(
                "CoreOrderColligateInfomation.getOrderLineInfo", params);
    }

    /**
     * 查询定价内容
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderPriceInfo(String priceNo) {
        Object[] params = new Object[] { priceNo };
        return this.getDataBySqlID(
                "CoreOrderColligateInfomation.getOrderPriceInfo", params);
    }

    /**
     * 查询定价明细
     * @param ordPk
     * @return
     */
    public CoreReturnObject getOrderPriceDetail(String ordLnPk) {
        Object[] params = new Object[] { ordLnPk };
        return this.getDataBySqlID(
                "CoreOrderColligateInfomation.getOrderPriceDetail", params);
    }

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

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

    /**
     * 保存工艺文件
     * @param craftFiles
     * @return
     */
    public CoreReturnObject saveCraftFile(
            ArrayList<PlnOrdCraftFileEntity> craftFiles) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            // 判断合同行是否生效,如果不是生效状态则不执行
            String sqlLine = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderColligateInfomation.getOrderLineStatus");
            Boolean lineflag = true;
            for (PlnOrdCraftFileEntity craftFile : craftFiles) {
                String lineStatus = "";
                ps = con.prepareStatement(sqlLine);
                ps.setObject(1, craftFile.getOrdLnPk());
                rs = ps.executeQuery();
                if (rs.next()) {
                    lineStatus = rs.getString("ORDER_LN_STATUS");
                }
                if (!lineStatus.equals("12020301")) {
                    lineflag = false;
                }
                rs.close();
                ps.close();
            }
            if (!lineflag) {
                core.setV_errMsg("合同行没有生效,不能保存工艺文件");
                return core;
            }
            String sql = CoreXmlSqlParsersFactory.getSql(
                    "CoreOrderColligateInfomation.getCraftFileByOrdLnPk");
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderColligateInfomation.addCraftFile");
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderColligateInfomation.updateCraftFile");
            for (PlnOrdCraftFileEntity craftFile : craftFiles) {
                ps = con.prepareStatement(sql);
                ps.setObject(1, craftFile.getOrdLnPk());
                rs = ps.executeQuery();
                boolean flag = false;
                if (rs.next()) {
                    flag = true;
                }
                rs.close();
                ps.close();
                if (flag) {
                    String sqlU = EntityTool.fillSqlByEntity(sqlUpdate,
                            craftFile);
                    ps = con.prepareStatement(sqlU);
                    ps.executeUpdate();
                    ps.close();
                }
                else {
                    String sqlA = EntityTool.fillSqlByEntity(sqlAdd, craftFile);
                    ps = con.prepareStatement(sqlA);
                    ps.executeUpdate();
                    ps.close();
                }
            }
            con.commit();
        }
        catch (SQLException e) {

            e.printStackTrace();
        }
        finally {
            try {
                if (con != null)
                    con.close();
            }
            catch (SQLException e) {

                e.printStackTrace();
            }
        }
        core.setV_errMsg("保存成功");
        return core;
    }

    /**
     * 判断合同下面结算价是否都有效
     * @param ordPk
     * @return
     */
    public CoreReturnObject queryBlaPriceStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID(
                "CoreOrderColligateInfomation.queryBlaPriceStatus", obj);
    }

    /**
     * 查询合同状态
     * @param ordPk
     * @return
     */
    public CoreReturnObject queryOrderHeadStatus(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.getDataBySqlID(
                "CoreOrderColligateInfomation.queryOrderHeadStatus", obj);
    }

    /**
     * 设置打印时间打印次数
     * @param ordPk
     * @return
     */
    public CoreReturnObject setPrintTimeAndCount(String ordPk) {
        Object[] obj = new Object[] { ordPk };
        return this.setDataBySqlID(
                "CoreOrderColligateInfomation.setPrintTimeAndCount", obj);
    }

}
