mitao
2024-12-27 9c717849bee3d6cc25f29ad69a93a507e3de7d13
medicalWaste-system/src/main/java/com/sinata/system/service/impl/MwProtectionEquipmentServiceImpl.java
@@ -1,10 +1,36 @@
package com.sinata.system.service.impl;
import com.sinata.system.domain.MwProtectionEquipment;
import com.sinata.system.mapper.MwProtectionEquipmentMapper;
import com.sinata.system.service.MwProtectionEquipmentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinata.common.core.domain.entity.SysDictData;
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.common.utils.StringUtils;
import com.sinata.system.domain.MwAttachment;
import com.sinata.system.domain.MwProtectionEquipment;
import com.sinata.system.domain.MwProtectionEquipmentRecord;
import com.sinata.system.domain.dto.MwProtectionEquipmentDTO;
import com.sinata.system.domain.dto.MwProtectionEquipmentRecordDTO;
import com.sinata.system.domain.query.MwProtectionEquipmentQuery;
import com.sinata.system.domain.query.MwProtectionEquipmentRecordQuery;
import com.sinata.system.domain.vo.MwAttachmentVO;
import com.sinata.system.domain.vo.MwProtectionEquipmentRecordVO;
import com.sinata.system.domain.vo.MwProtectionEquipmentVO;
import com.sinata.system.enums.AttachmentTypeEnum;
import com.sinata.system.mapper.MwProtectionEquipmentMapper;
import com.sinata.system.service.ISysDictDataService;
import com.sinata.system.service.MwAttachmentService;
import com.sinata.system.service.MwProtectionEquipmentRecordService;
import com.sinata.system.service.MwProtectionEquipmentService;
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 +41,141 @@
 * @since 2024-12-02
 */
@Service
@RequiredArgsConstructor
public class MwProtectionEquipmentServiceImpl extends ServiceImpl<MwProtectionEquipmentMapper, MwProtectionEquipment> implements MwProtectionEquipmentService {
    private final SysDepartmentService sysDepartmentService;
    private final ISysDictDataService sysDictDataService;
    private final MwProtectionEquipmentRecordService mwProtectionEquipmentRecordService;
    private final MwAttachmentService mwAttachmentService;
    /**
     * 防护器具分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwProtectionEquipmentVO> pageList(MwProtectionEquipmentQuery query) {
        String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
        if (StringUtils.isBlank(treeCode)) {
            return PageDTO.empty(0L, 0L);
        }
        Page<MwProtectionEquipmentVO> page = baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query, treeCode);
        return PageDTO.of(page);
    }
    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public MwProtectionEquipmentVO detail(Long id) {
        MwProtectionEquipmentVO mwProtectionEquipmentVO = BeanUtils.copyBean(this.getById(id), MwProtectionEquipmentVO.class);
        if (Objects.nonNull(mwProtectionEquipmentVO)) {
            //查询附件列表
            List<MwAttachment> list = mwAttachmentService.lambdaQuery().eq(MwAttachment::getType, AttachmentTypeEnum.PROTECTION_EQUIPMENT.getCode()).eq(MwAttachment::getTargetId, id).list();
            mwProtectionEquipmentVO.setAttachmentList(BeanUtils.copyToList(list, MwAttachmentVO.class));
        }
        return mwProtectionEquipmentVO;
    }
    /**
     * 新增防护器具
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MwProtectionEquipmentDTO dto) {
        MwProtectionEquipment mwProtectionEquipment = BeanUtils.copyBean(dto, MwProtectionEquipment.class);
        SysDictData dictData = sysDictDataService.lambdaQuery().eq(SysDictData::getDictCode, dto.getProtectionEquipmentType()).one();
        if (Objects.nonNull(dictData)) {
            mwProtectionEquipment.setProtectionEquipmentTypeStr(dictData.getDictLabel());
        }
        save(mwProtectionEquipment);
        //保存附件
        if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
            List<MwAttachment> mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
            mwAttachments.forEach(attachment -> {
                attachment.setTargetId(mwProtectionEquipment.getId());
                attachment.setType(AttachmentTypeEnum.PROTECTION_EQUIPMENT.getCode());
            });
            mwAttachmentService.saveBatch(mwAttachments);
        }
    }
    /**
     * 编辑
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(MwProtectionEquipmentDTO dto) {
        if (Objects.isNull(dto.getId())) {
            throw new ServiceException("防护器具id不能为空");
        }
        MwProtectionEquipment mwProtectionEquipment = BeanUtils.copyBean(dto, MwProtectionEquipment.class);
        SysDictData dictData = sysDictDataService.lambdaQuery().eq(SysDictData::getDictCode, dto.getProtectionEquipmentType()).one();
        if (Objects.nonNull(dictData)) {
            mwProtectionEquipment.setProtectionEquipmentTypeStr(dictData.getDictLabel());
        }
        updateById(mwProtectionEquipment);
        //保存附件
        if (CollUtils.isNotEmpty(dto.getAttachmentList())) {
            //删除原有的附件
            mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.PROTECTION_EQUIPMENT.getCode()).eq(MwAttachment::getTargetId, dto.getId()).remove();
            List<MwAttachment> mwAttachments = BeanUtils.copyToList(dto.getAttachmentList(), MwAttachment.class);
            mwAttachments.forEach(attachment -> {
                attachment.setTargetId(mwProtectionEquipment.getId());
                attachment.setType(AttachmentTypeEnum.PROTECTION_EQUIPMENT.getCode());
            });
            mwAttachmentService.saveBatch(mwAttachments);
        }
    }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        removeById(id);
        //删除原有的附件
        mwAttachmentService.lambdaUpdate().eq(MwAttachment::getType, AttachmentTypeEnum.PROTECTION_EQUIPMENT.getCode()).eq(MwAttachment::getTargetId, id).remove();
    }
    @Override
    public void addStock(MwProtectionEquipmentRecordDTO dto) {
        MwProtectionEquipment equipment = getById(dto.getProtectionEquipmentId());
        if (Objects.isNull(equipment)) {
            throw new ServiceException("防护器具不存在");
        }
        equipment.setStock(equipment.getStock() + dto.getChangeQuantity());
        updateById(equipment);
        // 新增库存记录
        MwProtectionEquipmentRecord mwProtectionEquipmentRecord = BeanUtils.copyBean(dto, MwProtectionEquipmentRecord.class);
        mwProtectionEquipmentRecord.setType(1);
        mwProtectionEquipmentRecordService.save(mwProtectionEquipmentRecord);
    }
    /**
     * 增减记录分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwProtectionEquipmentRecordVO> recordPage(MwProtectionEquipmentRecordQuery query) {
        Page<MwProtectionEquipmentRecordVO> page = baseMapper.recordPage(new Page<>(query.getPageCurr(), query.getPageSize()), query.getId());
        return PageDTO.of(page);
    }
}