| | |
| | | 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.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 JobService jobService; |
| | | |
| | | @Resource |
| | | private UserExcelVerify userExcelVerify; |
| | | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加用户") |
| | | //@RequiresPermissions(value = {"user:add","user:update"},logical = Logical.OR) |
| | | public Response add(@RequestBody User user){ |
| | | return userService.add(user); |
| | | } |
| | | |
| | | @GetMapping |
| | | @ApiOperation(value = "查询用户byId") |
| | | //@RequiresPermissions(value = {"user:get"}) |
| | | public Response getById(@RequestParam int id){ |
| | | User user = userService.getById(id); |
| | | if (user==null){ |
| | |
| | | |
| | | @GetMapping("/all") |
| | | @ApiOperation(value = "查询所有用户") |
| | | //@RequiresPermissions(value = {"user:get"}) |
| | | public List<User> getAll(){ |
| | | return userService.getAll(); |
| | | } |
| | | |
| | | @GetMapping("/page") |
| | | @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); |
| | | } |
| | | @GetMapping("/search") |
| | | @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 = "编辑用户") |
| | | //@RequiresPermissions(value = {"user:update"}) |
| | | public Response update(@RequestBody User user){ |
| | | return userService.update(user); |
| | | } |
| | | |
| | | @PutMapping("freezeUser") |
| | | @ApiOperation(value = "冻结用户") |
| | | public Response updateUserFreeze(@RequestParam int id){ |
| | | return userService.updateUserFreeze(id); |
| | | @ApiOperation(value = "启动/冻结用户") |
| | | //@RequiresPermissions(value = {"user:update"}) |
| | | public Response updateUserFreeze(@RequestParam int id,@RequestParam int state){ |
| | | return userService.updateUserFreeze(id,state); |
| | | } |
| | | |
| | | @PutMapping("deleteUser") |
| | | @ApiOperation(value = "删除用户") |
| | | //@RequiresPermissions(value = {"user:delete"}) |
| | | public Response delete(@RequestParam int id){ |
| | | return userService.deleteUser(id); |
| | | return userService.delete(id); |
| | | } |
| | | |
| | | /** |
| | |
| | | public Response addDept(@RequestBody Dept dept){ |
| | | return deptService.add(dept); |
| | | } |
| | | |
| | | @PutMapping("updateDept") |
| | | @ApiOperation(value = "编辑部门") |
| | | public Response update(@RequestBody Dept dept){ |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |