package org.king.modules.ad.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.king.common.constant.CacheConstant;
import org.king.common.util.RedisUtil;
import org.king.modules.ad.entity.AdChargingStatistics;
import org.king.modules.ad.entity.Depart;
import org.king.modules.ad.entity.DepartEcgComNo;
import org.king.modules.ad.mapper.AdDepartMapper;
import org.king.modules.ad.service.IDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 海事机构
 * @Author: king-boot
 * @Date: 2021-06-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class DepartServiceImpl extends ServiceImpl<AdDepartMapper, Depart> implements IDepartService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<AdChargingStatistics> queryJsDepart() {
        return baseMapper.queryJsDepart();
    }

    @Override
    public List<JSONObject> queryDepartAndEcg() {
        return baseMapper.queryDepartAndEcg();
    }

    @Override
    public void putEcgDepartDataToRedis() {
        String ecgKey = CacheConstant.ECG_DEPART;
        String comKey = CacheConstant.COM_NO_DEPART;
        if (!redisUtil.hasKey(ecgKey) || !redisUtil.hasKey(comKey)) {
            List<DepartEcgComNo> list = baseMapper.putEcgDepartDataToRedis();
            Map<String, Object> ecgMap = new HashMap<>();
            Map<String, Object> comMap = new HashMap<>();
            for (DepartEcgComNo departEcgComNo : list) {
                ecgMap.putIfAbsent(departEcgComNo.getEcgNo(), departEcgComNo.getDepartNam());
                if (StrUtil.isNotBlank(departEcgComNo.getComNo())) {
                    comMap.putIfAbsent(departEcgComNo.getComNo(), departEcgComNo.getDepartNam());
                }
            }
            redisUtil.hmset(ecgKey, ecgMap);
            redisUtil.hmset(comKey, comMap);
            log.info("ecg缓存加载数量：{}",ecgMap.size());
            log.info("com缓存加载数量：{}",comMap.size());
        }

    }
}
