gongfuzhu hai 1 ano
pai
achega
ce182348cc

+ 11 - 0
SpringBatchServiceServer/pom.xml

@@ -87,6 +87,17 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.zhongshu</groupId>
+            <artifactId>PaymentClient</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.zswl.cloud.shop</groupId>
+            <artifactId>zswl-cloud-shop-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 11 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/feign/AuthConfig.java

@@ -0,0 +1,11 @@
+package com.zswl.cloud.springBtach.server.core.config.feign;
+
+import com.github.microservice.auth.config.AuthClientConfiguration;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+
+@Configuration
+@Import(AuthClientConfiguration.class)
+public class AuthConfig {
+}

+ 10 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/feign/PaymentFeignConfig.java

@@ -0,0 +1,10 @@
+package com.zswl.cloud.springBtach.server.core.config.feign;
+
+import com.zhongshu.payment.client.config.PaymentClientConfiguration;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+@Configuration
+@Import(PaymentClientConfiguration.class)
+public class PaymentFeignConfig {
+}

+ 11 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/feign/ZswlCloudShopConfig.java

@@ -0,0 +1,11 @@
+package com.zswl.cloud.springBtach.server.core.config.feign;
+
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+@EnableFeignClients("com.zswl.cloud.shop.client.service")
+@ComponentScan("com.zswl.cloud.shop.client")
+@Configuration
+public class ZswlCloudShopConfig {
+}

+ 38 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/service/PaymentService.java

@@ -0,0 +1,38 @@
+package com.zswl.cloud.springBtach.server.core.service;
+
+import lombok.SneakyThrows;
+import org.springframework.batch.core.ExitStatus;
+import org.springframework.batch.core.Job;
+import org.springframework.batch.core.JobParameters;
+import org.springframework.batch.core.JobParametersBuilder;
+import org.springframework.batch.core.explore.JobExplorer;
+import org.springframework.batch.core.launch.JobLauncher;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+@Service
+public class PaymentService {
+    @Resource
+    private JobLauncher jobLauncher;
+
+    @Resource
+    private Job creatOrder;
+    @Resource
+    private JobExplorer jobExplorer;
+
+
+
+    @SneakyThrows
+    public void  test(String name){
+        JobParameters parameters = new JobParametersBuilder(jobExplorer)
+                .getNextJobParameters(creatOrder)
+                .addString("name", name)
+                .toJobParameters();
+         jobLauncher.run(creatOrder, parameters).getExitStatus();
+        System.out.println("最后一个被执行");
+
+
+    }
+}

+ 131 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/step/CreateOrderStep.java

@@ -0,0 +1,131 @@
+package com.zswl.cloud.springBtach.server.core.step;
+
+import com.github.microservice.auth.security.helper.AuthHelper;
+import com.github.microservice.core.util.result.content.ResultContent;
+import com.github.microservice.core.util.result.content.ResultState;
+import com.zhongshu.payment.client.model.order.AddOrderParamModel2;
+import com.zhongshu.payment.client.model.payment.zswl.CreatePaymentModel2;
+import com.zhongshu.payment.client.service.OrderService2;
+import com.zswl.cloud.shop.client.service.ShopService;
+import com.zswl.cloud.shop.client.vo.life.ShopDetailVo;
+import com.zswl.cloud.springBtach.server.core.helper.RedisHelper;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.batch.core.Job;
+import org.springframework.batch.core.Step;
+import org.springframework.batch.core.StepContribution;
+import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
+import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
+import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
+import org.springframework.batch.core.launch.support.RunIdIncrementer;
+import org.springframework.batch.core.scope.context.ChunkContext;
+import org.springframework.batch.core.step.tasklet.Tasklet;
+import org.springframework.batch.repeat.RepeatStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.util.Assert;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+@EnableBatchProcessing
+@Configuration
+@Log4j2
+public class CreateOrderStep {
+
+    ThreadLocal<ResultContent> resultContent = new ThreadLocal<>();
+
+    ThreadLocal<AddOrderParamModel2> parameter = new ThreadLocal<>();
+
+
+    @Resource
+    private JobBuilderFactory jobBuilderFactory;
+    @Resource
+    private StepBuilderFactory stepBuilderFactory;
+
+    @Autowired
+    OrderService2 orderService2;
+
+    @Autowired
+    ShopService shopService;
+
+    @Autowired
+    AuthHelper authHelper;
+
+    @Autowired
+    RedisHelper redisHelper;
+
+    @Lazy
+    @Autowired
+    CreateOrderStep createOrderStep;
+
+
+    @Bean
+    public Tasklet checkGoods() {
+
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                resultContent.remove();
+                Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
+                String shopId = String.valueOf(jobParameters.get("shopId"));
+
+                // 判断店铺是否存在
+                com.zswl.cloud.shop.client.ret.ResultContent<ShopDetailVo> shop = shopService.detail(shopId);
+                if (shop.getState().equals(com.zswl.cloud.shop.client.ret.ResultState.ShopNotExit)) {
+                    resultContent.set(ResultContent.build(ResultState.Fail, shop.getMsg()));
+                    return RepeatStatus.FINISHED;
+                }
+                // 幂等性
+                String idempotent = String.valueOf(jobParameters.get("idempotent"));
+                String userId = authHelper.getCurrentUser().getUserId();
+                idempotent = userId + idempotent;
+                if (redisHelper.containsKey(idempotent)) {
+                    resultContent.set(ResultContent.build(ResultState.Fail, "订单已存在"));
+                    return RepeatStatus.FINISHED;
+                }
+
+                return RepeatStatus.FINISHED;
+            }
+
+
+        };
+
+
+    }
+
+    @Bean
+    public Tasklet createCommonTasklet() {
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+
+                resultContent.remove();
+
+                ResultContent order = orderService2.creat(parameter.get());
+                resultContent.set(order);
+
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+
+    @Bean
+    public Step createCommonStep() {
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("createCommon")
+                .tasklet(createCommonTasklet())
+                .build();
+    }
+
+    @Bean
+    public Job creatOrder() {
+        return jobBuilderFactory
+                .get("creatOrder")
+                .start(createOrderStep.createCommonStep())
+                .incrementer(new RunIdIncrementer())
+                .build();
+    }
+
+}

+ 77 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/step/PaymentStep.java

@@ -0,0 +1,77 @@
+package com.zswl.cloud.springBtach.server.core.step;
+
+import com.github.microservice.auth.client.content.ResultContent;
+import com.zhongshu.payment.client.model.payment.CreateResponseModel;
+import com.zhongshu.payment.client.model.payment.zswl.CreatePaymentModel2;
+import com.zhongshu.payment.client.service.OrderService2;
+import com.zhongshu.payment.client.service.PaymentService2;
+import org.springframework.batch.core.Job;
+import org.springframework.batch.core.Step;
+import org.springframework.batch.core.StepContribution;
+import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
+import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
+import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
+import org.springframework.batch.core.launch.support.RunIdIncrementer;
+import org.springframework.batch.core.scope.context.ChunkContext;
+import org.springframework.batch.core.step.tasklet.Tasklet;
+import org.springframework.batch.repeat.RepeatStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Lazy;
+
+import javax.annotation.Resource;
+import java.util.Map;
+
+@EnableBatchProcessing
+@Configuration
+public class PaymentStep {
+
+    ThreadLocal<ResultContent> resultContent = new ThreadLocal<>();
+    ThreadLocal<CreatePaymentModel2> parameter = new ThreadLocal<>();
+    @Resource
+    private JobBuilderFactory jobBuilderFactory;
+    @Resource
+    private StepBuilderFactory stepBuilderFactory;
+
+    @Autowired
+    PaymentService2 paymentService2;
+
+    @Lazy
+    @Autowired
+    PaymentStep paymentStep;
+
+    @Bean
+    public Tasklet paymentCommonTasklet() {
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                resultContent.remove();
+                ResultContent<CreateResponseModel> createResponseModelResultContent = paymentService2.creatPayOrder(parameter.get());
+                resultContent.set(createResponseModelResultContent);
+
+
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+
+    @Bean
+    public Step paymentCommonStep() {
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("paymentCommonTasklet")
+                .tasklet(paymentCommonTasklet())
+                .build();
+    }
+
+    @Bean
+    public Job creatPayment() {
+        return jobBuilderFactory
+                .get("creatOrder")
+                .start(paymentStep.paymentCommonStep())
+                .incrementer(new RunIdIncrementer())
+                .build();
+    }
+
+
+}