package cn.cslg.pas.service;

import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.domain.SystemDict;
import cn.cslg.pas.mapper.SystemDictMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 系统字典 服务类
 * </p>
 *
 * @author 王岩
 * @since 2022-03-30
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class SystemDictService extends ServiceImpl<SystemDictMapper, SystemDict> {

    public List<SystemDict> getSystemDictListByType(List<String> type) {
        if (type == null || type.size() == 0) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SystemDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SystemDict::getType, type);
        return this.list(queryWrapper);
    }

    public String getSystemDictLabelByTypeAndValue(String type, Object value) {
        LambdaQueryWrapper<SystemDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemDict::getType, type);
        queryWrapper.eq(SystemDict::getValue, value);
        SystemDict systemDict = this.getOne(queryWrapper);
        if (systemDict != null) {
            return systemDict.getLabel();
        }
        return "";
    }

    public String getDictMessage() {
        List<SystemDict> systemDictList = this.list();
        Map<String, Object> map = new HashMap<>();
        for (SystemDict systemDict : systemDictList) {
            map.put(systemDict.getType(), systemDictList.stream().filter(item -> item.getType().equals(systemDict.getType())).collect(Collectors.toList()));
        }
        return Response.success(map);
    }

}
