RepairRecServiceImpl.java 6.9 KB
package vion.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.date.DateUtil;
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.data.id.IdUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.crypto.SecureUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.dto.RepairRecDTO;
import vion.mapper.RepairRecMapper;
import vion.model.*;
import vion.service.IFileService;
import vion.service.IRepairRecService;
import vion.third.DingMod;
import vion.vo.RepairRecVO;
import vion.vo.UserVO;

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

/**
 * @author HlQ
 * @date 2024/1/19
 */
@Service
@RequiredArgsConstructor
public class RepairRecServiceImpl extends MPJBaseServiceImpl<RepairRecMapper, RepairRec> implements IRepairRecService {

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

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

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

    @Override
    public List<RepairRecVO> list(RepairRecDTO dto) {
        MPJLambdaWrapper<RepairRec> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, RepairRec.class))
                .selectAll(RepairRec.class)
                .selectAs(Contract::getName, RepairRecVO::getContractName)
                .selectAs(Store::getName, RepairRecVO::getStoreName)
                .selectAs(Store::getMaintainStatus, RepairRecVO::getMaintainStatus)
                .selectCollection(RRepairDevice.class, RepairRecVO::getRepairDeviceList)
                .leftJoin(Contract.class, Contract::getId, RepairRec::getContractId)
                .leftJoin(Store.class, Store::getId, RepairRec::getStoreId)
                .leftJoin(RRepairDevice.class, on -> on.eq(RRepairDevice::getRId, RepairRec::getId).eq(RRepairDevice::getRType, 2))
                .between(RepairRec::getCreateTime, dto.getCreateTimeStart(), dto.getCreateTimeEnd())
                .between(ArrayUtil.isAllNotNull(dto.getShipDateStart(), dto.getShipDateEnd()), SparePart::getShipDate, dto.getShipDateStart(), dto.getShipDateEnd())
                .like(StrUtil.isNotBlank(dto.getDeviceName()), RRepairDevice::getDeviceName, dto.getDeviceName())
                .like(StrUtil.isNotBlank(dto.getDeviceNo()), RRepairDevice::getDeviceNo, dto.getDeviceNo())
                .orderByDesc(RepairRec::getCreateTime);
        return this.selectJoinList(RepairRecVO.class, wrapper);
    }

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

    @Override
    public String updById(Long id, RepairRecDTO dto) {
        if (this.updateById(converter.convert(dto, RepairRec.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, DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS"), fileExt);
                                String path =
                                        fileUrl + FileUtil.FILE_SEPARATOR + "repairRec" + 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(23);
                                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) {
        var repairRec = this.getById(id);

        var jsonObj = JSONUtil.ofObj()
                .set("agent_id", 2358374016L)
                .set("userid_list", userid);

        var msg = JSONUtil.ofObj();
        var content = JSONUtil.ofObj().set("title", "返修设备已发货");
        String text = StrUtil.format("""
                        ### 返修设备已发货
                        ### 项目名称:{}
                        ### 快递单号:{}
                        ### 发货日期:{}
                        ### 发送时间:{}
                        """, repairRec.getProjectName(), Opt.ofBlankAble(repairRec.getTrackingNumber()).orElse("暂无"),
                Opt.ofBlankAble(DateUtil.formatDate(repairRec.getShipDate())).orElse("暂无"), DateUtil.now());
        content.set("text", text);

        msg.set("msgtype", "markdown").set("markdown", content);
        jsonObj.set("msg", msg);

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