package com.th.demo.tools;

import com.th.demo.model.system.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

public class Tools {


    public static void out(HttpServletResponse response, String result) {
        //下半部分固定格式 给jsp返回数据
        try {
            System.out.println("result=" + result);
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-type", "text/html;charset=utf-8");
//            response.setHeader("Access-Control-Allow-Origin","*");
//            response.setHeader("Access-Control-Request-Methods","GET,POST,OPTIONS,PUT,DELETE");
//            response.setHeader("Access-Control-Request-Headers","x-requested-with,content-type");
            PrintWriter out = null;
            out = response.getWriter();
            out.print(result);
            out.flush();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getRandomFour() {

        int intCount = 0;

        intCount = (new Random()).nextInt(9999);//

        if (intCount < 1000)
            intCount += 1000;


        return intCount + "";
    }

    public static String moreThanZero(int num) {
        if (num > 0) {
            return Type.SUCCESS;
        } else {
            return Type.FAIL;
        }
    }


    public static String moreThanZeroResultJSON(int num) {
        if (num > 0) {
            return JSONTools.toStringyyyyMMdd(num);
        } else {
            return JSONTools.toString(1,"操作失败，未执行成功");
        }
    }

    public static List<TreeNode> getTree(List<SysMenu> list, String pid) {
        List<TreeNode> tree = new ArrayList<TreeNode>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getMenuPid().equals(pid)) {
                TreeNode tn = new TreeNode();
                tn.setId(list.get(i).getId());
                tn.setCode(list.get(i).getMenuCode());
                tn.setText(list.get(i).getMenuName());
                if (list.get(i).getMenuShow() == null) {
                    tn.setState("0");
                } else {
                    tn.setState(list.get(i).getMenuShow() + "");
                }
                tn.setUrl(list.get(i).getMenuOpenBs());

                List<TreeNode> children = getTree(list, list.get(i)
                        .getMenuCode());
                tn.setChildren(children);

                tree.add(tn);
            }
        }

        return tree;
    }



    public static List<TreeNode> getTreeVue(List<SysMenu> list, String pid) {
        List<TreeNode> tree = new ArrayList<TreeNode>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getMenuPid().equals(pid)) {
                TreeNode tn = new TreeNode();
                tn.setId(list.get(i).getId());
                tn.setCode(list.get(i).getMenuCode());
                tn.setText(list.get(i).getMenuName());
                tn.setState(list.get(i).getMenuOpenAnd());
                tn.setUrl(list.get(i).getMenuOpenCs());

                List<TreeNode> children = getTreeVue(list, list.get(i)
                        .getMenuCode());
                tn.setChildren(children);

                tree.add(tn);
            }
        }

        return tree;
    }


    public static List<TreeNode> getDeptTree(List<SysDepartment> list, String pid) {
        List<TreeNode> tree = new ArrayList<TreeNode>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getfId().equals(pid)) {
                TreeNode tn = new TreeNode();
                tn.setId(list.get(i).getId());
                //	tn.setCode(list.get(i).getMenuCode());
                tn.setText(list.get(i).getDeptName());
                tn.setState("open");
//				tn.setUrl(list.get(i).getMenuDesc());
//				tn.setMemo(list.get(i).getMenuOpen());
                List<TreeNode> children = getDeptTree(list, list.get(i)
                        .getId() + "");
                tn.setChildren(children);

                tree.add(tn);
            }
        }

        return tree;
    }





    ///123423456
    public static OutputStream getExportOutStream(String filename, HttpServletResponse response) {
        OutputStream out = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO8859-1"));
            out = response.getOutputStream();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return out;
        }
    }

    //888888
    public static String[] getDicKeyToArrStr(List<SysDic> list) {
        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i).getDicCode();
        }
        return result;
    }

    public static String[] getDicValueToArrStr(List<SysDic> list) {
        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i).getDicName();
        }
        return result;
    }

    public static Map<String, Integer[]> getStrToMapInt(String ids) {

        String[] srtIds = ids.split(",");
        Integer[] intIds = new Integer[srtIds.length];
        for (int i = 0; i < intIds.length; i++) {
            intIds[i] = Integer.parseInt(srtIds[i]);
        }
        Map<String, Integer[]> mapIds = new HashMap<String, Integer[]>();
        mapIds.put("ids", intIds);

        return mapIds;
    }

    public static Integer[] getStrToArrInt(String ids) {

        String[] srtIds = ids.split(",");
        Integer[] intIds = new Integer[srtIds.length];
        for (int i = 0; i < intIds.length; i++) {
            intIds[i] = Integer.parseInt(srtIds[i]);
        }

        return intIds;
    }

    public static List<Integer> getStrToListInt(String ids) {

        String[] srtIds = ids.split(",");

        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < srtIds.length; i++) {
            list.add(Integer.parseInt(srtIds[i]));
        }

        return list;
    }

    public static String getArrStrToStr(String[] arr) {

        String result = "";
        for (int i = 0; i < arr.length; i++) {
            result += arr[i] + ",";
        }
        if (result.endsWith(",")) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    public static List<TreeNode> getRoute(List<SysMenu> list, String pid) {

        List<TreeNode> tree = new ArrayList<TreeNode>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAttr1().equals(pid)) {
                TreeNode tn = new TreeNode();
                tn.setId(list.get(i).getId());
                tn.setCode(list.get(i).getMenuCode());
                tn.setText(list.get(i).getMenuName());
                if (list.get(i).getMenuShow() == null) {
                    tn.setState("0");
                } else {
                    tn.setState(list.get(i).getMenuShow() + "");
                }
                tn.setUrl(list.get(i).getMenuOpenBs());

                List<TreeNode> children = getRoute(list, list.get(i)
                        .getMenuCode());
                tn.setChildren(children);

                tree.add(tn);
            }
        }

        return tree;


    }
}
