From 76cf4f75648d443d8057759365558f339ffe7de3 Mon Sep 17 00:00:00 2001 From: YXY <932687738@qq.com> Date: Wed, 30 Aug 2023 23:20:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E9=97=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/DateUtils.java | 5 ++ .../excelStrategy/CustomMergeStrategy.java | 20 ++--- .../flyingbook/LarkHelper/LarkUserHelper.java | 31 ++++++- .../controller/LarkActiveController.java | 42 ++++++++-- .../ruoyi/flyingbook/domain/LarkLoginLog.java | 2 + .../domain/lark/LarkUserRequest.java | 2 + .../larkactive/LarkActiveExportDetailVo.java | 33 ++++++++ .../ILarkUserActiveRelatoinService.java | 2 + .../LarkUserActiveRelatoinServiceImpl.java | 81 ++++++++++++++++--- .../resources/mapper/LarkLoginLogMapper.xml | 6 +- .../framework/config/SecurityConfig.java | 1 + 11 files changed, 194 insertions(+), 31 deletions(-) create mode 100644 ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/larkactive/LarkActiveExportDetailVo.java diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java index 015aa52..f7eb03d 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java @@ -154,6 +154,11 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { return df.format(today_end); } + public static String date2str(Date time, String pattern) { + SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); + return dateFormat.format(time); + } + public static String ldt2str(LocalDateTime time, String pattern) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); return time.format(fmt); diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/excelStrategy/CustomMergeStrategy.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/excelStrategy/CustomMergeStrategy.java index 19ff4a7..002c320 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/excelStrategy/CustomMergeStrategy.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/excelStrategy/CustomMergeStrategy.java @@ -65,13 +65,13 @@ public class CustomMergeStrategy implements RowWriteHandler { // 获取上一行数据 Row lastRow = sheet.getRow(row.getRowNum() - 1); // 将本行和上一行是同一类型的数据(通过主键字段进行判断),则需要合并 - if (lastRow.getCell(pkIndex).getStringCellValue().equalsIgnoreCase(row.getCell(pkIndex).getStringCellValue())) { - for (Integer needMerIndex : needMergeColumnIndex) { - CellRangeAddress cellRangeAddress = new CellRangeAddress(row.getRowNum() - 1, row.getRowNum(), - needMerIndex, needMerIndex); - sheet.addMergedRegionUnsafe(cellRangeAddress); - } - } +// if (lastRow.getCell(pkIndex).getStringCellValue().equalsIgnoreCase(row.getCell(pkIndex).getStringCellValue())) { +// for (Integer needMerIndex : needMergeColumnIndex) { +// CellRangeAddress cellRangeAddress = new CellRangeAddress(row.getRowNum() - 1, row.getRowNum(), +// needMerIndex, needMerIndex); +// sheet.addMergedRegionUnsafe(cellRangeAddress); +// } +// } } /** @@ -126,9 +126,9 @@ public class CustomMergeStrategy implements RowWriteHandler { } // 没有指定主键,则异常 - if (null == this.pkIndex) { - throw new IllegalStateException("使用@CustomMerge注解必须指定主键"); - } +// if (null == this.pkIndex) { +// throw new IllegalStateException("使用@CustomMerge注解必须指定主键"); +// } } diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkUserHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkUserHelper.java index 641dfa1..f9091e2 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkUserHelper.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkUserHelper.java @@ -32,13 +32,17 @@ public class LarkUserHelper extends LarkHelper{ public static void main(String[] args) throws Exception { LarkUserHelper bk = new LarkUserHelper(); -// GetUserRespBody userRespBody = bk.getUserInfoDetail(new LarkUserRequest(appId, secret, openId)); + LarkUserRequest larkUserRequest = new LarkUserRequest(appId, secret, openId); + GetUserRespBody userRespBody = bk.getUserInfoDetail(larkUserRequest); + larkUserRequest.setDepartmentId(userRespBody.getUser().getDepartmentIds()[0]); + GetDepartmentRespBody departmentsInfoDetail = bk.getDepartmentsInfoDetail(larkUserRequest); + // userRespBody.getUser().getName(); // System.out.println(userRespBody); // LarkSessionResponse larkSessionResponse = bk.tokenLoginValidate(new LarkUserRequest(appId, secret, openId, code)); // System.out.println(larkSessionResponse); - GetUserRespBody userRespBody = bk.getUserInfo(new LarkUserRequest("cli_a4504c9fe9f95013", "oWiLzl2Tkt68tvZHjQIfOb27rPQkTUhu", "85623f9d")); - System.out.println(JSONObject.toJSONString(userRespBody)); +// GetUserRespBody userRespBody = bk.getUserInfo(new LarkUserRequest("cli_a4504c9fe9f95013", "oWiLzl2Tkt68tvZHjQIfOb27rPQkTUhu", "85623f9d")); + System.out.println(JSONObject.toJSONString(departmentsInfoDetail)); } @@ -73,6 +77,27 @@ public class LarkUserHelper extends LarkHelper{ } } + /** + * 获取行详情 + */ + public GetDepartmentRespBody getDepartmentsInfoDetail(LarkUserRequest request) { + GetDepartmentResp getDepartmentResp = null; + try { + getDepartmentResp = buildClient(request).contact().department().get( + GetDepartmentReq.newBuilder() + .departmentId(request.getDepartmentId()) + .build() + ); + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkUserHelper.getUserInfoDetail",e.getMessage(),request).getErrorMessageBody()); + } + if (getDepartmentResp != null && getDepartmentResp.getCode() == 0){ + return getDepartmentResp.getData(); + }else { + throw new RuntimeException(new LarkException("LarkUserHelper.getUserInfoDetail",getDepartmentResp.getMsg(),request).getErrorMessageBody()); + } + } + /** * 获取userToken */ diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/LarkActiveController.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/LarkActiveController.java index 0b277c0..418723a 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/LarkActiveController.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/controller/LarkActiveController.java @@ -1,7 +1,9 @@ package com.ruoyi.flyingbook.controller; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.write.metadata.WriteSheet; import com.lark.oapi.service.contact.v3.model.GetUserRespBody; import com.lark.oapi.service.contact.v3.model.User; import com.ruoyi.common.core.controller.BaseController; @@ -26,10 +28,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; @Slf4j @RestController @@ -73,6 +72,10 @@ public class LarkActiveController extends BaseController { larkLoginLog.setType(LarkUserTypeEnum.LARK_APP.getCode()); larkLoginLog.setCreateBy(user.getName()); larkLoginLog.setCreateTime(new Date()); + String[] departmentIds = user.getDepartmentIds(); + if (departmentIds != null && departmentIds.length > 0){ + larkLoginLog.setDepartmentId(departmentIds[0]); + } larkLoginLogService.insertLarkLoginLog(larkLoginLog); return new ResponseVo<>(user); } @@ -177,16 +180,39 @@ public class LarkActiveController extends BaseController { public void export(HttpServletResponse response) { try { LarkActiveCountVo larkActiveCountVo = larkUserActiveRelatoinService.queryCount(); + List larkActiveExportDetailVos = larkUserActiveRelatoinService.queryExportDetail(APP_ID,APP_SECRET); ExportUtils.writeFile(response, "fileName"); EasyExcel.write(response.getOutputStream()) .head(LarkActiveCountVo.class) .registerWriteHandler(new CustomMergeStrategy(LarkActiveCountVo.class)) .excelType(ExcelTypeEnum.XLSX) - .autoCloseStream(Boolean.TRUE) - .sheet("sheetName") + .sheet(0,"sheetName1") .doWrite(Arrays.asList(larkActiveCountVo)); - } catch (Exception e) { - log.info("导出信息错误", e); + EasyExcel.write(response.getOutputStream()) + .head(LarkActiveExportDetailVo.class) + .registerWriteHandler(new CustomMergeStrategy(LarkActiveExportDetailVo.class)) + .excelType(ExcelTypeEnum.XLSX) + .sheet(1,"sheetName2") + .doWrite(larkActiveExportDetailVos); + }catch (Exception e){ + log.info("导出信息错误",e); + } + } + + @ResponseBody + @GetMapping("/exportActiveFileDetail") + public void exportDetail(HttpServletResponse response) { + try { + List larkActiveExportDetailVos = larkUserActiveRelatoinService.queryExportDetail(APP_ID,APP_SECRET); + ExportUtils.writeFile(response, "detail"); + EasyExcel.write(response.getOutputStream()) + .head(LarkActiveExportDetailVo.class) + .registerWriteHandler(new CustomMergeStrategy(LarkActiveExportDetailVo.class)) + .excelType(ExcelTypeEnum.XLSX) + .sheet(1,"sheetName2") + .doWrite(larkActiveExportDetailVos); + }catch (Exception e){ + log.info("导出信息错误",e); } } diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkLoginLog.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkLoginLog.java index d2aec3f..e8b6a19 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkLoginLog.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/LarkLoginLog.java @@ -20,6 +20,8 @@ public class LarkLoginLog extends BaseEntity /** 用户唯一id */ private String openId; + /** 用户唯一id */ + private String departmentId; /** 公司名称 */ private String companyName; diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkUserRequest.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkUserRequest.java index cc58080..41079d1 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkUserRequest.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkUserRequest.java @@ -17,6 +17,8 @@ public class LarkUserRequest extends LarkRequest{ private String openId; //登录预授权码 private String code; + //部门id + private String departmentId; public LarkUserRequest(String appId, String appSecret) { super(appId, appSecret); } diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/larkactive/LarkActiveExportDetailVo.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/larkactive/LarkActiveExportDetailVo.java new file mode 100644 index 0000000..412bec5 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/larkactive/LarkActiveExportDetailVo.java @@ -0,0 +1,33 @@ +package com.ruoyi.flyingbook.domain.larkactive; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.ruoyi.common.utils.excelStrategy.CustomMerge; +import lombok.Data; + + +/** + * 【请填写功能名称】对象 lark_active + * + * @author ruoyi + * @date 2023-08-16 + */ +@Data +public class LarkActiveExportDetailVo { + + @ColumnWidth(20) + @ExcelProperty("账号名") + @CustomMerge(isPk = true) + private String userName; + @ColumnWidth(20) + @ExcelProperty("部门") + private String department; + @ColumnWidth(20) + @ExcelProperty("活动名称") + private String activeName; + @ColumnWidth(20) + @ExcelProperty("点亮时间") + private String activeTime; + + +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/ILarkUserActiveRelatoinService.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/ILarkUserActiveRelatoinService.java index ab558ad..df1709e 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/ILarkUserActiveRelatoinService.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/ILarkUserActiveRelatoinService.java @@ -3,6 +3,7 @@ package com.ruoyi.flyingbook.service; import com.ruoyi.flyingbook.domain.LarkUserActiveRelatoin; import com.ruoyi.flyingbook.domain.edi.ResponseVo; import com.ruoyi.flyingbook.domain.larkactive.LarkActiveCountVo; +import com.ruoyi.flyingbook.domain.larkactive.LarkActiveExportDetailVo; import com.ruoyi.flyingbook.domain.larkactive.LarkActiveImportVo; import com.ruoyi.flyingbook.domain.larkactive.LarkActiveUserRelationRequest; @@ -24,6 +25,7 @@ public interface ILarkUserActiveRelatoinService { public LarkUserActiveRelatoin selectLarkUserActiveRelatoinById(Long id); public LarkActiveCountVo queryCount(); + public List queryExportDetail(String appId,String appSecret); /** * 查询【请填写功能名称】列表 diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/impl/LarkUserActiveRelatoinServiceImpl.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/impl/LarkUserActiveRelatoinServiceImpl.java index be45ded..6c06db1 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/impl/LarkUserActiveRelatoinServiceImpl.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/service/impl/LarkUserActiveRelatoinServiceImpl.java @@ -1,6 +1,7 @@ package com.ruoyi.flyingbook.service.impl; import cn.hutool.core.util.IdUtil; +import com.lark.oapi.service.contact.v3.model.GetDepartmentRespBody; import com.lark.oapi.service.contact.v3.model.GetUserRespBody; import com.ruoyi.common.enums.FlagStatus; import com.ruoyi.common.enums.LarkActiveEnum; @@ -15,10 +16,7 @@ import com.ruoyi.flyingbook.domain.LarkUserActiveImage; import com.ruoyi.flyingbook.domain.LarkUserActiveRelatoin; import com.ruoyi.flyingbook.domain.edi.ResponseVo; import com.ruoyi.flyingbook.domain.lark.LarkUserRequest; -import com.ruoyi.flyingbook.domain.larkactive.LarkActiveCountVo; -import com.ruoyi.flyingbook.domain.larkactive.LarkActiveImportVo; -import com.ruoyi.flyingbook.domain.larkactive.LarkActiveUserRelationRequest; -import com.ruoyi.flyingbook.domain.larkactive.LarkActiveVo; +import com.ruoyi.flyingbook.domain.larkactive.*; import com.ruoyi.flyingbook.mapper.LarkActiveMapper; import com.ruoyi.flyingbook.mapper.LarkLoginLogMapper; import com.ruoyi.flyingbook.mapper.LarkUserActiveImageMapper; @@ -79,6 +77,7 @@ public class LarkUserActiveRelatoinServiceImpl implements ILarkUserActiveRelatoi return larkUserActiveRelatoinMapper.selectLarkUserActiveRelatoinById(id); } + @Override public LarkActiveCountVo queryCount() { LarkActiveCountVo larkActiveCountVo = new LarkActiveCountVo(); @@ -110,6 +109,70 @@ public class LarkUserActiveRelatoinServiceImpl implements ILarkUserActiveRelatoi return larkActiveCountVo; } + @Override + public List queryExportDetail(String appId,String appSecret) { + List result = new ArrayList<>(); + LarkUserActiveRelatoin larkUserActiveRelatoin = new LarkUserActiveRelatoin(); + larkUserActiveRelatoin.setActiveStage(LarkActiveStageEnum.ADJUST.getCode()); + larkUserActiveRelatoin.setFlag(FlagStatus.OK.getCode()); + List larkUserActiveRelatoins = larkUserActiveRelatoinMapper.selectLarkUserActiveRelatoinList(larkUserActiveRelatoin); + if (CollectionUtils.isEmpty(larkUserActiveRelatoins)){ + return result; + } + Map activeMap = queryActiveMap(); + List openIdList = larkUserActiveRelatoins.stream().filter(r -> { + return StringUtils.isNotBlank(r.getUserName()); + }).map(LarkUserActiveRelatoin::getUserName) + .distinct().collect(Collectors.toList()); + Map userInfoMap = larkLoginLogMapper.queryByOpenId(openIdList).stream().collect(Collectors.toMap(LarkLoginLog::getOpenId, Function.identity(), (k1, k2) -> k1)); + LarkUserRequest larkUserRequest = new LarkUserRequest(appId, appSecret); + Map departmentMap = new HashMap<>(); + for (LarkUserActiveRelatoin userActiveRelatoin : larkUserActiveRelatoins) { + Long activeId = userActiveRelatoin.getActiveId(); + String userName = userActiveRelatoin.getUserName(); + String activeName = activeMap.get(activeId); + LarkLoginLog larkLoginLog = userInfoMap.get(userName); + LarkActiveExportDetailVo vo = new LarkActiveExportDetailVo(); + vo.setActiveTime(DateUtils.date2str(userActiveRelatoin.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS)); + vo.setActiveName(activeName); + if (larkLoginLog != null){ + vo.setUserName(larkLoginLog.getUserName()); + if (StringUtils.isNotBlank(larkLoginLog.getDepartmentId())){ + larkUserRequest.setDepartmentId(larkLoginLog.getDepartmentId()); + fillDepartmentName(larkUserRequest,vo,departmentMap); + } + } + result.add(vo); + } + return result; + } + + private void fillDepartmentName(LarkUserRequest larkUserRequest,LarkActiveExportDetailVo vo,Map departmentMap){ + try { + String departmentName = departmentMap.get(larkUserRequest.getDepartmentId()); + if (StringUtils.isNotBlank(departmentName)){ + vo.setDepartment(departmentName); + }else { + GetDepartmentRespBody departmentsInfoDetail = larkUserHelper.getDepartmentsInfoDetail(larkUserRequest); + if (departmentsInfoDetail != null){ + vo.setDepartment(departmentsInfoDetail.getDepartment().getName()); + }else { + vo.setDepartment(larkUserRequest.getDepartmentId()); + } + departmentMap.put(larkUserRequest.getDepartmentId(),vo.getDepartment()); + } + }catch (Exception e){ + log.info("fillDepartmentName error",e); + } + } + + private Map queryActiveMap(){ + //每个活动的数量 + LarkActive query = new LarkActive(); + query.setFlag(FlagStatus.OK.getCode()); + return larkActiveMapper.queryActive(query).stream().collect(Collectors.toMap(LarkActive::getId,LarkActive::getActiveContent,(k1,k2)->k1)); + } + private void fillCount(LarkActiveCountVo larkActiveCountVo, Integer qty, String activeCode) { qty = qty == null ? 0 : qty; LarkActiveEnum activeEnum = LarkActiveEnum.getByCode(activeCode); @@ -248,26 +311,26 @@ public class LarkUserActiveRelatoinServiceImpl implements ILarkUserActiveRelatoi continue; } LarkActiveEnum active = LarkActiveEnum.getByCode(activeCode); - if (active == null){ + if (active == null) { continue; } List midList = map.getOrDefault(active.getCode(), new ArrayList<>()); midList.add(larkActiveImportVo); - map.put(active.getCode(),midList); + map.put(active.getCode(), midList); } for (Map.Entry> entry : map.entrySet()) { - submitByActive(entry.getValue(),entry.getKey(),larkUserRequest); + submitByActive(entry.getValue(), entry.getKey(), larkUserRequest); } } - private void submitByActive(List list,String activeCode,LarkUserRequest larkUserRequest){ + private void submitByActive(List list, String activeCode, LarkUserRequest larkUserRequest) { Set openIdSet = new HashSet<>(); for (LarkActiveImportVo larkActiveImportVo : list) { if (StringUtils.isBlank(larkActiveImportVo.getUserId()) || StringUtils.isBlank(activeCode)) { continue; } LarkActiveEnum active = LarkActiveEnum.getByCode(activeCode); - if (active == null){ + if (active == null) { continue; } try { diff --git a/ruoyi-flyingbook/src/main/resources/mapper/LarkLoginLogMapper.xml b/ruoyi-flyingbook/src/main/resources/mapper/LarkLoginLogMapper.xml index c6f2006..c77f77b 100644 --- a/ruoyi-flyingbook/src/main/resources/mapper/LarkLoginLogMapper.xml +++ b/ruoyi-flyingbook/src/main/resources/mapper/LarkLoginLogMapper.xml @@ -17,6 +17,7 @@ + @@ -31,7 +32,8 @@ update_by, update_time, flag, - remark + remark, + department_id from lark_login_log @@ -113,6 +115,7 @@ update_time, flag, remark, + department_id, #{openId}, @@ -126,6 +129,7 @@ #{updateTime}, #{flag}, #{remark}, + #{departmentId}, diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index 6bba9cc..b202d4e 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -113,6 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter , "/lark/active/queryComment" , "/lark/active/submit" , "/lark/active/exportActiveFile" + , "/lark/active/exportActiveFileDetail" , "/lark/active/importActiveFile").anonymous() .antMatchers( HttpMethod.GET,