Переглянути джерело

新增渠道流量排序字段
新增同步unionid字段接口

qt 3 днів тому
батько
коміт
9d35756827

+ 16 - 5
fs-qw-task/src/main/java/com/fs/app/controller/CommonController.java

@@ -2,12 +2,11 @@ package com.fs.app.controller;
 
 
 import cn.hutool.core.date.DateUtil;
-import com.fs.app.taskService.QwExternalContactRatingService;
-import com.fs.app.taskService.SopLogsChatTaskService;
-import com.fs.app.taskService.SopLogsTaskService;
-import com.fs.app.taskService.SopWxLogsService;
+import com.fs.app.taskService.*;
 import com.fs.common.core.domain.R;
 import com.fs.common.core.domain.ResponseResult;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.utils.StringUtils;
 import com.fs.company.service.ICompanyService;
 import com.fs.company.vo.RedPacketMoneyVO;
 import com.fs.course.mapper.FsCourseRedPacketLogMapper;
@@ -18,10 +17,12 @@ import com.fs.his.domain.FsUser;
 import com.fs.his.service.IFsInquiryOrderService;
 import com.fs.his.utils.qrcode.QRCodeUtils;
 import com.fs.qw.domain.QwCompany;
+import com.fs.qw.domain.QwExternalContact;
 import com.fs.qw.mapper.QwExternalContactMapper;
 import com.fs.qw.service.IQwCompanyService;
 import com.fs.qw.service.IQwExternalContactService;
 import com.fs.qw.service.IQwMaterialService;
+import com.fs.qwApi.domain.QwExternalContactResult;
 import com.fs.qwApi.service.QwApiService;
 import com.fs.sop.mapper.QwSopLogsMapper;
 import com.fs.sop.mapper.QwSopMapper;
@@ -40,6 +41,7 @@ import org.springframework.web.bind.annotation.RestController;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -114,10 +116,13 @@ public class CommonController {
     @Autowired
     private IQwMaterialService iQwMaterialService;
 
-
     @Autowired
     private IFsCourseLinkService iFsCourseLinkService;
 
+    @Autowired
+    private SyncQwExternalContactService syncQwExternalContactService;
+
+
     /**
     * 发官方通连
     */
@@ -318,6 +323,12 @@ public class CommonController {
         return R.ok();
     }
 
+    @GetMapping("/syncQwExternalContactUnionid")
+    public R syncQwExternalContactUnionid(){
+        return syncQwExternalContactService.syncQwExternalContactUnionid();
+    }
+
+
     @GetMapping("/queryRedPacketResult")
     public R queryRedPacketResult(String startTime , String  endTime) {
         fsCourseRedPacketLogService.queryRedPacketResult(startTime, endTime);

+ 11 - 0
fs-qw-task/src/main/java/com/fs/app/task/qwTask.java

@@ -78,6 +78,9 @@ public class qwTask {
     @Autowired
     private QwExternalContactRatingMoreSevenDaysService qwExternalContactRatingMoreSevenDaysService;
 
+    @Autowired
+    private SyncQwExternalContactService syncQwExternalContactService;
+
     /**
      * 定时任务:检查SOP规则时间
      * 执行时间:每天凌晨 1:10:00
@@ -374,4 +377,12 @@ public class qwTask {
         long endTimeMillis = System.currentTimeMillis();
         log.info("====== 更新掉所有前一天的所有待发送,耗时 {} 毫秒 ======", (endTimeMillis - startTimeMillis));
     }
+
+    @Scheduled(cron = "0 1 0 */2 * ?")
+    public void updateQwExternalContactUnionid() {
+        long startTimeMillis = System.currentTimeMillis();
+        log.info("====== 同步外部联系人的UnionId ======");
+        syncQwExternalContactService.syncQwExternalContactUnionid();
+
+    }
 }

+ 10 - 0
fs-qw-task/src/main/java/com/fs/app/taskService/SyncQwExternalContactService.java

@@ -0,0 +1,10 @@
+package com.fs.app.taskService;
+
+import com.fs.common.core.domain.R;
+
+public interface SyncQwExternalContactService {
+
+    R syncQwExternalContactUnionid();
+
+
+}

+ 78 - 0
fs-qw-task/src/main/java/com/fs/app/taskService/impl/SyncQwExternalContactServiceImpl.java

@@ -0,0 +1,78 @@
+package com.fs.app.taskService.impl;
+
+import com.fs.app.taskService.SyncQwExternalContactService;
+import com.fs.common.core.domain.R;
+import com.fs.common.core.redis.RedisCache;
+import com.fs.common.utils.StringUtils;
+import com.fs.qw.domain.QwExternalContact;
+import com.fs.qw.mapper.QwExternalContactMapper;
+import com.fs.qwApi.domain.QwExternalContactResult;
+import com.fs.qwApi.service.QwApiService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+@Slf4j
+public class SyncQwExternalContactServiceImpl implements SyncQwExternalContactService {
+    @Autowired
+    private RedisCache redisCache;
+    @Autowired
+    private QwExternalContactMapper qwExternalContactMapper;
+    @Autowired
+    private QwApiService qwApiService;
+    @Override
+    public R syncQwExternalContactUnionid() {
+        // 测试环境需要在sql加上:and corp_id='ww51717e2b71d5e2d3'
+        // 查询这次同步的最大id
+        Long maxId = qwExternalContactMapper.selectSyncMaxId();
+        log.info("同步最大id值:"+maxId);
+        if (maxId == null) {
+            return R.ok("无需同步");
+        }
+        Long recordId = 0L;
+        String recordIdStr = redisCache.getCacheObject("syncQwExternalContactUnionId");
+        if (StringUtils.isNotEmpty(recordIdStr)) {
+            try {
+                recordId = Long.parseLong(recordIdStr);
+            } catch (NumberFormatException e) {
+                log.info("Failed to parse recordId from redis: {}", recordIdStr);
+                recordId = 0L;
+            }
+        }
+        log.info("开始同步的recordId值:"+recordId);
+        // 循环同步直到recordId等于maxId
+        while (recordId < maxId) {
+            // 每次查询500条数据
+            List<QwExternalContact> qwExternalContacts = qwExternalContactMapper.selectSyncData(recordId, maxId);
+            if (qwExternalContacts.isEmpty()) {
+                break;
+            }
+            List<QwExternalContact> batchList = new ArrayList<>();
+            // 调用接口
+            for (QwExternalContact info : qwExternalContacts) {
+                QwExternalContactResult externalcontact = qwApiService.getExternalcontact(info.getExternalUserId(), info.getCorpId());
+                if (null!=externalcontact && null!=externalcontact.getExternal_contact() && null!=externalcontact.getExternal_contact().getUnionid() ) {
+                    info.setUnionid(externalcontact.getExternal_contact().getUnionid());
+                    batchList.add(info);
+                }
+            }
+            if (!batchList.isEmpty()) {
+                for (QwExternalContact qwExternalContact : batchList) {
+                    qwExternalContactMapper.batchUpdateUnionId(qwExternalContact);
+                }
+            }else{
+                log.info("集合为空:{recordId->"+recordId+";syncId->"+qwExternalContacts.get(qwExternalContacts.size() - 1).getId()+"}");
+            }
+            // 更新recordId为本次处理的最后一条记录的id
+            recordId = qwExternalContacts.get(qwExternalContacts.size() - 1).getId();
+            // 更新redis中的记录值
+            redisCache.setCacheObject("syncQwExternalContactUnionId", recordId.toString());
+        }
+        log.info("同步成功,同步完之后的recordId:"+recordId);
+        return R.ok();
+    }
+}

+ 4 - 0
fs-service/src/main/java/com/fs/qw/domain/QwContactWay.java

@@ -141,4 +141,8 @@ public class QwContactWay extends BaseEntity
     private String welcomeJson;
 
     private Long informationId;
+
+    /** 序号 */
+    private Integer sort;
+
 }

+ 13 - 1
fs-service/src/main/java/com/fs/qw/mapper/QwExternalContactMapper.java

@@ -227,10 +227,12 @@ public interface QwExternalContactMapper extends BaseMapper<QwExternalContact> {
     public int deleteQwExternalContactByIds(Long[] ids);
 
     @Select({"<script> " +
-            "select ec.*,qu.qw_user_name,qd.dept_name as departmentName from qw_external_contact ec " +
+            "select ec.*,qu.qw_user_name,qd.dept_name as departmentName,cw.name way_name,wg.group_name way_group_name from qw_external_contact ec " +
             "left join qw_user qu on ec.user_id=qu.qw_user_id and qu.corp_id=ec.corp_id " +
             "left join qw_dept qd on qd.dept_id=qu.department and qd.corp_id=qu.corp_id " +
             "left join company_user cu on ec.company_user_id=cu.user_id " +
+            "left join qw_contact_way cw on cw.id = ec.way_id " +
+            "left join qw_contact_way_group wg on wg.id=cw.group_id" +
             "<where>  \n" +
             "            <if test=\"userId != null  and userId != ''\"> and ec.user_id   like concat( #{userId}, '%') </if>\n" +
             "            <if test=\"qwUserName != null  and qwUserName != ''\"> and qu.qw_user_name   like concat( #{qwUserName}, '%') </if>\n" +
@@ -505,4 +507,14 @@ public interface QwExternalContactMapper extends BaseMapper<QwExternalContact> {
     List<QwExternalContact> selectQwExternalContactByFsUserIdAndCompany(@Param("userId")Long userId,@Param("companyUserId") Long companyUserId);
 
     void updateQwExternalContactIsRePlyById(@Param("id")Long id);
+
+    @Select("SELECT max(id) FROM qw_external_contact where status <>'4' and unionid is NULL")
+    Long selectSyncMaxId();
+
+    @Select("SELECT * FROM qw_external_contact where id >#{recordId} and id <=#{maxId} and status <>'4' and unionid is NULL order by id asc limit 500")
+    List<QwExternalContact> selectSyncData(@Param("recordId")Long recordId,@Param("maxId")Long maxId);
+
+    @Update("update qw_external_contact set unionid = #{item.unionid} where id = #{item.id}")
+    int batchUpdateUnionId(@Param("item") QwExternalContact item);
+
 }

+ 3 - 0
fs-service/src/main/java/com/fs/qw/service/impl/QwContactWayServiceImpl.java

@@ -104,6 +104,9 @@ public class QwContactWayServiceImpl implements IQwContactWayService
         log.info("新增企微活码corpId"+corpId);
         qwContactWay.setType(type);
         qwContactWay.setCreateTime(DateUtils.getNowDate());
+        if (null==qwContactWay.getSort()) {
+            qwContactWay.setSort(0);
+        }
         qwContactWayMapper.insertQwContactWay(qwContactWay);
         Long id = qwContactWay.getId();
         qwAddContactWayParam.setType(type);

+ 10 - 0
fs-service/src/main/java/com/fs/qw/vo/QwExternalContactVO.java

@@ -129,4 +129,14 @@ public class QwExternalContactVO {
      * 是否回复
      */
     private Integer isReply;
+
+    /**
+     * 联系我名称
+     */
+    private String wayName;
+
+    /**
+     * 联系我分组名称
+     */
+    private String wayGroupName;
 }

+ 5 - 2
fs-service/src/main/resources/mapper/qw/QwContactWayMapper.xml

@@ -42,10 +42,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="welcomeJson"    column="welcome_json"    />
         <result property="corpId"    column="corp_id"    />
         <result property="informationId"    column="information_id"    />
+        <result property="sort"    column="sort"    />
     </resultMap>
 
     <sql id="selectQwContactWayVo">
-        select id, type,corp_id,information_id, name,welcome_json, remark, skip_verify, state, user_ids, add_num, delete_num, num, qr_code, config_id, is_del, create_time, company_id, is_welcome, text_content, image_pic_url, is_user_limit, is_span_welcome, is_close_welcome, close_welcome_word, is_tag, tags, is_remark, remark_status, description, is_description, spare_user_ids, group_id, user_limit_json, user_time_json, user_type from qw_contact_way
+        select id, type,corp_id,information_id, name,welcome_json, remark, skip_verify, state, user_ids, add_num, delete_num, num, qr_code, config_id, is_del, create_time, company_id, is_welcome, text_content, image_pic_url, is_user_limit, is_span_welcome, is_close_welcome, close_welcome_word, is_tag, tags, is_remark, remark_status, description, is_description, spare_user_ids, group_id, user_limit_json, user_time_json, user_type,sort
+        from qw_contact_way
     </sql>
 
     <select id="selectQwContactWayList" parameterType="QwContactWay" resultMap="QwContactWayResult">
@@ -82,7 +84,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="userLimitJson != null  and userLimitJson != ''"> and user_limit_json = #{userLimitJson}</if>
             <if test="userTimeJson != null  and userTimeJson != ''"> and user_time_json = #{userTimeJson}</if>
             <if test="userType != null "> and user_type = #{userType}</if>
-        </where> order by id desc
+        </where> order by sort asc,create_time desc
     </select>
 
     <select id="selectQwContactWayById" parameterType="Long" resultMap="QwContactWayResult">
@@ -206,6 +208,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="welcomeJson != null">welcome_json = #{welcomeJson},</if>
             <if test="corpId != null">corp_id = #{corpId},</if>
             <if test="informationId != null">information_id = #{informationId},</if>
+            <if test="sort != null">sort = #{sort},</if>
         </trim>
         where id = #{id}
     </update>