邱宇豪
2023-11-20 942fd08345fe0f3b04b4a0e919516257213ad2e5
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
package com.thhy.secure.modules.biz.regionInspection.service.impl;
 
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thhy.general.common.BasicResult;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionEntity;
import com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionHazardInformEntity;
import com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionRecordEntity;
import com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionUserEntity;
import com.thhy.secure.modules.biz.regionInspection.mapper.RegionInspectionMapper;
import com.thhy.secure.modules.biz.regionInspection.mapper.RegionInspectionRecordMapper;
import com.thhy.secure.modules.biz.regionInspection.service.RegionInspectionRecordService;
import com.thhy.secure.modules.biz.regionInspection.service.RegionInspectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @Author QiuYuHao
 * @CreateDate 2023-11-10 11:26:29
 * 区域巡检接口实现
 */
@Service
public class RegionInspectionServiceImpl implements RegionInspectionService
{
 
    @Autowired
    private RegionInspectionMapper regionInspectionMapper;
 
    @Autowired
    private RegionInspectionRecordMapper regionInspectionRecordMapper;
 
    private static final Object lock = new Object();
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult insert(RegionInspectionEntity regionInspectionEntity) {
        SysUserInfo s = UserInfoUtils.getInstance().getUserInfo();
        String companyId = s.getCompanyId();
        String userId = s.getUserId();
        regionInspectionEntity.setCompanyId(companyId);
        //默认给uuid
        String regionInspectId = UUIDUtils.create();
        //修改区域巡检判断
        if (StringUtils.isNotBlank(regionInspectionEntity.getId())){
            regionInspectId = regionInspectionEntity.getId();
            //根据区域巡检id删除中间表
            regionInspectionEntity.setUpdateTime(new Date());
            regionInspectionEntity.setUpdateUser(userId);
            regionInspectionMapper.deleteUser(regionInspectId);
            regionInspectionMapper.deleteInspectionHazardInform(regionInspectId);
        }
        List<String> userIds = new ArrayList<>();
        List<String> regionIds = new ArrayList<>();
        synchronized (lock) {
            //添加巡检人
            for (RegionInspectionUserEntity userEntity : regionInspectionEntity.getRegionUsersList()) {
                String id = UUIDUtils.create();
                userIds.add(userEntity.getUserId());
                userEntity.setId(id);
                userEntity.setRegionInspectionId(regionInspectId);
                regionInspectionMapper.insertUser(userEntity);
            }
            //添加巡检的区域
            for (RegionInspectionHazardInformEntity region : regionInspectionEntity.getRegionList()) {
                String id = UUIDUtils.create();
                regionIds.add(region.getRegionHazardInformId());
                region.setId(id);
                region.setRegionInspectionId(regionInspectId);
                regionInspectionMapper.insertInspectionHazardInform(region);
            }
 
            //添加任务的时候添加打卡记录 新增的是待打卡状态
            if (StringUtils.isBlank(regionInspectionEntity.getId())){
                for (String addUserId : userIds) {
                    for (String regionId : regionIds) {
                        RegionInspectionRecordEntity regionInspectionRecordEntity =
                                RegionInspectionRecordEntity
                                        .builder()
                                        .id(UUIDUtils.create())
                                        .userId(addUserId)
                                        .regionHazardInformId(regionId)
                                        .regionInspectionId(regionInspectId)
                                        .createUser(userId)
                                        .build();
                        regionInspectionRecordMapper.insert(regionInspectionRecordEntity);
                    }
                }
            }
 
            //添加/修改区域巡检
            regionInspectionEntity.setId(regionInspectId);
            regionInspectionEntity.setCreateUser(userId);
            regionInspectionMapper.insert(regionInspectionEntity);
 
        }
 
        return BasicResult.success();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult delete(String id) {
        regionInspectionMapper.delete(id);
        regionInspectionMapper.deleteUser(id);
        regionInspectionMapper.deleteInspectionHazardInform(id);
        regionInspectionRecordMapper.deleteByRegionInspectionId(id);
        return BasicResult.success();
    }
 
    @Override
    public BasicResult selectPageList(Map map) {
        SysUserInfo s = UserInfoUtils.getInstance().getUserInfo();
        String companyId = s.getCompanyId();
        map.put("companyId",companyId);
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<RegionInspectionEntity> regionInspectionEntities = regionInspectionMapper.selectPageList(map);
        //处理区域巡检对象数组
        regionInspectionEntities.forEach(obj->{
            List<RegionInspectionHazardInformEntity> regionList = new ArrayList<>();
            List<RegionInspectionUserEntity> userList = new ArrayList<>();
            if(obj.getRealNames() !=null){
                String[] realNames = obj.getRealNames().intern().split("、");
                String[] userIds = obj.getUserIds().intern().split("、");
                //人员对象数组
                for (int i = 0; i < realNames.length; i++) {
                    RegionInspectionUserEntity userEntity = RegionInspectionUserEntity
                            .builder()
                            .regionInspectionId(obj.getId())
                            .userId(userIds[i])
                            .realName(realNames[i]).build();
                    userList.add(userEntity);
                }
            }
            if (obj.getRegionIds()!=null){
                String[] regionIds = obj.getRegionIds().intern().split(",");
                String[] regions = obj.getRegions().intern().split(",");
                //区域对象数组
                for (int i = 0; i < regionIds.length; i++) {
                    RegionInspectionHazardInformEntity regionEntity = RegionInspectionHazardInformEntity
                            .builder()
                            .regionInspectionId(obj.getId())
                            .regionHazardInformId(regionIds[i])
                            .region(regions[i]).build();
                    regionList.add(regionEntity);
                }
            }
            obj.setRegionList(regionList);
            obj.setRegionUsersList(userList);
        });
        return BasicResult.success(new PageInfo<>(regionInspectionEntities));
    }
 
}