package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * 生成合同号
 * @author WenHB XSXGQ-15A0001 GMXGQ-15A0002 EXSGQ-150001E 共13位
 * @date 2015-09-25 由于SQL很简单 就没有单独弄一个XML文件
 */
public class CoreGenerateOrderNo {

    /**
     * 生成合同号 XSXGQ-15A0001 GMXGQ-15A0002 EXSGQ-150001E 共13位 其中销售钢贸共用A0001
     * 字母流水不要O和 "I"
     * @param saleOrg 销售组织
     * @param tradeType 贸易类型
     * @param classify 分类 工程 一般
     * @param type 合同类型 期货 现货
     * @param busType 业务分类 只针对国贸
     * @return 返回信息
     */
    public static String[] generateOrderNo(String saleOrg, String tradeType,
            String classify, String type, String busType) {
        String[] message = new String[2]; // 返回信息 ----》 标志 信息
        String orderNo = "";
        String prefix = ""; // 前缀 前两位
        String prefix1 = ""; // 3-6位 共4位
        String prefix2 = ""; // 7-8位 年2位
        String prefix3 = ""; // 后面5位 A0001或者0001E
        if (saleOrg.equals("100101")) {
            prefix = "XS";
        }
        else if (saleOrg.equals("100102")) {
            prefix = "EX";
        }
        else if (saleOrg.equals("100103")) {
            prefix = "GM";
        }
        else if (saleOrg.equals("100105")) {
            prefix = "HG";
        }
        else if(saleOrg.equals("200101")){// 天淮
            prefix = "TH";
        }
        else {
            message[0] = "False";
            message[1] = "销售组织错误，无法生成合同号！";
            return message;
        }
        String memo = "";
        // 贸易类型 将需要表示的字母写在备注字段-这样更灵活
        memo = queryBaseInfoMemo(tradeType);
        if (memo == null || memo.equals("") || memo.length() > 1) {
            message[0] = "False";
            message[1] = "贸易类型备注字段不符合合同号规则，请在基础数据维护一个字符！";
            return message;
        }
        prefix1 = memo;
        // 分类
        memo = queryBaseInfoMemo(classify);
        if (memo == null || memo.equals("") || memo.length() > 1) {
            message[0] = "False";
            message[1] = "合同分类备注字段不符合合同号规则，请在基础数据维护一个字符！";
            return message;
        }
        prefix1 += memo;
        // 合同类型
        if (type.equals("120102")) {
            prefix1 += "X";
        }
        else if (type.equals("120101")) {
            prefix1 += "Q";
        }
        else if (type.equals("120103")) {
            prefix1 += "Y";
        }
        else if (type.equals("120104")) {
            prefix1 += "P";
        }
        else if (type.equals("120105")) {
            prefix1 += "C";
        }
        else if (type.equals("120106")) {
            prefix1 += "W";
        }
        else if (type.equals("120107")) {
            prefix1 += "L";
        }
        else if (type.equals("120108")) {
            prefix1 += "S";
        }
        else if (type.equals("120109")) {
            prefix1 += "D";
        }
        prefix1 += "-";
        // 年份
        DBCommon db = new DBCommon();
        java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
        prefix2 = date.toString().substring(2, 4);
        // 获取后面5位
        String sql = "";
        if (saleOrg.equals("100102")) { // 国贸
            sql = "SELECT MAX(SUBSTR(T.ORDER_NO, 9, 5)) SUFFIX FROM SLM_ORDER_HEAD T WHERE SUBSTR(T.ORDER_NO, 1, 2) = '"
                    + prefix
                    + "'  AND SUBSTR(T.ORDER_NO, 7, 2) = '"
                    + prefix2
                    + "'  AND LENGTH(T.Order_No) = 13";
        }
        else if(saleOrg.equals("100105")){//天淮
        	sql = "SELECT MAX(SUBSTR(T.ORDER_NO, 9, 5)) SUFFIX FROM SLM_ORDER_HEAD T WHERE SUBSTR(T.ORDER_NO, 1, 2) = '"
                    + prefix
                    + "'  AND SUBSTR(T.ORDER_NO, 7, 2) = '"
                    + prefix2
                    + "'  AND LENGTH(T.Order_No) = 13";
        }
        else {// 销售 钢贸共用 后面的流水号
            sql = "SELECT MAX(SUBSTR(T.ORDER_NO, 9, 5)) SUFFIX FROM SLM_ORDER_HEAD T WHERE SUBSTR(T.ORDER_NO, 1, 2) IN ('XS','GM','TH') AND SUBSTR(T.ORDER_NO, 7, 2) = '"
                    + prefix2 + "'  AND LENGTH(T.Order_No) = 13";
        }
        try {
            ResultSet rs = db.getDao(CoreConstantData.sysbeanstr)
                    .ExceuteQueryForResultSet(sql);
            if (rs.next()) {
                prefix3 = rs.getString(1);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        // 解析后面5位流水
        DecimalFormat df = new DecimalFormat("0000");
        if (saleOrg.equals("100102")) {// 国贸
            if (prefix3 == null || prefix3.equals("")) {
                prefix3 = "0001" + busType;
            }
            else {
                String str = prefix3.substring(0, 4);
                prefix3 = df.format(Integer.parseInt(str) + 1) + busType;
            }
        }
        else {// 销售、钢贸、天淮
            if (prefix3 == null || prefix3.equals("")) {// 系统还没有
                prefix3 = "A0001";
            }
            else {
                String preStr = prefix3.substring(0, 1);
                String suxStr = prefix3.substring(1, 5);
                int num = Integer.parseInt(suxStr);
                if (num == 9999) {
                    char pre = (char) (preStr.charAt(0) + 1);
                    preStr = String.valueOf(pre);
                    if (preStr.equals("I")) {
                        preStr = "J";
                    }
                    else if (preStr.equals("O")) {
                        preStr = "P";
                    }
                    prefix3 = preStr + "0001";
                }
                else {
                    num += 1;
                    prefix3 = preStr + df.format(num);
                }
            }
        }
        orderNo = prefix + prefix1 + prefix2 + prefix3;
        if (orderNo.length() == 13) {
            message[0] = "True";
            message[1] = orderNo;
        }
        else {
            message = null;
        }
        return message;
    }

    /**
     * 获取基础信息的备注字段
     * @param baseCode
     * @return
     */
    public static String queryBaseInfoMemo(String baseCode) {
        String sql = "SELECT TRIM(MEMO) FROM COM_BASE_INFO WHERE BASECODE = '"
                + baseCode + "' AND VALIDFLAG = '1'";
        String memo = "";
        try {
            DBCommon db = new DBCommon();
            ResultSet rs = db.getDao(CoreConstantData.sysbeanstr)
                    .ExceuteQueryForResultSet(sql);
            if (rs.next()) {
                memo = rs.getString(1);
            }
            rs.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return memo;
    }

    /**
     * @param parm
     * @return
     */
    public CoreReturnObject getOrderNoPrefix(ArrayList<String> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String saleOrg = parm.get(0);
        String tradeType = parm.get(1);
        String classify = parm.get(2);
        String type = parm.get(3);
        String prefix = ""; // 前缀 前两位
        String prefix1 = ""; // 3-6位 共4位

        if (saleOrg.equals("100101")) {
            prefix = "XS";
        }
        else if (saleOrg.equals("100102")) {
            prefix = "EX";
        }
        else if (saleOrg.equals("100103")) {
            prefix = "GM";
        }
        else if (saleOrg.equals("100105")) {
            prefix = "HG";
        }
        else if(saleOrg.equals("200101")){// 天淮
            prefix = "TH";
        }
        else {
            cro.setV_errCode(-1);
            cro.setV_errMsg("销售组织错误，无法生成合同号！");
            return cro;
        }
        String memo = "";
        // 贸易类型 将需要表示的字母写在备注字段-这样更灵活
        memo = queryBaseInfoMemo(tradeType);
        if (memo == null || memo.equals("") || memo.length() > 1) {
            cro.setV_errCode(-1);
            cro.setV_errMsg("贸易类型备注字段不符合合同号规则，请在基础数据维护一个字符！");
            return cro;
        }
        prefix1 = memo;
        // 分类
        memo = queryBaseInfoMemo(classify);
        if (memo == null || memo.equals("") || memo.length() > 1) {
            cro.setV_errCode(-1);
            cro.setV_errMsg("合同分类备注字段不符合合同号规则，请在基础数据维护一个字符！");
            return cro;
        }
        prefix1 += memo;
        // 合同类型
        // 合同类型
        if (type.equals("120102")) {
            prefix1 += "X";
        }
        else if (type.equals("120101")) {
            prefix1 += "Q";
        }
        else if (type.equals("120103")) {
            prefix1 += "Y";
        }
        else if (type.equals("120104")) {
            prefix1 += "P";
        }
        else if (type.equals("120105")) {
            prefix1 += "C";
        }
        else if (type.equals("120106")) {
            prefix1 += "W";
        }
        else if (type.equals("120107")) {
            prefix1 += "L";
        }
        else if (type.equals("120108")) {
            prefix1 += "S";
        }
        else if (type.equals("120109")) {
            prefix1 += "D";
        }
        prefix1 += "-";
        cro.setV_errCode(0);
        cro.setResult(prefix + prefix1);
        return cro;
    }

}
