钉钉考勤

This commit is contained in:
张松
2025-12-01 11:31:37 +08:00
parent 2f201c7015
commit 0b94d79a02
3 changed files with 33 additions and 19 deletions

View File

@@ -123,5 +123,7 @@ public class ShopConfig implements Serializable {
* 上菜时间 分钟 * 上菜时间 分钟
*/ */
private Integer serveTime; private Integer serveTime;
private String dingAppKey;
private String dingAppSecret;
} }

View File

@@ -3,6 +3,8 @@ package com.czg.service.market.service.impl;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.czg.account.entity.ShopConfig;
import com.czg.account.service.ShopConfigService;
import com.czg.market.service.AttendanceService; import com.czg.market.service.AttendanceService;
import com.czg.market.vo.DingAttendanceStatsVO; import com.czg.market.vo.DingAttendanceStatsVO;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@@ -15,6 +17,8 @@ import java.util.Date;
public class AttendanceServiceImpl implements AttendanceService { public class AttendanceServiceImpl implements AttendanceService {
@Resource @Resource
private DingService dingService; private DingService dingService;
@Resource
private ShopConfigService shopConfigService;
@Override @Override
public ArrayList<DingAttendanceStatsVO> list(Long shopId, Integer page, Integer size, String name, String startTime, String endTime) { public ArrayList<DingAttendanceStatsVO> list(Long shopId, Integer page, Integer size, String name, String startTime, String endTime) {
@@ -31,6 +35,12 @@ public class AttendanceServiceImpl implements AttendanceService {
}else { }else {
end = DateUtil.parse(endTime); end = DateUtil.parse(endTime);
} }
return dingService.getUserReport(start, end, name);
ShopConfig config = shopConfigService.getById(shopId);
if (config == null || StrUtil.isBlank(config.getDingAppSecret())) {
return new ArrayList<>();
}
return dingService.getUserReport(shopId, config.getDingAppKey(), config.getDingAppSecret(), start, end, name);
} }
} }

View File

@@ -28,15 +28,17 @@ public class DingService {
private RedisService redisService; private RedisService redisService;
public String getToken() { public String getToken(String key, String secret, Long shopId) {
Object accessToken = redisService.get("ding_access_token"); Object accessToken = redisService.get("ding_access_token:" + shopId);
if (accessToken instanceof String && StrUtil.isNotBlank((String) accessToken)) { if (accessToken instanceof String && StrUtil.isNotBlank((String) accessToken)) {
return (String) accessToken; return (String) accessToken;
} }
DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.com/gettoken"); DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.com/gettoken");
OapiGettokenRequest req = new OapiGettokenRequest(); OapiGettokenRequest req = new OapiGettokenRequest();
req.setAppkey("ding9bqzj4890z81o3y2"); // req.setAppkey("ding9bqzj4890z81o3y2");
req.setAppsecret("PiRU2CQ_KrZH104UoULeBfSbFw4YhPI0fZZC_bUr1aubvbsP6zmMv7nC1xxF9QBS"); // req.setAppsecret("PiRU2CQ_KrZH104UoULeBfSbFw4YhPI0fZZC_bUr1aubvbsP6zmMv7nC1xxF9QBS");
req.setAppkey(key);
req.setAppsecret(secret);
req.setHttpMethod("GET"); req.setHttpMethod("GET");
OapiGettokenResponse rsp; OapiGettokenResponse rsp;
try { try {
@@ -47,16 +49,16 @@ public class DingService {
System.out.println(rsp.getBody()); System.out.println(rsp.getBody());
JSONObject data = getData(rsp, true); JSONObject data = getData(rsp, true);
Long expiresIn = data.getLong("expires_in"); Long expiresIn = data.getLong("expires_in");
redisService.set("ding_access_token", data.getString("access_token"), expiresIn - 100); redisService.set("ding_access_token:" + shopId, data.getString("access_token"), expiresIn - 100);
return data.getString("access_token"); return data.getString("access_token");
} }
public Map<String, String> getColumns() { public Map<String, String> getColumns(String key, String secret, Long shopId) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns"); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns");
OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest(); OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest();
OapiAttendanceGetattcolumnsResponse rsp = null; OapiAttendanceGetattcolumnsResponse rsp = null;
try { try {
String token = getToken(); String token = getToken(key, secret, shopId);
rsp = client.execute(req, token); rsp = client.execute(req, token);
} catch (ApiException e) { } catch (ApiException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -96,7 +98,7 @@ public class DingService {
return jsonObject; return jsonObject;
} }
public List<DingUserVO> getUserByDeptId(Long deptId) throws ApiException { public List<DingUserVO> getUserByDeptId(Long deptId, String key, String secret, Long shopId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list"); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
OapiV2UserListRequest req = new OapiV2UserListRequest(); OapiV2UserListRequest req = new OapiV2UserListRequest();
req.setDeptId(deptId); req.setDeptId(deptId);
@@ -105,37 +107,37 @@ public class DingService {
req.setOrderField("modify_desc"); req.setOrderField("modify_desc");
req.setContainAccessLimit(true); req.setContainAccessLimit(true);
req.setLanguage("zh_CN"); req.setLanguage("zh_CN");
OapiV2UserListResponse rsp = client.execute(req, getToken()); OapiV2UserListResponse rsp = client.execute(req, getToken(key, secret, shopId));
JSONObject data = getData(rsp, true); JSONObject data = getData(rsp, true);
List<DingUserVO> userVOList = data.getJSONObject("result").getJSONArray("list").toJavaList(DingUserVO.class); List<DingUserVO> userVOList = data.getJSONObject("result").getJSONArray("list").toJavaList(DingUserVO.class);
return userVOList; return userVOList;
} }
public JSONObject getRepost(Date startTime, Date endTime, String userId, String columIdList) throws ApiException { public JSONObject getRepost(Date startTime, Date endTime, String userId, String columIdList, String key, String secret, Long shopId) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval"); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval");
OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest(); OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest();
req.setUserid(userId); req.setUserid(userId);
req.setColumnIdList(columIdList); req.setColumnIdList(columIdList);
req.setFromDate(startTime); req.setFromDate(startTime);
req.setToDate(endTime); req.setToDate(endTime);
OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, getToken()); OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, getToken(key, secret, shopId));
JSONObject data = getData(rsp, false); JSONObject data = getData(rsp, false);
return data == null ? null : data.getJSONObject("result"); return data == null ? null : data.getJSONObject("result");
} }
public ArrayList<DingUserVO> getUserList(String name) { public ArrayList<DingUserVO> getUserList(String name,String key, String secret, Long shopId) {
try { try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub"); DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest(); OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
req.setDeptId(1L); req.setDeptId(1L);
req.setLanguage("zh_CN"); req.setLanguage("zh_CN");
OapiV2DepartmentListsubResponse rsp = client.execute(req, getToken()); OapiV2DepartmentListsubResponse rsp = client.execute(req, getToken(key, secret, shopId));
Set<Long> deptIdList = getData(rsp, true).getJSONArray("result").stream().map(item -> ((JSONObject) item) Set<Long> deptIdList = getData(rsp, true).getJSONArray("result").stream().map(item -> ((JSONObject) item)
.getLong("dept_id")).collect(Collectors.toSet()); .getLong("dept_id")).collect(Collectors.toSet());
ArrayList<DingUserVO> dingUserVOS = new ArrayList<>(); ArrayList<DingUserVO> dingUserVOS = new ArrayList<>();
for (Long dptId : deptIdList) { for (Long dptId : deptIdList) {
dingUserVOS.addAll(getUserByDeptId(dptId)); dingUserVOS.addAll(getUserByDeptId(dptId, key, secret, shopId));
} }
if (StrUtil.isNotBlank(name)) { if (StrUtil.isNotBlank(name)) {
return dingUserVOS.stream().filter(item -> item.getName().contains(name)).collect(Collectors.toCollection(ArrayList::new)); return dingUserVOS.stream().filter(item -> item.getName().contains(name)).collect(Collectors.toCollection(ArrayList::new));
@@ -146,15 +148,15 @@ public class DingService {
} }
} }
public ArrayList<DingAttendanceStatsVO> getUserReport(DateTime startTime, DateTime endTime, String name) { public ArrayList<DingAttendanceStatsVO> getUserReport(Long shopId, String key, String secret, DateTime startTime, DateTime endTime, String name) {
Map<String, String> columns = getColumns(); Map<String, String> columns = getColumns(key, secret, shopId);
ArrayList<DingUserVO> userList = getUserList(name); ArrayList<DingUserVO> userList = getUserList(name, key, secret, shopId);
ArrayList<DingAttendanceStatsVO> statsVOS = new ArrayList<>(); ArrayList<DingAttendanceStatsVO> statsVOS = new ArrayList<>();
for (DingUserVO item : userList) { for (DingUserVO item : userList) {
JSONObject repost = null; JSONObject repost = null;
try { try {
repost = getRepost(startTime, endTime, item.getUserid(), repost = getRepost(startTime, endTime, item.getUserid(),
String.join(",", columns.keySet())); String.join(",", columns.keySet()), key, secret, shopId);
} catch (ApiException e) { } catch (ApiException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }