StoreServiceImpl.java 8.6 KB
package vion.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vion.Global;
import vion.dto.StatusDTO;
import vion.dto.StoreDTO;
import vion.mapper.ServiceOrderMapper;
import vion.mapper.StoreMapper;
import vion.mapper.TaskMapper;
import vion.model.*;
import vion.service.*;
import vion.vo.StoreVO;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class StoreServiceImpl extends MPJBaseServiceImpl<StoreMapper, Store> implements IStoreService {

    private final IAccountService accountService;
    private final IRContractStoreService contractStoreService;
    private final IContractService contractService;
    private final IFileService fileService;
    private final Converter converter;
    // 引入 taskService 会循环依赖
    private final TaskMapper taskMapper;
    private final ServiceOrderMapper serviceOrderMapper;

    @Value("${fileUrl:}")
    private String fileUrl;

    @Override
    public Page<StoreVO> getStoreList(StoreDTO data) {
        Store store = converter.convert(data, new Store());
        String name = store.getName();
        store.setName(null);
        Page<Store> storeList = this.lambdaQuery(store)
                .like(StringUtils.isNotBlank(name), Store::getName, name)
                .between(data.getStartdate() != null && data.getEnddate() != null, Store::getOrderdate, data.getStartdate(), data.getEnddate())
                .page(Page.of(data.getPageNum(), data.getPageSize()));
        // todo 缓存
        List<Account> accountList = accountService.list();
        List<FileInfo> fileInfoList = fileService.lambdaQuery().eq(FileInfo::getSourceType, 1).list();
        Map<Long, Long> store2CntMap = fileInfoList.stream().collect(Collectors.groupingBy(FileInfo::getStoreId, Collectors.counting()));

        List<Long> storeIdList = Opt.ofEmptyAble(storeList.getRecords())
                .map(recs -> recs.stream().map(Store::getId).collect(Collectors.toList()))
                .orElse(ListUtil.empty());
        Map<Long, List<RContractStore>> storeIdMap = Opt.ofEmptyAble(storeIdList)
                .map(l -> contractStoreService.lambdaQuery().in(RContractStore::getStoreId, l).list())
                .map(contractStoreList -> contractStoreList.stream().collect(Collectors.groupingBy(RContractStore::getStoreId)))
                .orElse(MapUtil.empty());
        Map<Long, Contract> id2ContractMap = Opt.ofEmptyAble(storeIdMap.values())
                .map(l -> l.stream().flatMap(List::stream).collect(Collectors.toList()))
                .filter(CollUtil::isNotEmpty)
                .map(contractStoreList -> {
                    List<Contract> contractList = contractService.lambdaQuery().in(Contract::getId, contractStoreList.stream().map(RContractStore::getContractId).collect(Collectors.toList())).list();
                    return contractList.stream().collect(Collectors.toMap(Contract::getId, Function.identity()));
                })
                .orElse(MapUtil.empty());

        Map<Long, List<Long>> storeTaskIdMap = Opt.ofEmptyAble(storeIdList)
                .map(l -> taskMapper.selectList(Wrappers.lambdaQuery(Task.class).in(Task::getStoreId, l))
                        .stream()
                        .collect(Collectors.groupingBy(Task::getStoreId, Collectors.mapping(Task::getId, Collectors.toList()))))
                .orElse(MapUtil.empty());
        List<Long> taskIds = storeTaskIdMap.values().stream()
                .flatMap(List::stream)
                .collect(Collectors.toList());
        Map<Long, List<ServiceOrder>> taskServiceOrderMap = Opt.ofEmptyAble(taskIds)
                .map(t -> serviceOrderMapper.selectList(Wrappers.lambdaQuery(ServiceOrder.class).in(ServiceOrder::getTaskId, taskIds))
                        .stream()
                        .collect(Collectors.groupingBy(ServiceOrder::getTaskId)))
                .orElse(MapUtil.empty());


        List<StoreVO> storeVOList = new ArrayList<>();
        storeList.getRecords().forEach(item -> {
            StoreVO storeVO = converter.convert(item, new StoreVO());
            storeVO.setAccountName(accountList.stream().filter(v -> v.getId().equals(item.getAccountId())).map(Account::getName).findFirst().orElse("--"));
            storeVO.setFileNum(store2CntMap.getOrDefault(item.getId(), 0L));

            Long storeId = item.getId();
            List<RContractStore> contractStores = storeIdMap.get(storeId);
            if (CollUtil.isEmpty(contractStores)) {
                storeVO.setMaintainStatus("--");
            } else {
                List<Long> contractIds = contractStores.stream().map(RContractStore::getContractId).collect(Collectors.toList());
                Set<Boolean> maintainStatusSet = new HashSet<>();
                contractIds.forEach(id -> {
                    Contract contract = id2ContractMap.get(id);
                    Date maintainSdate = contract.getMaintainSdate();
                    Date maintainEdate = contract.getMaintainEdate();
                    if (ArrayUtil.isAllNotNull(maintainSdate, maintainEdate)) {
                        boolean isIn = DateUtil.isIn(new Date(), maintainSdate, maintainEdate);
                        maintainStatusSet.add(isIn);
                    }
                });
                storeVO.setMaintainStatus(CollUtil.isEmpty(maintainStatusSet) ? "--" : maintainStatusSet.contains(true) ? "在保" : "脱保");
                storeVO.setContractCount(contractIds.size());
            }
            // 补充服务单
            if (storeTaskIdMap.containsKey(item.getId())) {
                List<ServiceOrder> serviceOrders = storeTaskIdMap.get(item.getId())
                        .stream().flatMap(taskId -> taskServiceOrderMap.getOrDefault(taskId, ListUtil.empty()).stream())
                        .collect(Collectors.toList());
                storeVO.setServiceOrderCount(serviceOrders.size());
            }

            storeVOList.add(storeVO);
        });
        return Page.<StoreVO>of(data.getPageNum(), data.getPageSize(), storeList.getTotal()).setRecords(storeVOList);
    }

    @Override
    public String updateStoreStage(StatusDTO statusDTO, String token) {
        this.update(Wrappers.<Store>lambdaUpdate()
                .set(Store::getProjectStage, statusDTO.getProjectStage())
                .eq(Store::getId, statusDTO.getSourceId()));

        Opt.ofNullable(statusDTO.getFiles()).ifPresent(tmpFiles -> {
            for (MultipartFile infile : tmpFiles) {
                //上传url地址
                String orgName = infile.getOriginalFilename();
                String fileName = orgName.substring(0, orgName.lastIndexOf("."));
                String fileExt = orgName.substring(orgName.lastIndexOf("."));
                String filename = fileName + "_" + DateUtil.format(new Date(), "yyyyMMdd_HHmmss") + fileExt;
                String path = fileUrl + FileUtil.FILE_SEPARATOR + statusDTO.getStoreId() + FileUtil.FILE_SEPARATOR + statusDTO.getSourceId() + FileUtil.FILE_SEPARATOR + filename;
                File file = FileUtil.touch(path);
                try {
                    infile.transferTo(file);
                } catch (IOException e) {
                    log.error("保存文件出错", e);
                }

                FileInfo tempFileInfo = new FileInfo();
                tempFileInfo.setStoreId(statusDTO.getStoreId());
                tempFileInfo.setSourceId(statusDTO.getSourceId());
                tempFileInfo.setSourceType(statusDTO.getSourceType());
                tempFileInfo.setName(filename);
                tempFileInfo.setUrl(path);
                tempFileInfo.setType(FileUtil.extName(file));
                tempFileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
                tempFileInfo.setUploader(Global.USERNAME_MAP.get(token).getUsername());

                fileService.save(tempFileInfo);
            }
        });
        return "更新成功";
    }
}