|
@@ -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();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|