heheng
昨天 6faa8da685a95b1fce5f432f5bd41a870ccb5ade
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
package com.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.AnnualVerificationDevice;
import com.gkhy.exam.system.domain.AnnualVerificationPlan;
import com.gkhy.exam.system.domain.OutsourcedReview;
import com.gkhy.exam.system.domain.OutsourcedReviewSubsidiary;
import com.gkhy.exam.system.mapper.AnnualVerificationDeviceMapper;
import com.gkhy.exam.system.mapper.AnnualVerificationPlanMapper;
import com.gkhy.exam.system.service.AnnualVerificationPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;
 
/**
 * <p>
 * 年度检定计划主表 服务实现类
 * </p>
 *
 * @author hh
 * @since 2025-08-25 15:01:44
 */
@Service
public class AnnualVerificationPlanServiceImpl extends ServiceImpl<AnnualVerificationPlanMapper, AnnualVerificationPlan> implements AnnualVerificationPlanService {
 
    @Autowired
    private AnnualVerificationPlanMapper annualVerificationPlanMapper;
 
    @Autowired
    private AnnualVerificationDeviceMapper annualVerificationDeviceMapper;
 
    @Override
    public CommonPage selectAnnualVerificationPlanList(AnnualVerificationPlan annualVerificationPlan) {
        PageUtils.startPage();
        List<AnnualVerificationPlan> annualVerificationPlans = annualVerificationPlanMapper.selectAnnualVerificationPlanList(annualVerificationPlan);
        return CommonPage.restPage(annualVerificationPlans);
    }
 
    @Override
    @Transactional
    public CommonResult saveAnnualVerificationPlan(AnnualVerificationPlan annualVerificationPlan) {
 
        List<AnnualVerificationDevice> annualVerificationDevices = annualVerificationPlan.getAnnualVerificationDevices();
        if (ObjectUtils.isEmpty(annualVerificationDevices)){
            return CommonResult.failed("保存参数不能为空");
        }
        int i = 0;
        if (annualVerificationPlan.getId() == null){
            annualVerificationPlan.setCreateBy(SecurityUtils.getUsername());
            annualVerificationPlan.setCreateTime(LocalDateTime.now());
            i = annualVerificationPlanMapper.insert(annualVerificationPlan);
        }else {
            annualVerificationPlan.setUpdateTime(LocalDateTime.now());
            annualVerificationPlan.setUpdateBy(SecurityUtils.getUsername());
            i = annualVerificationPlanMapper.updateById(annualVerificationPlan);
        }
        if (i > 0){
            batchSave(annualVerificationPlan.getId(), annualVerificationDevices, annualVerificationPlan.getDelData());
        }
        return i > 0 ? CommonResult.success() : CommonResult.failed();
    }
 
    private void batchSave(Long id, List<AnnualVerificationDevice> annualVerificationDevices, List<Long> delData) {
        if (ObjectUtils.isNotEmpty(delData)){
            int update = annualVerificationDeviceMapper.update(new AnnualVerificationDevice(),
                    new LambdaUpdateWrapper<AnnualVerificationDevice>().set(AnnualVerificationDevice::getDelFlag, UserConstant.DISENABLE)
                            .set(AnnualVerificationDevice::getUpdateTime, LocalDateTime.now()).set(AnnualVerificationDevice::getUpdateBy, SecurityUtils.getUsername())
                            .in(AnnualVerificationDevice::getId, delData)
            );
            if (update >= 0){
                throw new RuntimeException("操作失败");
            }
        }
        annualVerificationDevices.forEach(annualVerificationDevice -> {
            if (annualVerificationDevice.getId() == null){
                annualVerificationDevice.setAnnualVerificationId(id);
                annualVerificationDevice.setCreateTime(LocalDateTime.now());
                annualVerificationDevice.setCreateBy(SecurityUtils.getUsername());
                annualVerificationDeviceMapper.insert(annualVerificationDevice);
            }else {
                annualVerificationDevice.setUpdateTime(LocalDateTime.now());
                annualVerificationDevice.setUpdateBy(SecurityUtils.getUsername());
                annualVerificationDeviceMapper.updateById(annualVerificationDevice);
            }
 
        });
    }
 
 
    @Override
    public CommonResult getAnnualVerificationPlan(Long id) {
        AnnualVerificationPlan annualVerificationPlan = annualVerificationPlanMapper.selectById(id);
        if (annualVerificationPlan != null){
 
            List<AnnualVerificationDevice> annualVerificationDevices = annualVerificationDeviceMapper.selectList(new LambdaQueryWrapper<>(AnnualVerificationDevice.class).eq(AnnualVerificationDevice::getAnnualVerificationId, id)
                    .eq(AnnualVerificationDevice::getDelFlag, UserConstant.ENABLE).orderByDesc(AnnualVerificationDevice::getCreateTime));
            annualVerificationPlan.setAnnualVerificationDevices(annualVerificationDevices);
        }
        return CommonResult.success(annualVerificationPlan);
    }
 
    @Override
    @Transactional
    public CommonResult deletedAnnualVerificationPlan(Long id) {
 
        int i = annualVerificationPlanMapper.update(new AnnualVerificationPlan(),
                new LambdaUpdateWrapper<AnnualVerificationPlan>().eq(AnnualVerificationPlan::getId, id).set(AnnualVerificationPlan::getDelFlag, UserConstant.DISENABLE)
                        .set(AnnualVerificationPlan::getUpdateTime, LocalDateTime.now()).set(AnnualVerificationPlan::getUpdateBy, SecurityUtils.getUsername())
        );
        if (i > 0) {
            annualVerificationDeviceMapper.update(new AnnualVerificationDevice(),
                    new LambdaUpdateWrapper<AnnualVerificationDevice>().eq(AnnualVerificationDevice::getAnnualVerificationId, id)
                            .set(AnnualVerificationDevice::getDelFlag, UserConstant.DISENABLE)
                            .set(AnnualVerificationDevice::getUpdateTime, LocalDateTime.now())
                            .set(AnnualVerificationDevice::getUpdateBy, SecurityUtils.getUsername()));
        }
 
        return CommonResult.success();
    }
}