package com.tzy.system.service.impl;

import com.tzy.common.constant.Constants;
import com.tzy.common.core.domain.entity.SysDictData;
import com.tzy.common.core.text.Convert;
import com.tzy.system.mapper.SysDictDataMapper;
import com.tzy.system.service.ISysDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典 业务层处理
 * 
 * @author zheng
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;
    // @Resource
    // private RedisTemplate<String, Object> redisTemplate;


    private ISysDictDataService getCurrentProxy() {
        return (ISysDictDataService) AopContext.currentProxy();
    }

    @Override
    public SysDictData groupActs() {
        return dictDataMapper.groupActs();
    }

    /**
     * 根据条件分页查询字典数据
     * 
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     * 
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据
     * 
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(String ids)
    {
        int row = dictDataMapper.deleteDictDataByIds(Convert.toStrArray(ids));
//        if (row > 0)
//        {
//            DictUtils.clearDictCache();
//        }
        return row;
    }
    
    /**
     * 删除字典数据
     * 
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteDictDataById(String dictCode)
    {
        int row = dictDataMapper.deleteDictDataById(Long.parseLong(dictCode));
//        if (row > 0)
//        {
//            DictUtils.clearDictCache();
//        }
        return row;
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictData)
    {
        int row = dictDataMapper.insertDictData(dictData);
//        if (row > 0)
//        {
//            DictUtils.clearDictCache();
//        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData)
    {
        int row = dictDataMapper.updateDictData(dictData);
//        if (row > 0)
//        {
//            DictUtils.clearDictCache();
//        }
        return row;
    }
    /**
     * 查询指定表指定字段
     */
    @Override
    public String selectDictLabels(String table, String dicText , String dicCode, String key) {
        return dictDataMapper.selectDictLabels(table, dicText, dicCode, key);
    }

    @Override
    public List<SysDictData> getByTypeAndStatus(String dictType, String status) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(dictType);
        dictData.setStatus(status);
        return dictDataMapper.selectDictDataList(dictData);
    }

    @Override
    public Boolean checkValueByTypeAndStatus(String type, String value, String status) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(type);
        dictData.setStatus(status);
        dictData.setDictValue(value);
        List<SysDictData> sysDictData = dictDataMapper.selectDictDataList(dictData);
        if(sysDictData.isEmpty()){
            return false;
        }

        return true;
    }

    @Override
    public List<String> getPayTypeByPoint(Integer point) {
        if(point==null){
            point=0;
        }
        return dictDataMapper.getPayTypeByPoint(point);
    }

    @Override
    public List<SysDictData> selectDictDataByType(@NotNull String dictType, String dictLabel, String status ) {
        SysDictData condition = new SysDictData();
        condition.setStatus(status);
        condition.setDictType(dictType);
        condition.setDictLabel(dictLabel);
        return selectDictDataList(condition);
    }

    @Override
    public String selectOneDictDataValueByLabel(@NotNull String dictType, @NotNull String dictLabel){
        return dictDataMapper.selectOneDictDataValueByLabel(dictType, dictLabel);
    }

    /**
     * @param dictType
     * @param dictLabel
     * @param status
     * @param hasCache
     * @return
     */
    @Override
    @Deprecated
    //@Cacheable(cacheNames = "app_dict_cache#600", condition = "#hasCache", key = "{#dictType,#dictLabel,#status}", unless = "#result == null")
    public List<SysDictData> selectDictDataByCache(@NotNull String dictType, String dictLabel, String status, boolean hasCache) {
        // return selectDictDataByType(dictType, dictLabel, status);
        return selectDictDataByCacheV2(dictType, dictLabel, status, hasCache);
    }
    @Override
    @Cacheable(cacheNames = "DICT_DATA_CACHE_KEY#600", condition = "#hasCache", key = "#dictType", unless = "#result == null")
    public List<SysDictData> selectDictDataByCache(@NotNull String dictType, boolean hasCache) {
        return selectDictDataByType(dictType, null, null);
    }

    @Override
    public List<SysDictData> selectDictDataByCacheV2(@NotNull String dictType, String dictLabel, String status, boolean userCache) {
        if (!userCache) {
            return selectDictDataByType(dictType, dictLabel, status);
        }
        List<SysDictData> dataList = getCurrentProxy().selectDictDataByCache(dictType, userCache);
        return dataList.stream().filter(d -> checkDictCondition(dictLabel, status, d)).collect(Collectors.toList());
    }

    private boolean checkDictCondition(String dictLabel, String status, SysDictData dictData) {
        if (StringUtils.isNotEmpty(dictLabel) && !dictLabel.equals(dictData.getDictLabel())) {
            return false;
        }
        return StringUtils.isEmpty(status) || status.equals(dictData.getStatus());
    }

    @Override
    public String getMerchantPointMsg(@NotNull String type) {
        return "";
    }

    //@Cacheable(cacheNames = "merchant_point_show_msg#86400", key = "#type")
    public String getMerchantPointMsgV2(@NotNull String type) {
        List<SysDictData> data = selectDictDataByType(Constants.MERCHANT_POINT_SHOW, type, null);
        if(CollectionUtils.isEmpty(data)){
            return "";
        }
        return data.get(0).getDictValue();
    }

}
