mybatis-plus 和 lambda()

    @Override
    public List<String> selectSubject() {
        LambdaQueryWrapper<Faq> lambdaQueryWrapper = new QueryWrapper<Faq>()
                .lambda()
                .select(Faq::getSubject)
                .orderByDesc(Faq::getCreateDate)
                .eq(Faq::getState, StateEnum.NORMAL.getCode());
        List<Faq> faqList = faqMapper.selectList(lambdaQueryWrapper);
        List<String> subjectList = new ArrayList<>();
        for (Faq item : faqList) {
            if (!subjectList.contains(item.getSubject())) {
                subjectList.add(item.getSubject());
            }
        }
        return subjectList;
    }

 

SystemConfigService
package com.bfr.telinksemifourm.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bfr.telinksemifourm.manage.mapper.SystemConfigMapper;
import com.bfr.telinksemifourm.manage.service.ISystemConfigService;
import com.bfr.telinksemifourm.shared.model.Faq;
import com.bfr.telinksemifourm.shared.model.SystemConfig;
import com.bfr.telinksemifourm.shared.model.enums.StateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by .
 * Copyright (c) 2020, All Rights Reserved.
 * 
 */
@Service
@Transactional
public class SystemConfigService implements ISystemConfigService {

    @Autowired
    private SystemConfigMapper systemConfigMapper;

    private static Map SYSTEM_CONFIG;

    @Override
    public Map selectAllConfig() {
        List<SystemConfig> systemConfigs = systemConfigMapper.selectList(null);
        SYSTEM_CONFIG = systemConfigs.stream().filter(systemConfig -> systemConfig.getPid() != 0).collect(Collectors
                .toMap(SystemConfig::getKey, SystemConfig::getValue));
        return SYSTEM_CONFIG;
    }

    // 根据键取值
    @Override
    public SystemConfig selectByKey(String key) {
        QueryWrapper<SystemConfig> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SystemConfig::getKey, key);
        return systemConfigMapper.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new LinkedHashMap<>();
        LambdaQueryWrapper<SystemConfig> lambdaQueryWrapper = new QueryWrapper<SystemConfig>()
                .lambda()
                .eq(SystemConfig::getState, StateEnum.NORMAL.getCode());
        List<SystemConfig> systemConfigs = systemConfigMapper.selectList(lambdaQueryWrapper);
        // 先提取出所有父节点
        List<SystemConfig> p = systemConfigs.stream().filter(systemConfig -> systemConfig.getPid() == 0).collect
                (Collectors.toList());
        // 遍历父节点取父节点下的所有子节点
        p.forEach(systemConfig -> {
            List<SystemConfig> collect = systemConfigs.stream().filter(systemConfig1 -> systemConfig1.getPid().equals
                    (systemConfig.getId())).collect(Collectors.toList());
            map.put(systemConfig.getDescription(), collect);
        });
        return map;
    }

    // 在更新系统设置后,清一下selectAllConfig()的缓存
    @Override
    public void update(List<Map<String, String>> list) {
        for (Map map : list) {
            String key = (String) map.get("name");
            String value = (String) map.get("value");
            // 如果密码没有变动,则不做修改
            if ((key.equals("mail_password") && value.equals("*******")) || (key.equals("redis_password") && value.equals
                    ("*******")) || (key.equals("oauth_github_client_secret") && value.equals("*******"))) {
                continue;
            }
            SystemConfig systemConfig = new SystemConfig();
            systemConfig.setKey(key);
            systemConfig.setValue(value);
            QueryWrapper<SystemConfig> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(SystemConfig::getKey, systemConfig.getKey());
            systemConfigMapper.update(systemConfig, wrapper);
        }
        // 更新SYSTEM_CONFIG
        SYSTEM_CONFIG = null;
    }

    // 根据key更新数据
    @Override
    public void updateByKey(String key, SystemConfig systemConfig) {
        QueryWrapper<SystemConfig> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SystemConfig::getKey, key);
        systemConfigMapper.update(systemConfig, wrapper);
        // 更新SYSTEM_CONFIG
        SYSTEM_CONFIG = null;
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值