|
@@ -0,0 +1,177 @@
|
|
|
+package com.fdkankan.sale.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.fyun.face.FYunFileServiceInterface;
|
|
|
+import com.fdkankan.redis.util.RedisUtil;
|
|
|
+import com.fdkankan.sale.common.*;
|
|
|
+import com.fdkankan.sale.entity.DingConfig;
|
|
|
+import com.fdkankan.sale.entity.Repair;
|
|
|
+import com.fdkankan.sale.entity.RepairLog;
|
|
|
+import com.fdkankan.sale.entity.SysUser;
|
|
|
+import com.fdkankan.sale.exception.BusinessException;
|
|
|
+import com.fdkankan.sale.httpClient.client.DingClient;
|
|
|
+import com.fdkankan.sale.httpClient.request.DingMsgReq;
|
|
|
+import com.fdkankan.sale.httpClient.request.OaData;
|
|
|
+import com.fdkankan.sale.service.IDingConfigService;
|
|
|
+import com.fdkankan.sale.service.IRepairService;
|
|
|
+import com.fdkankan.sale.service.ISysUserService;
|
|
|
+import com.fdkankan.sale.util.StatusUtil;
|
|
|
+import com.fdkankan.sale.vo.response.RepairLogVo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class DingService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ IDingConfigService dingConfigService;
|
|
|
+ @Autowired
|
|
|
+ DingClient dingClient;
|
|
|
+ @Autowired
|
|
|
+ RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ FYunFileServiceInterface fYunFileServiceInterface;
|
|
|
+ @Autowired
|
|
|
+ IRepairService repairService;
|
|
|
+ @Autowired
|
|
|
+ RepairInfoService repairInfoService;
|
|
|
+ @Autowired
|
|
|
+ ISysUserService sysUserService;
|
|
|
+
|
|
|
+ private String getAccessToken(){
|
|
|
+ String redisKey = String.format(RedisKeyUtil.dingKeyToken, CacheUtil.dingServeName);
|
|
|
+ if(redisUtil.hasKey(redisKey)){
|
|
|
+ return redisUtil.get(redisKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ DingConfig dingConfig = dingConfigService.getByServeName(CacheUtil.dingServeName);
|
|
|
+ if(dingConfig == null){
|
|
|
+ throw new BusinessException(ResultCode.DING_CONFIG_EMPTY);
|
|
|
+ }
|
|
|
+ JSONObject token = dingClient.getToken(dingConfig.getAppKey(), dingConfig.getAppSecret());
|
|
|
+ Integer errcode = token.getInteger("errcode");
|
|
|
+ if(errcode != 0){
|
|
|
+ throw new BusinessException(ResultCode.DING_CONFIG_ERROR);
|
|
|
+ }
|
|
|
+ String access_token = token.getString("access_token");
|
|
|
+ if(StringUtils.isBlank(access_token)){
|
|
|
+ throw new BusinessException(ResultCode.DING_CONFIG_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ redisUtil.set(redisKey,access_token,7200);
|
|
|
+ return access_token;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getUserIdByPhone(String phone){
|
|
|
+ String redisKey = String.format(RedisKeyUtil.dingKeyUsrId, CacheUtil.dingServeName,phone);
|
|
|
+ if(redisUtil.hasKey(redisKey)){
|
|
|
+ return redisUtil.get(redisKey);
|
|
|
+ }
|
|
|
+ JSONObject userByMobile = dingClient.getUserByMobile(getAccessToken(), phone);
|
|
|
+ Integer errcode = userByMobile.getInteger("errcode");
|
|
|
+ if(errcode != 0){
|
|
|
+ throw new BusinessException(ResultCode.DING_PHONE_ERROR);
|
|
|
+ }
|
|
|
+ String userid = userByMobile.getString("userid");
|
|
|
+ if(StringUtils.isBlank(userid)){
|
|
|
+ throw new BusinessException(ResultCode.DING_PHONE_ERROR);
|
|
|
+ }
|
|
|
+ redisUtil.set(redisKey,userid,60 * 60 * 24 * 30);
|
|
|
+ return userid;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendDingMsg(RepairLog repairLog){
|
|
|
+ try {
|
|
|
+ DingConfig dingConfig = dingConfigService.getByServeName(CacheUtil.dingServeName);
|
|
|
+ if(dingConfig == null){
|
|
|
+ throw new BusinessException(ResultCode.DING_CONFIG_EMPTY);
|
|
|
+ }
|
|
|
+ List<String> phones = new ArrayList<>();
|
|
|
+
|
|
|
+ OaData oaData = getOaData(repairLog);
|
|
|
+ Long sysUserId = oaData.getSysUserId();
|
|
|
+ SysUser sysUser = sysUserService.getById(sysUserId);
|
|
|
+ if(StringUtils.isNotBlank(sysUser.getDingAccount())){
|
|
|
+ phones.add(sysUser.getDingAccount());
|
|
|
+
|
|
|
+ }
|
|
|
+ if(repairLog.getRepairStatus().equals(RepairStatusProcessComing.TO_BE_PREPARED.getStatus())){
|
|
|
+ List<SysUser> userList = sysUserService.getByRoleType(6);
|
|
|
+ for (SysUser user : userList) {
|
|
|
+ if(StringUtils.isNotBlank(user.getDingAccount())){
|
|
|
+ phones.add(user.getDingAccount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(phones.size() <=0){
|
|
|
+ throw new BusinessException(ResultCode.DING_SEND_ERROR);
|
|
|
+ }
|
|
|
+ DingMsgReq dingMsgReq = new DingMsgReq();
|
|
|
+ dingMsgReq.setAgent_id(dingConfig.getAgentId());
|
|
|
+ List<String> userIdList = new ArrayList<>();
|
|
|
+ for (String phone : phones) {
|
|
|
+ String userId = getUserIdByPhone(phone);
|
|
|
+ userIdList.add(userId);
|
|
|
+ }
|
|
|
+ if(userIdList.size() <=0){
|
|
|
+ throw new BusinessException(ResultCode.DING_SEND_ERROR);
|
|
|
+ }
|
|
|
+ String userIds = String.join(",", userIdList);
|
|
|
+
|
|
|
+ String oaPath = String.format(FilePath.oss_ding_path, CacheUtil.dingServeName,"OA.json");
|
|
|
+ String fileContent = fYunFileServiceInterface.getFileContent(oaPath);
|
|
|
+ if(StringUtils.isBlank(fileContent)){
|
|
|
+ throw new BusinessException(ResultCode.DING_SEND_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ fileContent = fileContent.replaceAll("\\{mailUrl}",oaData.getMailUrl());
|
|
|
+ fileContent = fileContent.replaceAll("\\{repairId}",oaData.getRepairId());
|
|
|
+ fileContent = fileContent.replaceAll("\\{repairCreateTime}",oaData.getRepairStatusStr());
|
|
|
+ fileContent = fileContent.replaceAll("\\{cameraTypeStr}",oaData.getCameraTypeStr());
|
|
|
+ fileContent = fileContent.replaceAll("\\{cameraSnCode}",oaData.getCameraSnCode());
|
|
|
+ fileContent = fileContent.replaceAll("\\{warrantyTypeStr}",oaData.getWarrantyTypeStr());
|
|
|
+ fileContent = fileContent.replaceAll("\\{repairStatusStr}",oaData.getRepairStatusStr());
|
|
|
+ fileContent = fileContent.replaceAll("\\{description}",oaData.getDescription());
|
|
|
+ dingMsgReq.setUserid_list(userIds);
|
|
|
+ dingMsgReq.setMsg(JSONObject.parseObject(fileContent));
|
|
|
+ dingMsgReq.setTo_all_user(false);
|
|
|
+ dingClient.sendMsg(getAccessToken(),dingMsgReq);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("ding--send--msg,error:",e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private OaData getOaData(RepairLog repairLog){
|
|
|
+ Repair repair = repairService.getById(repairLog.getRepairId());
|
|
|
+
|
|
|
+ OaData oaData = new OaData();
|
|
|
+ oaData.setMailUrl(CacheUtil.mainUrl);
|
|
|
+ oaData.setRepairId(repairLog.getRepairId());
|
|
|
+ oaData.setRepairCreateTime(repair.getCreateTime());
|
|
|
+ oaData.setCameraTypeStr(StatusUtil.getCameraName(repair.getCameraType()));
|
|
|
+ oaData.setCameraSnCode(repair.getCameraSnCode());
|
|
|
+ oaData.setWarrantyTypeStr(StatusUtil.getWarrantyType(repair.getWarrantyType()));
|
|
|
+ oaData.setRepairStatusStr(RepairStatusProcessComing.getByStatus(repair.getStatus()).getMsg());
|
|
|
+
|
|
|
+ RepairLogVo repairLogVo = new RepairLogVo();
|
|
|
+ BeanUtils.copyProperties(repairLog,repairLogVo);
|
|
|
+ RepairLogVo nextStepVo = repairInfoService.getNextStepVo(repairLogVo);
|
|
|
+ oaData.setDescription(nextStepVo.getSubTitle());
|
|
|
+ oaData.setSysUserId(nextStepVo.getSysUserId());
|
|
|
+ return oaData;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|