package com.steering.pss.plnsaleord.steelMarkingPlan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;

/**
 * 炼钢计划辅助类
 * @author Administrator
 */
public class SteelHelper extends DBCommon {
  
    /**
     * 初始化产线
     * @param processCode 　工序代码
     * @return CoreReturnObject
     */
    public CoreReturnObject initPline(String processCode) {
        Object[] params = new Object[] { processCode };
        return this.getDataBySqlID("SteelHelper.initPline", params);
    }

    /**
     * 初始化钢种
     * @return CoreReturnObject
     */
    public CoreReturnObject initGrade() {
        return this.getDataBySqlID("SteelHelper.initGrade");
    }

    /**
     * 初始化工序
     * @return
     */
    public CoreReturnObject initProcess(String processCode) {
        Object[] params = new Object[] { processCode };
        return this.getDataBySqlID("SteelHelper.initProcess", params);
    }

    /**
     * json转化为实体类
     * @param c 实体类类型
     * @param jsonStr 字符串
     * @return T
     */
    static <T> T jsonToEntity(Class<?> c, String jsonStr) {
        if (jsonStr.equals(""))
            return null;
        JSONObject obj = JSONObject.fromObject(jsonStr);
        @SuppressWarnings("unchecked")
        T t = (T) JSONObject.toBean(obj, c);
        return t;
    }

    /**
     * json集合转化实体类集合
     * @param c 实体类类型
     * @param jsonList 字符串集合
     * @return <T>
     */
    static <T> List<T> jsonsToEntityList(Class<?> c, List<String> jsonList) {
        if (jsonList == null || jsonList.size() == 0)
            return null;
        List<T> list = new ArrayList<T>();
        for (String str : jsonList) {
            T t = SteelHelper.jsonToEntity(c, str);
            list.add(t);
        }
        return list;
    }

    /**
     * 字符串数组任意组合
     * @param ss
     * @param i
     * @param list
     * @return
     */
    public static ArrayList<String> permutation(String[] ss, int i,
            ArrayList<String> list) {
        if (ss == null || i < 0 || i > ss.length) {
            return null;
        }
        if (i == ss.length) {
            list.add(StringUtils.join(ss, ","));
        }
        else {
            for (int j = i; j < ss.length; j++) {
                String temp = ss[j];// 交换前缀,使之产生下一个前缀
                ss[j] = ss[i];
                ss[i] = temp;
                permutation(ss, i + 1, list);
                temp = ss[j]; // 将前缀换回来,继续做上一个的前缀排列.
                ss[j] = ss[i];
                ss[i] = temp;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        ArrayList<String> strList = new ArrayList<String>();
        String[] ss = { "钢种1", "钢种2", "钢种3" };
        ArrayList<String> list = SteelHelper.permutation(ss, 0, strList);
        for (String string : list) {
            System.out.println(string);
        }
    }

    // 铸造订单自动拆分
    public String addAutoBreak(String[] caseNo, String createName)
            throws SQLException {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        String message = "";

        conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        ResultSet rs = null;

        // 根据浇次号查询订单号，订单序号
        String sqlQuery = CoreXmlSqlParsersFactory
                .getSql("ComPlanFurnace.addInsertZLPlan");
        sqlQuery += CoreConverter.toSqlConditionString("T.CAST_NO", caseNo);
        sqlQuery += " ) T2 GROUP BY T2.PRO_PLAN_ID, T2.GX_PLAN_NO";
        ps=conn.prepareStatement(sqlQuery);
        rs=ps.executeQuery();
        while(rs.next()){
            
        }
        rs.close();
        ps.close();
        return message;

    }
}
