IDeviceServiceImpl.java 5.4 KB
package vion.service.impl;

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.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.DeviceDTO;
import vion.mapper.DeviceMapper;
import vion.model.Contract;
import vion.model.Device;
import vion.model.DeviceLog;
import vion.service.IDeviceLogService;
import vion.service.IDeviceService;
import vion.vo.DeviceVO;

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

/**
 * @author HlQ
 * @date 2024/8/12
 */
@Service
@RequiredArgsConstructor
public class IDeviceServiceImpl extends MPJBaseServiceImpl<DeviceMapper, Device> implements IDeviceService {

    private final IDeviceLogService deviceLogService;
    private final Converter converter;

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

    @Override
    public DeviceVO getById(Long id) {
        var wrapper = new MPJLambdaWrapper<Device>()
                .selectAll(Device.class)
                .selectAs(Contract::getName, DeviceVO::getContractName)
                .selectCollection(DeviceLog.class, DeviceVO::getLogList)
                .leftJoin(DeviceLog.class, DeviceLog::getDeviceId, Device::getId)
                .leftJoin(Contract.class, Contract::getContractNo, Device::getContractNo)
                .orderByDesc(Device::getRecDate);
        return this.selectJoinOne(DeviceVO.class, wrapper);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(List<DeviceDTO> dtoList) {
        if (this.updateBatchById(converter.convert(dtoList, Device.class))) {
            var logList = dtoList.stream()
                    .map(dto -> {
                        var deviceLog = new DeviceLog();
                        deviceLog.setDeviceId(dto.getId());
                        deviceLog.setOperator(dto.getUserId());
                        deviceLog.setContent(getStatusStr(dto.getStatus()));
                        return deviceLog;
                    }).toList();
            deviceLogService.saveBatch(logList);
            return "更新成功";
        }
        return "更新失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String recordDevice(List<DeviceDTO> dtoList) {
        if (CollUtil.isEmpty(dtoList)) {
            return "设备为空";
        }
        var statusSet = dtoList.stream().map(DeviceDTO::getStatus).collect(Collectors.toSet());
        if (statusSet.size() > 1 || !statusSet.contains(1)) {
            return "该操作仅支持设备入库";
        }
        var userId = dtoList.getFirst().getUserId();
        var deviceList = converter.convert(dtoList, Device.class);
        if (this.saveBatch(deviceList)) {
            var logList = deviceList.stream()
                    .map(d -> {
                        var deviceLog = new DeviceLog();
                        deviceLog.setDeviceId(d.getId());
                        deviceLog.setOperator(userId);
                        deviceLog.setContent(getStatusStr(d.getStatus()));
                        return deviceLog;
                    }).toList();
            deviceLogService.saveBatch(logList);
            return "保存成功";
        }
        return "保存失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String circulateBatch(List<DeviceDTO> dtoList) {
        if (CollUtil.isEmpty(dtoList)) {
            return "设备为空";
        }
        var statusSet = dtoList.stream().map(DeviceDTO::getStatus).collect(Collectors.toSet());
        if (statusSet.size() > 1) {
            return "该操作不允许对设备进行不同处理";
        }
        if (statusSet.contains(1)) {
            return "该操作不支持入库操作";
        }
        var userId = dtoList.getFirst().getUserId();
        var deviceList = converter.convert(dtoList, Device.class);
        deviceList.forEach(d -> {
            this.lambdaUpdate().eq(Device::getSerialNum, d.getSerialNum()).update(d);
            var deviceLog = new DeviceLog();
            deviceLog.setDeviceId(d.getId());
            deviceLog.setOperator(userId);
            deviceLog.setContent(getStatusStr(d.getStatus()));
            deviceLogService.save(deviceLog);
        });
        return "保存成功";
    }

    private String getStatusStr(Integer status) {
        return switch (status) {
            case 1 -> "设备:入库";
            case 2 -> "设备:出库";
            case 3 -> "设备:维修";
            case 4 -> "设备:借测";
            case 5 -> "设备:废弃";
            default -> "未知操作";
        };
    }
}