package com.zcloud.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Dictionaries;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

public class Warden {

    private static final SimpleDateFormat yMd = new SimpleDateFormat("yyyy-MM-dd");

    public static List<PageData> getList(String raw) {
        return JSON.parseArray(raw, PageData.class);
    }

    public static List<PageData> getList(JSONArray raw) {
        return JSON.parseArray(raw.toJSONString(), PageData.class);
    }

    public static PageData getPageData(JSONObject raw) {
        return JSON.parseObject(raw.toJSONString(), PageData.class);
    }

    public static PageData getPageData(String raw) {
        return JSON.parseObject(raw, PageData.class);
    }

    public static String get32UUID() {
        return UuidUtil.get32UUID();
    }

    public static List<Map<String, String>> getVueUploadEntity(String url) {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("name", get32UUID());
        map.put("url", url);
        list.add(map);
        return list;
    }

    public static void initDate(PageData entity) {
        entity.put("CREATOR", Jurisdiction.getUSER_ID());
        entity.put("CREATOR_ID", Jurisdiction.getUSER_ID());
        entity.put("CREATOR_NAME", Jurisdiction.getName());
        entity.put("CREATE_TIME", DateUtil.getTime());
        entity.put("CREATTIME", DateUtil.getTime());
        entity.put("CREATED_TIME", DateUtil.getTime());

        entity.put("OPERATOR", Jurisdiction.getUSER_ID());
        entity.put("OPERATOR_ID", Jurisdiction.getUSER_ID());
        entity.put("OPERATOR_NAME", Jurisdiction.getName());
        entity.put("OPERAT_TIME", DateUtil.getTime());
        entity.put("OPERATE_TIME", DateUtil.getTime());
        entity.put("OPERATTIME", DateUtil.getTime());

        entity.put("ISDELETE", "0");
    }

    public static void updateDate(PageData entity) {
        entity.put("OPERATOR", Jurisdiction.getUSER_ID());
        entity.put("OPERATOR_ID", Jurisdiction.getUSER_ID());
        entity.put("OPERATOR_NAME", Jurisdiction.getName());
        entity.put("OPERAT_TIME", DateUtil.getTime());
        entity.put("OPERATE_TIME", DateUtil.getTime());
        entity.put("OPERATTIME", DateUtil.getTime());
        entity.put("ISDELETE", "0");
    }

    public static void deleteDate(PageData entity) {
        entity.put("DELETEOR_ID", Jurisdiction.getUSER_ID());
        entity.put("DELETEOR_NAME", Jurisdiction.getName());
        entity.put("DELETE_TIME", DateUtil.getTime());
        entity.put("ISDELETE", "1");
    }

    public static Date addDate(Date date, long day) {
        if (date == null) {
            return new Date();
        }
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
        time += day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }

    public static String checkId(Object object) {
        if (object == null || StringUtils.isEmpty(object.toString())) {
            return get32UUID();
        } else {
            return object.toString();
        }
    }

    private static List<List<String>> head() {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map0 = new HashMap<>();
        map0.put("SORT", "0");
        map0.put("NAME", "隐患来源");
        map0.put("TYPE", "0");
        list.add(map0);

        Map<String, String> map1 = new HashMap<>();
        map1.put("SORT", "1");
        map1.put("NAME", "风险点（单元）");
        map1.put("TYPE", "0");
        list.add(map1);

        Map<String, String> map2 = new HashMap<>();
        map2.put("SORT", "2");
        map2.put("NAME", "辨识部位");
        map2.put("TYPE", "0");
        list.add(map2);

        Map<String, String> map3 = new HashMap<>();
        map3.put("SORT", "3");
        map3.put("NAME", "存在风险");
        map3.put("TYPE", "0");
        list.add(map3);

        Map<String, String> map4 = new HashMap<>();
        map4.put("SORT", "4");
        map4.put("NAME", "风险分级");
        map4.put("TYPE", "0");
        list.add(map4);

        Map<String, String> map5 = new HashMap<>();
        map5.put("SORT", "5");
        map5.put("NAME", "检查内容");
        map5.put("TYPE", "0");
        list.add(map5);

        Map<String, String> map6 = new HashMap<>();
        map6.put("SORT", "6");
        map6.put("NAME", "隐患级别");
        map6.put("TYPE", "0");
        list.add(map6);

        Map<String, String> map7 = new HashMap<>();
        map7.put("SORT", "7");
        map7.put("NAME", "隐患状态");
        map7.put("TYPE", "0");
        list.add(map7);

        Map<String, String> map8 = new HashMap<>();
        map8.put("SORT", "8");
        map8.put("NAME", "隐患描述");
        map8.put("TYPE", "0");
        list.add(map8);

        Map<String, String> map9 = new HashMap<>();
        map9.put("SORT", "9");
        map9.put("NAME", "隐患部位");
        map9.put("TYPE", "0");
        list.add(map9);

        Map<String, String> map10 = new HashMap<>();
        map10.put("SORT", "10");
        map10.put("NAME", "隐患发现部门");
        map10.put("TYPE", "0");
        list.add(map10);

        Map<String, String> map11 = new HashMap<>();
        map11.put("SORT", "11");
        map11.put("NAME", "发现人");
        map11.put("TYPE", "0");
        list.add(map11);

        Map<String, String> map12 = new HashMap<>();
        map12.put("SORT", "12");
        map12.put("NAME", "发现时间");
        map12.put("TYPE", "0");
        list.add(map12);

        Map<String, String> map13 = new HashMap<>();
        map13.put("SORT", "13");
        map13.put("NAME", "整改类型");
        map13.put("TYPE", "0");
        list.add(map13);

        Map<String, String> map14 = new HashMap<>();
        map14.put("SORT", "14");
        map14.put("NAME", "整改期限");
        map14.put("TYPE", "0");
        list.add(map14);

        Map<String, String> map15 = new HashMap<>();
        map15.put("SORT", "15");
        map15.put("NAME", "隐患图片");
        map15.put("TYPE", "1");
        list.add(map15);

        Map<String, String> map16 = new HashMap<>();
        map16.put("SORT", "16");
        map16.put("NAME", "整改描述");
        map16.put("TYPE", "0");
        list.add(map16);

        Map<String, String> map17 = new HashMap<>();
        map17.put("SORT", "17");
        map17.put("NAME", "整改部门");
        map17.put("TYPE", "0");
        list.add(map17);

        Map<String, String> map18 = new HashMap<>();
        map18.put("SORT", "18");
        map18.put("NAME", "整改人");
        map18.put("TYPE", "0");
        list.add(map18);

        Map<String, String> map19 = new HashMap<>();
        map19.put("SORT", "19");
        map19.put("NAME", "整改时间");
        map19.put("TYPE", "0");
        list.add(map19);

        Map<String, String> map20 = new HashMap<>();
        map20.put("SORT", "20");
        map20.put("NAME", "整改后图片");
        map20.put("TYPE", "1");
        list.add(map20);

        Map<String, String> map21 = new HashMap<>();
        map21.put("SORT", "21");
        map21.put("NAME", "整改方案");
        map21.put("TYPE", "0");
        list.add(map21);

        Map<String, String> map22 = new HashMap<>();
        map22.put("SORT", "22");
        map22.put("NAME", "排查日期");
        map22.put("TYPE", "0");
        list.add(map22);

        Map<String, String> map23 = new HashMap<>();
        map23.put("SORT", "23");
        map23.put("NAME", "隐患清单");
        map23.put("TYPE", "0");
        list.add(map23);

        Map<String, String> map24 = new HashMap<>();
        map24.put("SORT", "24");
        map24.put("NAME", "治理标准要求");
        map24.put("TYPE", "0");
        list.add(map24);

        Map<String, String> map25 = new HashMap<>();
        map25.put("SORT", "25");
        map25.put("NAME", "治理方法");
        map25.put("TYPE", "0");
        list.add(map25);

        Map<String, String> map26 = new HashMap<>();
        map26.put("SORT", "26");
        map26.put("NAME", "经费和物资的落实");
        map26.put("TYPE", "0");
        list.add(map26);

        Map<String, String> map27 = new HashMap<>();
        map27.put("SORT", "27");
        map27.put("NAME", "负责治理人员");
        map27.put("TYPE", "0");
        list.add(map27);

        Map<String, String> map28 = new HashMap<>();
        map28.put("SORT", "28");
        map28.put("NAME", "工时安排");
        map28.put("TYPE", "0");
        list.add(map28);

        Map<String, String> map29 = new HashMap<>();
        map29.put("SORT", "29");
        map29.put("NAME", "时限要求");
        map29.put("TYPE", "0");
        list.add(map29);

        Map<String, String> map30 = new HashMap<>();
        map30.put("SORT", "30");
        map30.put("NAME", "应急预案附件");
        map30.put("TYPE", "0");
        list.add(map30);

        Map<String, String> map31 = new HashMap<>();
        map31.put("SORT", "31");
        map31.put("NAME", "工作要求");
        map31.put("TYPE", "0");
        list.add(map31);

        Map<String, String> map32 = new HashMap<>();
        map32.put("SORT", "32");
        map32.put("NAME", "其他事项");
        map32.put("TYPE", "0");
        list.add(map32);

        Map<String, String> map33 = new HashMap<>();
        map33.put("SORT", "33");
        map33.put("NAME", "方案图片");
        map33.put("TYPE", "1");
        list.add(map33);

        Map<String, String> map34 = new HashMap<>();
        map34.put("SORT", "34");
        map34.put("NAME", "应急预案附件");
        map34.put("TYPE", "0");
        list.add(map34);
        return null;
    }

    private static List<List<Object>> dataList(PageData info) {
        List<List<Object>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<Object> data = new ArrayList<>();
            data.add("字符串" + i);
            data.add(0.56);
            data.add(new Date());
            list.add(data);
        }
        return list;
    }

    public static void checkRequestHiddenCreateExcel(PageData lock) {
        lock.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); // 企业id

        if (Tools.notEmpty(lock.getString("KEYWORDS"))) {
            lock.put("KEYWORDS", lock.getString("KEYWORDS").trim());
        }
        if (Tools.notEmpty(lock.getString("ISQUALIFIED"))) {
            lock.put("ISQUALIFIED", lock.getString("ISQUALIFIED").trim());
        }

        if (Tools.notEmpty(lock.getString("HIDDENLEVELE"))) {
            lock.put("HIDDENLEVELE", lock.getString("HIDDENLEVELE").trim());
        }
        if (Tools.notEmpty(lock.getString("STARTTIME"))) {
            lock.put("STARTTIME", lock.getString("STARTTIME").trim());
        }
        if (Tools.notEmpty(lock.getString("STATE"))) {
            lock.put("STATE", lock.getString("STATE").trim());
        }
        if (Tools.notEmpty(lock.getString("ENDTIME"))) {
            lock.put("ENDTIME", lock.getString("ENDTIME") + " 23:59:59");
        }
        if (Tools.notEmpty(lock.getString("DISPOSESTATE"))) {
            lock.put("DISPOSESTATE", lock.getString("DISPOSESTATE").trim());
            lock.put("SELFUSERID", Jurisdiction.getUSER_ID());
            lock.put("USERDEPT", Jurisdiction.getDEPARTMENT_ID());
        }
        if (lock.get("IS_SELF") != null && lock.get("IS_SELF").toString().equals("1")) {
            lock.put("SELF_USER_ID", Jurisdiction.getUSER_ID());
        }
        if (Tools.notEmpty(lock.getString("DEPTIDS"))) {
            String[] DEPT_IDS = lock.getString("DEPTIDS").split(",");
            lock.put("DEPT_IDS", DEPT_IDS);
        }
    }

    /**
     * decription: 获取当月的第一天
     *
     * @return String(yyyy - MM - dd)
     * @throws Exception all
     */
    public static String getMonthFristDate() throws Exception {
        Calendar cale = Calendar.getInstance();
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return yMd.format(cale.getTime()) + " 00:00:00";
    }

    public static String getMonthFristDate(String year, String month) throws Exception {
        Calendar cale = Calendar.getInstance();
        //设置年份
        cale.set(Calendar.YEAR, Integer.parseInt(year));
        //设置月份
        cale.set(Calendar.MONTH, Integer.parseInt(month) - 1);
//        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return yMd.format(cale.getTime()) + " 00:00:00";
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getMonthFristDate("2023", "04"));
    }

    /**
     * decription: 获取当月的最后一天
     *
     * @return String(yyyy - MM - dd)
     * @throws Exception all
     */
    public static String getMonthLastDate() throws Exception {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return yMd.format(cale.getTime()) + " 23:59:59";
    }

    /**
     * decription: 获取当月的最后一天
     *
     * @return String(yyyy - MM - dd)
     * @throws Exception all
     */
    public static String getMonthLastDate(String year, String month) throws Exception {
        Calendar cale = Calendar.getInstance();
        //设置年份
        cale.set(Calendar.YEAR, Integer.parseInt(year));
        //设置月份
        cale.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return yMd.format(cale.getTime()) + " 23:59:59";
    }

    public static String getThisYear() {
        LocalDate now = LocalDate.now();
        return now.getYear() + "";
    }

    public static String getCurrentQuarter() {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int mouth = new Date().getMonth() + 1;

        if (mouth >= 1 && mouth <= 3) {
            return "1.0";
        } else if (mouth >= 4 && mouth <= 6) {
            return "2.0";
        } else if (mouth >= 7 && mouth <= 9) {
            return "3.0";
        } else if (mouth >= 10 && mouth <= 12) {
            return "4.0";
        } else {
            return "";
        }
    }

    public static String getFileName(String img_path) {
        if (StringUtils.isBlank(img_path)) {
            throw new RuntimeException("被解析路径为空");
        }
        String[] info = img_path.split("/");
        return info[info.length - 1];
    }

    public static String getYearFirstDate() {
        return getThisYear() + "-01-01";
    }

    public static int getLimitDate() {
        long _limit = new Date().getTime() - DateUtil.fomatDate(getYearFirstDate()).getTime();
        long limit = _limit / 1000 / 60 / 60 / 24;
        return (int) limit;
    }

    public static int getLimitDate(Date date) {
        long _limit = new Date().getTime() - date.getTime();
        double limit = (double) _limit / 1000 / 60 / 60 / 24;

        BigDecimal bigDecimal = new BigDecimal(limit);
        limit = bigDecimal.setScale(0, RoundingMode.CEILING).intValue();
        return (int) limit;
    }

    public static PageData addDic(PageData pageData) {
        pageData.put("DICTIONARIES_ID",pageData.getString("CATEGORY_ID"));
        pageData.put("NAME",pageData.getString("CATEGORY_NAME"));
        return pageData;
    }

    public static PageData DicToMap(Dictionaries dictionaries) {
        PageData entity = new PageData();
        entity.put("NAME",dictionaries.getNAME());
        entity.put("NAME_EN", dictionaries.getBIANMA());
        entity.put("BIANMA",dictionaries.getBIANMA());
        entity.put("ORDER_BY",dictionaries.getORDER_BY());
        entity.put("PARENT_ID",dictionaries.getPARENT_ID());
        entity.put("BZ",dictionaries.getBZ());
        entity.put("TBSNAME",dictionaries.getTBSNAME());
        entity.put("DICTIONARIES_ID",dictionaries.getDICTIONARIES_ID());
        entity.put("target",dictionaries.getTarget());
        entity.put("dict",dictionaries.getDict());
        if (dictionaries.getSubDict() != null && dictionaries.getSubDict().size() >0){
            entity.put("subDict",dictionaries.getSubDict().stream().map(Warden::DicToMap).collect(Collectors.toList()));
        }
        return entity;
    }
}
