|
|
@@ -0,0 +1,277 @@
|
|
|
+package com.linkwechat.coal.modules.bdshdb.customer.scheduled;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.domain.dto.LocalAppUserDto;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.domain.vo.LocalAppUserVo;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.service.ILocalAppUserService;
|
|
|
+import com.linkwechat.coal.modules.jzgdb.customer.domain.dto.SysUserJzgDto;
|
|
|
+import com.linkwechat.coal.modules.jzgdb.customer.domain.vo.SysUserJzgVo;
|
|
|
+import com.linkwechat.coal.modules.jzgdb.customer.service.ISysUserJzgService;
|
|
|
+import com.linkwechat.domain.WeCustomerUser;
|
|
|
+import com.linkwechat.domain.WeUser;
|
|
|
+import com.linkwechat.service.IWeCustomerUserService;
|
|
|
+import com.linkwechat.service.IWeUserService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * <p>
|
|
|
+ * 目前此类是单线程类,如果一个执行不完,另一个会卡死,错开时间执行
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class SyncUserDataScheduledService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ILocalAppUserService localAppUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWeUserService weUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWeCustomerUserService weCustomerUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserJzgService sysUserJzgService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定义一个按一定频率执行的定时任务 每天三点执行一次 更新同步用户
|
|
|
+ * 本地生活
|
|
|
+ */
|
|
|
+// @Scheduled(cron = "0 0 3 * * ?")
|
|
|
+// @PostConstruct
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void syncUserDataScheduled() {
|
|
|
+ try {
|
|
|
+ // 随机停几秒
|
|
|
+ Thread.sleep(RandomUtil.randomInt(5));
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ LocalAppUserDto localAppUserDto = new LocalAppUserDto();
|
|
|
+ this.setDate(localAppUserDto);
|
|
|
+ Date now = new Date();
|
|
|
+ // 查询本地生活 所有的用户 条件 普通用户 创建时间
|
|
|
+ List<LocalAppUserVo> localAppUserList = localAppUserService.selectUserList(localAppUserDto);
|
|
|
+ List<LocalAppUserVo> distinctList = localAppUserList.stream()
|
|
|
+ .collect(Collectors.collectingAndThen(
|
|
|
+ Collectors.toMap(
|
|
|
+ LocalAppUserVo::getPhone, // 按手机号作为Key
|
|
|
+ Function.identity(), // 值是对象本身
|
|
|
+ (existing, replacement) -> existing, // 有重复时保留第一个
|
|
|
+ LinkedHashMap::new // 保持原顺序
|
|
|
+ ),
|
|
|
+ m -> m.values().stream().collect(Collectors.toList())
|
|
|
+ ));
|
|
|
+ LambdaQueryWrapper<WeUser> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.select(WeUser::getPhone);
|
|
|
+ wrapper.eq(WeUser::getSource, 1);
|
|
|
+ List<WeUser> phoneList = weUserService.list(wrapper);
|
|
|
+ // 提取手机号并去重
|
|
|
+ Set<String> phoneSet = phoneList.stream()
|
|
|
+ .map(WeUser::getPhone) // 提取手机号
|
|
|
+ .filter(phone -> phone != null && !phone.isEmpty()) // 可选,去掉空值
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ if(CollUtil.isNotEmpty(distinctList)){
|
|
|
+ List<WeUser> users = new ArrayList<>();
|
|
|
+ for (LocalAppUserVo localAppUserVo : distinctList) {
|
|
|
+ if(phoneSet.contains(localAppUserVo.getPhone())){continue;}
|
|
|
+ localAppUserVo.setGender(Objects.isNull(localAppUserVo.getGender())? 1 : localAppUserVo.getGender());
|
|
|
+ WeUser weUser = new WeUser();
|
|
|
+ weUser.setUserCode(localAppUserVo.getId().toString());
|
|
|
+ weUser.setName(localAppUserVo.getName());
|
|
|
+ weUser.setPhone(localAppUserVo.getPhone());
|
|
|
+ weUser.setSex(localAppUserVo.getGender().equals(1) ? 1 : 0);
|
|
|
+ weUser.setBirthday(localAppUserVo.getBirthdayTime());
|
|
|
+ weUser.setSource(1); // 本地生活
|
|
|
+ weUser.setStatus(localAppUserVo.getIsFormal().equals(1)? 1 : 0);
|
|
|
+ weUser.setType(0);
|
|
|
+ weUser.setRegisterTime(localAppUserVo.getCreateTime());
|
|
|
+ weUser.setDeliveredStatus(0);
|
|
|
+ weUser.setCreateTime(now);
|
|
|
+ users.add(weUser);
|
|
|
+ }
|
|
|
+ weUserService.saveBatch(users);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ SysUserJzgDto sysUserDto = new SysUserJzgDto();
|
|
|
+ this.setDate(sysUserDto);
|
|
|
+ // 查询本地生活 所有的用户 条件 普通用户 创建时间
|
|
|
+ List<SysUserJzgVo> sysUserJzgVoList = sysUserJzgService.selectUserList(sysUserDto);
|
|
|
+ List<SysUserJzgVo> distinctUserList = sysUserJzgVoList.stream()
|
|
|
+ .collect(Collectors.collectingAndThen(
|
|
|
+ Collectors.toMap(
|
|
|
+ SysUserJzgVo::getMobile, // 按手机号作为Key
|
|
|
+ Function.identity(), // 值是对象本身
|
|
|
+ (existing, replacement) -> existing, // 有重复时保留第一个
|
|
|
+ LinkedHashMap::new // 保持原顺序
|
|
|
+ ),
|
|
|
+ m -> m.values().stream().collect(Collectors.toList())
|
|
|
+ ));
|
|
|
+ LambdaQueryWrapper<WeUser> userWrapper = new LambdaQueryWrapper<>();
|
|
|
+ userWrapper.select(WeUser::getPhone);
|
|
|
+ userWrapper.eq(WeUser::getSource, 0);
|
|
|
+ List<WeUser> userPhoneList = weUserService.list(userWrapper);
|
|
|
+ // 提取手机号并去重
|
|
|
+ Set<String> userPhoneSet = userPhoneList.stream()
|
|
|
+ .map(WeUser::getPhone) // 提取手机号
|
|
|
+ .filter(phone -> phone != null && !phone.isEmpty()) // 可选,去掉空值
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ if(CollUtil.isNotEmpty(distinctList)){
|
|
|
+ List<WeUser> users = new ArrayList<>();
|
|
|
+ for (SysUserJzgVo sysUserJzgVo : distinctUserList) {
|
|
|
+ if(userPhoneSet.contains(sysUserJzgVo.getMobile())){continue;}
|
|
|
+ WeUser weUser = new WeUser();
|
|
|
+ weUser.setUserCode(sysUserJzgVo.getId().toString());
|
|
|
+ weUser.setName(sysUserJzgVo.getName());
|
|
|
+ weUser.setPhone(sysUserJzgVo.getMobile());
|
|
|
+ weUser.setSex(sysUserJzgVo.getSex().equals("M") ? 1 : 0);
|
|
|
+ weUser.setBirthday(sysUserJzgVo.getBirthday());
|
|
|
+ weUser.setSource(0); // 晋掌柜
|
|
|
+ weUser.setType(0);
|
|
|
+ weUser.setRegisterTime(sysUserJzgVo.getCreateTime());
|
|
|
+ weUser.setDeliveredStatus(0);
|
|
|
+ weUser.setCreateTime(now);
|
|
|
+ users.add(weUser);
|
|
|
+ }
|
|
|
+ weUserService.saveBatch(users);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定义一个按一定频率执行的定时任务 每天三点半执行一次 更新微信客户
|
|
|
+ * 本地生活
|
|
|
+ */
|
|
|
+// @Scheduled(cron = "0 30 3 * * ?")
|
|
|
+// @PostConstruct
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void syncCustomerUserDataScheduled() {
|
|
|
+ try {
|
|
|
+ // 随机停几秒
|
|
|
+ Thread.sleep(RandomUtil.randomInt(5));
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ LocalAppUserDto localAppUserDto = new LocalAppUserDto();
|
|
|
+ this.setDate(localAppUserDto);
|
|
|
+ // 查询本地生活 所有的用户 条件 普通用户 创建时间
|
|
|
+ localAppUserDto.setIsWx(1);
|
|
|
+ List<LocalAppUserVo> localAppUserList = localAppUserService.selectUserList(localAppUserDto);
|
|
|
+
|
|
|
+ List<LocalAppUserVo> distinctList = localAppUserList.stream()
|
|
|
+ .collect(Collectors.collectingAndThen(
|
|
|
+ Collectors.toMap(
|
|
|
+ LocalAppUserVo::getPhone, // 按手机号作为Key
|
|
|
+ Function.identity(), // 值是对象本身
|
|
|
+ (existing, replacement) -> existing, // 有重复时保留第一个
|
|
|
+ LinkedHashMap::new // 保持原顺序
|
|
|
+ ),
|
|
|
+ m -> m.values().stream().collect(Collectors.toList())
|
|
|
+ ));
|
|
|
+
|
|
|
+ List<String> userPhoneList = distinctList.stream().map(LocalAppUserVo::getPhone).collect(Collectors.toList());
|
|
|
+ LambdaQueryWrapper<WeUser> phoenWrapper = new LambdaQueryWrapper<>();
|
|
|
+ phoenWrapper.select(WeUser::getId);
|
|
|
+ phoenWrapper.select(WeUser::getUserCode);
|
|
|
+ phoenWrapper.select(WeUser::getPhone);
|
|
|
+ phoenWrapper.eq(WeUser::getPhone, userPhoneList);
|
|
|
+ phoenWrapper.eq(WeUser::getSource, 1);
|
|
|
+ List<WeUser> users = weUserService.list(phoenWrapper);
|
|
|
+ // 转为 Map,手机号为 key
|
|
|
+ Map<String, WeUser> userMap = users.stream()
|
|
|
+ .filter(user -> user.getPhone() != null && !user.getPhone().isEmpty()) // 去掉空手机号
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ WeUser::getPhone, // key = phone
|
|
|
+ user -> user, // value = WeUser 对象
|
|
|
+ (existing, replacement) -> existing // 如果有重复手机号,保留第一个
|
|
|
+ ));
|
|
|
+
|
|
|
+
|
|
|
+ LambdaQueryWrapper<WeCustomerUser> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.select(WeCustomerUser::getWxPhone);
|
|
|
+ List<WeCustomerUser> phoneList = weCustomerUserService.list(wrapper);
|
|
|
+
|
|
|
+ // 提取手机号并去重
|
|
|
+ Set<String> phoneSet = phoneList.stream()
|
|
|
+ .map(WeCustomerUser::getWxPhone) // 提取手机号
|
|
|
+ .filter(phone -> phone != null && !phone.isEmpty()) // 可选,去掉空值
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ if(CollUtil.isNotEmpty(distinctList)) {
|
|
|
+ Date now = new Date();
|
|
|
+ List<WeCustomerUser> weCustomerUserList = new ArrayList<>();
|
|
|
+ for (LocalAppUserVo localAppUserVo : distinctList) {
|
|
|
+ if(phoneSet.contains(localAppUserVo.getPhone())){continue;}
|
|
|
+ WeCustomerUser customerUser = new WeCustomerUser();
|
|
|
+ if(userMap.containsKey(localAppUserVo.getPhone())){
|
|
|
+ customerUser.setUserId(userMap.get(localAppUserVo.getPhone()).getId());
|
|
|
+ customerUser.setUserCode(userMap.get(localAppUserVo.getPhone()).getUserCode());
|
|
|
+ customerUser.setStatus(1);
|
|
|
+ customerUser.setBindTime(now);
|
|
|
+ }else{
|
|
|
+ customerUser.setStatus(0);
|
|
|
+ }
|
|
|
+ customerUser.setWxNickname(localAppUserVo.getWechatNickname());
|
|
|
+ customerUser.setWxPhone(localAppUserVo.getPhone());
|
|
|
+ customerUser.setWxOpenid(localAppUserVo.getOpenId());
|
|
|
+ customerUser.setWxAvatar(localAppUserVo.getWechatHeadPhoto());
|
|
|
+ customerUser.setCreateTime(now);
|
|
|
+ weCustomerUserList.add(customerUser);
|
|
|
+ }
|
|
|
+ weCustomerUserService.saveBatch(weCustomerUserList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setDate(LocalAppUserDto localAppUserDto){
|
|
|
+ // 获取 Calendar 实例
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+
|
|
|
+ // 设置昨天 03:00
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 3);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ Date start = cal.getTime();
|
|
|
+
|
|
|
+ // 设置今天 03:00
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ Date end = cal.getTime();
|
|
|
+
|
|
|
+ localAppUserDto.setStartDate(start);
|
|
|
+ localAppUserDto.setEndDate(end);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setDate(SysUserJzgDto sysUserDto){
|
|
|
+ // 获取 Calendar 实例
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+
|
|
|
+ // 设置昨天 03:00
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 3);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ Date start = cal.getTime();
|
|
|
+
|
|
|
+ // 设置今天 03:00
|
|
|
+ cal.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ Date end = cal.getTime();
|
|
|
+
|
|
|
+ sysUserDto.setStartDate(start);
|
|
|
+ sysUserDto.setEndDate(end);
|
|
|
+ }
|
|
|
+}
|