heheng
3 天以前 a8a6760635f0642a2cbf61854b5587d9d0944985
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.gkhy.exam.system.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.domain.entity.SysDept;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.InternalAuditCheck;
import com.gkhy.exam.system.domain.InternalAuditCheckPerson;
import com.gkhy.exam.system.domain.vo.InternalAuditCheckVo;
import com.gkhy.exam.system.mapper.InternalAuditCheckMapper;
import com.gkhy.exam.system.mapper.InternalAuditCheckPersonMapper;
import com.gkhy.exam.system.mapper.SysDeptMapper;
import com.gkhy.exam.system.mapper.SysUserMapper;
import com.gkhy.exam.system.service.InternalAuditCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 内审检查 服务实现类
 * </p>
 *
 * @author hh
 * @since 2025-07-10 15:11:50
 */
@Service
public class InternalAuditCheckServiceImpl extends ServiceImpl<InternalAuditCheckMapper, InternalAuditCheck> implements InternalAuditCheckService {
 
    @Autowired
    private InternalAuditCheckMapper internalAuditCheckMapper;
    @Autowired
    private InternalAuditCheckPersonMapper internalAuditCheckPersonMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Override
    public CommonPage selectInternalAuditCheckList(Integer companyId) {
        PageUtils.startPage();
        List<InternalAuditCheck> internalAuditChecks = internalAuditCheckMapper.selectInternalAuditCheckList(companyId);
        return CommonPage.restPage(internalAuditChecks);
    }
 
    @Override
    @Transactional
    public CommonResult insertInternalAuditCheck(InternalAuditCheckVo internalAuditCheck) {
        InternalAuditCheck internalAuditCheck1 = new InternalAuditCheck();
        BeanUtils.copyProperties(internalAuditCheck,internalAuditCheck1);
        internalAuditCheck1.setCreateTime(LocalDateTime.now());
        internalAuditCheck1.setCreateBy(SecurityUtils.getUsername());
 
        int insert = internalAuditCheckMapper.insert(internalAuditCheck1);
        if (insert > 0) {
            if (ObjectUtil.isNotEmpty(internalAuditCheck.getInternalAuditCheckPeople())){
                batchInsert(internalAuditCheck.getInternalAuditCheckPeople(),internalAuditCheck1.getId());
            }
 
        }
        return CommonResult.failed();
    }
 
    private void batchInsert(List<InternalAuditCheckPerson> internalAuditCheckPeople,Long auditId) {
        internalAuditCheckPeople.forEach(e -> {
            e.setAuditId(auditId);
            e.setCreateTime(LocalDateTime.now());
            e.setCreateBy(SecurityUtils.getUsername());
        });
        internalAuditCheckPersonMapper.batchInsert(internalAuditCheckPeople);
    }
 
    private void batchUpdate(List<InternalAuditCheckPerson> internalAuditCheckPeople) {
        internalAuditCheckPeople.forEach(e -> {
            e.setUpdateTime(LocalDateTime.now());
            e.setUpdateBy(SecurityUtils.getUsername());
        });
        internalAuditCheckPersonMapper.batchUpdate(internalAuditCheckPeople);
    }
 
    @Override
    public CommonResult updateInternalAuditCheck(InternalAuditCheckVo internalAuditCheck) {
        InternalAuditCheck internalAuditCheck1 = new InternalAuditCheck();
        BeanUtils.copyProperties(internalAuditCheck,internalAuditCheck1);
        internalAuditCheck1.setUpdateTime(LocalDateTime.now());
        internalAuditCheck1.setUpdateBy(SecurityUtils.getUsername());
 
        int update = internalAuditCheckMapper.updateById(internalAuditCheck1);
        if (update > 0) {
            List<InternalAuditCheckPerson> internalAuditCheckPeople = internalAuditCheck.getInternalAuditCheckPeople();
            if (ObjectUtil.isNotEmpty(internalAuditCheckPeople)){
                List<Long> collect = internalAuditCheckPeople.stream().map(InternalAuditCheckPerson::getAuditUserId)
                        .collect(Collectors.toList());
                if (collect.size() != internalAuditCheckPeople.size()){
                    throw new RuntimeException("选择的受审人员重复");
                }
                LambdaQueryWrapper<InternalAuditCheckPerson> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(InternalAuditCheckPerson::getAuditId, internalAuditCheck.getId());
                queryWrapper.eq(InternalAuditCheckPerson::getDelFlag, 0);
                List<InternalAuditCheckPerson> internalAuditCheckPeople1 = internalAuditCheckPersonMapper.selectList(queryWrapper);
 
                List<Long> idsAll = internalAuditCheckPeople1.stream()
                        .map(InternalAuditCheckPerson::getId)
                        .collect(Collectors.toList());
 
 
                List<Long> idsUpdate = internalAuditCheckPeople.stream()
                        .map(InternalAuditCheckPerson::getId)
                        .filter(id -> id != null) // 过滤掉 null 值
                        .collect(Collectors.toList());
                if (!idsUpdate.isEmpty() && !idsAll.isEmpty()){
                    //不存在的删除
                    List<Long> result = idsAll.stream()
                            .filter(id -> !idsUpdate.contains(id))
                            .collect(Collectors.toList());
                    if (!result.isEmpty()){
                        batchDel(internalAuditCheck.getId(),result);
                    }
                }
 
 
 
                List<InternalAuditCheckPerson> withId = internalAuditCheckPeople.stream()
                        .filter(e -> e.getId() != null)
                        .collect(Collectors.toList());
                //修改的
                if (!withId.isEmpty()){
                    batchUpdate(withId);
                }
 
                List<InternalAuditCheckPerson> withoutId = internalAuditCheckPeople.stream()
                        .filter(e -> e.getId() == null)
                        .collect(Collectors.toList());
 
                //新增的
                if (!withoutId.isEmpty()){
                    batchInsert(withoutId,internalAuditCheck.getId());
                }
            }else {
                batchDel(internalAuditCheck.getId(),null);
            }
 
 
            return CommonResult.success(update);
        }
        return CommonResult.failed();
    }
 
    private void batchDel(Long id,List<Long> ids){
        LambdaQueryWrapper<InternalAuditCheckPerson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InternalAuditCheckPerson::getAuditId, id);
        if (ObjectUtil.isNotEmpty(ids)){
            queryWrapper.in(InternalAuditCheckPerson::getId, ids);
        }
        InternalAuditCheckPerson internalAuditCheckPerson = new InternalAuditCheckPerson();
        internalAuditCheckPerson.setDelFlag(1);
        internalAuditCheckPerson.setUpdateTime(LocalDateTime.now());
        internalAuditCheckPerson.setUpdateBy(SecurityUtils.getUsername());
        internalAuditCheckPersonMapper.update(internalAuditCheckPerson,queryWrapper);
    }
 
    @Override
    @Transactional
    public CommonResult deletedInternalAuditCheck(Integer id) {
        int delete = internalAuditCheckMapper.deleteById(id);
        LambdaQueryWrapper<InternalAuditCheckPerson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InternalAuditCheckPerson::getAuditId, id);
        InternalAuditCheckPerson internalAuditCheckPerson = new InternalAuditCheckPerson();
        internalAuditCheckPerson.setDelFlag(1);
        internalAuditCheckPerson.setUpdateTime(LocalDateTime.now());
        internalAuditCheckPerson.setUpdateBy(SecurityUtils.getUsername());
        internalAuditCheckPersonMapper.update(internalAuditCheckPerson,queryWrapper);
        return CommonResult.success();
    }
 
    @Override
    public CommonResult internalAuditCheckInfo(Integer id) {
        InternalAuditCheck internalAuditCheck = internalAuditCheckMapper.selectById(id);
 
        InternalAuditCheckVo internalAuditCheckVo = new InternalAuditCheckVo();
        BeanUtils.copyProperties(internalAuditCheck,internalAuditCheckVo);
        internalAuditCheckVo.setDeptName(sysDeptMapper.selectDeptById(internalAuditCheck.getDeptId()).getDeptName());
        internalAuditCheckVo.setAuditName(sysUserMapper.selectById(internalAuditCheck.getAuditId()).getName());
        LambdaQueryWrapper<InternalAuditCheckPerson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InternalAuditCheckPerson::getAuditId, id);
        queryWrapper.eq(InternalAuditCheckPerson::getDelFlag, 0);
        List<InternalAuditCheckPerson> internalAuditCheckPeople = internalAuditCheckPersonMapper.selectList(queryWrapper);
        internalAuditCheckVo.setInternalAuditCheckPeople(internalAuditCheckPeople);
 
        return CommonResult.success(internalAuditCheck);
    }
}