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

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

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


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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nebula.boxes.mould.entity.Firm;
import com.nebula.boxes.mould.service.IFirmService;
import com.spring.boxes.dollar.$;
import com.spring.boxes.dollar.enums.EnableEnum;
import com.spring.boxes.dollar.support.ContextAwarePoolExecutor;
import com.spring.boxes.dollar.support.PaginationPlus;
import com.spring.boxes.dollar.term.PairView;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
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 FirmTrunkApi {

    @Autowired
    private IFirmService firmService;

    @Autowired
    private ContextAwarePoolExecutor contextAwarePoolExecutor;

    public CompletableFuture selectOneById(Long id) {
        Firm one = Optional.ofNullable(id)
                .map(x -> firmService.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 = firmService.listByIds(ids);
        return CompletableFuture.supplyAsync(() -> list, contextAwarePoolExecutor);
    }

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

    public CompletableFuture selectOneByUuid(String uuid) {
        QueryWrapper wp = new QueryWrapper<>();
        wp.lambda().eq(Firm::getUuid, uuid);
        Firm one = firmService.getOne(wp);
        return CompletableFuture.supplyAsync(() -> one, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectListByUuids(Collection uuids) {
        QueryWrapper wp = new QueryWrapper<>();
        wp.lambda().in(Firm::getUuid, uuids);
        List list = firmService.list(wp);
        return CompletableFuture.supplyAsync(() -> list, contextAwarePoolExecutor);
    }

    public CompletableFuture> selectMapByUuids(Collection uuids) {
        QueryWrapper wp = new QueryWrapper<>();
        wp.lambda().in(Firm::getUuid, uuids);
        List list = firmService.list(wp);
        if (CollectionUtils.isEmpty(list)) {
            return CompletableFuture.supplyAsync(HashMap::new, contextAwarePoolExecutor);
        }
        // 针对重复key的  覆盖之前的value
        Map map = list.stream().collect(Collectors.toMap(Firm::getUuid, Function.identity(), (k, v) -> v));
        return CompletableFuture.supplyAsync(() -> map, contextAwarePoolExecutor);
    }

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

    public CompletableFuture>> selectByName(String name, int size) {
        LambdaQueryWrapper wp = Wrappers.lambdaQuery();
        wp.like(Firm::getName, name).or().like(Firm::getAlias, name);
        List list = firmService.selectPagination(PaginationPlus.page(1, size), wp).getItems();
        List> pairs = ListUtils.emptyIfNull(list)
                .stream().map(x -> new PairView<>(x.getId(), x.getName())).collect(Collectors.toList());
        return CompletableFuture.supplyAsync(() -> pairs, contextAwarePoolExecutor);
    }

    public CompletableFuture selectFirmTotal() {
        LambdaQueryWrapper wp = Wrappers.lambdaQuery();
        wp.eq(Firm::getEnabled, EnableEnum.ENABLED.getValue());
        long total = firmService.count(wp);
        return CompletableFuture.supplyAsync(() -> total, contextAwarePoolExecutor);
    }

    // select industry_id, count(0) count from firm GROUP BY industry_id order by count desc ;
    public CompletableFuture>> selectFirmIndustryGroupBy() {
        QueryWrapper wp = new QueryWrapper();
        wp.select("industry_id as item", "count(0) as total");
        wp.groupBy("industry_id").orderByDesc("total");
        List> map = firmService.listMaps(wp);
        return CompletableFuture.supplyAsync(() -> map, 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 = firmService.listMaps(wp);
        return CompletableFuture.supplyAsync(() -> $.getPairMap(list, "date", "total"), contextAwarePoolExecutor);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy