package com.inspect.system.service.impl;

import com.inspect.base.core.exception.ServiceException;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.common.security.utils.DictUtils;
import com.inspect.system.base.domain.SysDictData;
import com.inspect.system.base.domain.SysDictType;
import com.inspect.system.mapper.SysDictDataMapper;
import com.inspect.system.mapper.SysDictTypeMapper;
import com.inspect.system.service.ISysDictTypeService;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysDictTypeServiceImpl implements ISysDictTypeService {
    private final SysDictTypeMapper dictTypeMapper;
    private final SysDictDataMapper dictDataMapper;

    public SysDictTypeServiceImpl(SysDictTypeMapper dictTypeMapper, SysDictDataMapper dictDataMapper) {
        this.dictTypeMapper = dictTypeMapper;
        this.dictDataMapper = dictDataMapper;
    }

    @PostConstruct
    public void init() {
        this.loadingDictCache();
    }

    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        return this.dictTypeMapper.selectDictTypeList(dictType);
    }

    public List<SysDictType> selectDictTypeAll() {
        return this.dictTypeMapper.selectDictTypeAll();
    }

    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        } else {
            dictDatas = this.dictDataMapper.selectDictDataByType(dictType);
            if (StringUtils.isNotEmpty(dictDatas)) {
                DictUtils.setDictCache(dictType, dictDatas);
                return dictDatas;
            } else {
                return null;
            }
        }
    }

    public SysDictType selectDictTypeById(Long dictId) {
        return this.dictTypeMapper.selectDictTypeById(dictId);
    }

    public SysDictType selectDictTypeByType(String dictType) {
        return this.dictTypeMapper.selectDictTypeByType(dictType);
    }

    public void deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds) {
            SysDictType dictType = this.selectDictTypeById(dictId);
            if (this.dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }

            this.dictTypeMapper.deleteDictTypeById(dictId);
            DictUtils.removeDictCache(dictType.getDictType());
        }

    }

    public void loadingDictCache() {
        SysDictData dictData = new SysDictData();
        dictData.setStatus("0");
        Map<String, List<SysDictData>> dictDataMap = (Map) this.dictDataMapper.selectDictDataList(dictData).stream().collect(Collectors.groupingBy(SysDictData::getDictType));

        for (Entry<String, List<SysDictData>> stringListEntry : dictDataMap.entrySet()) {
            Entry<String, List<SysDictData>> entry = (Entry) stringListEntry;
            DictUtils.setDictCache(entry.getKey(), (List) ((List) entry.getValue()).stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList()));
        }

    }

    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    public void resetDictCache() {
        this.clearDictCache();
        this.loadingDictCache();
    }

    public int insertDictType(SysDictType dict) {
        int row = this.dictTypeMapper.insertDictType(dict);
        if (row > 0) {
            DictUtils.setDictCache(dict.getDictType(), (List) null);
        }

        return row;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    public int updateDictType(SysDictType dict) {
        SysDictType oldDict = this.dictTypeMapper.selectDictTypeById(dict.getDictId());
        this.dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        int row = this.dictTypeMapper.updateDictType(dict);
        if (row > 0) {
            List<SysDictData> dictDatas = this.dictDataMapper.selectDictDataByType(dict.getDictType());
            DictUtils.setDictCache(dict.getDictType(), dictDatas);
        }

        return row;
    }

    public String checkDictTypeUnique(SysDictType dict) {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        SysDictType dictType = this.dictTypeMapper.checkDictTypeUnique(dict.getDictType());
        return StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue() ? "1" : "0";
    }
}
