xuhy
2025-12-31 ffef20d703aaa9b60c9234dd0b42f3d006426dc7
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.system.export.LargeKeyGridUserImport;
import com.ruoyi.system.model.TLargeKeyGridUser;
import com.ruoyi.system.model.TLargeKeyPersonnelType;
import com.ruoyi.system.query.TLargeKeyGridUserQuery;
import com.ruoyi.system.service.TLargeKeyGridUserService;
import com.ruoyi.system.service.TLargeKeyPersonnelTypeService;
import com.ruoyi.system.vo.TLargeKeyGridUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 大屏-重点人员-网格人员 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-11-11
 */
@Slf4j
@Api(tags = "大屏-重点人员-网格人员")
@RestController
@RequestMapping("/t-large-key-grid-user")
public class TLargeKeyGridUserController {
 
    private final TLargeKeyGridUserService largeKeyGridUserService;
    private final TLargeKeyPersonnelTypeService largeKeyPersonnelTypeService;
    @Autowired
    public TLargeKeyGridUserController(TLargeKeyGridUserService largeKeyGridUserService, TLargeKeyPersonnelTypeService largeKeyPersonnelTypeService) {
        this.largeKeyGridUserService = largeKeyGridUserService;
        this.largeKeyPersonnelTypeService = largeKeyPersonnelTypeService;
    }
 
    /**
     * 获取重点人员-网格人员管理列表
     */
    @ApiOperation(value = "获取重点人员-网格人员分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TLargeKeyGridUserVO>> pageList(@Validated @RequestBody TLargeKeyGridUserQuery query) {
        return R.ok(largeKeyGridUserService.pageList(query));
    }
    /**
     * 获取重点人员-网格人员管理列表
     */
    @ApiOperation(value = "获取重点人员-网格人员分页列表-大屏使用")
    @PostMapping(value = "/pageListLarge")
    public R<PageInfo<TLargeKeyGridUserVO>> pageListLarge(@RequestBody TLargeKeyGridUserQuery query) {
        return R.ok(largeKeyGridUserService.pageList(query));
    }
 
    /**
     * 获取重点人员-网格人员管理列表
     */
    @ApiOperation(value = "获取重点人员-网格人员列表")
    @PostMapping(value = "/list")
    public R<List<TLargeKeyGridUser>> list() {
        return R.ok(largeKeyGridUserService.list(Wrappers.lambdaQuery(TLargeKeyGridUser.class).orderByDesc(TLargeKeyGridUser::getCreateTime)));
    }
 
    /**
     * 添加重点人员-网格人员管理
     */
    @Log(title = "重点人员-网格人员信息-新增重点人员-网格人员", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加重点人员-网格人员")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TLargeKeyGridUser dto) {
        return R.ok(largeKeyGridUserService.save(dto));
    }
 
    /**
     * 修改重点人员-网格人员
     */
    @Log(title = "重点人员-网格人员信息-修改重点人员-网格人员", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改重点人员-网格人员")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TLargeKeyGridUser dto) {
        return R.ok(largeKeyGridUserService.updateById(dto));
    }
 
    /**
     * 查看重点人员-网格人员详情
     */
    @ApiOperation(value = "查看重点人员-网格人员详情")
    @GetMapping(value = "/getDetailById")
    public R<TLargeKeyGridUser> getDetailById(@RequestParam String id) {
        return R.ok(largeKeyGridUserService.getById(id));
    }
 
    /**
     * 删除重点人员-网格人员
     */
    @Log(title = "重点人员-网格人员信息-删除重点人员-网格人员", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除重点人员-网格人员")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(largeKeyGridUserService.removeById(id));
    }
 
    /**
     * 批量删除重点人员-网格人员
     */
    @Log(title = "重点人员-网格人员信息-删除重点人员-网格人员", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除重点人员-网格人员")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        return R.ok(largeKeyGridUserService.removeByIds(ids));
    }
 
    @ApiOperation(value = "重点人员-网格人员导入模板下载")
    @GetMapping("/import-template")
    public void importTemplate() {
        List<LargeKeyGridUserImport> largeLargeKeyGridUserImports = new ArrayList<>();
        LargeKeyGridUserImport largeLargeKeyGridUserImport = new LargeKeyGridUserImport();
        largeLargeKeyGridUserImports.add(largeLargeKeyGridUserImport);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), LargeKeyGridUserImport.class, largeLargeKeyGridUserImports);
        HttpServletResponse response = WebUtils.response();
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("重点人员-网格人员信息导入模板.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("重点人员-网格人员导入模板下载失败!");
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    @Log(title = "重点人员-网格人员信息-重点人员信息导入", businessType = BusinessType.INSERT)
    @ApiOperation(value = "重点人员信息导入")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/import")
    public R<String> importInfo(@RequestPart("file") MultipartFile file) {
        List<TLargeKeyGridUser> largeKeyGridUsers = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        List<LargeKeyGridUserImport> largeKeyGridUserImports;
        try {
            inputStream = file.getInputStream();
            largeKeyGridUserImports = ExcelImportUtil.importExcel(inputStream,
                    LargeKeyGridUserImport.class, params);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("重点人员信息导入失败:{}", e.getMessage());
            throw new ServiceException("重点人员信息导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (CollectionUtils.isEmpty(largeKeyGridUserImports)) {
            throw new ServiceException("重点人员信息数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (LargeKeyGridUserImport largeKeyGridUserImport : largeKeyGridUserImports) {
            if (!StringUtils.hasLength(largeKeyGridUserImport.getGridInfo())){
                result.append("第"+i+"行:[", "网格"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getCommunityName())){
                result.append("第"+i+"行:[", "小区名称"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getBuildingInfo())){
                result.append("第"+i+"行:[", "楼栋信息"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUnitNumber())){
                result.append("第"+i+"行:[", "单元号"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getHouseNumber())){
                result.append("第"+i+"行:[", "门牌号"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUserName())){
                result.append("第"+i+"行:[", "姓名"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUserSexStr())){
                result.append("第"+i+"行:[", "性别"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getIdCard())){
                result.append("第"+i+"行:[", "身份证号"+"]不能为空");
                i++;
                continue;
            }
            if (!largeKeyGridUserImport.getIdCard().matches("^$|(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)")){
                result.append("第"+i+"行:[", "身份证号"+"]格式不正确");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getContactNumber())){
                result.append("第"+i+"行:[", "联系电话"+"]不能为空");
                i++;
                continue;
            }
            if (!largeKeyGridUserImport.getContactNumber().matches("^$|^1[3-9]\\d{9}$")){
                result.append("第"+i+"行:[", "联系电话"+"]格式不正确");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getTypeName())){
                result.append("第"+i+"行:[", "类型名称"+"]不能为空");
                i++;
                continue;
            }
            TLargeKeyGridUser largeKeyGridUser = new TLargeKeyGridUser();
            BeanUtils.copyProperties(largeKeyGridUserImport, largeKeyGridUser);
 
            switch (largeKeyGridUserImport.getUserSexStr()){
                case "男":
                    largeKeyGridUser.setUserSex(1);
                    break;
                case "女":
                    largeKeyGridUser.setUserSex(2);
                    break;
                default:
                    result.append("第"+i+"行:[", "性别"+"]格式错误");
                    i++;
                    continue;
            }
 
            TLargeKeyPersonnelType largeKeyPersonnelType = largeKeyPersonnelTypeService.getOne(Wrappers.lambdaQuery(TLargeKeyPersonnelType.class)
                    .eq(TLargeKeyPersonnelType::getTypeName, largeKeyGridUserImport.getTypeName())
                    .last("LIMIT 1"));
            if(Objects.isNull(largeKeyPersonnelType)){
                result.append("第"+i+"行:[", "类型名称"+"]不存在");
                i++;
                continue;
            }
            largeKeyGridUser.setTypeId(largeKeyPersonnelType.getId());
            largeKeyGridUser.setUserType(1);
            largeKeyGridUsers.add(largeKeyGridUser);
            i++;
        }
        largeKeyGridUserService.saveBatch(largeKeyGridUsers);
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
 
    @Log(title = "重点人员-网格人员信息-网格人员信息导入", businessType = BusinessType.INSERT)
    @ApiOperation(value = "网格人员信息导入")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/importGrid")
    public R<String> importGrid(@RequestPart("file") MultipartFile file) {
        List<TLargeKeyGridUser> largeKeyGridUsers = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        List<LargeKeyGridUserImport> largeKeyGridUserImports;
        try {
            inputStream = file.getInputStream();
            largeKeyGridUserImports = ExcelImportUtil.importExcel(inputStream,
                    LargeKeyGridUserImport.class, params);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("网格人员信息导入失败:{}", e.getMessage());
            throw new ServiceException("网格人员信息导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (CollectionUtils.isEmpty(largeKeyGridUserImports)) {
            throw new ServiceException("网格人员信息数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (LargeKeyGridUserImport largeKeyGridUserImport : largeKeyGridUserImports) {
            if (!StringUtils.hasLength(largeKeyGridUserImport.getGridInfo())){
                result.append("第"+i+"行:[", "网格"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getCommunityName())){
                result.append("第"+i+"行:[", "小区名称"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getBuildingInfo())){
                result.append("第"+i+"行:[", "楼栋信息"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUnitNumber())){
                result.append("第"+i+"行:[", "单元号"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getHouseNumber())){
                result.append("第"+i+"行:[", "门牌号"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUserName())){
                result.append("第"+i+"行:[", "姓名"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUserSexStr())){
                result.append("第"+i+"行:[", "性别"+"]不能为空");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getIdCard())){
                result.append("第"+i+"行:[", "身份证号"+"]不能为空");
                i++;
                continue;
            }
            if (!largeKeyGridUserImport.getIdCard().matches("^$|(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)")){
                result.append("第"+i+"行:[", "身份证号"+"]格式不正确");
                i++;
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getContactNumber())){
                result.append("第"+i+"行:[", "联系电话"+"]不能为空");
                i++;
                continue;
            }
            if (!largeKeyGridUserImport.getContactNumber().matches("^$|^1[3-9]\\d{9}$")){
                result.append("第"+i+"行:[", "联系电话"+"]格式不正确");
                i++;
                continue;
            }
            TLargeKeyGridUser largeKeyGridUser = new TLargeKeyGridUser();
            BeanUtils.copyProperties(largeKeyGridUserImport, largeKeyGridUser);
 
            switch (largeKeyGridUserImport.getUserSexStr()){
                case "男":
                    largeKeyGridUser.setUserSex(1);
                    break;
                case "女":
                    largeKeyGridUser.setUserSex(2);
                    break;
                default:
                    result.append("第"+i+"行:[", "性别"+"]格式错误");
                    i++;
                    continue;
            }
 
            if(StringUtils.hasLength(largeKeyGridUserImport.getTypeName())){
                TLargeKeyPersonnelType largeKeyPersonnelType = largeKeyPersonnelTypeService.getOne(Wrappers.lambdaQuery(TLargeKeyPersonnelType.class)
                        .eq(TLargeKeyPersonnelType::getTypeName, largeKeyGridUserImport.getTypeName())
                        .last("LIMIT 1"));
                if(Objects.isNull(largeKeyPersonnelType)){
                    result.append("第"+i+"行:[", "类型名称"+"]不存在");
                    i++;
                    continue;
                }
                largeKeyGridUser.setTypeId(largeKeyPersonnelType.getId());
            }
            largeKeyGridUser.setUserType(2);
            largeKeyGridUsers.add(largeKeyGridUser);
            i++;
        }
        largeKeyGridUserService.saveBatch(largeKeyGridUsers);
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
 
}