package com.chushang.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.system.entity.dto.ListDictDTO;
import com.chushang.system.entity.po.SysDictData;
import com.chushang.system.mapper.SysDictDataMapper;
import com.chushang.system.service.SysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author by zhaowenyuan create 2022/8/26 11:42
 */
@Slf4j
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    @Autowired
    RedissonClient redissonClient;

    String SYS_DICT_KEY = "sys_dict:";

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        RBucket<List<SysDictData>> bucket = redissonClient.getBucket(getCacheKey(dictType));
        List<SysDictData> dictDataList = bucket.get();
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        List<SysDictData> list = list(new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictType, dictType)
                .eq(SysDictData::getStatus, true)
                .orderByAsc(SysDictData::getDictSort)
        );
        if (CollectionUtil.isNotEmpty(list)) {
            bucket.set(list);
        }
        return list;
    }

    @Override
    public PageResult pageDictDataList(ListDictDTO dictData, CommonParam commonParam) {
        IPage<SysDictData> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                buildWrapper(dictData, commonParam)
        );
        return new PageResult(page);
    }

    @Override
    public List<SysDictData> allDictDataList(ListDictDTO dictData, CommonParam commonParam) {
        return this.list(buildWrapper(dictData, commonParam));
    }


    private String getCacheKey(String cacheKey) {
        return SYS_DICT_KEY + cacheKey;
    }

    private LambdaQueryWrapper<SysDictData> buildWrapper(ListDictDTO dictData, CommonParam commonParam) {
        return new QueryWrapper<SysDictData>()
                .orderBy(true, "asc".equals(commonParam.getIsAsc()), commonParam.getOrderBy())
                .lambda()
                .eq(null != dictData.getDictCode(), SysDictData::getDictCode, dictData.getDictCode())
                .eq(StringUtils.isNotEmpty(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType())
                .eq(StringUtils.isNotEmpty(dictData.getCssClass()), SysDictData::getCssClass, dictData.getCssClass())
                .eq(StringUtils.isNotEmpty(dictData.getListClass()), SysDictData::getListClass, dictData.getListClass())
                .eq(StringUtils.isNotEmpty(dictData.getIsDefault()), SysDictData::getIsDefault, dictData.getIsDefault())
                .eq(null != dictData.getStatus(), SysDictData::getStatus, dictData.getStatus());
    }

}
