Browse Source

定时处理品牌名称多样化问题

JensionDzero 1 year ago
parent
commit
c6c94f7d67

+ 53 - 28
benyun-core/src/main/java/com/ruoyi/benyun/config/QuartzConfig.java

@@ -2,6 +2,7 @@ package com.ruoyi.benyun.config;
 
 
 import com.ruoyi.benyun.job.AnalyseJob;
+import com.ruoyi.benyun.job.BrandStatsiticsJob;
 import com.ruoyi.benyun.job.WriteRedisJob;
 import lombok.extern.slf4j.Slf4j;
 import org.quartz.*;
@@ -32,8 +33,7 @@ public class QuartzConfig {
     @Bean
     public Trigger AnalyseTrigger() {
         //定义Cron表达式
-        //CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 0 1/15 * ? ");
-        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 30 17 11 11 ? ");
+        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 0 1/15 * ? ");
         cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 
         return TriggerBuilder
@@ -44,31 +44,56 @@ public class QuartzConfig {
             .build();
     }
 
-//    @Bean
-//    public JobDetail WriteRedisJobDetail() {
-//        log.info("WriteRedisJobDetail");
-//
-//        return JobBuilder.newJob(WriteRedisJob.class) //newJob方法就是在绑定要运行的Job接口实现。
-//            .withIdentity("writeRedisJobDetail") //给当前JobDetail对象取名字
-//            .storeDurably() //及时没有触发器绑定当前JobDetail对象,也不会被删除
-//            .build();
-//    }
-
-
-
-//    @Bean
-//    public Trigger WriteRedisTrigger(){
-//        //定义Cron表达式
-//        CronScheduleBuilder cron  = CronScheduleBuilder.cronSchedule("0 0 0 * * ? *");
-//
-//        cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
-//
-//        return TriggerBuilder
-//            .newTrigger()
-//            .forJob(WriteRedisJobDetail()) //绑定要运行的JobDetail对象,这是得到的是Spring容器里的JobDetail
-//            .withIdentity("writeRedisTrigger") //给当前Trigger对象起名字,名字任意
-//            .withSchedule(cron) //绑定cron表达式
-//            .build();
-//    }
+    @Bean
+    public JobDetail WriteRedisJobDetail() {
+        log.info("WriteRedisJobDetail");
+
+        return JobBuilder.newJob(WriteRedisJob.class) //newJob方法就是在绑定要运行的Job接口实现。
+            .withIdentity("writeRedisJobDetail") //给当前JobDetail对象取名字
+            .storeDurably() //及时没有触发器绑定当前JobDetail对象,也不会被删除
+            .build();
+    }
+
+
+
+    @Bean
+    public Trigger WriteRedisTrigger(){
+        //定义Cron表达式
+        CronScheduleBuilder cron  = CronScheduleBuilder.cronSchedule("0 0 0 * * ? *");
+
+        cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
+
+        return TriggerBuilder
+            .newTrigger()
+            .forJob(WriteRedisJobDetail()) //绑定要运行的JobDetail对象,这是得到的是Spring容器里的JobDetail
+            .withIdentity("writeRedisTrigger") //给当前Trigger对象起名字,名字任意
+            .withSchedule(cron) //绑定cron表达式
+            .build();
+    }
+
+    @Bean
+    public JobDetail BrandStatsiticsJobDetail() {
+        log.info("BrandStatsiticsJobDetail");
+
+        return JobBuilder.newJob(BrandStatsiticsJob.class) //newJob方法就是在绑定要运行的Job接口实现。
+            .withIdentity("BrandStatsiticsJobDetail") //给当前JobDetail对象取名字
+            .storeDurably() //及时没有触发器绑定当前JobDetail对象,也不会被删除
+            .build();
+    }
+
+    //触发器声明,设置job的运行时机,并放入Spring容器中
+    @Bean
+    public Trigger BrandStatsiticsTrigger() {
+        //定义Cron表达式
+        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 22 1W 1/1 ? ");
+        cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
+
+        return TriggerBuilder
+            .newTrigger()
+            .forJob(BrandStatsiticsJobDetail()) //绑定要运行的JobDetail对象,这是得到的是Spring容器里的JobDetail
+            .withIdentity("BrandStatsiticsTrigger") //给当前Trigger对象起名字,名字任意
+            .withSchedule(cron) //绑定cron表达式
+            .build();
+    }
 
 }

+ 19 - 23
benyun-core/src/main/java/com/ruoyi/benyun/entity/Brand.java

@@ -1,6 +1,8 @@
 package com.ruoyi.benyun.entity;
 
 import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -15,66 +17,60 @@ import java.time.LocalDateTime;
 @Data
 @TableName("ddt_brand")
 public class Brand {
-    @TableField("brand_id")
+    @TableId("brand_id")
     private String brandId;
-
     @TableField("brand_img")
     private String brandImg;
-
     @TableField("brand_name")
     private String brandName;
-
     @TableField("industry_code")
     private String industryCode;
-
     @TableField("enterprise_usci")
     private String enterpriseUsci;
-
     @TableField("per_capita_consumption")
-    private BigDecimal perCapitaConsumption;
-
+    private Float perCapitaConsumption;
     @TableField("comment_count")
-    private Integer commentCount;
-
+    private Long commentCount;
     @TableField("score")
-    private BigDecimal score;
-
+    private Float score;
     @TableField("menu_info")
     private String menuInfo;
-
-    @TableField("indes_score")
-    private BigDecimal indexScore;
-
+    @TableField("index_score")
+    private Float indexScore;
+    @TableField("contact")
+    private String contact;
+    @TableField("telephone")
+    private String telephone;
     @TableField("create_date")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @JsonSerialize(using = LocalDateTimeSerializer.class)
     @JsonDeserialize(using = LocalDateTimeDeserializer.class)
     private LocalDateTime createDate;
-
     @TableField("audit")
-    private Integer audit;
-
+    private Long audit;
+    @TableField("collect_user_id")
+    private String collectUserId;
     @TableField("collect_person")
     private String collectPerson;
-
     @TableField("collect_time")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @JsonSerialize(using = LocalDateTimeSerializer.class)
     @JsonDeserialize(using = LocalDateTimeDeserializer.class)
     private LocalDateTime collectTime;
-
     @TableField("enter_time")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @JsonSerialize(using = LocalDateTimeSerializer.class)
     @JsonDeserialize(using = LocalDateTimeDeserializer.class)
     private LocalDateTime enterTime;
-
     @TableField("hash")
     private String hash;
-
     @TableField("update_time")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @JsonSerialize(using = LocalDateTimeSerializer.class)
     @JsonDeserialize(using = LocalDateTimeDeserializer.class)
     private LocalDateTime updateTime;
+
+    @TableLogic
+    @TableField("show_delete")
+    private int showDelete;
 }

+ 90 - 0
benyun-core/src/main/java/com/ruoyi/benyun/job/ClearJob.java

@@ -0,0 +1,90 @@
+package com.ruoyi.benyun.job;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.ruoyi.benyun.entity.Brand;
+import com.ruoyi.benyun.entity.StoreWd;
+import com.ruoyi.benyun.mapper.BrandMapper;
+import com.ruoyi.benyun.mapper.StoreWdDao;
+import com.ruoyi.benyun.mapper.WdInfoMapper;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Component
+public class ClearJob implements Job {
+    @Autowired
+    BrandMapper brandMapper;
+
+    @Autowired
+    StoreWdDao storeWdDao;
+
+    @Value("${clear.BrandNames}")
+    String[] brandNames;
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        ArrayList<String> moreBrandName = new ArrayList<>();
+        ArrayList<String> existBrandName = new ArrayList<>();
+        for (String brandName : brandNames) {
+            //1.找到主品牌
+            QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
+            queryWrapper.select("brand_id","brand_name");
+            queryWrapper.eq("brand_name",brandName);
+            try {
+                Brand brand = brandMapper.selectOne(queryWrapper);
+                if (brand == null){
+                    existBrandName.add(brandName);
+                    continue;
+                }
+
+
+                //2.找到其他类似的品牌名
+                QueryWrapper<Brand> queryWrapper1 = new QueryWrapper<>();
+                queryWrapper1.select("brand_id","brand_name");
+                queryWrapper1.like("brand_name",brandName);
+
+                queryWrapper1.and(brandQueryWrapper -> {
+                    brandQueryWrapper.ne("brand_id",brand.getBrandId());
+                });
+                List<Brand> brands = brandMapper.selectList(queryWrapper1);
+
+                //3.修改对应的品牌绑定
+                List<Object> collect = brands.stream().map(item -> {
+                    return item.getBrandId();
+                }).collect(Collectors.toList());
+                UpdateWrapper<StoreWd> updateWrapper = new UpdateWrapper<>();
+                updateWrapper.set("brand_id",brand.getBrandId());
+                updateWrapper.set("brand_name",brand.getBrandName());
+                updateWrapper.in("brand_id",collect);
+                storeWdDao.update(null,updateWrapper);
+
+                //4.修改完毕后,删除对应的品牌信息
+                QueryWrapper<Brand> queryWrapper2 = new QueryWrapper<>();
+                queryWrapper2.in("brand_id",collect);
+                brandMapper.delete(queryWrapper2);
+            }catch (Exception exception){
+                exception.printStackTrace();
+                moreBrandName.add(brandName);
+            }
+        }
+
+        System.out.println("不存在品牌:");
+        for (String s : existBrandName) {
+            System.out.println(s);
+        }
+
+        System.out.println("出错品牌:");
+        for (String s : moreBrandName) {
+            System.out.println(s);
+        }
+    }
+}

+ 3 - 0
benyun-core/src/main/resources/application-prod.yaml

@@ -189,3 +189,6 @@ pagehelper:
   reasonable: true
   support-methods-arguments: true
   params: count=countSql
+
+clear:
+  BrandNames: 麦当劳,肯德基,益禾堂,华莱士,茶百道,书亦烧仙草,星巴克,三品王

+ 74 - 0
benyun-core/src/test/java/com/ruoyi/benyun/BenyunCoreApplicationTests.java

@@ -1,12 +1,19 @@
 package com.ruoyi.benyun;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.ruoyi.benyun.constant.RedisContant;
 import com.ruoyi.benyun.constant.RedisInitContant;
+import com.ruoyi.benyun.entity.Brand;
+import com.ruoyi.benyun.entity.StoreWd;
+import com.ruoyi.benyun.mapper.BrandMapper;
+import com.ruoyi.benyun.mapper.StoreWdDao;
 import com.ruoyi.benyun.mapper.WdInfoDao;
 import com.ruoyi.benyun.service.WriteService;
 import com.ruoyi.benyun.utils.WdRedisStoreage;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.data.redis.core.BoundValueOperations;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -18,6 +25,7 @@ import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 @SpringBootTest
 class BenyunCoreApplicationTests {
@@ -36,8 +44,74 @@ class BenyunCoreApplicationTests {
     @Autowired
     WriteService writeService;
 
+    @Value("${clear.BrandNames}")
+    String[] brandNames;
+
+    @Autowired
+    BrandMapper brandMapper;
+
+    @Autowired
+    StoreWdDao storeWdDao;
+
     @Test
     void contextLoads() {
+        ArrayList<String> moreBrandName = new ArrayList<>();
+        ArrayList<String> existBrandName = new ArrayList<>();
+        for (String brandName : brandNames) {
+            //1.找到主品牌
+            QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
+            queryWrapper.select("brand_id","brand_name");
+            queryWrapper.eq("brand_name",brandName);
+            try {
+                Brand brand = brandMapper.selectOne(queryWrapper);
+                if (brand == null){
+                    existBrandName.add(brandName);
+                    continue;
+                }
+
+
+                //2.找到其他类似的品牌名
+                QueryWrapper<Brand> queryWrapper1 = new QueryWrapper<>();
+                queryWrapper1.select("brand_id","brand_name");
+                queryWrapper1.like("brand_name",brandName);
+
+                queryWrapper1.and(brandQueryWrapper -> {
+                    brandQueryWrapper.ne("brand_id",brand.getBrandId());
+                });
+                List<Brand> brands = brandMapper.selectList(queryWrapper1);
+
+                //3.修改对应的品牌绑定
+                List<Object> collect = brands.stream().map(item -> {
+                    return item.getBrandId();
+                }).collect(Collectors.toList());
+                if (collect.isEmpty())
+                    continue;
+
+                UpdateWrapper<StoreWd> updateWrapper = new UpdateWrapper<>();
+                updateWrapper.set("brand_id",brand.getBrandId());
+                updateWrapper.set("brand_name",brand.getBrandName());
+                updateWrapper.in("brand_id",collect);
+                storeWdDao.update(null,updateWrapper);
+
+                //4.修改完毕后,删除对应的品牌信息
+                QueryWrapper<Brand> queryWrapper2 = new QueryWrapper<>();
+                queryWrapper2.in("brand_id",collect);
+                brandMapper.delete(queryWrapper2);
+            }catch (Exception exception){
+                exception.printStackTrace();
+                moreBrandName.add(brandName);
+            }
+        }
+
+        System.out.println("不存在品牌:");
+        for (String s : existBrandName) {
+            System.out.println(s);
+        }
+
+        System.out.println("出错品牌:");
+        for (String s : moreBrandName) {
+            System.out.println(s);
+        }
     }