| | |
| | | package com.ruoyi.web.controller.api; |
| | | |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.ruoyi.common.basic.PageDTO; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.BeanUtils; |
| | | import com.ruoyi.common.utils.ExcelUtil; |
| | | import com.ruoyi.system.domain.TbDept; |
| | | import com.ruoyi.common.utils.CollUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.system.dto.update.DeptFocusDTO; |
| | | import com.ruoyi.system.dto.update.DeptUpdateDTO; |
| | | import com.ruoyi.system.query.DeptQuery; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import com.ruoyi.system.service.TbDeptService; |
| | | import com.ruoyi.system.vo.DeptVO; |
| | | import com.ruoyi.web.controller.excel.DeptExcel; |
| | | import com.ruoyi.web.controller.lisenter.DeptImportListener; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.Optional; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | |
| | | private final HttpServletResponse response; |
| | | private final TbDeptService tbDeptService; |
| | | |
| | | private final ISysUserService sysUserService; |
| | | /** |
| | | * 导入模板下载 |
| | | */ |
| | |
| | | @ApiOperation("模板下载") |
| | | public void download() { |
| | | try { |
| | | ArrayList<TbDept> list = new ArrayList<>(); |
| | | ExcelUtil.exportExcel(list, "部门导入模板", "部门导入模板", TbDept.class, "部门导入模板", response); |
| | | //ArrayList<TbDept> list = new ArrayList<>(); |
| | | //ExcelUtil.exportExcel(list, "部门导入模板", "部门导入模板", TbDept.class, "部门导入模板", response); |
| | | // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 |
| | | String fileName = URLEncoder.encode("部门导入模板", "UTF-8"); |
| | | response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); |
| | | EasyExcel.write(response.getOutputStream(), DeptExcel.class).sheet("模板").doWrite(CollUtils.emptyList()); |
| | | } catch (Exception e) { |
| | | log.error("模板下载异常",e); |
| | | throw new ServiceException("模板下载失败,请联系管理员!"); |
| | |
| | | */ |
| | | @PostMapping("/import") |
| | | @ApiOperation("导入") |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R<Void> importExcel(@RequestPart("file")MultipartFile file) { |
| | | try { |
| | | tbDeptService.importExcel(file); |
| | | //tbDeptService.importExcel(file); |
| | | InputStream inputStream = file.getInputStream(); |
| | | EasyExcel.read(inputStream, DeptExcel.class, new DeptImportListener(sysUserService)).sheet().doRead(); |
| | | inputStream.close(); |
| | | } catch (Exception e) { |
| | | if (e instanceof ServiceException) { |
| | | return R.fail(e.getMessage()); |
| | |
| | | |
| | | /** |
| | | * 根据id查询部门详情 |
| | | * @param id 部门id |
| | | * @param userId 部门id |
| | | * @return DeptVO |
| | | */ |
| | | @GetMapping("/get-details") |
| | | @ApiOperation("根据id查询部门详情") |
| | | public R<DeptVO> getDetails(@RequestParam Integer id) { |
| | | public R<DeptVO> getDetails(@RequestParam Long userId) { |
| | | try { |
| | | TbDept dept = tbDeptService.getById(id); |
| | | return R.ok(BeanUtils.copyBean(dept,DeptVO.class)); |
| | | Optional<SysUser> sysUser = sysUserService.lambdaQuery().eq(SysUser::getUserId, userId).oneOpt(); |
| | | if (sysUser.isPresent()) { |
| | | return R.ok(BeanUtils.copyBean(sysUser.get(),DeptVO.class)); |
| | | } |
| | | } catch (Exception e) { |
| | | if (e instanceof ServiceException) { |
| | | return R.fail(e.getMessage()); |
| | |
| | | log.error("根据id查询部门详情异常", e); |
| | | return R.fail(); |
| | | } |
| | | return R.ok(new DeptVO()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @PostMapping("/edit") |
| | | @ApiOperation("编辑") |
| | | public R<Object> edit(@RequestBody DeptUpdateDTO dto){ |
| | | public R<Void> edit(@RequestBody DeptUpdateDTO dto){ |
| | | try { |
| | | TbDept tbDept = BeanUtils.copyBean(dto, TbDept.class); |
| | | tbDeptService.updateById(tbDept); |
| | | dto.setPassword(SecurityUtils.encryptPassword(dto.getPassword())); |
| | | sysUserService.lambdaUpdate().set(SysUser::getAreaAlias, dto.getAreaAlias()) |
| | | .set(SysUser::getPersonInCharge, dto.getPersonInCharge()) |
| | | .set(SysUser::getPhoneNumber, dto.getPhoneNumber()) |
| | | .set(SysUser::getUserName, dto.getUserName()) |
| | | .set(SysUser::getPassword, dto.getPassword()) |
| | | .eq(SysUser::getUserId, dto.getUserId()) |
| | | .update(); |
| | | } catch (Exception e) { |
| | | if (e instanceof ServiceException) { |
| | | return R.fail(e.getMessage()); |
| | |
| | | tbDeptService.focus(dto); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 部门未上传季度数据提示 |
| | | * @return Void |
| | | */ |
| | | @PostMapping("/reporting-message") |
| | | @ApiOperation(value = "部门未上传季度数据提示",notes = "code=200不展示,code=500 展示返回的msg") |
| | | public R<Void> reportingMessage(){ |
| | | return tbDeptService.reportingMessage(); |
| | | } |
| | | } |
| | | |