xuhy
2025-11-07 bc396702694c57176a3414dc9375bb2846194691
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
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.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.system.export.OldsterImport;
import com.ruoyi.system.model.TOldster;
import com.ruoyi.system.query.BannerListQuery;
import com.ruoyi.system.query.OldsterListQuery;
import com.ruoyi.system.service.TOldsterService;
import com.ruoyi.system.service.TOldsterService;
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.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.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 老人户籍信息 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-11-06
 */
@RestController
@Slf4j
@RequestMapping("/t-oldster")
@Api(tags = "老人户籍信息")
public class TOldsterController {
    @Autowired
    private TOldsterService oldsterService; 
    @ApiOperation(value = "老人户籍信息分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TOldster>> pageList(@RequestBody OldsterListQuery query) {
        return R.ok(oldsterService.pageList(query));
    }
 
    @ApiOperation(value = "添加")
    @Transactional
    @Log(title = "老人户籍信息-添加", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public R<Boolean> save(@RequestBody TOldster dto) {
        oldsterService.save(dto);
        return R.ok();
    }
    @ApiOperation(value = "修改")
    @Log(title = "老人户籍信息-修改", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public R<Boolean> edit(@RequestBody TOldster dto) {
        oldsterService.updateById(dto);
        return R.ok();
    }
    /**
     * 删除老人户籍信息
     */
    @Log(title = "老人户籍信息-删除", businessType = BusinessType.DELETE)
    @ApiOperation(value = "老人户籍信息-删除")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(oldsterService.removeById(id));
    }
 
    /**
     * 批量删除老人户籍信息
     */
    @Log(title = "老人户籍信息-批量删除", businessType = BusinessType.DELETE)
    @ApiOperation(value = "老人户籍信息-批量删除")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        return R.ok(oldsterService.removeByIds(ids));
    }
    @ApiOperation(value = "导入模板下载")
    @GetMapping("/import-template")
    public void importTemplate() {
        List<OldsterImport> assetHouseInspectionImports = new ArrayList<>();
        OldsterImport temp = new OldsterImport();
        assetHouseInspectionImports.add(temp);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), OldsterImport.class, assetHouseInspectionImports);
        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> importLocation(@RequestPart("file") MultipartFile file) {
        List<TOldster> oldsters = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        List<OldsterImport> assetHouseInspectionImports;
        try {
            inputStream = file.getInputStream();
            assetHouseInspectionImports = ExcelImportUtil.importExcel(inputStream,
                    OldsterImport.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(assetHouseInspectionImports)) {
            throw new ServiceException("老人户籍信息数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (OldsterImport oldsterImport : assetHouseInspectionImports) {
            if (!StringUtils.hasLength(oldsterImport.getOldsterName())){
                result.append("第"+i+"行:[", "老人姓名"+"]不能为空");
            }
            if (oldsterImport.getOldsterAge()!=null){
                result.append("第"+i+"行:[", "老人年龄"+"]不能为空");
            }
            if (oldsterImport.getOldsterSex()!=null){
                result.append("第"+i+"行:[", "老人性别"+"]不能为空");
            }
            if (!StringUtils.hasLength(oldsterImport.getContactPhone())){
                result.append("第"+i+"行:[", "联系方式"+"]不能为空");
            }
            if (oldsterImport.getOldsterType()!=null){
                result.append("第"+i+"行:[", "人员类型"+"]不能为空");
            }
            if (!StringUtils.hasLength(oldsterImport.getAddress())){
                result.append("第"+i+"行:[", "居住地址"+"]不能为空");
            }
            TOldster tOldster = new TOldster();
            BeanUtils.copyProperties(oldsterImport, tOldster);
            oldsters.add(tOldster);
            i++;
        }
        oldsterService.saveBatch(oldsters);
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
}