SparePartServiceImpl.java 7.23 KB
package vion.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.dto.SparePartDTO;
import vion.mapper.SparePartMapper;
import vion.model.*;
import vion.service.IFileService;
import vion.service.ISparePartService;
import vion.third.DingMod;
import vion.utils.JsonUtil;
import vion.vo.SparePartVO;
import vion.vo.UserVO;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HlQ
 * @date 2024/1/19
 */
@Service
@RequiredArgsConstructor
public class SparePartServiceImpl extends MPJBaseServiceImpl<SparePartMapper, SparePart> implements ISparePartService {

    private final IFileService fileService;
    private final DingMod dingMod;
    private final Converter converter;

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

    @Override
    public Object frontSubmit(SparePartDTO dto) {
        dto.setUuid(IdUtil.nanoId());
        SparePart sparePart = converter.convert(dto, SparePart.class);
        return this.save(sparePart) ? MapUtil.<String, Long>builder()
                .put("id", sparePart.getId())
                .build() : "提交失败";
    }

    @Override
    public List<SparePartVO> list(SparePartDTO dto) {
        MPJLambdaWrapper<SparePart> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, SparePart.class))
                .selectAll(SparePart.class)
                .selectAs(Contract::getName, SparePartVO::getContractName)
                .selectCollection(RRepairDevice.class, SparePartVO::getRepairDeviceList)
                .leftJoin(Contract.class, Contract::getId, SparePart::getContractId)
                .leftJoin(RRepairDevice.class, on -> on.eq(RRepairDevice::getRId, SparePart::getId).eq(RRepairDevice::getRType, 1))
                .between(SparePart::getCreateTime, dto.getCreateTimeStart(), dto.getCreateTimeEnd())
                .between(ArrayUtil.isAllNotNull(dto.getShipDateStart(), dto.getShipDateEnd()), SparePart::getShipDate, dto.getShipDateStart(), dto.getShipDateEnd())
                .orderByDesc(SparePart::getCreateTime);
        return this.selectJoinList(SparePartVO.class, wrapper);
    }

    @Override
    public SparePartVO getSparePartDetail(Long id, String uuid) {
        MPJLambdaWrapper<SparePart> wrapper = new MPJLambdaWrapper<SparePart>()
                .selectAll(SparePart.class)
                .eq(ObjUtil.isNotEmpty(id), SparePart::getId, id)
                .eq(StrUtil.isNotBlank(uuid), SparePart::getUuid, uuid)
                .orderByDesc(PointInfo::getCreateTime);
        return this.selectJoinOne(SparePartVO.class, wrapper);
    }

    @Override
    public String updById(Long id, SparePartDTO dto) {
        if (this.updateById(converter.convert(dto, SparePart.class))) {
            UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");

            Opt.ofNullable(dto.getFiles())
                    .ifPresent(fileList ->
                            Arrays.stream(fileList).forEach(infile -> {
                                //上传url地址
                                String orgName = infile.getOriginalFilename();
                                String mainName = FileNameUtil.mainName(orgName);
                                String fileExt = FileNameUtil.extName(orgName);
                                String filename = StrUtil.format("{}_{}.{}", mainName,
                                        TimeUtil.format(LocalDateTime.now(), "yyyyMMdd_HHmmssSSS"), fileExt);
                                String path = fileUrl + FileUtil.FILE_SEPARATOR + "sparePart" + FileUtil.FILE_SEPARATOR + id + 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(-1L);
                                fileInfo.setSourceId(id);
                                fileInfo.setSourceType(22);
                                fileInfo.setName(filename);
                                fileInfo.setUrl(path);
                                fileInfo.setType(fileExt);
                                fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
                                fileInfo.setUploader(user.getUsername());
                                fileService.save(fileInfo);
                            }));
            return "更新成功";
        }
        return "更新失败";
    }

    @Override
    public Boolean notify(Long id, String userid) {
        MPJLambdaWrapper<SparePart> wrapper = new MPJLambdaWrapper<SparePart>()
                .selectAll(SparePart.class)
                .selectCollection(RRepairDevice.class, SparePartVO::getRepairDeviceList)
                .leftJoin(RRepairDevice.class, on -> on.eq(RRepairDevice::getRId, SparePart::getId).eq(RRepairDevice::getRType, 1))
                .eq(SparePart::getId, id);
        var sparePartVO = this.selectJoinOne(SparePartVO.class, wrapper);

        var deviceNoStr = Opt.ofEmptyAble(sparePartVO.getRepairDeviceList())
                .map(l -> l.stream().map(RRepairDevice::getDeviceNo).collect(Collectors.joining(",")))
                .orElse("暂无");

        var jsonObj = JsonUtil.createObj()
                .put("agent_id", 2358374016L)
                .put("userid_list", userid);

        var content = JsonUtil.createObj().put("title", "备件已发货");
        String text = StrUtil.format("""
                        ### 备件已发货
                        ### 项目名称:{}
                        ### 快递单号:{}
                        ### 借出设备序列号:{}
                        ### 发货日期:{}
                        ### 发送时间:{}
                        """, sparePartVO.getProjectName(), Opt.ofBlankAble(sparePartVO.getTrackingNumber()).orElse("暂无"),
                deviceNoStr,
                Opt.ofBlankAble(sparePartVO.getShipDate().toString()).orElse("暂无"), TimeUtil.formatNormal(LocalDateTime.now()));
        content.put("text", text);

        var msg = JsonUtil.createObj().put("msgtype", "markdown").set("markdown", content);
        jsonObj.set("msg", msg);

        var pushRes = dingMod.workMsg(jsonObj.toString());
        return StrUtil.containsIgnoreCase(pushRes, "ok");
    }

}