青羊经侦大队-数据平台
baizonghao
2023-05-22 aa8836a65e97e297340fff3a42615f0a36f314e7
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
package com.example.jz.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.jz.modle.R;
import com.example.jz.modle.dto.ManagerDto;
import com.example.jz.modle.dto.UserExpDto;
import com.example.jz.modle.entity.*;
import com.example.jz.service.*;
import com.example.jz.utils.EasyExcelUtils;
import com.example.jz.utils.Md5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * 用户表(User)表控制层
 *
 * @author 安瑾然
 * @since 2022-07-11 16:35:57
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户管理")
public class UserController extends ApiController {
    private UserService userService;
 
    @Autowired
    ReportService reportService;
    @Autowired
    GroupUserService groupUserService;
    @Autowired
    MessageService messageService;
    @Autowired
    CauseService causeService;
 
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
 
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("分页查询所有数据")
    public R selectAll(Page<User> page, User user) {
        R<Page<User>> res = R.ok(this.userService.page(page, new QueryWrapper<User>()
                .eq("role", 0)
                .and(StringUtils.isNotBlank(user.getUserMobile()) && StringUtils.isNotBlank(user.getRealName()), new Consumer<QueryWrapper<User>>() {
                    @Override
                    public void accept(QueryWrapper<User> userQueryWrapper) {
                        userQueryWrapper.like("real_name", user.getRealName()).or().like("user_mobile", user.getUserMobile());
                    }
                })
                .like(StringUtils.isNotBlank(user.getUserMobile()) && StringUtils.isBlank(user.getRealName()), "user_mobile", user.getUserMobile())
                .and(StringUtils.isNotBlank(user.getRealName()) && StringUtils.isBlank(user.getNickName()), new Consumer<QueryWrapper<User>>() {
                            @Override
                            public void accept(QueryWrapper<User> userQueryWrapper) {
                                userQueryWrapper.like("real_name", user.getRealName());
                            }
                        })
//                .like(StringUtils.isNotBlank(user.getRealName()) && StringUtils.isBlank(user.getNickName()), "real_name", user.getRealName())
                .orderByDesc("ctime")));
        return res;
    }
 
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.userService.getById(id));
    }
 
    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改用户数据")
    public R update(@RequestBody User user) {
        return R.ok(userService.updateById(user));
    }
 
    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("删除用户数据")
    public R delete(@RequestParam("id") Integer id) {
        reportService
                .list(new LambdaQueryWrapper<Report>().eq(Report::getUserId, id))
                .stream()
                .forEach(item -> {
                    groupUserService.remove(new LambdaQueryWrapper<GroupUser>().eq(GroupUser::getUserId, item.getId()));
                    messageService.remove(new LambdaQueryWrapper<Message>().eq(Message::getUserId, item.getId()));
                });
        reportService.remove(new LambdaQueryWrapper<Report>().eq(Report::getUserId, id));
        return R.ok(this.userService.removeById(id));
    }
 
 
    @GetMapping("/status")
    @ApiOperation("禁用/启用用户")
    public R updateStatus(Integer id, Integer status) {
        int newStatus = 0;
        if (status != null) {
            if (status == 1) {
                newStatus = 0;
            } else {
                newStatus = 1;
            }
        }
        userService.update(new UpdateWrapper<User>().set("status", newStatus).eq("id", id));
        return R.ok();
    }
 
    @PostMapping("/manager")
    @ApiOperation("添加管理")
    public R addManager(@RequestBody ManagerDto managerDto) {
        User user = new User().setRealName(managerDto.getRealName())
                .setLoginUsername(managerDto.getRealName())
                .setUserMobile(managerDto.getPhoneNumber())
                .setLoginPassword(Md5Utils.md5(managerDto.getPassword()))
                .setRole(1)
                .setCtime(new Date());
        return R.ok(userService.save(user));
    }
 
    @DeleteMapping("/manager")
    @ApiOperation("删除管理人员")
    public R removeManager(@RequestParam Integer id) {
        List<Cause> list = causeService.list(new LambdaQueryWrapper<Cause>().eq(Cause::getUserId, id));
        if (!Objects.isNull(list) && list.size() != 0){
            ArrayList<Map> maps = new ArrayList<>();
            for (Cause cause : list) {
                HashMap<String, String> map = new HashMap<>();
                map.put("caseName", cause.getName());
                map.put("caseNumber", cause.getNumber());
                maps.add(map);
            }
            return R.failed(maps, "该警员还有案件关联,若要删除请先修改对应案件关联的警员");
        }
//        causeService.list(new LambdaQueryWrapper<Cause>().eq(Cause::getUserId, id)).forEach(item -> {
//            causeService.deleteCause(item.getId());
//        });
//        userService.remove(new LambdaQueryWrapper<User>().eq(User::getId, id));
        messageService.remove(new LambdaQueryWrapper<Message>().eq(Message::getCopId, id));
        return R.ok(userService.removeById(id));
    }
 
    @DeleteMapping("/crowd")
    @ApiOperation("删除普通用户")
    public R removeCrowd(@RequestParam Integer id){
        List<Report> list = reportService.list(Wrappers.<Report>lambdaQuery().eq(Report::getUserId, id));
        for (Report report : list) {
            groupUserService.remove(new LambdaQueryWrapper<GroupUser>().eq(GroupUser::getUserId, report.getId()));
            messageService.remove(new LambdaQueryWrapper<Message>().eq(Message::getUserId, report.getId()));
            reportService.removeById(report.getId());
        }
        userService.remove(Wrappers.<User>lambdaQuery().eq(User::getId, id));
        return R.ok();
    }
 
    @GetMapping("/manager")
    @ApiOperation("分页查询所有数据")
    public R selectManager(Page<User> page, User user) {
        return R.ok(userService.page(page, new QueryWrapper<User>()
                .eq("role", 1).ne("id", 1)
                .and(StringUtils.isNotBlank(user.getUserMobile()) && StringUtils.isNotBlank(user.getRealName()), new Consumer<QueryWrapper<User>>() {
                    @Override
                    public void accept(QueryWrapper<User> userQueryWrapper) {
                        userQueryWrapper.like("real_name", user.getRealName()).or().like("user_mobile", user.getUserMobile());
                    }
                })
                .like(StringUtils.isNotBlank(user.getUserMobile()) && StringUtils.isBlank(user.getRealName()), "user_mobile", user.getUserMobile())
                .or()
                .like(StringUtils.isNotBlank(user.getRealName()) && StringUtils.isBlank(user.getUserMobile()), "real_name", user.getRealName())
                .orderByDesc("ctime")));
    }
 
    @PutMapping("/manager")
    @ApiOperation("修改管理员密码")
    public R modify(@RequestBody ManagerDto managerDto) {
        User user = new User().setLoginPassword(Md5Utils.md5(managerDto.getPassword()));
        return R.ok(userService.update(user, new UpdateWrapper<User>().eq("user_mobile", managerDto.getPhoneNumber()).eq("real_name", managerDto.getRealName())));
    }
 
    @PostMapping("/add")
    @ApiOperation("添加")
    public R add(@RequestBody User user) {
        user.setCtime(new Date());
        return R.ok(userService.save(user));
    }
 
 
    /**
     * 重置管理员密码
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/resetPassword/{id}")
    @ApiOperation("重置管理员密码")
    public R resetPassword(@PathVariable Serializable id) {
        User user = userService.getById(id);
        // 重置初始密码为身份证后六位
        user.setLoginPassword(Md5Utils.md5(user.getUserIdcard().substring(user.getUserIdcard().length() - 6)));
        return R.ok(userService.updateById(user));
    }
 
    /**
     * 转为管理员
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/toManager/{id}")
    @ApiOperation("转为管理员")
    public R toManager(@PathVariable Serializable id) {
        User user = userService.getById(id);
        user.setRole(1);
        return R.ok(userService.updateById(user));
    }
 
 
    @ApiOperation("用户信息导出")
    @PostMapping("/exp")
    public void UserExp(HttpServletResponse response){
        List<User> list = userService.list(Wrappers.<User>lambdaQuery().eq(User::getRole, 0).eq(User::getStatus, 1));
        List<UserExpDto> userExpDtos = list.stream().map(user -> {
            UserExpDto userExpDto = new UserExpDto();
            BeanUtils.copyProperties(user, userExpDto);
            return userExpDto;
        }).collect(Collectors.toList());
        String sheetName = "用户导出";
        EasyExcelUtils.export1(response, sheetName, UserExpDto.class, userExpDtos, "用户导出");
    }
}