| | |
| | | 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.api.R; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.example.jz.modle.entity.User; |
| | | import com.example.jz.service.UserService; |
| | | 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.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.Serializable; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.function.Consumer; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 用户表(User)表控制层 |
| | | * |
| | | * @author makejava |
| | | * @author 安瑾然 |
| | | * @since 2022-07-11 16:35:57 |
| | | */ |
| | | @RestController |
| | | @RequestMapping("user") |
| | | @Api(tags = "用户管理") |
| | | public class UserController extends ApiController { |
| | | /** |
| | | * 服务对象 |
| | | */ |
| | | @Resource |
| | | private UserService userService; |
| | | |
| | | @Autowired |
| | | ReportService reportService; |
| | | @Autowired |
| | | GroupUserService groupUserService; |
| | | @Autowired |
| | | MessageService messageService; |
| | | @Autowired |
| | | CauseService causeService; |
| | | |
| | | @Autowired |
| | | public void setUserService(UserService userService) { |
| | | this.userService = userService; |
| | | } |
| | | |
| | | /** |
| | | * 分页查询所有数据 |
| | |
| | | * @return 所有数据 |
| | | */ |
| | | @GetMapping |
| | | @ApiOperation("分页查询所有数据") |
| | | public R selectAll(Page<User> page, User user) { |
| | | return success(this.userService.page(page, new QueryWrapper<>(user))); |
| | | return R.ok(this.userService.page(page, new QueryWrapper<User>() |
| | | .eq("role", 0) |
| | | .and(StringUtils.isNotBlank(user.getNickName()) && StringUtils.isNotBlank(user.getRealName()), new Consumer<QueryWrapper<User>>() { |
| | | @Override |
| | | public void accept(QueryWrapper<User> userQueryWrapper) { |
| | | userQueryWrapper.like("real_name", user.getRealName()).or().like("nick_name", user.getNickName()); |
| | | } |
| | | }) |
| | | .like(StringUtils.isNotBlank(user.getNickName()) && StringUtils.isBlank(user.getRealName()), "nick_name", user.getNickName()) |
| | | .or() |
| | | .like(StringUtils.isNotBlank(user.getRealName()) && StringUtils.isBlank(user.getNickName()), "real_name", user.getRealName()) |
| | | .orderByDesc("ctime"))); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 单条数据 |
| | | */ |
| | | @GetMapping("{id}") |
| | | @ApiOperation("通过主键查询单条数据") |
| | | public R selectOne(@PathVariable Serializable id) { |
| | | return success(this.userService.getById(id)); |
| | | } |
| | | |
| | | /** |
| | | * 新增数据 |
| | | * |
| | | * @param user 实体对象 |
| | | * @return 新增结果 |
| | | */ |
| | | @PostMapping |
| | | public R insert(@RequestBody User user) { |
| | | return success(this.userService.save(user)); |
| | | return R.ok(this.userService.getById(id)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 修改结果 |
| | | */ |
| | | @PutMapping |
| | | @ApiOperation("修改用户数据") |
| | | public R update(@RequestBody User user) { |
| | | return success(this.userService.updateById(user)); |
| | | return R.ok(userService.updateById(user)); |
| | | } |
| | | |
| | | /** |
| | | * 删除数据 |
| | | * |
| | | * @param idList 主键结合 |
| | | * @param id 主键结合 |
| | | * @return 删除结果 |
| | | */ |
| | | @DeleteMapping |
| | | public R delete(@RequestParam("idList") List<Long> idList) { |
| | | return success(this.userService.removeByIds(idList)); |
| | | @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) |
| | | .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, "用户导出"); |
| | | } |
| | | } |