package com.chinaweal.aiccs.crgs.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.common.enums.DataTypeCodeEnum;
import com.chinaweal.aiccs.crgs.system.entity.TDataTypeCode;
import com.chinaweal.aiccs.crgs.system.mapper.TDataTypeCodeMapper;
import com.chinaweal.aiccs.crgs.system.service.TDataTypeCodeService;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 数据类型码值表 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-04-08
 */
@Service
public class TDataTypeCodeServiceImpl extends BaseServiceImpl<TDataTypeCodeMapper, TDataTypeCode> implements TDataTypeCodeService {

    private final static Map<String, Map<String, TDataTypeCode>> CACHE_MAP = new ConcurrentHashMap<>();

    @Override
    public Map<String, TDataTypeCode> getDataTypeMapFormCache(String type, boolean flash) {
        if(flash){
            // 按类型编码顺序查询所有码值信息
            List<TDataTypeCode> codeList = list(new LambdaQueryWrapper<TDataTypeCode>().orderByAsc(TDataTypeCode::getType));
            // 非空则遍历存储
            if(CollectionUtils.isNotEmpty(codeList)){
                Map<String, TDataTypeCode> atomMap = new HashMap<>();
                String tempType = null;
                for (TDataTypeCode each : codeList) {
                    if(tempType == null) {
                        tempType = each.getType();
                    }
                    if(tempType != null && !tempType.equals(each.getType())){
                        CACHE_MAP.put(tempType, atomMap);
                        tempType = each.getType();
                        atomMap = new HashMap<>();
                    }
                    atomMap.put(each.getCode(), each);
                }
                CACHE_MAP.put(tempType, atomMap);
            }
            if(type != null) {
                Map<String, TDataTypeCode> result = CACHE_MAP.get(type);
                if(result == null){
                    return new HashMap<>();
                }
                return new HashMap<>(result);
            }
        }else{
            AssertUtils.isNotBlank(type);
            return new HashMap<>(CACHE_MAP.get(type));
        }
        return null;
    }

    @Override
    public TDataTypeCode getDataTypeFormCache(String code, String type) {
        AssertUtils.isNotBlank(type, code);
        Map<String, TDataTypeCode> typeCodeMap = CACHE_MAP.get(type);
        if(typeCodeMap == null) {
            return null;
        }
        return typeCodeMap.get(code);
    }

    @Override
    public String getRegulatoryName(String code) {
        TDataTypeCode type = getDataTypeFormCache(code, DataTypeCodeEnum.REGULATORY.getCode());
        if(type == null){
            return null;
        }
        return type.getName();
    }

    @Override
    public String getRegistrationName(String code) {
        TDataTypeCode type = getDataTypeFormCache(code, DataTypeCodeEnum.REGISTRATION.getCode());
        if(type == null){
            return null;
        }
        return type.getName();
    }
}
