InspectServiceImpl.java 3.23 KB
package vion.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Opt;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vion.dto.InspectDTO;
import vion.mapper.InspectMapper;
import vion.model.Account;
import vion.model.Form;
import vion.model.Inspect;
import vion.model.Store;
import vion.service.IFormService;
import vion.service.IInspectService;
import vion.vo.FormVO;
import vion.vo.InspectVO;

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

@Service
@RequiredArgsConstructor
public class InspectServiceImpl extends MPJBaseServiceImpl<InspectMapper, Inspect> implements IInspectService {

    private final IFormService formService;
    private final Converter converter;

    @Override
    public Page<InspectVO> getInspectList(InspectDTO data) {
        MPJLambdaWrapper<Inspect> wrapper = new MPJLambdaWrapper<>(converter.convert(data, Inspect.class))
                .selectAll(Inspect.class)
                .selectAs(Account::getName, InspectVO::getAccountName)
                .selectAs(Store::getName, InspectVO::getStoreName)
                .leftJoin(Account.class, Account::getId, Inspect::getAccountId)
                .leftJoin(Store.class, Store::getId, Inspect::getStoreId)
                .between(data.getStartdate() != null && data.getEnddate() != null, Inspect::getInspectDate, data.getStartdate(), data.getEnddate())
                .orderByDesc(Inspect::getCreateTime);

        Page<InspectVO> inspectVOPage = this.selectJoinListPage(Page.of(data.getPageNum(), data.getPageSize()), InspectVO.class, wrapper);

        Opt.ofEmptyAble(inspectVOPage.getRecords())
                .map(l -> l.stream().map(InspectVO::getId).collect(Collectors.toList()))
                .map(idList -> formService.lambdaQuery().eq(Form::getSourceId, 2).in(Form::getPid, idList).list())
                .filter(CollUtil::isNotEmpty)
                .ifPresent(formList -> {
                    Map<Long, List<FormVO>> inspectId2FormIdMap = formList.stream().collect(Collectors.groupingBy(Form::getPid, Collectors.mapping(form -> converter.convert(form, FormVO.class), Collectors.toList())));
                    inspectVOPage.getRecords().forEach(inspectVO -> {
                        inspectVO.setFormList(inspectId2FormIdMap.getOrDefault(inspectVO.getId(), ListUtil.empty()));
                    });
                });
        return inspectVOPage;
    }

    @Override
    public InspectVO getById(Long id) {
        MPJLambdaWrapper<Inspect> wrapper = new MPJLambdaWrapper<Inspect>()
                .selectAll(Inspect.class)
                .selectAs(Account::getName, InspectVO::getAccountName)
                .selectAs(Store::getName, InspectVO::getStoreName)
                .leftJoin(Account.class, Account::getId, Inspect::getAccountId)
                .leftJoin(Store.class, Store::getId, Inspect::getStoreId)
                .eq(Inspect::getId, id);
        return this.selectJoinOne(InspectVO.class, wrapper);
    }
}