package com.zcloud.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.PageData;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.UuidUtil;
import org.apache.commons.lang.StringUtils;

import java.util.*;

public class Warden {

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

    public static <T> List<T> getList(String raw, Class<T> clazz) {
        return JSON.parseArray(raw, clazz);
    }

    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("CREATE_TIME", DateUtil.getTime());
        entity.put("CREATOR_NAME", Jurisdiction.getName());
        entity.put("CREATTIME", DateUtil.getTime());
        entity.put("OPERATOR", Jurisdiction.getUSER_ID());
        entity.put("OPERATOR_NAME", Jurisdiction.getName());
        entity.put("OPERAT_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("OPERAT_TIME", DateUtil.getTime());
        entity.put("OPERATTIME", DateUtil.getTime());
        entity.put("ISDELETE", "0");
    }

    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();
        }
    }


}
