|
|
@@ -0,0 +1,363 @@
|
|
|
+package com.linkwechat.coal.modules.bdshdb.customer.scheduled;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.domain.entity.LocaOrder;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.domain.entity.LocalAppUser;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.domain.entity.LocalAppUserLevel;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.domain.entity.WeCustViewRecord;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.mapper.LocalAppUserLevelMapper;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.mapper.LocalAppUserMapper;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.mapper.LocalOrderMapper;
|
|
|
+import com.linkwechat.coal.modules.bdshdb.customer.mapper.WeCustViewRecordMapper;
|
|
|
+import com.linkwechat.domain.WeCustomer;
|
|
|
+import com.linkwechat.domain.WeIntelligentMark;
|
|
|
+import com.linkwechat.domain.WeKfMsg;
|
|
|
+import com.linkwechat.domain.WeTag;
|
|
|
+import com.linkwechat.domain.customer.WeMakeCustomerTag;
|
|
|
+import com.linkwechat.mapper.WeCustomerMapper;
|
|
|
+import com.linkwechat.mapper.WeKfMsgMapper;
|
|
|
+import com.linkwechat.mapper.WeTagMapper;
|
|
|
+import com.linkwechat.mapper.weIntelligentMarkMapper;
|
|
|
+import com.linkwechat.service.IWeCustomerService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class ScheduledIntelligentService {
|
|
|
+
|
|
|
+ private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WeCustomerMapper weCustomerMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private weIntelligentMarkMapper weIntelligentMarkMapper;
|
|
|
+ //打标签
|
|
|
+ @Resource
|
|
|
+ private IWeCustomerService weCustomerService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WeTagMapper weTagMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private LocalAppUserMapper localAppUserMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private LocalOrderMapper localOrderMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private LocalAppUserLevelMapper localAppUserLevelMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WeKfMsgMapper weKfMsgMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WeCustViewRecordMapper weCustViewRecordMapper;
|
|
|
+
|
|
|
+
|
|
|
+ @Async("asyncTaskExecutor")
|
|
|
+ @Scheduled(cron = "0 0 23 * * *") //每天23:00, 每天执行
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void scheduledIntelligent() {
|
|
|
+ // 打印执行信息(包含线程名,验证是否异步) 15513573860 乐意
|
|
|
+ String currentTime = LocalDateTime.now().format(DATE_FORMATTER);
|
|
|
+ log.info("【异步任务1】执行开始时间:{},执行线程:{}", currentTime, Thread.currentThread().getName());
|
|
|
+ //获取智能打标列表
|
|
|
+ List<WeIntelligentMark> markList = weIntelligentMarkMapper.selectList(new LambdaQueryWrapper<WeIntelligentMark>()
|
|
|
+ .orderByAsc(WeIntelligentMark::getActionId));
|
|
|
+
|
|
|
+ markList.forEach(s -> {
|
|
|
+ String actionId = s.getActionId();
|
|
|
+ WeTag weTag1 = weTagMapper.selectOne(new LambdaQueryWrapper<WeTag>().eq(WeTag::getTagId, s.getTagId()));
|
|
|
+ if ("ACT001".equals(actionId)) { //好友添加
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ log.info("----------定时任务操作企业客户数量:-----------" + weCustomerList.size());
|
|
|
+ //查询企业客户未打添加好友标签的人员,打标签
|
|
|
+ List<WeCustomer> customerList = weCustomerList.stream().filter(weCustomer -> weCustomer.getTrackState() == 1)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (customerList.size() > 0) {
|
|
|
+ customerList.forEach(c -> {
|
|
|
+ List<String> list = getStrings(c);
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ extracted(weTag1, c, list);
|
|
|
+ log.info("新客户:" + c.getCustomerName() + "----------首次添加标签打标成功!-----------");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } else if ("ACT003".equals(actionId)) {//链接点击
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ weCustomerList.forEach(t -> {
|
|
|
+ //查看客户是否有这个标签
|
|
|
+ List<String> list = getStrings(t);
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ LocalAppUser localAppUser = getLocalAppUser(t);
|
|
|
+ if (localAppUser != null) {
|
|
|
+ //客户点击的所有活动链接数据
|
|
|
+ List<WeCustViewRecord> weCustViewRecords = weCustViewRecordMapper.selectList(new LambdaQueryWrapper<WeCustViewRecord>()
|
|
|
+ .eq(WeCustViewRecord::getCreateUserId, localAppUser.getId()));
|
|
|
+ if (weCustViewRecords.size() > 0) {
|
|
|
+ Map<String, List<WeCustViewRecord>> listMap = weCustViewRecords.stream()
|
|
|
+ .collect(Collectors.groupingBy(WeCustViewRecord::getActId));
|
|
|
+ if (CollectionUtils.isNotEmpty(listMap)) {
|
|
|
+ for (List<WeCustViewRecord> recordList : listMap.values()) {
|
|
|
+
|
|
|
+ List<WeCustViewRecord> collect = recordList.stream()
|
|
|
+ .filter(record -> {
|
|
|
+ // 避免record为null或viewTime为null导致空指针
|
|
|
+ if (record == null || record.getCreateTime() == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ //起始时间
|
|
|
+ Date recordTime = recordList.get(0).getCreateTime();
|
|
|
+ String format1 = sdf.format(recordTime);
|
|
|
+ Date parse1 = null;
|
|
|
+ try {
|
|
|
+ parse1 = sdf.parse(format1);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(recordTime);
|
|
|
+ calendar.add(Calendar.HOUR_OF_DAY, 24);
|
|
|
+ Date result = calendar.getTime();
|
|
|
+ String format = sdf.format(result);
|
|
|
+ Date parse = null;
|
|
|
+ try {
|
|
|
+ parse = sdf.parse(format);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ // 时间匹配:大于等于起始时间 且 小于等于结束时间
|
|
|
+ return !recordTime.before(parse1) && !recordTime.after(parse);
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (collect.size() > Integer.parseInt(s.getMarkRules1())) {
|
|
|
+ extracted(weTag1, t, list);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ } else if ("ACT010".equals(actionId)) {//历史消费行为
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ weCustomerList.forEach(t -> {
|
|
|
+ //打过标签,不再打此标签
|
|
|
+ List<String> list = getStrings(t);
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ LocalAppUser localAppUser = getLocalAppUser(t);
|
|
|
+ if (localAppUser != null) {
|
|
|
+ List<LocaOrder> locaOrders = localOrderMapper.selectList(new LambdaQueryWrapper<LocaOrder>()
|
|
|
+ .eq(LocaOrder::getUserId, localAppUser.getId()).ne(LocaOrder::getState, "1"));
|
|
|
+ if (locaOrders.size() > 0) {
|
|
|
+ BigDecimal reduce = locaOrders.stream().map(order -> Optional.ofNullable(order.getOrderPrice()).orElse(BigDecimal.ZERO)).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ String markRules1 = s.getMarkRules1();
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(markRules1);
|
|
|
+ //订单金额超过规则值
|
|
|
+ if (reduce.compareTo(bigDecimal) > 0) {
|
|
|
+ //查询客户已有的标签信息
|
|
|
+ extracted(weTag1, t, list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("------------生活平台{" + t.getCustomerName() + "}用户不存在!历史消费标签未打成功!------------");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if ("ACT006".equals(actionId)) {//沉默行为
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ weCustomerList.forEach(w -> {
|
|
|
+ List<String> list = getStrings(w);
|
|
|
+ //查询每个用户生活平台30天内是否登陆
|
|
|
+ LocalAppUser localAppUser = getLocalAppUser(w);
|
|
|
+ if (localAppUser != null && localAppUser.getLoginTime() != null) {
|
|
|
+ Date loginTime = localAppUser.getLoginTime();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(loginTime);
|
|
|
+ calendar.add(Calendar.DATE, 30);
|
|
|
+ //30天后的时间
|
|
|
+ Date result = calendar.getTime();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String format = sdf.format(result);
|
|
|
+ Date parse = null;
|
|
|
+ try {
|
|
|
+ parse = sdf.parse(format);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ //当前时间
|
|
|
+ Date date = new Date();
|
|
|
+ String date1 = sdf.format(date);
|
|
|
+ Date parse1 = null;
|
|
|
+ try {
|
|
|
+ parse1 = sdf.parse(date1);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ boolean before = parse.before(parse1);
|
|
|
+ if (before) {
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ //打客户已有的标签和沉睡标签
|
|
|
+ extracted(weTag1, w, list);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (list.contains(weTag1.getTagId())) {
|
|
|
+ //用户已登录,取掉沉睡用户标签
|
|
|
+ list.remove(weTag1.getTagId());
|
|
|
+ extracted2(weTag1, w, list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("------------生活平台{" + w.getCustomerName() + "}用户不存在,或者无登录时间!沉默客户30天标签未打成功!------------");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if ("ACT005".equals(actionId)) {//首次消费下单
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ weCustomerList.forEach(t -> {
|
|
|
+ //客户已经搭过首次消费下单标签,不在打此标签
|
|
|
+ List<String> list = getStrings(t);
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ LocalAppUser localAppUser = getLocalAppUser(t);
|
|
|
+ if (localAppUser != null) {
|
|
|
+ List<LocaOrder> locaOrders = localOrderMapper.selectList(new LambdaQueryWrapper<LocaOrder>()
|
|
|
+ .eq(LocaOrder::getUserId, localAppUser.getId()).ne(LocaOrder::getState, 1));
|
|
|
+ if (locaOrders.size() > 0) {
|
|
|
+ extracted(weTag1, t, list);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("------------生活平台{" + t.getCustomerName() + "}用户不存在!首次消费下单标签未打成功!------------");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ } else if ("ACT008".equals(actionId)) {//会员钻石等级变动
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ weCustomerList.forEach(t -> {
|
|
|
+ List<String> list = getStrings(t);
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ LocalAppUser localAppUser = getLocalAppUser(t);
|
|
|
+ if (localAppUser != null && StringUtils.isNotBlank(localAppUser.getLevelId())) {
|
|
|
+ LocalAppUserLevel localAppUserLevel = localAppUserLevelMapper.selectById(localAppUser.getLevelId());
|
|
|
+ if (localAppUserLevel.getLevelName().equals("钻石")) {
|
|
|
+ extracted(weTag1, t, list);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("------------生活平台{" + t.getCustomerName() + "}用户不存在!会员钻石等级变动标签未打成功!------------");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if ("ACT009".equals(actionId)) {//关键词互动
|
|
|
+ List<WeCustomer> weCustomerList = getWeCustomers();
|
|
|
+ weCustomerList.forEach(t -> {
|
|
|
+ List<String> list = getStrings(t);
|
|
|
+ String[] split = s.getMarkRules1().split(",");
|
|
|
+ Date date = new Date();
|
|
|
+ date.setHours(0);
|
|
|
+ date.setMinutes(0);
|
|
|
+ date.setSeconds(1);
|
|
|
+ List<WeKfMsg> weKfMsgList = weKfMsgMapper.selectList(new LambdaQueryWrapper<WeKfMsg>()
|
|
|
+ .eq(WeKfMsg::getExternalUserid, t.getExternalUserid())
|
|
|
+ .eq(WeKfMsg::getOrigin, 3)
|
|
|
+ .eq(WeKfMsg::getDelFlag, 0)
|
|
|
+ .eq(WeKfMsg::getMsgType, "text")
|
|
|
+ .like(WeKfMsg::getContent, split[0])
|
|
|
+ .or().like(WeKfMsg::getContent, split[1])
|
|
|
+ .or().like(WeKfMsg::getContent, split[2])
|
|
|
+ .gt(WeKfMsg::getCreateTime, date));
|
|
|
+ if (weKfMsgList.size() > 0) {
|
|
|
+ if (!list.contains(weTag1.getTagId())) {
|
|
|
+ extracted(weTag1, t, list);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (list.contains(weTag1.getTagId())) {
|
|
|
+ //取掉敏感词标签
|
|
|
+ list.remove(weTag1.getTagId());
|
|
|
+ extracted2(weTag1, t, list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ String endTime = LocalDateTime.now().format(DATE_FORMATTER);
|
|
|
+ log.info("【异步任务1】执行结束时间:{},执行线程:{}", endTime, Thread.currentThread().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ private LocalAppUser getLocalAppUser(WeCustomer t) {
|
|
|
+ return localAppUserMapper.selectOne(new LambdaQueryWrapper<LocalAppUser>()
|
|
|
+ .eq(LocalAppUser::getUserType, "1").eq(LocalAppUser::getPhone, t.getPhone()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<WeCustomer> getWeCustomers() {
|
|
|
+ return weCustomerMapper.selectList(new LambdaQueryWrapper<WeCustomer>()
|
|
|
+ .eq(WeCustomer::getDelFlagBa, 0)
|
|
|
+ .isNotNull(WeCustomer::getPhone)
|
|
|
+ .ne(WeCustomer::getPhone, ""));
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> getStrings(WeCustomer t) {
|
|
|
+ String tagIds = t.getTagIds();
|
|
|
+ String[] split = tagIds.split(",");
|
|
|
+ return new ArrayList<>(Arrays.asList(split));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void extracted(WeTag weTag, WeCustomer t, List<String> list) {
|
|
|
+ List<WeTag> weTagList = new ArrayList<>();
|
|
|
+ WeMakeCustomerTag weMakeCustomerTag = new WeMakeCustomerTag();
|
|
|
+ if (list.size() > 0) {
|
|
|
+ //查询客户已有的标签信息
|
|
|
+ List<WeTag> weTagList2 = weTagMapper.selectList(new LambdaQueryWrapper<WeTag>().in(WeTag::getTagId, list));
|
|
|
+ //已有标签
|
|
|
+ weTagList.addAll(weTagList2);
|
|
|
+ }
|
|
|
+ //新赠标签
|
|
|
+ weTagList.add(weTag);
|
|
|
+ weMakeCustomerTag.setAddTag(weTagList);
|
|
|
+
|
|
|
+ weMakeCustomerTag.setExternalUserid(t.getExternalUserid());
|
|
|
+ weMakeCustomerTag.setUserId(t.getAddUserId());
|
|
|
+ //打标签
|
|
|
+ weCustomerService.makeLabel(weMakeCustomerTag);
|
|
|
+ log.info("客户:" + t.getCustomerName() + "---智能打标成功---标签为" + weTag.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void extracted2(WeTag weTag, WeCustomer t, List<String> list) {
|
|
|
+ List<WeTag> weTagList = new ArrayList<>();
|
|
|
+ WeMakeCustomerTag weMakeCustomerTag = new WeMakeCustomerTag();
|
|
|
+ if (list.size() > 0) {
|
|
|
+ //查询客户已有的标签信息
|
|
|
+ List<WeTag> weTagList2 = weTagMapper.selectList(new LambdaQueryWrapper<WeTag>().in(WeTag::getTagId, list));
|
|
|
+ //已有标签
|
|
|
+ weTagList.addAll(weTagList2);
|
|
|
+ }
|
|
|
+ weMakeCustomerTag.setAddTag(weTagList);
|
|
|
+
|
|
|
+ weMakeCustomerTag.setExternalUserid(t.getExternalUserid());
|
|
|
+ weMakeCustomerTag.setUserId(t.getAddUserId());
|
|
|
+ //打标签
|
|
|
+ weCustomerService.makeLabel(weMakeCustomerTag);
|
|
|
+ log.info("客户:" + t.getCustomerName() + "---智能打标成功---删除标签为" + weTag.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|