package com.muchinfo.mtp3century.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.muchinfo.mtp3century.service.IRedisInitService;
import com.muchinfo.mtp3century.utils.RedisServiceUtil;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3interface.mapper.IDividefeedescriptionMapper;
import com.muchinfo.mtp3interface.mapper.IEnumdicitemMapper;
import com.muchinfo.mtp3interface.mapper.IErrorcodeMapper;
import com.muchinfo.mtp3interface.mapper.ISystemparamMapper;
import com.muchinfo.mtp3pojos.entity.Dividefeedescription;
import com.muchinfo.mtp3pojos.entity.Enumdicitem;
import com.muchinfo.mtp3pojos.entity.Errorcode;
import com.muchinfo.mtp3pojos.entity.Systemparam;
import com.muchinfo.mtp3pojos.vo.ProtoErrorCodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

@Service
public class RedisInitServiceImpl implements IRedisInitService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RedisServiceUtil redisServiceUtil;
    @Resource
    private IErrorcodeMapper iErrorcodeMapper;
    @Resource
    private ISystemparamMapper iSystemparamMapper;
    @Resource
    private IEnumdicitemMapper iEnumdicitemMapper;
    @Resource
    private IDividefeedescriptionMapper iDividefeedescriptionMapper;

    @Override
    public void init() {
        logger.info("开始加载 REDIS 静态缓存--------------------");
        //不需要判断是否已缓存过，缓存过直接更新一下
        try {
            logger.info("开始加载 数据字典--------------------");
            redisServiceUtil.deleteByPrex(Constants.REDIS_HEADER_DICT);
            loadDictionary();
            logger.info("加载 数据字典 完成--------------------");
        } catch (Exception e) {
            logger.error("加载数据字典失败", e);
        }

        try {
            logger.info("开始加载 系统参数--------------------");
            redisServiceUtil.deleteObject(Constants.REDIS_HEADER_SYSPARAM);
            loadSysParam();
            logger.info("加载 系统参数 完成--------------------");
        } catch (Exception e) {
            logger.error("加载系统参数失败", e);
        }

        try {
            logger.info("开始加载 错误码--------------------");
            loadError();
            logger.info("加载 错误码 完成--------------------");
        } catch (Exception e) {
            logger.error("加载错误码失败", e);
        }

        logger.info("加载 REDIS 静态缓存 完成--------------------");
    }

    private void loadError() {
        List<Errorcode> list = iErrorcodeMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(list)) {
            for (Errorcode p : list) {
                ProtoErrorCodes.errorMap.put(p.getErrorcode(), p.getDescription());
            }
        }
    }

    private void loadSysParam() {
        List<Systemparam> list = iSystemparamMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(list)) {
            for (Systemparam p : list) {
                //code是唯一的 ，用hash的方式好取，不用每次都全部取出比较
                redisServiceUtil.setCacheMapValue(Constants.REDIS_HEADER_SYSPARAM, p.getParamcode().trim()
                        , p.getParamvalue() == null ? "" : p.getParamvalue().trim());
            }
        }
    }

    private void loadDictionary() {
        List<Enumdicitem> enumdicitemList = iEnumdicitemMapper.selectList(new QueryWrapper<Enumdicitem>().eq("enumitemstatus", 1));
        if (CollectionUtils.isEmpty(enumdicitemList)) {
            return;
        }
        for (Enumdicitem dict : enumdicitemList) {
            if (dict.getEnumdiccode() == null || dict.getEnumitemname() == null ||
                    dict.getEnumdicname() == null || dict.getEnumitemstatus() == 2) {
                continue;
            }
            redisServiceUtil.setCacheMapValue(Constants.REDIS_HEADER_DICT + dict.getEnumdiccode(),
                    dict.getEnumitemname().toString(), dict.getEnumdicname());
        }
        //处理一级分润费用类型个性化处理
        List<Dividefeedescription> delist = iDividefeedescriptionMapper.selectList(new QueryWrapper<>());
        delist.sort(Comparator.comparing(Dividefeedescription::getDividefeename));
        for (Dividefeedescription dividefeedescription : delist) {
            redisServiceUtil.setCacheMapValue(Constants.REDIS_HEADER_DICT + "dividefeetype",
                    dividefeedescription.getDividefeeid().toString(), dividefeedescription.getDividefeename());
        }
    }
}
