package com.hzs.system.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.Constants;
import com.hzs.common.core.enums.ESystemConfig;
import com.hzs.common.core.enums.ETransportType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.domain.EnumEntity;
import com.hzs.common.domain.system.base.BdSystemConfig;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.system.base.mapper.BdSystemConfigMapper;
import com.hzs.system.base.service.IBdSystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 系统配置表 服务实现类
 */
@Service
@Slf4j
public class BdSystemConfigServiceImpl extends ServiceImpl<BdSystemConfigMapper, BdSystemConfig> implements IBdSystemConfigService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private ITransactionCommonService transactionCommonService;

    @Override
    public List<EnumEntity> getTransportTypeEnumEntity(Integer pkCountry) {
        String key = CacheConstants.TRANSPORT_TYPE + pkCountry;
        List<Integer> transportTypeValue = redisService.getCacheList(key);
        if (transportTypeValue == null || transportTypeValue.size() <= 0) {
            LambdaQueryWrapper<BdSystemConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdSystemConfig::getPkCountry, pkCountry);
            List<BdSystemConfig> bdSystemConfigList = baseMapper.selectList(queryWrapper);
            List<Integer> transportList = new ArrayList<>();
            bdSystemConfigList.forEach(systemConfig -> {
                if (systemConfig.getKey().equals(ESystemConfig.IS_LAND_TRANS.getKey()) &&
                        systemConfig.getValue().equals(Integer.toString(EYesNo.YES.getIntValue()))) {
                    transportList.add(ETransportType.LAND.getValue());
                } else if (systemConfig.getKey().equals(ESystemConfig.IS_AIR_TRANS.getKey()) &&
                        systemConfig.getValue().equals(Integer.toString(EYesNo.YES.getIntValue()))) {
                    transportList.add(ETransportType.AIR.getValue());
                } else if (systemConfig.getKey().equals(ESystemConfig.IS_OCEAN_TRANS.getKey()) &&
                        systemConfig.getValue().equals(Integer.toString(EYesNo.YES.getIntValue()))) {
                    transportList.add(ETransportType.OCEAN.getValue());
                }
            });
            if (transportList.size() > 0) {
                redisService.setCacheList(key, transportList);
                redisService.expire(key, Constants.TIMEOUT_DAY, TimeUnit.DAYS);
            }
            transportTypeValue = transportList;
        }
        List<EnumEntity> enumEntities = new ArrayList<>();
        // 获取需要翻译的枚举翻译
        Map<String, String> transactionMap = transactionCommonService.exportEnumTransaction(ETransportType.values());
        for (ETransportType transportType : ETransportType.values()) {
            if (transportTypeValue.contains(transportType.getValue())) {
                EnumEntity enumEntity = new EnumEntity(transportType.getValue(), transactionMap.get(transportType.getKey()), transportType.getKey());
                enumEntities.add(enumEntity);
            }
        }
        return enumEntities;
    }

    @Override
    public List<BdSystemConfig> querySystemConfig(String key, Integer pkCountry) {
        LambdaQueryWrapper<BdSystemConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdSystemConfig::getPkCountry, pkCountry);
        queryWrapper.likeRight(BdSystemConfig::getKey, key);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void updateSystemConfig(BdSystemConfig systemConfig) {
        LambdaQueryWrapper<BdSystemConfig> queryWrapper = getBdSystemConfigQueryWrapper(systemConfig.getPkCountry(), systemConfig.getKey());
        if (baseMapper.update(systemConfig, queryWrapper) > 0) {
            String sysKey = CacheConstants.SYSTEM_CONFIG + systemConfig.getPkCountry();
            String key = CacheConstants.TRANSPORT_TYPE + systemConfig.getPkCountry();
            if (redisService.hasKey(sysKey)) {
                redisService.deleteMapObject(sysKey, systemConfig.getKey());
            }
            redisService.deleteObjectByKey(key);
        }
    }

    private LambdaQueryWrapper<BdSystemConfig> getBdSystemConfigQueryWrapper(Integer pkCountry, String key) {
        LambdaQueryWrapper<BdSystemConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdSystemConfig::getPkCountry, pkCountry);
        queryWrapper.eq(BdSystemConfig::getKey, key);
        return queryWrapper;
    }

    @Override
    public String getSystemConfig(Integer pkCountry, String key) {
        String value = (String) redisService.getCacheMap(CacheConstants.SYSTEM_CONFIG + pkCountry).get(key);
        if (StringUtils.isEmpty(value)) {
            LambdaQueryWrapper<BdSystemConfig> queryWrapper = getBdSystemConfigQueryWrapper(pkCountry, key);
            BdSystemConfig systemConfig = baseMapper.selectOne(queryWrapper);
            if (systemConfig != null) {
                redisService.setCacheMapValue(CacheConstants.SYSTEM_CONFIG + systemConfig.getPkCountry(),
                        systemConfig.getKey(), systemConfig.getValue());
                redisService.expire(CacheConstants.SYSTEM_CONFIG + systemConfig.getPkCountry(), Constants.TIMEOUT_DAY, TimeUnit.DAYS);
                return systemConfig.getValue();
            }
        }
        return value;
    }

    @Override
    public Map<String, String> getSystemConfig(Integer pkCountry) {
        LambdaQueryWrapper<BdSystemConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdSystemConfig::getPkCountry, pkCountry);
        List<BdSystemConfig> bdSystemConfigs = baseMapper.selectList(queryWrapper);
        Map<String, String> newCacheMap = new HashMap<>(ComputeUtil.mapInitCapacity(bdSystemConfigs.size()));
        if (bdSystemConfigs.size() > 0) {
            bdSystemConfigs.forEach(bdSystemConfig -> {
                newCacheMap.put(bdSystemConfig.getKey(), bdSystemConfig.getValue());
            });
        }
        return newCacheMap;
    }
}
