package cn.cslg.report.common.utils;

import cn.cslg.report.common.core.base.RedisConf;
import cn.cslg.report.common.model.dto.CommonData;
import cn.cslg.report.common.model.dto.ProjectFieldOrderDTO;
import cn.cslg.report.common.model.vo.PersonnelVO;
import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class CacheUtils {

    @Resource
    private RedisUtil redisUtil;

    public void setCommonData(CommonData commonData) {
        redisUtil.set(RedisConf.COMMON_DATA, JsonUtils.objectToJson(commonData));
    }

    public CommonData getCommonData() {
        String json = redisUtil.get(RedisConf.COMMON_DATA);
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            return JsonUtils.jsonToPojo(json, CommonData.class);
        }
    }

    //    public void setLoginUser(User user) {
//        redisUtil.set(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + user.getId(), JsonUtils.objectToJson(user));
//    }
//
    public PersonnelVO getLoginUser(Object userId) {
        String json = redisUtil.get(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + userId);
        if (StringUtils.isEmpty(json)) {
            throw new NotLoginException("无数据", "user", "");
        } else {
            return com.alibaba.fastjson2.JSONObject.parseObject(json, PersonnelVO.class);
        }
    }

    public PersonnelVO getLoginUserPersonnel(Object userId) {
        String json = redisUtil.get(RedisConf.LOGIN_USER + RedisConf.SYMBOL_COLON + userId);
        if (StringUtils.isEmpty(json)) {
            throw new NotLoginException("无数据", "user", "");
        } else {
            return com.alibaba.fastjson2.JSONObject.parseObject(json, PersonnelVO.class);
        }
    }

    public String getSelectPatentIds(String patentKey) {
        return redisUtil.get(RedisConf.SELECT_PATENT + RedisConf.SYMBOL_COLON + patentKey);
    }

    public Integer getSelectPatentNum(String patentKey) {
        String ids = getSelectPatentIds(patentKey);
        if (StringUtils.isEmpty(ids)) {
            return 0;
        }
        return StringUtils.changeStringToInteger(ids, ",").size();
    }

//    public void setAnalysisCountV2(String patentKey, Integer field, Integer expand, String value, String ids) {
//        String md5 = SecureUtil.md5(field + RedisConf.SYMBOL_COLON + expand + RedisConf.SYMBOL_COLON + patentKey);
//        String key = RedisConf.ANALYSIS_COUNT + RedisConf.SYMBOL_COLON + md5;
//        if (StringUtils.isNotEmpty(value) && StringUtils.isNotEmpty(ids)) {
//            redisUtil.hPut(key, value, ids);
//            redisUtil.expire(key, 7200, TimeUnit.SECONDS);
//        }
//    }
//
//    public Object getAnalysisCountIdsV2(String patentKey, Integer field, Integer expand, String name) {
//        String md5 = SecureUtil.md5(field + RedisConf.SYMBOL_COLON + expand + RedisConf.SYMBOL_COLON + patentKey);
//        String key = RedisConf.ANALYSIS_COUNT + RedisConf.SYMBOL_COLON + md5;
//        return redisUtil.hGet(key, name);
//    }
//
//    public List<AnalysisItemResultDTO> getAnalysisCountV2(String patentKey, Integer field, Integer expand) {
//        List<AnalysisItemResultDTO> resultList = new ArrayList<>();
//        String md5 = SecureUtil.md5(field + RedisConf.SYMBOL_COLON + expand + RedisConf.SYMBOL_COLON + patentKey);
//        String key = RedisConf.ANALYSIS_COUNT + RedisConf.SYMBOL_COLON + md5;
//        Map<Object, Object> result = redisUtil.hGetAll(key);
//        for (Object name : result.keySet()) {
//            Object value = result.get(name);
//            if (StringUtils.isNotNull(value)) {
//                List<Integer> ids = StringUtils.changeStringToInteger(value.toString(), ",");
//                AnalysisItemResultDTO analysisItemResultDTO = new AnalysisItemResultDTO();
//                analysisItemResultDTO.setIds(value.toString());
//                analysisItemResultDTO.setName(name.toString());
//                analysisItemResultDTO.setCount(ids.size());
//                resultList.add(analysisItemResultDTO);
//            }
//        }
//        return resultList;
//    }

    public List<Tree> setAreaTreeList(List<Tree<Integer>> treeList) {
        redisUtil.set(RedisConf.COMMON_DATA + RedisConf.SYMBOL_COLON + RedisConf.AREA_LIST, JsonUtils.objectToJson(treeList));
        return this.getAreaTreeList();
    }

    public List<Tree> getAreaTreeList() {
        String json = redisUtil.get(RedisConf.COMMON_DATA + RedisConf.SYMBOL_COLON + RedisConf.AREA_LIST);
        if (StringUtils.isNotEmpty(json)) {
            return JsonUtils.jsonToList(json, Tree.class);
        }
        return null;
    }

    public String setSelectPatentIds(List<Integer> patentIds) {
        String uuid = IdUtil.simpleUUID();
        redisUtil.setEx(RedisConf.SELECT_PATENT + RedisConf.SYMBOL_COLON + uuid, StringUtils.join(patentIds, ","), 43200, TimeUnit.SECONDS);
        return uuid;
    }

    public void deleteUserSystemFieldSetting(Integer projectId, String type, String view, Integer userId) {
        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + type + RedisConf.SYMBOL_COLON + view + RedisConf.SYMBOL_COLON + userId);
        redisUtil.delete(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key);
    }

//    public void setUserSystemFieldSetting(Integer projectId, String type, String view, Integer userId, List<PatentField> defaultField) {
//        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + type + RedisConf.SYMBOL_COLON + view + RedisConf.SYMBOL_COLON + userId);
//        redisUtil.set(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(defaultField));
//    }

//    public List<PatentField> getUserSystemFieldSetting(Integer projectId, String type, String view, Integer userId) {
//        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + type + RedisConf.SYMBOL_COLON + view + RedisConf.SYMBOL_COLON + userId);
//        String json = redisUtil.get(RedisConf.USER_FIELD + RedisConf.SYMBOL_COLON + key);
//        if (StringUtils.isNotEmpty(json)) {
//            return JsonUtils.jsonToList(json, PatentField.class);
//        }
//        return new ArrayList<>();
//    }

    public List<ProjectFieldOrderDTO> getProjectFieldOrder(Integer projectId, Integer userId) {
        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + userId);
        String json = redisUtil.get(RedisConf.FIELD_ORDER + RedisConf.SYMBOL_COLON + key);
        if (StringUtils.isNotEmpty(json)) {
            return JsonUtils.jsonToList(json, ProjectFieldOrderDTO.class);
        }
        return new ArrayList<>();
    }

    public void setProjectFieldOrder(Integer projectId, Integer userId, List<ProjectFieldOrderDTO> data) {
        String key = SecureUtil.md5(projectId + RedisConf.SYMBOL_COLON + userId);
        redisUtil.set(RedisConf.FIELD_ORDER + RedisConf.SYMBOL_COLON + key, JsonUtils.objectToJson(data));
    }

    public void setUserImportId(Integer userId, Integer importId) {
        redisUtil.set(RedisConf.USER_IMPORT + RedisConf.SYMBOL_COLON + userId, String.valueOf(importId));
    }

    public String getUserImportId(Integer userId) {
        return redisUtil.get(RedisConf.USER_IMPORT + RedisConf.SYMBOL_COLON + userId);
    }

    public void deleteUserImport(Integer userId) {
        redisUtil.delete(RedisConf.USER_IMPORT + RedisConf.SYMBOL_COLON + userId);
    }
}
