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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.mapper.TRBaseCodeMapper;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
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.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础代码表 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-02-18
 */
@Service
public class TRBaseCodeServiceImpl extends BaseServiceImpl<TRBaseCodeMapper, TRBaseCode> implements TRBaseCodeService {

    private static final Map<String, List<TRBaseCode>> CACHE = new ConcurrentHashMap<>();

    @Override
    public Map<String, TRBaseCode> getBaseCodeMapFormCache(String codeTypeId) {
        List<TRBaseCode> baseCodeListFromCache = getBaseCodeListFromCache(codeTypeId);
        return baseCodeListFromCache.stream().collect(Collectors.toMap(TRBaseCode::getCode, each -> each));
    }

    @Override
    public List<TRBaseCode> getBaseCodeListFromCache(String codeTypeId) {
        List<TRBaseCode> trBaseCodes = CACHE.get(codeTypeId);
        if (trBaseCodes != null) {
            return JSON.parseArray(JSON.toJSONString(trBaseCodes), TRBaseCode.class);
        }
        flushCache(codeTypeId);
        return getBaseCodeListFromCache(codeTypeId);
    }

    @Override
    public void flushCache(String codeTypeId) {
        if (codeTypeId == null) {
            List<TRBaseCode> allCodes = list(new LambdaQueryWrapper<TRBaseCode>().orderByAsc(TRBaseCode::getCodeid, TRBaseCode::getCode));

            codeTypeId = allCodes.get(0).getCodeid();
            List<TRBaseCode> tempList = new ArrayList<>();

            // foreach build cache
            for (TRBaseCode each : allCodes) {
                if (!each.getCodeid().equals(codeTypeId)) {
                    CACHE.put(codeTypeId, tempList);
                    tempList = new ArrayList<>();
                    codeTypeId = each.getCodeid();
                }
                tempList.add(each);
            }

            CACHE.put(codeTypeId, tempList);
        } else {
            List<TRBaseCode> codes = list(new LambdaQueryWrapper<TRBaseCode>().eq(TRBaseCode::getCodeid, codeTypeId).orderByAsc(TRBaseCode::getCodeid, TRBaseCode::getCode));

            if (codes == null) {
                codes = new ArrayList<>();
            }

            CACHE.put(codeTypeId, codes);
        }
    }

    @Override
    public String getRegorgName(String code) {
        if(StringUtils.isNotEmpty(code)){
            String regorgName = baseMapper.getRegorgName(code);
            if(StringUtils.isNotEmpty(regorgName)){
                return regorgName;
            }
        }
        return null;
    }
    @Override
    public String getSuporg(String code) {
        if(StringUtils.isNotEmpty(code)){
            String regorgName = baseMapper.getRegorgName(code);
            if(StringUtils.isNotEmpty(regorgName)){
                return regorgName;
            }
        }
        return null;
    }

    @Override
    public Map<String, String> queryOrgName() {
        Map<String, String> rs = new HashMap();
        List<Map> list = baseMapper.queryOrgName();
        for (Map map : list) {
            String orgNumber = map.get("orgNumber").toString();
            String orgUnitName = map.get("orgUnitName").toString();
            rs.put(orgNumber, orgUnitName);
        }
        return rs;
    }

    @Override
    public Map<String, String> queryEntType() {
        Map<String, String> rs = new HashMap<>();
        List<Map> list = baseMapper.queryEntType();
        for (Map map : list) {
            String ENTTYPEID = map.get("ENTTYPEID").toString();
            String DISPLAYVALUE = map.get("DISPLAYVALUE").toString();
            rs.put(ENTTYPEID, DISPLAYVALUE);
        }
        return rs;
    }

    @Override
    public List<TRBaseCode> listXrbasecodes(TRBaseCode query) {
        QueryWrapper<TRBaseCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TRBaseCode::getCodeid, query.getCodeid());
        return list(queryWrapper);
    }

}
