|
|
@@ -1,20 +1,35 @@
|
|
|
package com.tzld.piaoquan.api.service.wecom.thirdparty.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
|
|
|
+import com.tzld.piaoquan.api.common.enums.wecom.ThirdPartWeComStaffStatusEnum;
|
|
|
+import com.tzld.piaoquan.api.common.exception.CommonException;
|
|
|
import com.tzld.piaoquan.api.component.WeComThirdPartyApiClient;
|
|
|
+import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffConfigMapper;
|
|
|
+import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffConfigTaskContentMapper;
|
|
|
+import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffConfigTaskMapper;
|
|
|
+import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffMapper;
|
|
|
+import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ext.ThirdPartWeComStaffMapperExt;
|
|
|
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.*;
|
|
|
+import com.tzld.piaoquan.api.model.po.wecom.thirdpart.*;
|
|
|
import com.tzld.piaoquan.api.model.vo.IdNameVO;
|
|
|
import com.tzld.piaoquan.api.model.vo.contentplatform.AccountQrCodeVO;
|
|
|
+import com.tzld.piaoquan.api.model.vo.wecom.thirdpart.ThirdPartyAccountConfigTaskContentVO;
|
|
|
+import com.tzld.piaoquan.api.model.vo.wecom.thirdpart.ThirdPartyAccountConfigTaskVO;
|
|
|
import com.tzld.piaoquan.api.model.vo.wecom.thirdpart.ThirdPartyAccountConfigVO;
|
|
|
import com.tzld.piaoquan.api.model.vo.wecom.thirdpart.ThirdPartyAccountVO;
|
|
|
import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyAccountService;
|
|
|
import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyService;
|
|
|
+import com.tzld.piaoquan.growth.common.utils.DistributedIdGenerator;
|
|
|
import com.tzld.piaoquan.growth.common.utils.RedisUtils;
|
|
|
import com.tzld.piaoquan.growth.common.utils.page.Page;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -24,28 +39,99 @@ public class WeComThirdPartyAccountServiceImpl implements WeComThirdPartyAccount
|
|
|
private WeComThirdPartyApiClient apiClient;
|
|
|
@Autowired
|
|
|
private WeComThirdPartyService weComThirdPartyService;
|
|
|
+ @Autowired
|
|
|
+ private ThirdPartWeComStaffMapper staffMapper;
|
|
|
+ @Autowired
|
|
|
+ private ThirdPartWeComStaffConfigMapper staffConfigMapper;
|
|
|
+ @Autowired
|
|
|
+ private ThirdPartWeComStaffConfigTaskMapper staffConfigTaskMapper;
|
|
|
+ @Autowired
|
|
|
+ private ThirdPartWeComStaffConfigTaskContentMapper staffConfigTaskContentMapper;
|
|
|
+ @Autowired
|
|
|
+ private ThirdPartWeComStaffMapperExt staffMapperExt;
|
|
|
|
|
|
@Autowired
|
|
|
private RedisUtils redisUtils;
|
|
|
|
|
|
@Override
|
|
|
- public AccountQrCodeVO getQrCode(String id) {
|
|
|
- return null;
|
|
|
+ public AccountQrCodeVO getQrCode(Long id) {
|
|
|
+ InitRequest initRequest = new InitRequest();
|
|
|
+ if (Objects.nonNull(id)) {
|
|
|
+ ThirdPartWeComStaff staff = weComThirdPartyService.getStaffById(id);
|
|
|
+ if (Objects.isNull(staff)) {
|
|
|
+ throw new CommonException(ExceptionEnum.PARAM_ERROR.getCode(), "企微账号不存在");
|
|
|
+ }
|
|
|
+ initRequest.setVid(String.valueOf(staff.getThirdStaffId()));
|
|
|
+ }
|
|
|
+ initRequest.setProxySituation(0);
|
|
|
+ initRequest.setDeverType("ipad");
|
|
|
+ String initResponseStr = apiClient.init(initRequest);
|
|
|
+ CommonResponse<InitResponse> initResponse =
|
|
|
+ JSONObject.parseObject(initResponseStr, new TypeReference<CommonResponse<InitResponse>>() {});
|
|
|
+ String uuid = initResponse.getData().getUuid();
|
|
|
+ String qrCodeResponseStr = apiClient.getQrCode(new UuidRequest(uuid));
|
|
|
+ CommonResponse<QrCodeResponse> qrCodeResponse =
|
|
|
+ JSONObject.parseObject(qrCodeResponseStr, new TypeReference<CommonResponse<QrCodeResponse>>() {});
|
|
|
+ if (qrCodeResponse.getErrcode() != 0) {
|
|
|
+ log.error("WeComThirdPartyService get qr code failed, request: {}, response: {}", initRequest, qrCodeResponse);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ AccountQrCodeVO result = new AccountQrCodeVO();
|
|
|
+ result.setUuid(uuid);
|
|
|
+ result.setQrcode(qrCodeResponse.getData().getQrcode());
|
|
|
+ result.setKey(qrCodeResponse.getData().getKey());
|
|
|
+ result.setTtl(qrCodeResponse.getData().getTtl());
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Boolean getNeedCheckCode(String uuid) {
|
|
|
- return null;
|
|
|
+ String needVerifyCode = "needVerifyCode:uuid:" + uuid;
|
|
|
+ return redisUtils.containsKey(needVerifyCode);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setCheckCode(CheckCodeRequest param) {
|
|
|
-
|
|
|
+ apiClient.checkCode(param);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Page<ThirdPartyAccountVO> list(ThirdPartyAccountListParam param) {
|
|
|
- return null;
|
|
|
+ Page<ThirdPartyAccountVO> result = new Page<>(param.getPageNum(), param.getPageSize());
|
|
|
+ int offset = (param.getPageNum() - 1) * param.getPageSize();
|
|
|
+ int count = staffMapperExt.getAccountCount(param);
|
|
|
+ result.setTotalSize(count);
|
|
|
+ if (count == 0) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ List<ThirdPartWeComStaff> staffList = staffMapperExt.getAccountList(param, offset, param.getPageSize());
|
|
|
+ List<ThirdPartyAccountVO> list = buildThirdPartyAccountVOList(staffList);
|
|
|
+ result.setObjs(list);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ThirdPartyAccountVO> buildThirdPartyAccountVOList(List<ThirdPartWeComStaff> staffList) {
|
|
|
+ List<Long> corpIds = staffList.stream().map(ThirdPartWeComStaff::getCorpId).collect(Collectors.toList());
|
|
|
+ List<ThirdPartWeComCorp> corpList = weComThirdPartyService.getCorpListByIds(corpIds);
|
|
|
+ Map<Long, ThirdPartWeComCorp> corpMap = corpList.stream()
|
|
|
+ .collect(Collectors.toMap(ThirdPartWeComCorp::getId, corp -> corp));
|
|
|
+
|
|
|
+ List<ThirdPartyAccountVO> result = new ArrayList<>();
|
|
|
+ for (ThirdPartWeComStaff staff : staffList) {
|
|
|
+ ThirdPartyAccountVO vo = new ThirdPartyAccountVO();
|
|
|
+ vo.setId(staff.getThirdStaffId());
|
|
|
+ vo.setCorpName(corpMap.get(staff.getCorpId()).getName());
|
|
|
+ vo.setAccountName(staff.getName());
|
|
|
+ vo.setRealName(staff.getRealName());
|
|
|
+ vo.setMobile(staff.getMobile());
|
|
|
+ vo.setStatus(staff.getStatus());
|
|
|
+ vo.setQrImage(staff.getQrImage());
|
|
|
+ vo.setAvatar(staff.getAvatar());
|
|
|
+ vo.setThirdStaffId(staff.getThirdStaffId());
|
|
|
+ vo.setThirdUuid(staff.getThirdUuid());
|
|
|
+ result.add(vo);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -55,18 +141,149 @@ public class WeComThirdPartyAccountServiceImpl implements WeComThirdPartyAccount
|
|
|
|
|
|
@Override
|
|
|
public ThirdPartyAccountConfigVO getAccountConfig(ThirdPartyConfigGetParam param) {
|
|
|
- return null;
|
|
|
+ ThirdPartWeComStaffConfig config = getStaffConfig(param.getId());
|
|
|
+ if (Objects.isNull(config)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<ThirdPartWeComStaff> pushStaffList = getStaffConfigPushList(config);
|
|
|
+ Map<Long, String> pushStaffMap = pushStaffList.stream()
|
|
|
+ .collect(Collectors.toMap(ThirdPartWeComStaff::getThirdStaffId, ThirdPartWeComStaff::getName));
|
|
|
+ List<ThirdPartWeComStaffConfigTask> taskList = getStaffConfigTasks(config.getId());
|
|
|
+ List<String> taskIds = taskList.stream().map(ThirdPartWeComStaffConfigTask::getId).collect(Collectors.toList());
|
|
|
+ List<ThirdPartWeComStaffConfigTaskContent> contentList = getStaffConfigTaskContents(taskIds);
|
|
|
+ Map<String, List<ThirdPartWeComStaffConfigTaskContent>> taskContentMap = contentList.stream()
|
|
|
+ .collect(Collectors.groupingBy(ThirdPartWeComStaffConfigTaskContent::getTaskId));
|
|
|
+ ThirdPartyAccountConfigVO result = new ThirdPartyAccountConfigVO();
|
|
|
+ result.setPutScene(config.getPutScene());
|
|
|
+ result.setChannel(config.getChannel());
|
|
|
+ result.setSwitchFlag(config.getSwitchFlag());
|
|
|
+ result.setOpenRoomSendSwitchNum(config.getOpenRoomSendSwitchNum());
|
|
|
+ if (Objects.nonNull(config.getPrimaryThirdStaffId())) {
|
|
|
+ result.setPrimaryThirdStaffId(config.getPrimaryThirdStaffId());
|
|
|
+ result.setPrimaryThirdStaffName(pushStaffMap.get(config.getPrimaryThirdStaffId()));
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(config.getSecondThirdStaffId())) {
|
|
|
+ result.setSecondThirdStaffId(config.getSecondThirdStaffId());
|
|
|
+ result.setSecondThirdStaffName(pushStaffMap.get(config.getSecondThirdStaffId()));
|
|
|
+ }
|
|
|
+ result.setTaskList(taskList.stream().map(task -> {
|
|
|
+ ThirdPartyAccountConfigTaskVO vo = new ThirdPartyAccountConfigTaskVO();
|
|
|
+ vo.setSwitchFlag(task.getSwitchFlag());
|
|
|
+ vo.setSendTime(JSONObject.parseArray(task.getSendTime(), String.class));
|
|
|
+ vo.setRandomMin(task.getRandomMin());
|
|
|
+ vo.setSeq(task.getSeq());
|
|
|
+ List<ThirdPartWeComStaffConfigTaskContent> contents = taskContentMap.get(task.getId());
|
|
|
+ vo.setContentList(contents.stream().map(content -> {
|
|
|
+ ThirdPartyAccountConfigTaskContentVO contentVO = new ThirdPartyAccountConfigTaskContentVO();
|
|
|
+ contentVO.setType(content.getType());
|
|
|
+ contentVO.setContent(content.getContent());
|
|
|
+ contentVO.setSource(content.getSource());
|
|
|
+ contentVO.setSourceContent(content.getSourceContent());
|
|
|
+ contentVO.setSeq(content.getSeq());
|
|
|
+ return contentVO;
|
|
|
+ }).collect(Collectors.toList()));
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList()));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ThirdPartWeComStaff> getStaffConfigPushList(ThirdPartWeComStaffConfig config) {
|
|
|
+ List<Long> thirdStaffIds = new ArrayList<>();
|
|
|
+ if (Objects.nonNull(config.getPrimaryThirdStaffId())) {
|
|
|
+ thirdStaffIds.add(config.getPrimaryThirdStaffId());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(config.getSecondThirdStaffId())) {
|
|
|
+ thirdStaffIds.add(config.getSecondThirdStaffId());
|
|
|
+ }
|
|
|
+ return weComThirdPartyService.getStaffListByThirdStaffIds(thirdStaffIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ThirdPartWeComStaffConfig getStaffConfig(Long accountId) {
|
|
|
+ ThirdPartWeComStaffConfigExample example = new ThirdPartWeComStaffConfigExample();
|
|
|
+ example.createCriteria().andStaffIdEqualTo(accountId).andStatusEqualTo(1);
|
|
|
+ List<ThirdPartWeComStaffConfig> configs = staffConfigMapper.selectByExample(example);
|
|
|
+ if (configs.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return configs.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ThirdPartWeComStaffConfigTask> getStaffConfigTasks(String configId) {
|
|
|
+ ThirdPartWeComStaffConfigTaskExample example = new ThirdPartWeComStaffConfigTaskExample();
|
|
|
+ example.createCriteria().andConfigIdEqualTo(configId);
|
|
|
+ return staffConfigTaskMapper.selectByExample(example);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ThirdPartWeComStaffConfigTaskContent> getStaffConfigTaskContents(List<String> taskIds) {
|
|
|
+ ThirdPartWeComStaffConfigTaskContentExample example = new ThirdPartWeComStaffConfigTaskContentExample();
|
|
|
+ example.createCriteria().andTaskIdIn(taskIds);
|
|
|
+ return staffConfigTaskContentMapper.selectByExample(example);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void saveAccountConfig(ThirdPartyAccountConfigParam param) {
|
|
|
-
|
|
|
+ // todo check param
|
|
|
+ ThirdPartWeComStaffConfig config = getStaffConfig(param.getAccountId());
|
|
|
+ if (Objects.nonNull(config)) {
|
|
|
+ config.setStatus(0);
|
|
|
+ config.setUpdateTime(new Date());
|
|
|
+ staffConfigMapper.updateByPrimaryKey(config);
|
|
|
+ }
|
|
|
+ ThirdPartWeComStaffConfig newConfig = new ThirdPartWeComStaffConfig();
|
|
|
+ newConfig.setId(DistributedIdGenerator.generate());
|
|
|
+ newConfig.setStaffId(param.getAccountId());
|
|
|
+ newConfig.setPutScene(param.getPutScene());
|
|
|
+ newConfig.setChannel(param.getChannel());
|
|
|
+ newConfig.setSwitchFlag(param.getSwitchFlag());
|
|
|
+ newConfig.setOpenRoomSendSwitchNum(param.getOpenRoomSendSwitchNum());
|
|
|
+ newConfig.setPrimaryThirdStaffId(param.getPrimaryThirdStaffId());
|
|
|
+ newConfig.setSecondThirdStaffId(param.getSecondThirdStaffId());
|
|
|
+ newConfig.setStatus(1);
|
|
|
+ newConfig.setCreateTime(new Date());
|
|
|
+ newConfig.setUpdateTime(new Date());
|
|
|
+ staffConfigMapper.insert(newConfig);
|
|
|
+ for (ThirdPartyAccountConfigTaskParam taskParam : param.getTaskList()) {
|
|
|
+ ThirdPartWeComStaffConfigTask newTask = new ThirdPartWeComStaffConfigTask();
|
|
|
+ newTask.setConfigId(newConfig.getId());
|
|
|
+ newTask.setSwitchFlag(taskParam.getSwitchFlag());
|
|
|
+ newTask.setSendTime(JSONObject.toJSONString(taskParam.getSendTime()));
|
|
|
+ newTask.setRandomMin(taskParam.getRandomMin());
|
|
|
+ newTask.setSeq(taskParam.getSeq());
|
|
|
+ newTask.setCreateTime(new Date());
|
|
|
+ newTask.setUpdateTime(new Date());
|
|
|
+ staffConfigTaskMapper.insert(newTask);
|
|
|
+ for (ThirdPartyAccountConfigTaskContentParam taskContentParam : taskParam.getContentList()) {
|
|
|
+ ThirdPartWeComStaffConfigTaskContent newContent = new ThirdPartWeComStaffConfigTaskContent();
|
|
|
+ newContent.setTaskId(newTask.getId());
|
|
|
+ newContent.setType(taskContentParam.getType());
|
|
|
+ newContent.setContent(taskContentParam.getContent());
|
|
|
+ newContent.setSource(taskContentParam.getSource());
|
|
|
+ newContent.setSourceContent(taskContentParam.getSourceContent());
|
|
|
+ newContent.setSeq(taskContentParam.getSeq());
|
|
|
+ newContent.setCreateTime(new Date());
|
|
|
+ newContent.setUpdateTime(new Date());
|
|
|
+ staffConfigTaskContentMapper.insert(newContent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // todo 配置同步
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void logout(ThirdPartyAccountLogOutParam param) {
|
|
|
-
|
|
|
+ ThirdPartWeComStaff staff = weComThirdPartyService.getStaffById(param.getId());
|
|
|
+ if (Objects.isNull(staff)) {
|
|
|
+ throw new CommonException(ExceptionEnum.PARAM_ERROR.getCode(), "企微账号不存在");
|
|
|
+ }
|
|
|
+ String logoutResponseStr = apiClient.loginOut(new UuidRequest(staff.getThirdUuid()));
|
|
|
+ CommonResponse<LoginOutResponse> logoutResponse =
|
|
|
+ JSONObject.parseObject(logoutResponseStr, new TypeReference<CommonResponse<LoginOutResponse>>() {});
|
|
|
+ if (logoutResponse.getErrcode() != 0) {
|
|
|
+ log.error("WeComThirdPartyService logout failed, request: {}, response: {}", staff.getThirdUuid(), logoutResponse);
|
|
|
+ throw new CommonException(ExceptionEnum.THIRD_PART_LOGOUT_FAILED.getCode(), "企微账号退出失败");
|
|
|
+ }
|
|
|
+ staff.setStatus(ThirdPartWeComStaffStatusEnum.QUIT.getVal());
|
|
|
+ staff.setUpdateTime(new Date());
|
|
|
+ staffMapper.updateByPrimaryKey(staff);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|