mitao
2025-03-27 b22f118eabab8f2f27dbd73e7c6cb1090cd82c82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.sinata.system.service.impl;
 
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.common.utils.SecurityUtils;
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>
 * 红黄码监管记录 服务实现类
 * </p>
 *
 * @author mitao
 * @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);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add1(MwRegulatoryRecordDTO dto) {
        dto.setCheckBy(SecurityUtils.getLoginUser().getUser().getNickName());
        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);
    }
}