heheng
2025-10-15 a4f1e1a9b97f7606347ba1b6a5c5957c3fc28a59
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
package com.gkhy.system.service.impl;
 
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.common.constant.Constants;
import com.gkhy.common.core.domain.entity.SysDept;
import com.gkhy.common.exception.ServiceException;
import com.gkhy.common.utils.SecurityUtils;
import com.gkhy.system.domain.DailySafetyInspection;
import com.gkhy.system.domain.DailySafetyInspectionDept;
import com.gkhy.system.domain.DailySafetyInspectionUser;
import com.gkhy.system.mapper.DailySafetyInspectionDeptMapper;
import com.gkhy.system.mapper.DailySafetyInspectionMapper;
import com.gkhy.system.mapper.DailySafetyInspectionUserMapper;
import com.gkhy.system.mapper.SysDeptMapper;
import com.gkhy.system.service.DailySafetyInspectionService;
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-09-08 10:36:52
 */
@Service
public class DailySafetyInspectionServiceImpl extends ServiceImpl<DailySafetyInspectionMapper, DailySafetyInspection> implements DailySafetyInspectionService {
 
 
    @Autowired
    private DailySafetyInspectionMapper dailySafetyInspectionMapper;
 
    @Autowired
    private DailySafetyInspectionUserMapper dailySafetyInspectionUserMapper;
 
    @Autowired
    private DailySafetyInspectionDeptMapper dailySafetyInspectionDeptMapper;
 
    @Autowired
    private SysDeptMapper sysDeptMapper;
 
    @Override
    public List<DailySafetyInspection> selectDailySafetyInspectionList(DailySafetyInspection inspection) {
        return dailySafetyInspectionMapper.getDailySafetyInspectionList(inspection);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveDailySafetyInspection(DailySafetyInspection inspection) {
//        List<DailySafetyInspectionUser> dailySafetyInspectionUsers = inspection.getDailySafetyInspectionUsers();
//        if (ObjectUtil.isEmpty(dailySafetyInspectionUsers)) {
//            throw new ServiceException("参检人员不能为空");
//        }
//        Long researchGroup = inspection.getResearchGroup();
//        SysDept sysDept = sysDeptMapper.selectDeptById(researchGroup);
//        if (sysDept == null) {
//            throw new ServiceException("部门不存在");
//        }
//        if (!"1".equals(sysDept.getSafety())){
//            throw new ServiceException("所选部门未开启安全检查");
//        }
 
        int i = 0;
        if (inspection.getId() == null) {
            inspection.setCreateTime(LocalDateTime.now());
            inspection.setCreateBy(SecurityUtils.getUsername());
            i = dailySafetyInspectionMapper.insert(inspection);
        } else {
            inspection.setUpdateTime(LocalDateTime.now());
            inspection.setUpdateBy(SecurityUtils.getUsername());
            i = dailySafetyInspectionMapper.updateById(inspection);
        }
        if (i > 0){
            saveDailySafetyInspectionDept(inspection.getDailySafetyInspectionDepts(),inspection.getId());
        }
//        if (i > 0) {
//            saveDailySafetyInspectionUser(dailySafetyInspectionUsers, inspection.getId());
//        }
        return i;
    }
 
    private void saveDailySafetyInspectionUser(List<DailySafetyInspectionUser> dailySafetyInspectionUsers, Long id) {
 
        DailySafetyInspectionUser dailySafetyInspectionUser = new DailySafetyInspectionUser();
        dailySafetyInspectionUser.setDailySafetyInspectionId(id);
        dailySafetyInspectionUserMapper.deleteById(dailySafetyInspectionUser);
        dailySafetyInspectionUsers.forEach(user -> {
            user.setDailySafetyInspectionId(id);
            dailySafetyInspectionUserMapper.insert(user);
        });
 
    }
    private void saveDailySafetyInspectionDept(List<DailySafetyInspectionDept> dailySafetyInspectionDepts, Long id) {
 
        DailySafetyInspectionDept dept = new DailySafetyInspectionDept();
        dept.setDailySafetyInspectionId(id);
        dailySafetyInspectionDeptMapper.deleteById(dept);
        dailySafetyInspectionDepts.forEach(dept1 -> {
            dept1.setDailySafetyInspectionId(id);
            dailySafetyInspectionDeptMapper.insert(dept1);
        });
 
    }
 
    @Override
    public int deleteDailySafetyInspection(Long id) {
        return dailySafetyInspectionMapper.update(new DailySafetyInspection(),
                new LambdaUpdateWrapper<DailySafetyInspection>().eq(DailySafetyInspection::getId, id)
                        .set(DailySafetyInspection::getDelFlag, Constants.FAIL).set(DailySafetyInspection::getUpdateTime, LocalDateTime.now())
                        .set(DailySafetyInspection::getUpdateBy, SecurityUtils.getUsername()));
    }
 
    @Override
    public int getCheckCount() {
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        if (sysDept == null){
            return 1;
        }else {
            if (!"1".equals(sysDept.getSafety())){
                return 1;
            }
        }
        return dailySafetyInspectionMapper.getCheckCount(deptId);
    }
}