mitao
2024-12-27 9c717849bee3d6cc25f29ad69a93a507e3de7d13
medicalWaste-system/src/main/java/com/sinata/system/service/impl/MwContractServiceImpl.java
@@ -1,10 +1,29 @@
package com.sinata.system.service.impl;
import com.sinata.system.domain.MwContract;
import com.sinata.system.mapper.MwContractMapper;
import com.sinata.system.service.MwContractService;
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.MwContract;
import com.sinata.system.domain.SysDepartment;
import com.sinata.system.domain.dto.MwContractDTO;
import com.sinata.system.domain.query.MwContractQuery;
import com.sinata.system.domain.vo.MwAttachmentVO;
import com.sinata.system.domain.vo.MwContractVO;
import com.sinata.system.enums.AttachmentTypeEnum;
import com.sinata.system.mapper.MwContractMapper;
import com.sinata.system.service.MwAttachmentService;
import com.sinata.system.service.MwContractService;
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,102 @@
 * @since 2024-12-02
 */
@Service
@RequiredArgsConstructor
public class MwContractServiceImpl extends ServiceImpl<MwContractMapper, MwContract> implements MwContractService {
    private final SysDepartmentService sysDepartmentService;
    private final MwAttachmentService mwAttachmentService;
    /**
     * 合同分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwContractVO> pageList(MwContractQuery query) {
        if (Objects.isNull(query.getDepartmentId())) {
            SysDepartment myDepartment = sysDepartmentService.getMyDepartment();
            if (Objects.isNull(myDepartment)) {
                return PageDTO.empty(0L, 0L);
            }
            query.setDepartmentId(myDepartment.getId());
        }
        Page<MwContractVO> page = baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query);
        return PageDTO.of(page);
    }
    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MwContractVO detail(Long id) {
        MwContractVO mwContractVO = BeanUtils.copyBean(getById(id), MwContractVO.class);
        if (Objects.nonNull(mwContractVO)) {
            List<MwAttachment> list = mwAttachmentService.lambdaQuery().eq(MwAttachment::getType, AttachmentTypeEnum.CONTRACT.getCode()).eq(MwAttachment::getTargetId, id).list();
            mwContractVO.setAttachmentList(BeanUtils.copyToList(list, MwAttachmentVO.class));
        }
        return mwContractVO;
    }
    /**
     * 新增合同
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MwContractDTO dto) {
        MwContract mwContract = BeanUtils.copyBean(dto, MwContract.class);
        if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
            List<MwAttachment> mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
            mwAttachments.forEach(attachment -> {
                attachment.setTargetId(mwContract.getId());
                attachment.setType(AttachmentTypeEnum.CONTRACT.getCode());
            });
            mwAttachmentService.saveBatch(mwAttachments);
        }
    }
    /**
     * 编辑合同
     *
     * @param dto
     * @return
     */
    @Override
    public void edit(MwContractDTO dto) {
        if (Objects.isNull(dto.getId())) {
            throw new ServiceException("合同id不能为空");
        }
        MwContract mwContract = BeanUtils.copyBean(dto, MwContract.class);
        updateById(mwContract);
        if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
            //删除原来的附件
            mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.CONTRACT.getCode()).eq(MwAttachment::getTargetId, dto.getId()).remove();
            List<MwAttachment> mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
            mwAttachments.forEach(attachment -> {
                attachment.setTargetId(mwContract.getId());
                attachment.setType(AttachmentTypeEnum.CONTRACT.getCode());
            });
            mwAttachmentService.saveBatch(mwAttachments);
        }
    }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        //删除附件
        mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.CONTRACT.getCode()).eq(MwAttachment::getTargetId, id).remove();
        //执行删除
        removeById(id);
    }
}