package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.SysDictionaryInfoDTO;
import com.diagbot.entity.SysDictionaryInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.SysDictionaryInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

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

/**
 * @Description:
 * @Author:zhoutg
 * @time: 2018/11/23 11:37
 */
@Component
public class SysDictionaryFacade extends SysDictionaryInfoServiceImpl {

    /**
     * 返回字典信息
     *
     * @return
     */
    public Map<Long, List<SysDictionaryInfoDTO>> getDictionary() {
        List<SysDictionaryInfo> list = this.list(new QueryWrapper<SysDictionaryInfo>()
                .in("return_type", ListUtil.arrayToList(new Long[] { 0L, 2L }))
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .orderByAsc("group_type", "order_no"));
        List<SysDictionaryInfoDTO> listRes = BeanUtil.listCopyTo(list, SysDictionaryInfoDTO.class);
        return EntityUtil.makeEntityListMap(listRes, "groupType");
    }


    /**
     * 返回字典信息
     *
     * @return
     */
    @Cacheable(value = "cache", key = "'qcdict'")
    public Map<String, Map<String, String>> getDictionaryWithKey() {
        Map<String, Map<String, String>> res = new HashMap<>();
        List<SysDictionaryInfo> list = this.list(new QueryWrapper<SysDictionaryInfo>()
                .in("return_type", ListUtil.arrayToList(new Long[] { 0L, 2L }))
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .orderByAsc("group_type", "order_no"));
        Map<Long, List<SysDictionaryInfo>> mapSource = EntityUtil.makeEntityListMap(list, "groupType");
        for (Long group : mapSource.keySet()) {
            Map<String, String> m = new HashMap<>();
            for (SysDictionaryInfo dic : mapSource.get(group)) {
                m.put(dic.getName(), dic.getVal());
            }
            res.put(String.valueOf(group), m);
        }
        return res;
    }
    public long getAccessTokenOutTime() {
        long accessToken = 24 * 3600*1l;
        if (getDictionaryWithKey() != null
                && getDictionaryWithKey().containsKey("31")
                && getDictionaryWithKey().get("31").containsKey("accessToken")) {
            accessToken = Long.parseLong(getDictionaryWithKey().get("31").get("accessToken"));
        }
        return accessToken;
    }
}
