From 97ccfaa912e75d93d0a895ac343603564eb877a1 Mon Sep 17 00:00:00 2001 From: liukang <644012909@qq.com> Date: Fri, 21 Feb 2025 15:51:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A2=E7=A5=A8=20=E6=8F=90=E5=89=8D9?= =?UTF-8?q?=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.sh | 2 +- .../main/java/com/ruoyi/web/task/MyReq.java | 30 +++++ .../main/java/com/ruoyi/web/task/RyTask.java | 127 +++++++++++++----- .../src/main/resources/application.yml | 2 +- .../java/com/ruoyi/system/domain/Apply.java | 36 +++-- .../com/ruoyi/system/mapper/ApplyMapper.java | 4 + .../ruoyi/system/mapper/PerformMapper.java | 8 ++ .../service/impl/PerformServiceImpl.java | 16 ++- .../src/main/resources/mapper/ApplyMapper.xml | 51 ++++--- .../resources/mapper/system/PerformMapper.xml | 14 +- 10 files changed, 229 insertions(+), 61 deletions(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/task/MyReq.java diff --git a/run.sh b/run.sh index e4dc4c4..2b26f29 100644 --- a/run.sh +++ b/run.sh @@ -14,7 +14,7 @@ echo '----rm image----' # 打包编译docker镜像 docker build -t ${group_name}/${app_name}:${app_version} . echo '----build image----' -docker run -it -p 7072:8080 --name ${app_name} \ +docker run -it -p 7072:8080 --name ${app_name} --restart=always \ --env=SPRING_CONFIG_LOCATION=/config/application.yml \ -v /etc/localtime:/etc/localtime \ -v /home/d1/project/weverse-api/logs:/home/weverse/logs \ diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/task/MyReq.java b/ruoyi-admin/src/main/java/com/ruoyi/web/task/MyReq.java new file mode 100644 index 0000000..8222c66 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/task/MyReq.java @@ -0,0 +1,30 @@ +package com.ruoyi.web.task; + +import java.net.http.HttpClient; +import java.net.http.HttpRequest; + +public class MyReq { + private HttpClient client; + private HttpRequest request; + + public HttpClient getClient() { + return client; + } + + public void setClient(HttpClient client) { + this.client = client; + } + + public HttpRequest getRequest() { + return request; + } + + public void setRequest(HttpRequest request) { + this.request = request; + } + + public MyReq(HttpClient client, HttpRequest request) { + this.client = client; + this.request = request; + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/task/RyTask.java b/ruoyi-admin/src/main/java/com/ruoyi/web/task/RyTask.java index 363cdf1..21ee645 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/task/RyTask.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/task/RyTask.java @@ -1,12 +1,14 @@ package com.ruoyi.web.task; -import com.ruoyi.common.utils.StringUtils; +import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.quartz.domain.SysJob; import com.ruoyi.quartz.service.ISysJobService; +import com.ruoyi.system.domain.Apply; import com.ruoyi.system.domain.Perform; +import com.ruoyi.system.mapper.ApplyMapper; import com.ruoyi.system.mapper.PerformMapper; -import com.ruoyi.system.service.IPerformService; -import org.quartz.SchedulerException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -14,6 +16,7 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.*; import java.util.concurrent.CompletableFuture; /** @@ -26,45 +29,109 @@ public class RyTask { @Autowired private ISysJobService jobService; @Autowired - private IPerformService performService; - @Autowired private PerformMapper performMapper; + @Autowired + private ApplyMapper applyMapper; + + private static final Logger log = LoggerFactory.getLogger(RyTask.class); /** * 抢票 * * @param pid */ - public void rob(Long pid, Long jobId) throws SchedulerException { - Perform perform = performService.selectPerformById(pid); - - System.out.println(pid + "," + jobId); - - for (int i = 0; i < 100; i++) { - System.out.println("开始"); - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("http://example.com")) - .GET() - .build(); - // 发起异步请求,但不等待结果 - CompletableFuture> future2 = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); - future2.thenAccept(response -> { - // 处理响应(例如,记录日志) - System.out.println("Response received: " + response.statusCode()); - }).exceptionally(ex -> { - // 处理异常 - ex.printStackTrace(); - return null; - }); - System.out.println("异步任务完成"); - } - + public void rob(Long pid, Long jobId) throws Exception { + //status + Perform pStatus1 = new Perform(); + pStatus1.setStatus("2"); + pStatus1.setId(pid); + performMapper.updatePerform(pStatus1); //执行完成停止任务 SysJob job = new SysJob(); job.setJobId(jobId); job.setStatus("1"); jobService.changeStatus(job); + + //演出 + Perform perform = performMapper.selectPerformById(pid); + + if (perform == null) { + throw new Exception("未找到演出"); + } + + //URL + String url = String.format("https://faneventapi.weverse.io/api/v1/events/%s/register", perform.getNum()); + + //人员 + Apply apply = new Apply(); + apply.setPerformId(pid); + List applies = applyMapper.selectApplyListAllow(apply); + + if (applies == null || applies.isEmpty()) { + throw new Exception("没有参与人员"); + } + + if (perform.getAgree() == null || "".equals(perform.getAgree())) { + throw new Exception("协议为空"); + } + //协议 + String[] agree = perform.getAgree().split(","); + int[] agrees = Arrays.stream(agree).mapToInt(Integer::parseInt).toArray(); + + + List req = new ArrayList<>(); + for (Apply a : applies) { + // 准备JSON负载 + Map eventData = new HashMap<>(); + eventData.put("eventId", Integer.parseInt(perform.getNum())); + eventData.put("language", "zh-cn"); + Map data = new HashMap<>(); + data.put("birthDate", a.getBirthday()); + data.put("phoneNumber", a.getCountryCode() + " " + a.getPhoneNum()); + + data.put("agreedTermIds", agrees); + eventData.put("data", data); + + ObjectMapper mapper = new ObjectMapper(); + String jsonPayload = mapper.writeValueAsString(eventData); + System.out.println(jsonPayload); + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(url)) + .header("Content-Type", "application/json") + .header("Authorization", "Bearer " + a.getToken()) + .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") + .POST(HttpRequest.BodyPublishers.ofString(jsonPayload)) + .build(); + req.add(new MyReq(client, request)); + } + + Calendar calendar = Calendar.getInstance(); + calendar.setTime(perform.getStartTime()); + calendar.add(Calendar.HOUR_OF_DAY, -1); + long start = calendar.getTimeInMillis(); + long now = System.currentTimeMillis(); + Thread.sleep(start - now); + log.info("抢票开始了"); + for (MyReq r : req) { + // 发起异步请求,但不等待结果 + CompletableFuture> future2 = r.getClient().sendAsync(r.getRequest(), HttpResponse.BodyHandlers.ofString()); + future2.thenAccept(response -> { + // 处理响应(例如,记录日志) + System.out.println("Response received: " + response.statusCode()); + System.out.println(response.body()); + }).exceptionally(ex -> { + // 处理异常 + ex.printStackTrace(); + return null; + }); + } + + //status + Perform pStatus2 = new Perform(); + pStatus2.setStatus("3"); + pStatus2.setId(pid); + performMapper.updatePerform(pStatus2); } } diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index c5aefc9..73b4486 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -72,7 +72,7 @@ spring: # 端口,默认为6379 port: 6379 # 数据库索引 - database: 0 + database: 1 # 密码 password: Yy5203344. # 连接超时时间 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/Apply.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/Apply.java index 4bfa78d..4a8f7a3 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/Apply.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/Apply.java @@ -1,10 +1,13 @@ package com.ruoyi.system.domain; +import com.fasterxml.jackson.annotation.JsonFormat; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import com.ruoyi.common.annotation.Excel; import com.ruoyi.common.core.domain.BaseEntity; +import java.util.Date; + /** * 申请对象 apply * @@ -59,19 +62,13 @@ public class Apply extends BaseEntity @Excel(name = "状态") private String status; - /** - * 演出 - */ - private Perform perform; + /** 名称 */ + private String name; + /** 开始时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date startTime; - public Perform getPerform() { - return perform; - } - - public void setPerform(Perform perform) { - this.perform = perform; - } public Long getUserId() { return userId; @@ -172,6 +169,23 @@ public class Apply extends BaseEntity return status; } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + @Override public String toString() { return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ApplyMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ApplyMapper.java index bdec4b7..13a7fff 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ApplyMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ApplyMapper.java @@ -27,6 +27,10 @@ public interface ApplyMapper */ public List selectApplyList(Apply apply); + public List selectApplyListAllow(Apply apply); + + + public Apply selectApplyPerform(Apply apply); /** * 新增申请 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/PerformMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/PerformMapper.java index e4a32b3..8157b3a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/PerformMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/PerformMapper.java @@ -34,6 +34,14 @@ public interface PerformMapper */ public List selectPerformList(Perform perform); + /** + * 查询演出列表 + * + * @param perform 演出 + * @return 演出集合 + */ + public List selectPerformVerify(Perform perform); + /** * 查询演出列表 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/PerformServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/PerformServiceImpl.java index aa127ee..6ffd471 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/PerformServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/PerformServiceImpl.java @@ -80,6 +80,12 @@ public class PerformServiceImpl implements IPerformService { if (perform.getStartTime().compareTo(new Date()) <= 0) { throw new Exception("开始时间必须大于当前时间"); } + Perform query = new Perform(); + query.setNum(perform.getNum()); + List performs = performMapper.selectPerformVerify(query); + if (!performs.isEmpty()) { + throw new Exception("演出Num已存在"); + } perform.setCreateTime(DateUtils.getNowDate()); //未开始 perform.setStatus("1"); @@ -96,7 +102,7 @@ public class PerformServiceImpl implements IPerformService { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.HOUR_OF_DAY, -1); - calendar.add(Calendar.MINUTE, 9); + calendar.add(Calendar.MINUTE, -9); // 获取年、月、日、时、分、秒 int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,所以加1 @@ -114,6 +120,7 @@ public class PerformServiceImpl implements IPerformService { SysJob job = new SysJob(); job.setJobName(name + " " + formattedDate); + job.setConcurrent("0"); job.setCronExpression(cron); job.setInvokeTarget("ryTask.rob(" + pid + "L)"); job.setJobGroup("DEFAULT"); @@ -137,6 +144,13 @@ public class PerformServiceImpl implements IPerformService { if (perform.getStartTime().compareTo(new Date()) <= 0) { throw new Exception("开始时间必须大于当前时间"); } + Perform query = new Perform(); + query.setNum(perform.getNum()); + query.setId(perform.getId()); + List performs = performMapper.selectPerformVerify(query); + if (!performs.isEmpty()) { + throw new Exception("演出Num已存在"); + } perform.setUpdateTime(DateUtils.getNowDate()); return performMapper.updatePerform(perform); } diff --git a/ruoyi-system/src/main/resources/mapper/ApplyMapper.xml b/ruoyi-system/src/main/resources/mapper/ApplyMapper.xml index bf1f26e..d51d99a 100644 --- a/ruoyi-system/src/main/resources/mapper/ApplyMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/ApplyMapper.xml @@ -20,11 +20,8 @@ - - - - - + + @@ -66,19 +63,43 @@ p.start_time from apply a left join perform p on p.id = a.perform_id - and user_id = #{userId} - and perform_id = #{performId} - and weverse_email = #{weverseEmail} - and weverse_password = #{weversePassword} - and birthday = #{birthday} - and country_code = #{countryCode} - and phone_num = #{phoneNum} - and wechat_num = #{wechatNum} - and token = #{token} - and status = #{status} + and a.user_id = #{userId} + and a.perform_id = #{performId} + and a.weverse_email = #{weverseEmail} + and a.weverse_password = #{weversePassword} + and a.birthday = #{birthday} + and a.country_code = #{countryCode} + and a.phone_num = #{phoneNum} + and a.wechat_num = #{wechatNum} + and a.token = #{token} + and a.status = #{status} + + + +