Merge remote-tracking branch 'origin/test' into test
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.czg.service.account.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.czg.account.dto.ShopConfigDTO;
|
||||
import com.czg.account.entity.ShopConfig;
|
||||
import com.czg.account.entity.ShopInfo;
|
||||
@@ -50,6 +51,24 @@ public class ShopConfigServiceImpl extends ServiceImpl<ShopConfigMapper, ShopCon
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean editConfig(Long shopId, ShopConfig shopConfig) {
|
||||
ShopConfig info = getById(shopId);
|
||||
if (info == null) {
|
||||
info = new ShopConfig();
|
||||
BeanUtil.copyProperties(shopConfig, info);
|
||||
info.setId(shopId);
|
||||
info.setMainId(shopInfoMapper.selectOneByQuery(new QueryWrapper().eq(ShopInfo::getId, shopId)).getMainId());
|
||||
info.setId(shopId);
|
||||
save(info);
|
||||
}else {
|
||||
BeanUtil.copyProperties(shopConfig, info);
|
||||
updateById(info);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editStatusByShopIdList(Long mainShopId, Integer isEnable, boolean onyUpValid, String property, String useShopType, List<Long> shopIdList) {
|
||||
ShopConfig shopConfig = getById(mainShopId);
|
||||
|
||||
@@ -22,6 +22,18 @@
|
||||
<artifactId>pay-service</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dingtalk</artifactId>
|
||||
<version>2.2.40</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.czg.service.market.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
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.vo.DingAttendanceStatsVO;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class AttendanceServiceImpl implements AttendanceService {
|
||||
@Resource
|
||||
private DingService dingService;
|
||||
@Resource
|
||||
private ShopConfigService shopConfigService;
|
||||
|
||||
@Override
|
||||
public ArrayList<DingAttendanceStatsVO> list(Long shopId, Integer page, Integer size, String name, String startTime, String endTime) {
|
||||
DateTime start;
|
||||
DateTime end;
|
||||
if (StrUtil.isBlank(startTime)) {
|
||||
start = DateUtil.beginOfMonth(new Date());
|
||||
}else {
|
||||
start = DateUtil.parse(startTime);
|
||||
}
|
||||
|
||||
if (StrUtil.isBlank(endTime)) {
|
||||
end = DateUtil.date();
|
||||
}else {
|
||||
end = DateUtil.parse(endTime);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package com.czg.service.market.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.market.vo.DingAttendanceStatsVO;
|
||||
import com.czg.market.vo.DingUserVO;
|
||||
import com.czg.service.RedisService;
|
||||
import com.dingtalk.api.DefaultDingTalkClient;
|
||||
import com.dingtalk.api.DingTalkClient;
|
||||
import com.dingtalk.api.request.*;
|
||||
import com.dingtalk.api.response.*;
|
||||
import com.taobao.api.ApiException;
|
||||
import com.taobao.api.TaobaoResponse;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class DingService {
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
public String getToken(String key, String secret, Long shopId) {
|
||||
Object accessToken = redisService.get("ding_access_token:" + shopId);
|
||||
if (accessToken instanceof String && StrUtil.isNotBlank((String) accessToken)) {
|
||||
return (String) accessToken;
|
||||
}
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://api.dingtalk.com/gettoken");
|
||||
OapiGettokenRequest req = new OapiGettokenRequest();
|
||||
// req.setAppkey("ding9bqzj4890z81o3y2");
|
||||
// req.setAppsecret("PiRU2CQ_KrZH104UoULeBfSbFw4YhPI0fZZC_bUr1aubvbsP6zmMv7nC1xxF9QBS");
|
||||
req.setAppkey(key);
|
||||
req.setAppsecret(secret);
|
||||
req.setHttpMethod("GET");
|
||||
OapiGettokenResponse rsp;
|
||||
try {
|
||||
rsp = client.execute(req);
|
||||
} catch (ApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println(rsp.getBody());
|
||||
JSONObject data = getData(rsp, true);
|
||||
Long expiresIn = data.getLong("expires_in");
|
||||
redisService.set("ding_access_token:" + shopId, data.getString("access_token"), expiresIn - 100);
|
||||
return data.getString("access_token");
|
||||
}
|
||||
|
||||
public Map<String, String> getColumns(String key, String secret, Long shopId) {
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getattcolumns");
|
||||
OapiAttendanceGetattcolumnsRequest req = new OapiAttendanceGetattcolumnsRequest();
|
||||
OapiAttendanceGetattcolumnsResponse rsp = null;
|
||||
try {
|
||||
String token = getToken(key, secret, shopId);
|
||||
rsp = client.execute(req, token);
|
||||
} catch (ApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
|
||||
if (jsonObject.getInteger("errcode") != 0) {
|
||||
throw new RuntimeException(jsonObject.getString("errmsg"));
|
||||
}
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
Map<String, String> columMap = jsonObject.getJSONObject("result").getJSONArray("columns").stream()
|
||||
.filter(item -> {
|
||||
JSONObject item1 = (JSONObject) item;
|
||||
return item1.containsKey("id") && CollUtil.newArrayList("出勤天数", "休息天数", "工作时长", "迟到次数", "迟到时长", "应出勤天数").contains(item1.getString("name"));
|
||||
})
|
||||
.collect(Collectors.toMap(item -> ((JSONObject) item).getLong("id").toString(), item -> ((JSONObject) item).getString("name")));
|
||||
|
||||
return columMap;
|
||||
}
|
||||
|
||||
public static JSONObject getData(TaobaoResponse response, boolean checkNull) {
|
||||
if (!"0".equals(response.getErrorCode())) {
|
||||
Map<String, Object> map = BeanUtil.beanToMap(response);
|
||||
throw new RuntimeException(map.get("errmsg").toString());
|
||||
}
|
||||
String body = response.getBody();
|
||||
if (body == null) {
|
||||
if (checkNull) {
|
||||
throw new RuntimeException("body is null");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
JSONObject jsonObject = JSONObject.parseObject(body);
|
||||
if (jsonObject.getInteger("errcode") != 0) {
|
||||
throw new RuntimeException(jsonObject.getString("errmsg"));
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
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");
|
||||
OapiV2UserListRequest req = new OapiV2UserListRequest();
|
||||
req.setDeptId(deptId);
|
||||
req.setCursor(0L);
|
||||
req.setSize(50L);
|
||||
req.setOrderField("modify_desc");
|
||||
req.setContainAccessLimit(true);
|
||||
req.setLanguage("zh_CN");
|
||||
OapiV2UserListResponse rsp = client.execute(req, getToken(key, secret, shopId));
|
||||
JSONObject data = getData(rsp, true);
|
||||
List<DingUserVO> userVOList = data.getJSONObject("result").getJSONArray("list").toJavaList(DingUserVO.class);
|
||||
return userVOList;
|
||||
}
|
||||
|
||||
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");
|
||||
OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest();
|
||||
req.setUserid(userId);
|
||||
req.setColumnIdList(columIdList);
|
||||
req.setFromDate(startTime);
|
||||
req.setToDate(endTime);
|
||||
OapiAttendanceGetcolumnvalResponse rsp = client.execute(req, getToken(key, secret, shopId));
|
||||
JSONObject data = getData(rsp, false);
|
||||
return data == null ? null : data.getJSONObject("result");
|
||||
}
|
||||
|
||||
public ArrayList<DingUserVO> getUserList(String name,String key, String secret, Long shopId) {
|
||||
try {
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
|
||||
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
|
||||
req.setDeptId(1L);
|
||||
req.setLanguage("zh_CN");
|
||||
OapiV2DepartmentListsubResponse rsp = client.execute(req, getToken(key, secret, shopId));
|
||||
Set<Long> deptIdList = getData(rsp, true).getJSONArray("result").stream().map(item -> ((JSONObject) item)
|
||||
.getLong("dept_id")).collect(Collectors.toSet());
|
||||
|
||||
ArrayList<DingUserVO> dingUserVOS = new ArrayList<>();
|
||||
for (Long dptId : deptIdList) {
|
||||
dingUserVOS.addAll(getUserByDeptId(dptId, key, secret, shopId));
|
||||
}
|
||||
if (StrUtil.isNotBlank(name)) {
|
||||
return dingUserVOS.stream().filter(item -> item.getName().contains(name)).collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
return dingUserVOS;
|
||||
}catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<DingAttendanceStatsVO> getUserReport(Long shopId, String key, String secret, DateTime startTime, DateTime endTime, String name) {
|
||||
Map<String, String> columns = getColumns(key, secret, shopId);
|
||||
ArrayList<DingUserVO> userList = getUserList(name, key, secret, shopId);
|
||||
ArrayList<DingAttendanceStatsVO> statsVOS = new ArrayList<>();
|
||||
for (DingUserVO item : userList) {
|
||||
JSONObject repost = null;
|
||||
try {
|
||||
repost = getRepost(startTime, endTime, item.getUserid(),
|
||||
String.join(",", columns.keySet()), key, secret, shopId);
|
||||
} catch (ApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
DingAttendanceStatsVO statsVO = new DingAttendanceStatsVO();
|
||||
statsVOS.add(statsVO);
|
||||
statsVO.setUserId(item.getUserid());
|
||||
statsVO.setName(item.getName());
|
||||
if (repost != null) {
|
||||
statsVO.setActive(true);
|
||||
repost.getJSONArray("column_vals").forEach(info -> {
|
||||
JSONObject infoObj = (JSONObject) info;
|
||||
Long id = infoObj.getJSONObject("column_vo").getLong("id");
|
||||
|
||||
|
||||
BigDecimal value = BigDecimal.ZERO;
|
||||
for (Object column : infoObj.getJSONArray("column_vals")) {
|
||||
JSONObject columnObj = (JSONObject) column;
|
||||
value = value.add(new BigDecimal(columnObj.getString("value")));
|
||||
}
|
||||
statsVO.setValByName(columns.get(id.toString()), value.stripTrailingZeros().toPlainString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return statsVOS;
|
||||
}
|
||||
|
||||
static void main() throws ApiException {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user