DeliveryRecordServiceImpl.java 6.86 KB
package vion.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.dto.ContractDTO;
import vion.dto.DeliveryRecordDTO;
import vion.mapper.DeliveryRecordMapper;
import vion.model.Contract;
import vion.model.DeliveryRecord;
import vion.model.FileInfo;
import vion.service.IContractService;
import vion.service.IDeliveryRecordService;
import vion.service.IFileService;
import vion.vo.DeliveryRecordVO;
import vion.vo.UserVO;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

/**
 * @author HlQ
 * @date 2023/12/5
 */
@Service
@RequiredArgsConstructor
public class DeliveryRecordServiceImpl extends MPJBaseServiceImpl<DeliveryRecordMapper, DeliveryRecord> implements IDeliveryRecordService {

    private final IFileService fileService;
    private final IContractService contractService;
    private final Converter converter;

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

    @Override
    public Page<DeliveryRecordVO> list(DeliveryRecordDTO dto) {
        MPJLambdaWrapper<DeliveryRecord> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, DeliveryRecord.class))
                .selectAll(DeliveryRecord.class)
                .selectAs(Contract::getName, DeliveryRecordVO::getContractName)
                .selectAs(Contract::getCustomerName, DeliveryRecordVO::getCustomerName)
                .leftJoin(Contract.class, Contract::getContractNo, DeliveryRecord::getContractNo)
                .like(StrUtil.isNotBlank(dto.getContractName()), Contract::getName, dto.getContractName())
                .like(StrUtil.isNotBlank(dto.getCustomerName()), Contract::getCustomerName, dto.getCustomerName())
                .orderByDesc(DeliveryRecord::getCreateTime);
        return this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), DeliveryRecordVO.class, wrapper);
    }

    @Override
    public String save(DeliveryRecordDTO dto) {
        DeliveryRecord record = converter.convert(dto, DeliveryRecord.class);
        if (this.save(record)) {
            ContractDTO contractDTO = new ContractDTO();
            contractDTO.setStatus(2);
            contractService.updateById(null, dto.getContractNo(), contractDTO);

            Opt.ofNullable(dto.getFiles())
                    .ifPresent(fileList -> {
                        UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");

                        Arrays.stream(fileList).forEach(infile -> {
                            String orgName = infile.getOriginalFilename();
                            String mainName = FileUtil.mainName(orgName);
                            String fileExt = FileUtil.extName(orgName);
                            String filename = StrUtil.format("{}_{}.{}", mainName, DateUtil.format(new Date(), "yyyyMMdd_HHmmss"), fileExt);;
                            String path = fileUrl + FileUtil.FILE_SEPARATOR + "delivery" + FileUtil.FILE_SEPARATOR + dto.getContractId() + FileUtil.FILE_SEPARATOR + record.getId() + FileUtil.FILE_SEPARATOR + filename;
                            File file = FileUtil.touch(path);
                            try {
                                infile.transferTo(file);
                            } catch (IOException e) {
                                log.error("保存文件出错", e);
                            }

                            FileInfo fileInfo = new FileInfo();
                            fileInfo.setStoreId(0L);
                            fileInfo.setSourceId(record.getId());
                            fileInfo.setContractId(dto.getContractId());
                            fileInfo.setSourceType(6);
                            fileInfo.setName(filename);
                            fileInfo.setUrl(path);
                            fileInfo.setType(FileUtil.extName(file));
                            fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
                            fileInfo.setUploader(userVO.getUsername());
                            fileService.save(fileInfo);
                        });
                    });
            return "新增成功";
        } else {
            return "新增失败";
        }
    }

    @Override
    public String updateById(DeliveryRecordDTO dto) {
        DeliveryRecord record = converter.convert(dto, DeliveryRecord.class);
        if (this.updateById(record)) {
            DeliveryRecord deliveryRecord = this.getById(dto.getId());
            Opt.ofNullable(dto.getFiles())
                    .ifPresent(fileList -> {
                        UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");

                        Arrays.stream(fileList).forEach(infile -> {
                            String orgName = infile.getOriginalFilename();
                            String mainName = FileUtil.mainName(orgName);
                            String fileExt = FileUtil.extName(orgName);
                            String filename = StrUtil.format("{}_{}.{}", mainName, DateUtil.format(new Date(), "yyyyMMdd_HHmmss"), fileExt);;
                            String path = fileUrl + FileUtil.FILE_SEPARATOR + "delivery" + FileUtil.FILE_SEPARATOR + deliveryRecord.getContractId() + FileUtil.FILE_SEPARATOR + record.getId() + FileUtil.FILE_SEPARATOR + filename;
                            File file = FileUtil.touch(path);
                            try {
                                infile.transferTo(file);
                            } catch (IOException e) {
                                log.error("保存文件出错", e);
                            }

                            FileInfo fileInfo = new FileInfo();
                            fileInfo.setStoreId(0L);
                            fileInfo.setSourceId(record.getId());
                            fileInfo.setContractId(deliveryRecord.getContractId());
                            fileInfo.setSourceType(6);
                            fileInfo.setName(filename);
                            fileInfo.setUrl(path);
                            fileInfo.setType(FileUtil.extName(file));
                            fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
                            fileInfo.setUploader(userVO.getUsername());
                            fileService.save(fileInfo);
                        });
                    });
            return "更新成功";
        } else {
            return "更新失败";
        }
    }
}