package com.zhiqiyun.open.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhiqiyun.open.config.RedisKeys;
import com.zhiqiyun.open.core.mapper.SystemConfigItemMapper;
import com.zhiqiyun.open.core.mapper.SystemConfigMapper;
import com.zhiqiyun.open.core.models.settings.SystemConfig;
import com.zhiqiyun.open.core.models.settings.SystemConfigItem;
import com.zhiqiyun.open.core.service.SystemConfigService;
import com.zhiqiyun.open.exception.ServiceException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService {
    @Autowired
    private SystemConfigItemMapper systemConfigItemMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<SystemConfigItem> findAllItems(String configKey) {
        QueryWrapper<SystemConfigItem> queryWrapper = new QueryWrapper<>();
        return this.systemConfigItemMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateConfig(SystemConfig config, List<SystemConfigItem> listItems) {
        try {
            this.baseMapper.updateById(config);
            for (SystemConfigItem item : listItems) {
                String redisKey = String.format(RedisKeys.SYSTEM_CONFIG_KEY, item.getId());
                this.stringRedisTemplate.delete(redisKey);
                this.systemConfigItemMapper.updateById(item);
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }


    @Override
    public boolean getBoolValue(String configKey, String itemKey) {
        String itemValue = this.getStringValue(configKey, itemKey);
        try {
            return Boolean.parseBoolean(itemValue);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public BigDecimal getNumberValue(String configKey, String itemKey) {
        String itemValue = this.getStringValue(configKey, itemKey);
        try {
            return new BigDecimal(itemValue);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    @Override
    public String getStringValue(String configKey, String itemKey) {
        String id = DigestUtils.md5Hex(String.format("%s@%s", configKey, itemKey));
        String redisKey = String.format(RedisKeys.SYSTEM_CONFIG_KEY, id);
        String itemValue = this.stringRedisTemplate.opsForValue().get(redisKey);

        if (StringUtils.isBlank(itemValue)) {

            SystemConfigItem item = this.systemConfigItemMapper.selectById(id);
            if (item == null) {
                itemValue = "";
            } else {
                itemValue = item.getItemValue();
            }
            this.stringRedisTemplate.opsForValue().set(redisKey, itemValue, 30, TimeUnit.MINUTES);
        }

        return itemValue;
    }
}
