/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.common.constant.CacheNames;
import com.jeeplus.sys.domain.DictType;
import com.jeeplus.sys.domain.DictValue;
import com.jeeplus.sys.mapper.DictTypeMapper;
import com.jeeplus.sys.service.dto.DictTypeDTO;
import com.jeeplus.sys.service.dto.DictValueDTO;
import com.jeeplus.sys.service.mapstruct.DictTypeWrapper;
import com.jeeplus.sys.service.mapstruct.DictValueWrapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据字典Service
 *
 * @author lgf
 * @version 2021-01-16
 */
@Service
@Transactional
public class DictTypeService extends ServiceImpl <DictTypeMapper, DictType> {

    @Autowired
    private DictValueService dictValueService;
    @Autowired
    private DictTypeWrapper dictTypeWrapper;
    @Autowired
    private DictValueWrapper dictValueWrapper;

    @Transactional(readOnly = true)
    public DictTypeDTO get(String id) {
        DictTypeDTO dictTypeDTO = dictTypeWrapper.toDTO ( super.getById ( id ) );
        dictTypeDTO.setDictValueDTOList ( dictValueService.lambdaQuery ( ).eq ( DictValue::getDictTypeId, id ).orderByAsc ( DictValue::getSort ).list ( ).stream ( ).map ( dictValueWrapper::toDTO ).collect ( Collectors.toList ( ) ) );
        return dictTypeDTO;
    }


    @CacheEvict(value = CacheNames.SYS_CACHE_DICT_MAP, allEntries = true)
    public boolean saveOrUpdate(DictType dictType) {
        return super.saveOrUpdate ( dictType );
    }

    @CacheEvict(value = CacheNames.SYS_CACHE_DICT_MAP, allEntries = true)
    public void saveDictValue(DictValue dictValue) {
        dictValueService.saveOrUpdate ( dictValue );
    }

    @CacheEvict(value = CacheNames.SYS_CACHE_DICT_MAP, allEntries = true)
    public void batchDeleteDictValue(String[] ids) {
        dictValueService.removeByIds ( Lists.newArrayList ( ids ) );
    }

    @CacheEvict(value = CacheNames.SYS_CACHE_DICT_MAP, allEntries = true)
    public void delete(String id) {
        super.removeById ( id );
        dictValueService.lambdaUpdate ( ).eq ( DictValue::getDictTypeId, id ).remove ( );
    }

    @Cacheable(CacheNames.SYS_CACHE_DICT_MAP)
    public List <DictTypeDTO> getDict() {
        List <DictTypeDTO> list = dictTypeWrapper.toDTO ( super.list ( ) );
        list.forEach ( dictTypeDTO -> {
            List <DictValueDTO> dictValueDTOs = dictValueService.lambdaQuery ( ).eq ( DictValue::getDictTypeId, dictTypeDTO.getId ( ) ).orderByAsc ( DictValue::getSort ).list ( ).stream ( ).map ( dictValueWrapper::toDTO ).collect ( Collectors.toList ( ) );
            dictTypeDTO.setDictValueDTOList ( dictValueDTOs );
        } );
        return list;
    }

    /**
     * 根据value获取集合
     * @param value
     * @return
     */
    @Cacheable(CacheNames.SYS_CACHE_DICT_MAP)
    public List<DictValue> getList(String value) {
        List<DictValue> dictValues=dictValueService.list(new LambdaQueryWrapper<DictValue>().eq(DictValue::getValue,value));
        return dictValues;
    }


    public List<Map<String, Object>> getListData() {
        List<Map<String, Object>> list=new ArrayList<>();
        //首先获取合同类型的大类
        DictTypeDTO dictTypeDTO = this.get("1706920812493086722");
        for (DictValueDTO dictValueDTO : dictTypeDTO.getDictValueDTOList()) {
            Map<String, Object> map=new HashMap<>();
            map.put("value",dictValueDTO.getValue());
            map.put("label",dictValueDTO.getLabel());
            String id = dictValueDTO.getId();
            List<DictValue> listChild = this.getList(id);
            List<Map<String, String>> arrChild=new ArrayList<>();
            for (DictValue dictValue : listChild) {
                Map<String, String> mapChild=new HashMap<>();
                mapChild.put("value",dictValue.getId());
                mapChild.put("label",dictValue.getLabel());
                arrChild.add(mapChild);
            }
            map.put("children",arrChild);
            list.add(map);
        }
        return list;
    }

    /**
     * 用印类型级联
     * @return
     */
    public List<Map<String, Object>> getSealType() {
        List<Map<String, Object>> list=new ArrayList<>();
        //首先获取用印类型的大类
        DictTypeDTO dictTypeDTO = this.get("1713750553254629378");
        for (DictValueDTO dictValueDTO : dictTypeDTO.getDictValueDTOList()) {
            Map<String, Object> map=new HashMap<>();
            //存入到map集合中
            map.put("value",dictValueDTO.getValue());
            map.put("label",dictValueDTO.getLabel());
            String id = dictValueDTO.getId();
            //通过id找到子类
            List<DictValue> listChild = this.getList(id);
            List<Map<String, String>> arrChild=new ArrayList<>();
            //遍历子类
            for (DictValue dictValue : listChild) {
                //存入到list中
                Map<String, String> mapChild=new HashMap<>();
                mapChild.put("value",dictValue.getId());
                mapChild.put("label",dictValue.getLabel());
                arrChild.add(mapChild);
            }
            map.put("children",arrChild);
            list.add(map);
        }
        return list;
    }
}
