| | |
| | | package com.whyc.controller; |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.api.R; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.Dept; |
| | | import com.whyc.pojo.Job; |
| | | import com.whyc.pojo.User; |
| | | import com.whyc.service.DeptService; |
| | | import com.whyc.service.JobService; |
| | | import com.whyc.service.LoginService; |
| | | import com.whyc.service.UserService; |
| | | import com.whyc.util.DigestsUtil; |
| | | import com.whyc.util.UserExcelVerify; |
| | | import io.swagger.annotations.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.shiro.authz.annotation.Logical; |
| | | import org.apache.shiro.authz.annotation.RequiresPermissions; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | |
| | | @Resource |
| | | private UserService userService; |
| | | |
| | | @Resource |
| | | private DeptService deptService; |
| | | |
| | | @Resource |
| | | private JobService jobService; |
| | | |
| | | @Resource |
| | | private UserExcelVerify userExcelVerify; |
| | | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加") |
| | | public boolean add(@RequestBody User user){ |
| | | user.setCreateTime(new Date()); |
| | | @ApiOperation(value = "添加用户") |
| | | //@RequiresPermissions(value = {"user:add","user:update"},logical = Logical.OR) |
| | | public Response add(@RequestBody User user){ |
| | | return userService.add(user); |
| | | } |
| | | |
| | | @PostMapping("/batch") |
| | | @ApiOperation(value = "添加批量") |
| | | public boolean addBatch(@RequestBody List<User> users){ |
| | | for (User temp:users){ |
| | | temp.setCreateTime(new Date()); |
| | | } |
| | | return userService.addBatch(users); |
| | | } |
| | | |
| | | @GetMapping |
| | | @ApiOperation(value = "查询byId") |
| | | public User getById(@RequestParam int id){ |
| | | return userService.getById(id); |
| | | @ApiOperation(value = "查询用户byId") |
| | | //@RequiresPermissions(value = {"user:get"}) |
| | | public Response getById(@RequestParam int id){ |
| | | User user = userService.getById(id); |
| | | if (user==null){ |
| | | return new Response().set(0,null,"此用户不存在"); |
| | | } |
| | | return new Response().set(1,user); |
| | | } |
| | | |
| | | @GetMapping("/all") |
| | | @ApiOperation(value = "查询所有") |
| | | @ApiOperation(value = "查询所有用户") |
| | | //@RequiresPermissions(value = {"user:get"}) |
| | | public List<User> getAll(){ |
| | | return userService.getAll(); |
| | | } |
| | | |
| | | @GetMapping("/page") |
| | | @ApiOperation(value = "查询分页") |
| | | public IPage<User> getPage(@RequestParam int pageNum,int pageSize){ |
| | | @ApiOperation(value = "查询用户分页") |
| | | //@RequiresPermissions(value = {"user:get"}) |
| | | public IPage<User> getPage(@RequestParam int pageNum,@RequestParam int pageSize){ |
| | | Page<Object> page = new Page<>(pageNum, pageSize); |
| | | return userService.getAllWithPage(page); |
| | | } |
| | | @PostMapping("/search") |
| | | @ApiOperation(value = "查询用户分页-根据筛选条件") |
| | | //@RequiresPermissions(value = {"user:get"}) |
| | | public Response<IPage<User>> getPageByCondition(@RequestParam int pageNum,@RequestParam int pageSize,@RequestBody User user){ |
| | | return userService.getPageByCondition(pageNum,pageSize,user); |
| | | } |
| | | |
| | | @GetMapping("validatePassword") |
| | | @ApiOperation(value = "验证密码") |
| | | public boolean validatePassword(@RequestParam int id, @RequestParam String password){ |
| | | return userService.validatePassword(id,password); |
| | | } |
| | | @GetMapping("getSaltPassword") |
| | | @ApiOperation(value = "获取加密密码") |
| | | public String getSaltPassword(@RequestParam String password,@RequestParam String salt){ |
| | | return DigestsUtil.sha1(password,salt); |
| | | } |
| | | @PutMapping("updatePassword") |
| | | @ApiOperation(value = "修改密码") |
| | | public Response updatePassword(@RequestParam int id,@RequestParam String password){ |
| | | return userService.updatePassword(id,password); |
| | | } |
| | | |
| | | @PutMapping("resetPassword") |
| | | @ApiOperation(value = "重置密码") |
| | | public Response resetPassword(@RequestParam int id){ |
| | | return userService.resetPassword(id); |
| | | } |
| | | |
| | | |
| | | @PutMapping |
| | | @ApiOperation(value = "编辑") |
| | | public boolean update(@RequestBody User user){ |
| | | @ApiOperation(value = "编辑用户") |
| | | //@RequiresPermissions(value = {"user:update"}) |
| | | public Response update(@RequestBody User user){ |
| | | return userService.update(user); |
| | | } |
| | | |
| | | @DeleteMapping |
| | | @ApiOperation(value = "删除") |
| | | public boolean delete(@RequestParam int id){ |
| | | return userService.delete(id); |
| | | @PutMapping("freezeUser") |
| | | @ApiOperation(value = "启动/冻结用户") |
| | | //@RequiresPermissions(value = {"user:update"}) |
| | | public Response updateUserFreeze(@RequestParam int id,@RequestParam int state){ |
| | | return userService.updateUserFreeze(id,state); |
| | | } |
| | | |
| | | @PostMapping("login") |
| | | @ApiOperation(value = "登录",position = 1) |
| | | public boolean login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){ |
| | | return userService.login(username,password,request); |
| | | @PutMapping("deleteUser") |
| | | @ApiOperation(value = "删除用户") |
| | | //@RequiresPermissions(value = {"user:delete"}) |
| | | public Response delete(@RequestParam int id){ |
| | | return userService.delete(id); |
| | | } |
| | | |
| | | /** |
| | |
| | | return userService.loginCheck(request); |
| | | } |
| | | |
| | | |
| | | |
| | | @GetMapping("/getDepts") |
| | | @ApiOperation(value = "获取部门") |
| | | public List<Dept> getDepts(){ |
| | | return deptService.getAll(); |
| | | } |
| | | @PostMapping("addDept") |
| | | @ApiOperation(value = "添加部门") |
| | | public Response addDept(@RequestBody Dept dept){ |
| | | return deptService.add(dept); |
| | | } |
| | | |
| | | @PutMapping("updateDept") |
| | | @ApiOperation(value = "编辑部门") |
| | | public Response update(@RequestBody Dept dept){ |
| | | return deptService.update(dept); |
| | | } |
| | | |
| | | @DeleteMapping("deleteDept") |
| | | @ApiOperation(value = "删除部门") |
| | | public Response deleteDept(@RequestParam int id){ |
| | | return deptService.delete(id); |
| | | } |
| | | |
| | | |
| | | @GetMapping("/getJobs") |
| | | @ApiOperation(value = "获取职务") |
| | | public List<Job> getJobs(){ |
| | | return jobService.getAll(); |
| | | } |
| | | |
| | | @PostMapping("addJob") |
| | | @ApiOperation(value = "添加职务") |
| | | public Response addJob(@RequestBody Job job){ |
| | | return jobService.add(job); |
| | | } |
| | | @PutMapping("updateJob") |
| | | @ApiOperation(value = "编辑职务") |
| | | public Response update(@RequestBody Job job){ |
| | | return jobService.update(job); |
| | | } |
| | | |
| | | @DeleteMapping("deleteJob") |
| | | @ApiOperation(value = "删除职务") |
| | | public Response deleteJob(@RequestParam int id){ |
| | | return jobService.delete(id); |
| | | } |
| | | |
| | | |
| | | @SuppressWarnings("AlibabaRemoveCommentedCode") |
| | | @PostMapping("/importUserExcel") |
| | | @ApiOperation(value = "导入用户数据") |
| | | public Response importExcel2(@RequestParam("file") MultipartFile file) { |
| | | Response response = new Response(); |
| | | ImportParams importParams = new ImportParams(); |
| | | // 数据处理 |
| | | //设置标题第1行开始 |
| | | importParams.setTitleRows(0); |
| | | //设置开始行从第2行开始 |
| | | importParams.setHeadRows(1); |
| | | // 需要验证 |
| | | importParams.setNeedVerify(true); |
| | | //设及一个自定义校验 (自定义校验名字不可重复) 使用注解注入 |
| | | //UserExcelVerify userExcelVerify = new UserExcelVerify(); |
| | | importParams.setVerifyHandler(userExcelVerify); |
| | | try { |
| | | ExcelImportResult<User> result = ExcelImportUtil.importExcelMore(file.getInputStream(), User.class, |
| | | importParams); |
| | | List<User> successList = result.getList(); |
| | | //失败结果集 |
| | | List<User> failList = result.getFailList(); |
| | | /*failList.forEach(user -> { |
| | | System.out.println("验证失败的信息:"+user+userExcelVerify.verifyHandler(user).getMsg()); |
| | | }); |
| | | successList.forEach(x->{ |
| | | System.out.println("通过验证的数据:"+x.toString()); |
| | | });*/ |
| | | String msg = ""; |
| | | for (User entity : result.getFailList()) { |
| | | msg += "第" + entity.getRowNum() + "行的错误是:" + entity.getErrorMsg()+";\n"; |
| | | //System.out.println(msg); |
| | | } |
| | | if (!successList.isEmpty()){ |
| | | boolean success = userService.addBatch(successList); |
| | | if (success && failList.isEmpty()){ |
| | | response.set(1,"成功导入"+successList.size()+"条数据;"); |
| | | } |
| | | }else{ |
| | | response.set(1,failList,"成功导入"+successList.size()+"条数据"+";导入失败"+failList.size()+"条数据;\n"+msg); |
| | | } |
| | | |
| | | |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | response.setMsg(0,"导入失败"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | response.setMsg(0,"导入失败"); |
| | | } |
| | | return response; |
| | | } |
| | | |
| | | } |