Pārlūkot izejas kodu

供应商端后台的用户管理优化---新增导出用户信息功能

JiangChongBo 2 gadi atpakaļ
vecāks
revīzija
ebc04f20d3

+ 7 - 1
src/main/java/com/caimei/controller/admin/auth/DownloadApi.java

@@ -89,5 +89,11 @@ public class DownloadApi {
         }
         return downloadService.downloadProductData(authIds, request, response);
     }
-
+    @GetMapping("/Clubuser/excel")
+    public ResponseJson downloadClubuserlistInfo(String clubUserIds, HttpServletResponse response){
+        if (StringUtils.isEmpty(clubUserIds)) {
+            return ResponseJson.error("用户id不能为空");
+        }
+        return downloadService.downloadClubuserlistInfo(clubUserIds,response);
+    }
 }

+ 2 - 0
src/main/java/com/caimei/mapper/cmMapper/AuthMapper.java

@@ -219,4 +219,6 @@ public interface AuthMapper {
     List<RossChallengeRoundVo> getRoundByAuthUserID(Integer authUserId);
 
     String getAuthparty(Integer authUserId);
+
+    List<ClubUserVo> getclubuserInfo(@Param("clubUserIds")String clubUserIds);
 }

+ 3 - 0
src/main/java/com/caimei/model/vo/ClubUserVo.java

@@ -36,4 +36,7 @@ public class ClubUserVo {
 
     @ApiModelProperty("添加时间")
     private Date addTime;
+
+    @ApiModelProperty("认证状态")
+    private String authenticationStatus;
 }

+ 8 - 0
src/main/java/com/caimei/service/auth/DownloadService.java

@@ -40,4 +40,12 @@ public interface DownloadService {
     ResponseJson downloadProductData(String authIds, HttpServletRequest request, HttpServletResponse response) throws IOException;
 
     void downloadAuthTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException;
+
+    /**
+     * 下载用户信息
+     * @param clubUserIds
+     * @param response
+     * @return
+     */
+    ResponseJson downloadClubuserlistInfo(String clubUserIds, HttpServletResponse response);
 }

+ 120 - 0
src/main/java/com/caimei/service/auth/impl/DownloadServiceImpl.java

@@ -10,6 +10,7 @@ import com.caimei.model.po.ProductParamPo;
 import com.caimei.model.po.UploadFilePo;
 import com.caimei.model.vo.AuthFormVo;
 import com.caimei.model.vo.AuthVo;
+import com.caimei.model.vo.ClubUserVo;
 import com.caimei.model.vo.ProductFormVo;
 import com.caimei.service.auth.DownloadService;
 import com.caimei.service.auth.UploadService;
@@ -484,4 +485,123 @@ public class DownloadServiceImpl implements DownloadService {
             }
         }
     }
+
+    /**
+     * 下载用户信息
+     * @param clubUserIds
+     * @param response
+     * @return
+     */
+    @Override
+    public ResponseJson downloadClubuserlistInfo(String clubUserIds, HttpServletResponse response) {
+        //导出的用户表名
+        String fileName=new String("用户信息.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
+        //获取用户信息
+        List<ClubUserVo> clubUserVosList = authMapper.getclubuserInfo(clubUserIds);
+        try {
+            OutputStream outputStream = response.getOutputStream();
+            response.reset();
+            response.setHeader("Access-Control-Allow-Origin", "*");
+            response.setHeader("Access-Control-Allow-Credentials", "true");
+            response.setHeader("Content-disposition",
+                    "attachment; filename="+fileName);
+//            response.setContentType("application/vnd.ms-excel"); -- xls
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            return  generateClubUserExcel(clubUserVosList,outputStream);
+        } catch (IOException e) {
+            e.printStackTrace();
+            return ResponseJson.error("导出失败");
+        }
+    }
+
+    private ResponseJson generateClubUserExcel(List<ClubUserVo> clubUserVosList, OutputStream outputStream) {
+        try {
+            XSSFWorkbook workbook = new XSSFWorkbook();
+            // 创建机构工作表
+            XSSFSheet authPartySheet = workbook.createSheet("用户信息");
+            // sheet样式定义
+            XSSFCellStyle topCellStyle = PoiUtils.getXSSFTopCellStyle(workbook);
+            XSSFCellStyle customCellStyle = PoiUtils.getXSSFCustomCellStyle(workbook);
+            // 创建列头行
+            XSSFRow authTopRow = authPartySheet.createRow(0);
+            // 添加列头单元格
+            String[] rowName = {"序号", "机构名称", "手机号", "注册时间",
+                    "认证状态", "用户状态"};
+            for (int i = 0; i < rowName.length; i++) {
+                XSSFCell authTopRowCell = authTopRow.createCell(i);
+                authTopRowCell.setCellType(CellType.valueOf("STRING"));
+                XSSFRichTextString columnText = new XSSFRichTextString(rowName[i]);
+                authTopRowCell.setCellValue(columnText);
+                authTopRowCell.setCellStyle(topCellStyle);
+            }
+            // 行索引
+            AtomicInteger authPartyRowNum = new AtomicInteger(1);
+            int serialNum=1;
+//            clubUserVosList.forEach(club -> {
+            for (ClubUserVo club:clubUserVosList) {
+                // 创建机构行
+                XSSFRow authRow = authPartySheet.createRow(authPartyRowNum.get());
+                // 组装机构数据
+                List<String> authData = new ArrayList<>();
+                authData.add(serialNum + "");
+                authData.add(club.getName());
+                authData.add(club.getMobile());
+                SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
+                Date authDate = club.getAddTime();
+                if (null != authDate) {
+                    authData.add(format.format(authDate));
+                } else {
+                    authData.add("");
+                }
+                authData.add(club.getAuthenticationStatus());
+                authData.add(club.getStatus() + "");
+                for (int j = 0; j < authData.size(); j++) {
+                    XSSFCell authCell = authRow.createCell(j);
+                    authCell.setCellStyle(customCellStyle);
+//                    if (j == 7) {
+//                        authCell.setCellType(CellType.NUMERIC);
+//                        authCell.setCellValue(Integer.parseInt(authData.get(j)));
+//                    } else {
+                    authCell.setCellType(CellType.valueOf("STRING"));
+                    XSSFRichTextString authCellValue = new XSSFRichTextString(authData.get(j));
+                    authCell.setCellValue(authCellValue);
+//                    }
+                }
+                authPartyRowNum.getAndIncrement();
+                serialNum++;
+            }
+//            });
+            // 让列宽随着导出的列长自动适应
+            for (int colNum = 0; colNum < rowName.length; colNum++) {
+                int productColumnWidth = authPartySheet.getColumnWidth(colNum) / 256;
+                for (int rowNum = 0; rowNum < authPartySheet.getLastRowNum(); rowNum++) {
+                    XSSFRow currentRow;
+                    // 当前行未被使用过
+                    if (authPartySheet.getRow(rowNum) == null) {
+                        currentRow = authPartySheet.createRow(rowNum);
+                    } else {
+                        currentRow = authPartySheet.getRow(rowNum);
+                    }
+                    if (currentRow.getCell(colNum) != null) {
+                        XSSFCell currentCell = currentRow.getCell(colNum);
+                        if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
+                            int length = currentCell.getStringCellValue()
+                                    .getBytes().length;
+                            if (productColumnWidth < length) {
+                                productColumnWidth = length;
+                            }
+                        }
+                    }
+                }
+                authPartySheet.setColumnWidth(colNum, (productColumnWidth + 4) * 256);
+            }
+            outputStream.flush();
+            workbook.write(outputStream);
+            outputStream.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResponseJson.error("导出失败");
+        }
+        return null;
+    }
 }

+ 11 - 0
src/main/resources/mapper/AuthMapper.xml

@@ -915,4 +915,15 @@
     <select id="getAuthparty" resultType="java.lang.String">
         select name from cm_brand_auth_user where authUserId=#{authUserId}
     </select>
+    <select id="getclubuserInfo" resultType="com.caimei.model.vo.ClubUserVo">
+        select cu.name,cu.mobile,cu.addTime,
+            (case
+                 when cu.name is null then '未认证'
+                 when cu.name is not null then '已认证' end
+            ) as authenticationStatus,
+               cu.status
+        from cm_brand_club_user cu
+        LEFT JOIN cm_brand_auth a ON cu.authId = a.id
+        where find_in_set(cu.id,#{clubUserIds})
+    </select>
 </mapper>