mitao
2024-12-23 71542a84d8461b3ae216ee35fda214752a94d350
medicalWaste-system/src/main/java/com/sinata/system/service/impl/MwRegulatoryRecordServiceImpl.java
@@ -1,10 +1,29 @@
package com.sinata.system.service.impl;
import com.sinata.system.domain.MwRegulatoryRecord;
import com.sinata.system.mapper.MwRegulatoryRecordMapper;
import com.sinata.system.service.MwRegulatoryRecordService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.entity.PageDTO;
import com.sinata.common.exception.ServiceException;
import com.sinata.common.utils.BeanUtils;
import com.sinata.common.utils.CollUtils;
import com.sinata.system.domain.MwAttachment;
import com.sinata.system.domain.MwRegulatoryRecord;
import com.sinata.system.domain.SysDepartment;
import com.sinata.system.domain.dto.MwRegulatoryRecordDTO;
import com.sinata.system.domain.query.MwRegulatoryRecordQuery;
import com.sinata.system.domain.vo.MwAttachmentVO;
import com.sinata.system.domain.vo.MwRegulatoryRecordVO;
import com.sinata.system.enums.AttachmentTypeEnum;
import com.sinata.system.mapper.MwRegulatoryRecordMapper;
import com.sinata.system.service.MwAttachmentService;
import com.sinata.system.service.MwRegulatoryRecordService;
import com.sinata.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
/**
 * <p>
@@ -15,6 +34,106 @@
 * @since 2024-12-02
 */
@Service
@RequiredArgsConstructor
public class MwRegulatoryRecordServiceImpl extends ServiceImpl<MwRegulatoryRecordMapper, MwRegulatoryRecord> implements MwRegulatoryRecordService {
    private final SysDepartmentService sysDepartmentService;
    private final MwAttachmentService mwAttachmentService;
    /**
     * 分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwRegulatoryRecordVO> pageList(MwRegulatoryRecordQuery query) {
        String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
        Page<MwRegulatoryRecordVO> page = baseMapper.pageList(new Page<>(), query, treeCode);
        return PageDTO.of(page);
    }
    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MwRegulatoryRecordVO detail(Long id) {
        MwRegulatoryRecord regulatoryRecord = getById(id);
        MwRegulatoryRecordVO vo = BeanUtils.copyBean(regulatoryRecord, MwRegulatoryRecordVO.class);
        if (Objects.nonNull(vo)) {
            List<MwAttachment> list = mwAttachmentService.lambdaQuery().eq(MwAttachment::getType, AttachmentTypeEnum.REGULATORY.getCode()).eq(MwAttachment::getTargetId, id).list();
            vo.setAttachmentList(BeanUtils.copyToList(list, MwAttachmentVO.class));
            SysDepartment department = sysDepartmentService.getById(vo.getDepartmentId());
            if (Objects.nonNull(department)) {
                vo.setDepartmentName(department.getDepartmentName());
            }
        }
        return vo;
    }
    /**
     * 新增检查记录
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MwRegulatoryRecordDTO dto) {
        MwRegulatoryRecord mwRegulatoryRecord = BeanUtils.copyBean(dto, MwRegulatoryRecord.class);
        save(mwRegulatoryRecord);
        //保存附件
        if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
            List<MwAttachment> mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
            mwAttachments.forEach(attachment -> {
                attachment.setTargetId(mwRegulatoryRecord.getId());
                attachment.setType(AttachmentTypeEnum.REGULATORY.getCode());
            });
            mwAttachmentService.saveBatch(mwAttachments);
        }
    }
    /**
     * 编辑检查记录
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(MwRegulatoryRecordDTO dto) {
        if (Objects.isNull(dto.getId())) {
            throw new ServiceException("检查记录id不能为空");
        }
        MwRegulatoryRecord mwRegulatoryRecord = BeanUtils.copyBean(dto, MwRegulatoryRecord.class);
        updateById(mwRegulatoryRecord);
        //删除原来的附件
        mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.REGULATORY.getCode()).eq(MwAttachment::getTargetId, dto.getId()).remove();
        //保存附件
        if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
            List<MwAttachment> mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
            mwAttachments.forEach(attachment -> {
                attachment.setTargetId(mwRegulatoryRecord.getId());
                attachment.setType(AttachmentTypeEnum.REGULATORY.getCode());
            });
            mwAttachmentService.saveBatch(mwAttachments);
        }
    }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //删除附件
        mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.REGULATORY.getCode()).eq(MwAttachment::getTargetId, id).remove();
        removeById(id);
    }
}