邱宇豪
2023-11-27 b79b3b5376bc5a54d897390718ddfee173c4eb32
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.thhy.secure.modules.biz.regionInspection.service.impl;
 
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.ExcelUtils;
import com.thhy.general.utils.UUIDUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.secure.modules.biz.regionInspection.entity.*;
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 org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @Author QiuYuHao
 * @CreateDate 2023-11-17 15:51:37
 * 区域巡检打卡记录表实现
 */
@Service
public class RegionInspectionRecordServiceImpl implements RegionInspectionRecordService {
 
    @Autowired
    private RegionInspectionRecordMapper regionInspectionRecordMapper;
 
    @Autowired
    private RegionInspectionMapper regionInspectionMapper;
 
    /**
     * 小程序提交打卡
     * @param regionInspectionEntity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BasicResult insertAndUpdate(RegionInspectionRecordEntity regionInspectionEntity) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String userId = sysUserInfo.getUserId();
        //修改逻辑
        if (StringUtils.isNotBlank(regionInspectionEntity.getId())){
            regionInspectionEntity.setUpdateUser(userId);
            regionInspectionEntity.setInspectionTime(new Date());
            regionInspectionEntity.setUpdateTime(new Date());
        }
        synchronized (this){
            //修改打卡记录
            regionInspectionEntity.setCreateUser(userId);
            regionInspectionRecordMapper.insert(regionInspectionEntity);
 
            //添加记录相对应的图片
            if (!regionInspectionEntity.getImgList().isEmpty()){
                regionInspectionEntity.getImgList().forEach(obj->{
                    RegionInspectionRecordImgEntity recordImgEntity = RegionInspectionRecordImgEntity
                            .builder()
                            .id(UUIDUtils.create())
                            .regionInspectionRecordId(regionInspectionEntity.getId())
                            .imgName(obj.getImgName())
                            .imgPath(obj.getImgPath())
                            .build();
                    regionInspectionRecordMapper.insertIMG(recordImgEntity);
                });
            }
            //判断每个人提交打卡的时候 判断一次这个任务是否完全被所有人打过卡了
            Map map = new HashMap();
            map.put("companyId",sysUserInfo.getCompanyId());
            map.put("regionInspectionId",regionInspectionEntity.getRegionInspectionId());
            //此任务每个人对任务打卡是否完成 不精确到此人去哪个区域打卡 只精确到这个人是否完成任务 任务分多个区域
            List<RegionInspectionRecordEntity> recordEntities = regionInspectionRecordMapper.selectPageList(map);
            int peopleDKCount = recordEntities.size();//若干人对应的若干任务
            AtomicInteger atomicInteger = new AtomicInteger();//记录完成打卡次数
            if (!recordEntities.isEmpty()){
                recordEntities.forEach(obj->{
                    String statusName = obj.getStatusName();
                    if("已完成".equals(statusName)){
                        atomicInteger.incrementAndGet();
                    }
                });
            }
            //如果若干人对应若干任务数量和 完成打卡次数相等 那么此任务 已经完结 修改任务状态
            if (peopleDKCount == atomicInteger.get()){
                RegionInspectionEntity r = RegionInspectionEntity
                        .builder()
                        .id(regionInspectionEntity.getRegionInspectionId())
                        .status(1)
                        .build();
                //修改任务状态
                regionInspectionMapper.insert(r);
            }
 
        }
        return BasicResult.success();
    }
 
    @Override
    public BasicResult delete(String id) {
        regionInspectionRecordMapper.delete(id);
        return BasicResult.success();
    }
 
    @Override
    public BasicResult selectPageList(Map map) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        String startHour = " 00:00:00";
        String endHour = " 23:59:59";
        String startTime = map.get("startTime").toString();
        String endTime = map.get("endTime").toString();
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            startTime = startTime+startHour;
            endTime = endTime +endHour;
            map.put("startTime",startTime);
            map.put("endTime",endTime);
        }
        if (StringUtils.isNotBlank(map.get("status").toString())){
            int intStatus = Integer.valueOf(map.get("status").toString());
            map.put("intStatus",intStatus);
        }
        map.put("companyId",companyId);
        map.put("page",(pageNum-1)*10);
        map.put("limit",pageSize);
        List<RegionInspectionRecordEntity> recordEntities = regionInspectionRecordMapper.selectPageList(map);
        Integer integer = regionInspectionRecordMapper.selectCount(map);
        map.put("list",recordEntities);
        map.put("total",integer);
        return BasicResult.success(map);
    }
 
    @Override
    public BasicResult selectInfo(String regionInspectionId, String userId) {
        RegionInspectionEntity obj = regionInspectionRecordMapper.selectInfo(regionInspectionId, userId);
        List<RegionInspectionHazardInformEntity> regionList = new ArrayList<>();
        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);
        return BasicResult.success(obj);
    }
 
    @Override
    public BasicResult selectAppInfo(String regionInspectionId, String userId) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        userId = sysUserInfo.getUserId();
        RegionInspectionEntity obj = regionInspectionRecordMapper.selectInfo(regionInspectionId, userId);
        List<RegionInspectionHazardInformEntity> regionList = new ArrayList<>();
        if (obj.getRegionIds()!=null){
            String[] regionIds = obj.getRegionIds().intern().split(",");
            String[] regions = obj.getRegions().intern().split(",");
            //区域对象数组
            for (int i = 0; i < regionIds.length; i++) {
                RegionInspectionRecordEntity recordEntity = regionInspectionRecordMapper.regionInfo(
                        regionInspectionId,
                        userId,
                        regionIds[i]);
                RegionInspectionHazardInformEntity regionEntity = RegionInspectionHazardInformEntity
                        .builder()
                        .regionInspectionId(obj.getId())
                        .regionHazardInformId(regionIds[i])
                        .region(regions[i])
                        .recordEntity(recordEntity).build();
                regionList.add(regionEntity);
            }
        }
        obj.setRegionList(regionList);
        return BasicResult.success(obj);
    }
 
    @Override
    public BasicResult regionInfo(String regionInspectionId, String userId, String regionHazardInformId) {
        return BasicResult.success(
                regionInspectionRecordMapper.regionInfo(
                regionInspectionId,
                userId,
                regionHazardInformId));
    }
 
    @Override
    public BasicResult selectAppList(Map map) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        String userId = sysUserInfo.getUserId();
        String platUserId = regionInspectionRecordMapper.selectPlatUserId(userId);
        map.put("userId",platUserId);
        map.put("companyId",companyId);
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        return BasicResult.success(new PageInfo<>(regionInspectionRecordMapper.selectAppList(map)));
    }
 
    @Override
    public void export(Map map, HttpServletResponse response) {
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String companyId = sysUserInfo.getCompanyId();
        String startHour = " 00:00:00";
        String endHour = " 23:59:59";
        String startTime = map.get("startTime").toString();
        String endTime = map.get("endTime").toString();
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
            startTime = startTime+startHour;
            endTime = endTime +endHour;
            map.put("startTime",startTime);
            map.put("endTime",endTime);
        }
        if (StringUtils.isNotBlank(map.get("status").toString())){
            int intStatus = Integer.valueOf(map.get("status").toString());
            map.put("intStatus",intStatus);
        }
        map.put("companyId",companyId);
        List<RegionInspectionRecordEntity> recordEntities = regionInspectionRecordMapper.selectPageList(map);
        ExcelUtils.downExcel(recordEntities,RegionInspectionRecordEntity.class,response,"打卡导出");
    }
}