luodangjia
2025-01-01 2dc478231fd09a88a4d86d44388ae807aca08bc5
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
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.StringUtils;
import com.sinata.system.domain.MwAttachment;
import com.sinata.system.domain.MwContract;
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.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author mitao
 * @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) {
        String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
        if (StringUtils.isBlank(treeCode)) {
            return PageDTO.empty(0L, 0L);
        }
        Page<MwContractVO> page = baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query, treeCode);
        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);
        save(mwContract);
        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);
    }
 
    /**
     * 查询未过期合同列表
     *
     * @param date
     * @return
     */
    @Override
    public List<MwContractVO> queryListTerminationDateBeforeNow(Date date) {
        return baseMapper.queryListTerminationDateBeforeNow(date);
    }
}