huliguo
2 天以前 d8143b9121bbe941f116230eaa5524ab2cc12a66
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
package com.linghu.controller;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linghu.model.common.ResponseResult;
import com.linghu.model.entity.Sectionalization;
import com.linghu.model.entity.User;
import com.linghu.model.excel.UserExcel;
import com.linghu.model.vo.UserPageVO;
import com.linghu.service.SectionalizationService;
import com.linghu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/user")
@Api(value = "账号相关接口", tags = "设置-账号管理")
public class UserController {
    @Autowired
    private UserService userService;
 
    /**
     * 新增用户
     */
 
    @PostMapping
    @ApiOperation(value = "添加")
    public ResponseResult add(@RequestBody User user) {
        List<User> list = userService.list(new LambdaQueryWrapper<User>().eq(User::getUser_email, user.getUser_email()));
        if (list != null && list.size() > 0) {
            return ResponseResult.error("该邮箱已存在");
        }
        user.setStatus("正常");
        userService.save(user);
        return ResponseResult.success();
 
    }
 
 
    /**
     * 修改用户
     */
    @PutMapping
    @ApiOperation(value = "修改")
    public ResponseResult edit(@RequestBody User user) {
        User user1 = userService.getById(user.getUser_id());
        if (user1 == null) {
            return ResponseResult.error("该账户不存在");
        }
        List<User> list = userService.list(new LambdaQueryWrapper<User>()
                        .ne(User::getUser_id, user.getUser_id())
                .eq(User::getUser_email, user.getUser_email()));
        if (list != null && list.size() > 0) {
            return ResponseResult.error("该邮箱已存在");
        }
        userService.updateById(user);
        return ResponseResult.success();
 
    }
 
    /**
     * 删除用户
     */
 
    @DeleteMapping("/{user_id}")
    @ApiOperation(value = "删除")
    public ResponseResult delete(@PathVariable("user_id") Integer user_id) {
        userService.removeById(user_id);
        return ResponseResult.success();
 
    }
    /**
     * 分页查询
     */
    @GetMapping
    @ApiOperation(value = "分页")
    public ResponseResult<Page<UserPageVO>> page(@RequestParam(value = "pageSize", required = false, defaultValue = "10")Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false,defaultValue = "1")Integer pageNum,
                                                 @RequestParam(value = "sectionalization_id",required = false)Integer sectionalization_id,
                                                 @RequestParam(value = "status" ,required = false)String status) {
        Page<UserPageVO> page = new Page<>(pageNum, pageSize);
        return ResponseResult.success( userService.getPage(page,sectionalization_id,status));
    }
 
    /**
     * 下载模板
     */
 
    @GetMapping("/downloadTemplate")
    @ApiOperation("下载模板")
    public ResponseEntity<byte[]> downloadTemplate() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, UserExcel.class).sheet("账号模板").doWrite(new ArrayList<>());
 
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=keyword_template.xlsx")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(out.toByteArray());
    }
 
 
    /**
     * 导入文件
     */
    // 导入文件
    @Autowired
    private SectionalizationService sectionalizationService;
    @PostMapping("/importUserExcel")
    @ApiOperation("导入用户数据")
    public ResponseResult importUserExcel(@RequestParam("file") MultipartFile file) {
        //查找出用户列表
        List<User> userList = userService.list();
        //名称
        Map<String, User> nameMap = userList.stream()
                .collect(Collectors.toMap(
                        User::getUser_name,  // Key映射
                        excel -> excel,  // Value映射
                        (oldValue, newValue) -> oldValue  // 键冲突处理(保留旧值)
                ));
 
        //查找出分组列表
        List<Sectionalization>  list = sectionalizationService.list();
        //分组
        Map<String, Sectionalization> sectionalizationMap = list.stream()
                .collect(Collectors.toMap(
                        Sectionalization::getSectionalization_name,  // Key映射
                        excel -> excel,  // Value映射
                        (oldValue, newValue) -> oldValue  // 键冲突处理(保留旧值)
                ));
 
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return ResponseResult.error("上传文件不能为空");
            }
 
            // 读取Excel数据
            List<UserExcel> excelList = EasyExcel.read(file.getInputStream())
                    .head(UserExcel.class)
                    .sheet()
                    .doReadSync();
 
            // 数据转换与验证
            List<String> errorMessages = new ArrayList<>();
            for (UserExcel excel : excelList) {
                // 检查必要字段
                if (!StringUtils.hasText(excel.getUser_name())) {
                    errorMessages.add("账户名不能为空");
                    continue;
                }
                if (!StringUtils.hasText(excel.getUser_email())) {
                    errorMessages.add("邮箱不能为空");
                    continue;
                }
                if (!StringUtils.hasText(String.valueOf(excel.getPassword()))) {
                    errorMessages.add("密码不能为空");
                    continue;
                }
 
                if (!StringUtils.hasText(excel.getSectionalization_name())) {
                    errorMessages.add("分组不能为空");
                    continue;
                }
                if (nameMap.containsKey(excel.getUser_name())){
                    errorMessages.add("用户名称重复");
                    continue;
                }
            }
            // 处理错误
            if (!errorMessages.isEmpty()) {
                return ResponseResult.error("数据验证失败: " + String.join("; ", errorMessages));
            }
            // 开始添加
            for (UserExcel excel : excelList) {
                User user = new User();
                if (sectionalizationMap.containsKey(excel.getSectionalization_name())) {
                    user.setSectionalization_id(sectionalizationMap.get(excel.getSectionalization_name()).getSectionalization_id());
                }else {
                    //新增
                    Sectionalization sectionalization = new Sectionalization();
                    sectionalization.setSectionalization_name(excel.getSectionalization_name());
                    sectionalizationService.save(sectionalization);
                    user.setSectionalization_id(sectionalization.getSectionalization_id());
                }
                user.setUser_email(excel.getUser_email());
                user.setUser_name(excel.getUser_name());
                user.setPassword(excel.getPassword());
                user.setStatus("正常");
                userService.save(user);
 
            }
            // 返回信息
            return ResponseResult.success();
        }
        catch (Exception e) {
            // 记录详细异常信息
 
            return ResponseResult.error("文件解析失败:" + e.getMessage());
        }
    }
 
 
 
}