All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nebula.boxes.mould.fetcher.async.FirmStaffTrunkApi Maven / Gradle / Ivy

The newest version!
package com.nebula.boxes.mould.fetcher.async;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Optional;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.nebula.boxes.mould.entity.FirmStaff;
import com.nebula.boxes.mould.service.IFirmStaffService;
import com.spring.boxes.dollar.$;
import com.spring.boxes.dollar.enums.EnableEnum;
import com.spring.boxes.dollar.support.ContextAwarePoolExecutor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;

@Lazy
@Slf4j
@Service
public class FirmStaffTrunkApi {

    @Autowired
    private IFirmStaffService firmStaffService;

    @Autowired
    private ContextAwarePoolExecutor contextAwarePoolExecutor;

    public CompletableFuture selectOneById(Long id) {
        FirmStaff one = Optional.ofNullable(id)
                .map(x -> firmStaffService.getById(id))
                .orElse(null);
        return CompletableFuture.supplyAsync(() -> one, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectListByIds(Collection ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return CompletableFuture.supplyAsync(ArrayList::new, contextAwarePoolExecutor);
        }
        Collection list = firmStaffService.listByIds(ids);
        return CompletableFuture.supplyAsync(() -> list, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectMapByIds(List ids) {
        Collection list = firmStaffService.listByIds(ids);
        if (CollectionUtils.isEmpty(list)) {
            return CompletableFuture.supplyAsync(HashMap::new, contextAwarePoolExecutor);
        }
        // 针对重复key的  覆盖之前的value
        Map map = list.stream().collect(Collectors.toMap(FirmStaff::getId, Function.identity(), (k, v) -> v));
        return CompletableFuture.supplyAsync(() -> map, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectListByWrapper(QueryWrapper wp) throws Exception {
        List list = this.firmStaffService.list(wp);
        return CompletableFuture.supplyAsync(() -> list, contextAwarePoolExecutor);
    }

    public CompletableFuture selectStaffByUserId(long userId) {
        LambdaQueryWrapper wp = Wrappers.lambdaQuery();
        wp.eq(FirmStaff::getUserId, userId).eq(FirmStaff::getEnabled, EnableEnum.ENABLED.getValue());
        FirmStaff one = firmStaffService.getOne(wp);
        return CompletableFuture.supplyAsync(() -> one, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectStaffListByUserIds(Collection userIds) {
        LambdaQueryWrapper wp = Wrappers.lambdaQuery();
        wp.in(FirmStaff::getUserId, userIds).eq(FirmStaff::getEnabled, EnableEnum.ENABLED.getValue());
        List list = firmStaffService.list(wp);
        return CompletableFuture.supplyAsync(() -> list, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectStaffMapByUserIds(Collection userIds) {
        LambdaQueryWrapper wp = Wrappers.lambdaQuery();
        wp.in(FirmStaff::getUserId, userIds).eq(FirmStaff::getEnabled, EnableEnum.ENABLED.getValue());
        List list = firmStaffService.list(wp);
        if (CollectionUtils.isEmpty(list)) {
            return CompletableFuture.supplyAsync(HashMap::new, contextAwarePoolExecutor);
        }
        // 针对重复key的  覆盖之前的value
        Map map = list.stream().collect(Collectors.toMap(FirmStaff::getUserId, Function.identity(), (k, v) -> v));
        return CompletableFuture.supplyAsync(() -> map, contextAwarePoolExecutor);
    }

    // select firm_id, count(0) count from firm_staff GROUP BY firm_id order by count desc ;
    public CompletableFuture> selectFirmIdGroupBy() {
        QueryWrapper wp = new QueryWrapper<>();
        wp.select("firm_id as item", "count(0) as total").eq("enabled", EnableEnum.ENABLED.getValue());
        wp.groupBy("firm_id").orderByDesc("total");
        List> list = firmStaffService.listMaps(wp);
        return CompletableFuture.supplyAsync(() -> $.getPairMap(list, "item", "total"), contextAwarePoolExecutor);
    }

    public CompletableFuture> selectMonthTotalGroupBy() {
        QueryWrapper wp = new QueryWrapper<>();
        wp.select("DATE_FORMAT(create_time,'%Y/%m') date", "count(0) as total");
        wp.eq("enabled", EnableEnum.ENABLED.getValue());
        wp.groupBy("date").orderByDesc("date");
        List> list = this.firmStaffService.listMaps(wp);
        return CompletableFuture.supplyAsync(() -> $.getPairMap(list, "date", "total"), contextAwarePoolExecutor);
    }

    public CompletableFuture selectStaffTotal(long firmId) {
        QueryWrapper wp = new QueryWrapper<>();
        wp.lambda().eq(FirmStaff::getFirmId, firmId).eq(FirmStaff::getEnabled, EnableEnum.ENABLED.getValue());
        long total = firmStaffService.count(wp);
        return CompletableFuture.supplyAsync(() -> total, contextAwarePoolExecutor);
    }

    public CompletableFuture selectFirmStaffTotal() {
        QueryWrapper wp = new QueryWrapper<>();
        wp.lambda().eq(FirmStaff::getEnabled, EnableEnum.ENABLED.getValue());
        long total = firmStaffService.count(wp);
        return CompletableFuture.supplyAsync(() -> total, contextAwarePoolExecutor);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy