From d60df6d71270e8031251e2a4abffc76c44469740 Mon Sep 17 00:00:00 2001 From: Bravepping <1131302745@qq.com> Date: Sat, 19 Apr 2025 14:32:41 +0800 Subject: [PATCH] init --- .gitignore | 38 ++++++ .idea/.gitignore | 8 ++ .idea/dataSources.xml | 17 +++ .idea/encodings.xml | 8 ++ .idea/misc.xml | 14 ++ .idea/mybatisx/templates.xml | 66 ++++++++++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + pom.xml | 80 +++++++++++ src/main/java/com/ping/study/App.java | 22 ++++ .../ping/study/config/GlobalException.java | 13 ++ .../java/com/ping/study/config/WebMvc.java | 15 +++ .../ping/study/controller/NbaController.java | 64 +++++++++ .../com/ping/study/mapper/GamesMapper.java | 30 +++++ .../com/ping/study/mapper/UrlsMapper.java | 34 +++++ .../study/model/dto/NbaStatsRequestDto.java | 26 ++++ .../com/ping/study/model/dto/addUrls.java | 14 ++ .../com/ping/study/model/vo/ApiResponse.java | 10 ++ .../java/com/ping/study/model/vo/Game.java | 14 ++ .../java/com/ping/study/model/vo/Group.java | 10 ++ .../com/ping/study/model/vo/ResponseData.java | 10 ++ .../com/ping/study/model/vo/live/LiveUrl.java | 20 +++ src/main/java/com/ping/study/pojo/Games.java | 51 +++++++ src/main/java/com/ping/study/pojo/Urls.java | 30 +++++ .../com/ping/study/service/GamesService.java | 13 ++ .../com/ping/study/service/UrlsService.java | 18 +++ .../study/service/impl/GamesServiceImpl.java | 34 +++++ .../study/service/impl/UrlsServiceImpl.java | 37 ++++++ .../java/com/ping/study/utils/NbaApi.java | 90 +++++++++++++ src/main/resources/application.yml | 16 +++ src/main/resources/mapper/GamesMapper.xml | 120 +++++++++++++++++ src/main/resources/mapper/UrlsMapper.xml | 91 +++++++++++++ src/test/java/com/ping/study/AppTest.java | 38 ++++++ 33 files changed, 1181 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/dataSources.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/mybatisx/templates.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/ping/study/App.java create mode 100644 src/main/java/com/ping/study/config/GlobalException.java create mode 100644 src/main/java/com/ping/study/config/WebMvc.java create mode 100644 src/main/java/com/ping/study/controller/NbaController.java create mode 100644 src/main/java/com/ping/study/mapper/GamesMapper.java create mode 100644 src/main/java/com/ping/study/mapper/UrlsMapper.java create mode 100644 src/main/java/com/ping/study/model/dto/NbaStatsRequestDto.java create mode 100644 src/main/java/com/ping/study/model/dto/addUrls.java create mode 100644 src/main/java/com/ping/study/model/vo/ApiResponse.java create mode 100644 src/main/java/com/ping/study/model/vo/Game.java create mode 100644 src/main/java/com/ping/study/model/vo/Group.java create mode 100644 src/main/java/com/ping/study/model/vo/ResponseData.java create mode 100644 src/main/java/com/ping/study/model/vo/live/LiveUrl.java create mode 100644 src/main/java/com/ping/study/pojo/Games.java create mode 100644 src/main/java/com/ping/study/pojo/Urls.java create mode 100644 src/main/java/com/ping/study/service/GamesService.java create mode 100644 src/main/java/com/ping/study/service/UrlsService.java create mode 100644 src/main/java/com/ping/study/service/impl/GamesServiceImpl.java create mode 100644 src/main/java/com/ping/study/service/impl/UrlsServiceImpl.java create mode 100644 src/main/java/com/ping/study/utils/NbaApi.java create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/mapper/GamesMapper.xml create mode 100644 src/main/resources/mapper/UrlsMapper.xml create mode 100644 src/test/java/com/ping/study/AppTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..72a378b --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://110.42.255.182:3306/NBA + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63574ec --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/mybatisx/templates.xml b/.idea/mybatisx/templates.xml new file mode 100644 index 0000000..840a8f8 --- /dev/null +++ b/.idea/mybatisx/templates.xml @@ -0,0 +1,66 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a242365 --- /dev/null +++ b/pom.xml @@ -0,0 +1,80 @@ + + 4.0.0 + + com.ping.study + NBA + 1.0-SNAPSHOT + jar + + + org.springframework.boot + spring-boot-starter-parent + 3.4.1 + + + NBA + http://maven.apache.org + + + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + mysql + mysql-connector-java + 8.0.33 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 3.0.3 + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-configuration-processor + + + org.springframework.boot + spring-boot-starter-validation + + + + junit + junit + 3.8.1 + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.4.1 + + + + repackage + + + + + + + diff --git a/src/main/java/com/ping/study/App.java b/src/main/java/com/ping/study/App.java new file mode 100644 index 0000000..9087a17 --- /dev/null +++ b/src/main/java/com/ping/study/App.java @@ -0,0 +1,22 @@ +package com.ping.study; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * Hello world! + * + */ +@EnableScheduling +@SpringBootApplication +@MapperScan("com.ping.study.mapper") +public class App +{ + public static void main( String[] args ) + { + SpringApplication.run(App.class, args); + System.out.println( "Hello World!" ); + } +} diff --git a/src/main/java/com/ping/study/config/GlobalException.java b/src/main/java/com/ping/study/config/GlobalException.java new file mode 100644 index 0000000..fdbac2b --- /dev/null +++ b/src/main/java/com/ping/study/config/GlobalException.java @@ -0,0 +1,13 @@ +package com.ping.study.config; + +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalException { + @ExceptionHandler(Exception.class) + public Object handleException(Exception e) { + e.printStackTrace(); + return e.getMessage(); + } +} diff --git a/src/main/java/com/ping/study/config/WebMvc.java b/src/main/java/com/ping/study/config/WebMvc.java new file mode 100644 index 0000000..87445bf --- /dev/null +++ b/src/main/java/com/ping/study/config/WebMvc.java @@ -0,0 +1,15 @@ +package com.ping.study.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvc implements WebMvcConfigurer { + + @Bean + public WebClient webClient() { + return WebClient.create("https://api.nba.cn/sib/v2"); + } +} diff --git a/src/main/java/com/ping/study/controller/NbaController.java b/src/main/java/com/ping/study/controller/NbaController.java new file mode 100644 index 0000000..7c7154a --- /dev/null +++ b/src/main/java/com/ping/study/controller/NbaController.java @@ -0,0 +1,64 @@ +package com.ping.study.controller; + + +import com.ping.study.model.dto.addUrls; +import com.ping.study.pojo.Games; +import com.ping.study.service.GamesService; +import com.ping.study.service.UrlsService; +import com.ping.study.utils.NbaApi; + +import lombok.Data; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; + +@RequestMapping("/api") +@RestController +@Data +@Slf4j +//@CrossOrigin(origins = "http://nba.new9.me") +@CrossOrigin +public class NbaController { + + @Autowired + private NbaApi nbaApi; + @Autowired + private GamesService gamesService; + @Autowired + private UrlsService urlsService; + + //添加定时任务,每日凌晨0点执行一次 + // 每天 00:00:00 执行 + @Scheduled(cron = "0 0 0 * * ?") + @RequestMapping("/add") + public List addGames() { + log.info("执行定时方法添加当天赛程"); + return nbaApi.addGames(); + } + @RequestMapping("/games") + public List getGames() { + log.info("获取所有赛程"); + return gamesService.getGames(); + } + + @RequestMapping("/urls") + public List>>> getUrls() { + log.info("获取所有赛程直播链接"); + return urlsService.getUrls(); + } + + @RequestMapping("/go") + public Boolean go(@RequestParam("pwd")String pwd) { + return pwd.equals("inspur123"); + } + @RequestMapping(value = "/addUrls",method = RequestMethod.POST) + public void addUrls(@RequestBody addUrls addUrls) { + log.info("添加所有赛程直播链接"); + urlsService.addUrls(addUrls); + } +} diff --git a/src/main/java/com/ping/study/mapper/GamesMapper.java b/src/main/java/com/ping/study/mapper/GamesMapper.java new file mode 100644 index 0000000..d79153d --- /dev/null +++ b/src/main/java/com/ping/study/mapper/GamesMapper.java @@ -0,0 +1,30 @@ +package com.ping.study.mapper; + +import com.ping.study.pojo.Games; + +import java.util.List; + +/** +* @author Administrator +* @description 针对表【games】的数据库操作Mapper +* @createDate 2025-04-17 21:23:08 +* @Entity com.ping.study.pojo.Games +*/ +public interface GamesMapper { + + int deleteByPrimaryKey(Long id); + + int insert(Games record); + + int insertSelective(Games record); + + Games selectByPrimaryKey(Long id); + + int updateByPrimaryKeySelective(Games record); + + int updateByPrimaryKey(Games record); + + Games selectByGameId(String gameId); + + List selectAll(); +} diff --git a/src/main/java/com/ping/study/mapper/UrlsMapper.java b/src/main/java/com/ping/study/mapper/UrlsMapper.java new file mode 100644 index 0000000..344dc76 --- /dev/null +++ b/src/main/java/com/ping/study/mapper/UrlsMapper.java @@ -0,0 +1,34 @@ +package com.ping.study.mapper; + +import com.ping.study.model.vo.live.LiveUrl; +import com.ping.study.pojo.Urls; +import org.apache.ibatis.annotations.Param; + +import java.util.HashMap; +import java.util.List; + +/** +* @author Administrator +* @description 针对表【urls】的数据库操作Mapper +* @createDate 2025-04-18 00:09:37 +* @Entity com.ping.study.pojo.Urls +*/ +public interface UrlsMapper { + + int deleteByPrimaryKey(Long id); + + int insert(Urls record); + + int insertSelective(Urls record); + + Urls selectByPrimaryKey(Long id); + + int updateByPrimaryKeySelective(Urls record); + + int updateByPrimaryKey(Urls record); + List selectGameIds(); + + List> selectUrlsListByGameId(String gameId); + + void insertUrlsWithGameId(@Param("gameId") String gameId, @Param("list") List urls); +} diff --git a/src/main/java/com/ping/study/model/dto/NbaStatsRequestDto.java b/src/main/java/com/ping/study/model/dto/NbaStatsRequestDto.java new file mode 100644 index 0000000..cc5d880 --- /dev/null +++ b/src/main/java/com/ping/study/model/dto/NbaStatsRequestDto.java @@ -0,0 +1,26 @@ +package com.ping.study.model.dto; + +import lombok.Data; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +@Data +public class NbaStatsRequestDto { + // 默认值字段 + private final String appKey = "tiKB2tNdncnZFPOi"; + private final String appVersion = "1.1.0"; + private final String channel = "NBA"; + private final String deviceId = "cd4920b68041f06fcc6ea358c85710bd"; + private final String installId = "1502934360"; + private final String network = "N/A"; + private final String osType = "3"; + private final String osVersion = "1.0.0"; + private final String sign = "sign_v2"; + private final String sign2 = "67BB8937A32E512826D59467E803B28CD82E179FCE8B2A470C20AA0BD4AE08DB"; + + // 动态字段 + private final long t = System.currentTimeMillis() / 1000; // 当前时间戳(秒) + private final String start = LocalDate.now().format(DateTimeFormatter.ISO_DATE); // 当天日期 + private final String end = LocalDate.now().format(DateTimeFormatter.ISO_DATE); // 当天日期 +} diff --git a/src/main/java/com/ping/study/model/dto/addUrls.java b/src/main/java/com/ping/study/model/dto/addUrls.java new file mode 100644 index 0000000..569f37b --- /dev/null +++ b/src/main/java/com/ping/study/model/dto/addUrls.java @@ -0,0 +1,14 @@ +package com.ping.study.model.dto; + + +import com.ping.study.model.vo.live.LiveUrl; +import lombok.Data; + +import java.util.List; + +@Data +public class addUrls { + + private String gameId; + private List urls; +} diff --git a/src/main/java/com/ping/study/model/vo/ApiResponse.java b/src/main/java/com/ping/study/model/vo/ApiResponse.java new file mode 100644 index 0000000..91da2ef --- /dev/null +++ b/src/main/java/com/ping/study/model/vo/ApiResponse.java @@ -0,0 +1,10 @@ +package com.ping.study.model.vo; +import com.ping.study.model.vo.ResponseData; +import lombok.Data; + +@Data +public class ApiResponse { + private int code; + private ResponseData data; + private String msg; +} diff --git a/src/main/java/com/ping/study/model/vo/Game.java b/src/main/java/com/ping/study/model/vo/Game.java new file mode 100644 index 0000000..40efd4b --- /dev/null +++ b/src/main/java/com/ping/study/model/vo/Game.java @@ -0,0 +1,14 @@ +package com.ping.study.model.vo; + +import lombok.Data; + +@Data +public class Game { + private String gameId; + private String homeTeamName; + private String awayTeamName; + private String homeTeamLogoDark; + private String awayTeamLogoDark; + private String startDate; // 格式:"2025-04-17" + private String startTime; // 格式:"07:30:00" +} \ No newline at end of file diff --git a/src/main/java/com/ping/study/model/vo/Group.java b/src/main/java/com/ping/study/model/vo/Group.java new file mode 100644 index 0000000..a3682cb --- /dev/null +++ b/src/main/java/com/ping/study/model/vo/Group.java @@ -0,0 +1,10 @@ +package com.ping.study.model.vo; + +import lombok.Data; + +import java.util.List; + +@Data +public class Group { + private List games; +} diff --git a/src/main/java/com/ping/study/model/vo/ResponseData.java b/src/main/java/com/ping/study/model/vo/ResponseData.java new file mode 100644 index 0000000..1c96935 --- /dev/null +++ b/src/main/java/com/ping/study/model/vo/ResponseData.java @@ -0,0 +1,10 @@ +package com.ping.study.model.vo; + +import lombok.Data; + +import java.util.List; + +@Data +public class ResponseData { + private List groups; +} \ No newline at end of file diff --git a/src/main/java/com/ping/study/model/vo/live/LiveUrl.java b/src/main/java/com/ping/study/model/vo/live/LiveUrl.java new file mode 100644 index 0000000..4d52eaa --- /dev/null +++ b/src/main/java/com/ping/study/model/vo/live/LiveUrl.java @@ -0,0 +1,20 @@ +package com.ping.study.model.vo.live; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + + + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class LiveUrl { + +// private Integer gameId; + private String type; + private String url; + + +} diff --git a/src/main/java/com/ping/study/pojo/Games.java b/src/main/java/com/ping/study/pojo/Games.java new file mode 100644 index 0000000..12b1cc1 --- /dev/null +++ b/src/main/java/com/ping/study/pojo/Games.java @@ -0,0 +1,51 @@ +package com.ping.study.pojo; + +import java.util.Date; +import lombok.Data; + +/** + * + * @TableName games + */ +@Data +public class Games { + /** + * + */ + private Integer id; + + /** + * + */ + private String date; + + /** + * + */ + private String gameId; + + /** + * + */ + private Date startTime; + + /** + * + */ + private String homeTeamName; + + /** + * + */ + private String awayTeamName; + + /** + * + */ + private String homeTeamLogoDark; + + /** + * + */ + private String awayTeamLogoDark; +} \ No newline at end of file diff --git a/src/main/java/com/ping/study/pojo/Urls.java b/src/main/java/com/ping/study/pojo/Urls.java new file mode 100644 index 0000000..39acbd5 --- /dev/null +++ b/src/main/java/com/ping/study/pojo/Urls.java @@ -0,0 +1,30 @@ +package com.ping.study.pojo; + +import lombok.Data; + +/** + * + * @TableName urls + */ +@Data +public class Urls { + /** + * + */ + private Integer id; + + /** + * + */ + private String gameId; + + /** + * + */ + private String url; + + /** + * + */ + private String type; +} \ No newline at end of file diff --git a/src/main/java/com/ping/study/service/GamesService.java b/src/main/java/com/ping/study/service/GamesService.java new file mode 100644 index 0000000..1ca003f --- /dev/null +++ b/src/main/java/com/ping/study/service/GamesService.java @@ -0,0 +1,13 @@ +package com.ping.study.service; + +import com.ping.study.pojo.Games; + +import java.util.List; + +public interface GamesService { + //插入每日比赛数据到数据库 + void insertGames(Games games); + + //获取当时比赛赛程 + List getGames(); +} diff --git a/src/main/java/com/ping/study/service/UrlsService.java b/src/main/java/com/ping/study/service/UrlsService.java new file mode 100644 index 0000000..883e81c --- /dev/null +++ b/src/main/java/com/ping/study/service/UrlsService.java @@ -0,0 +1,18 @@ +package com.ping.study.service; + +import com.ping.study.model.dto.addUrls; +import com.ping.study.model.vo.live.LiveUrl; + +import java.util.HashMap; +import java.util.List; + + +public interface UrlsService { + + + //查询当天的直播地址 + public List>>> getUrls(); + + //添加直播地址到对应赛事 + public void addUrls(addUrls addUrls); +} diff --git a/src/main/java/com/ping/study/service/impl/GamesServiceImpl.java b/src/main/java/com/ping/study/service/impl/GamesServiceImpl.java new file mode 100644 index 0000000..a2042e5 --- /dev/null +++ b/src/main/java/com/ping/study/service/impl/GamesServiceImpl.java @@ -0,0 +1,34 @@ +package com.ping.study.service.impl; + +import com.ping.study.mapper.GamesMapper; +import com.ping.study.pojo.Games; +import com.ping.study.service.GamesService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class GamesServiceImpl implements GamesService { + + @Autowired + private GamesMapper gamesMapper; + //先查询数据库有没有比赛信息 + public Games getGames(String gameId) { + return gamesMapper.selectByGameId(gameId); + } + @Override + public void insertGames(Games games) { + if (getGames(games.getGameId())!=null){ + //抛出异常 + throw new RuntimeException("该比赛已存在"); + } + gamesMapper.insert(games); + } + + @Override + public List getGames() { + + return gamesMapper.selectAll(); + } +} diff --git a/src/main/java/com/ping/study/service/impl/UrlsServiceImpl.java b/src/main/java/com/ping/study/service/impl/UrlsServiceImpl.java new file mode 100644 index 0000000..7ee43a1 --- /dev/null +++ b/src/main/java/com/ping/study/service/impl/UrlsServiceImpl.java @@ -0,0 +1,37 @@ +package com.ping.study.service.impl; + +import com.ping.study.mapper.UrlsMapper; +import com.ping.study.model.dto.addUrls; +import com.ping.study.model.vo.live.LiveUrl; +import com.ping.study.service.UrlsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +@Service +public class UrlsServiceImpl implements UrlsService { + + @Autowired + private UrlsMapper urlsMapper; + @Override + public List>>> getUrls() { + List>>> urlsList = new ArrayList<>(); + List gameIds = urlsMapper.selectGameIds(); + gameIds.forEach(gameId -> { + List> maps = urlsMapper.selectUrlsListByGameId(gameId); + HashMap>> map = new HashMap<>(); + map.put(gameId,maps); + urlsList.add(map); + }); // 添加右括号和分号 + return urlsList; + } + + @Override + public void addUrls(addUrls addUrls) { + urlsMapper.insertUrlsWithGameId(addUrls.getGameId(), addUrls.getUrls()); + } + +} diff --git a/src/main/java/com/ping/study/utils/NbaApi.java b/src/main/java/com/ping/study/utils/NbaApi.java new file mode 100644 index 0000000..0669e3d --- /dev/null +++ b/src/main/java/com/ping/study/utils/NbaApi.java @@ -0,0 +1,90 @@ +package com.ping.study.utils; + +import com.ping.study.model.dto.NbaStatsRequestDto; +import com.ping.study.model.vo.ApiResponse; +import com.ping.study.model.vo.Game; +import com.ping.study.model.vo.Group; +import com.ping.study.pojo.Games; +import com.ping.study.service.GamesService; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Component +@Data +@Slf4j +public class NbaApi { + + @Autowired + private GamesService gamesService; + + private WebClient webClient; + + public NbaApi(WebClient webClient) { + this.webClient = webClient; + } + + public List addGames() { + // 创建请求DTO(会自动设置当前时间戳和日期) + NbaStatsRequestDto requestParams = new NbaStatsRequestDto(); + log.info("{}", requestParams); + log.info("进入{},执行{}", this.getClass().getName(), "getGames"); + List gameEntities = webClient.get() + .uri("/game/schedule", uriBuilder -> uriBuilder + .queryParam("app_key", requestParams.getAppKey()) + .queryParam("app_version", requestParams.getAppVersion()) + .queryParam("channel", requestParams.getChannel()) + .queryParam("device_id", requestParams.getDeviceId()) + .queryParam("install_id", requestParams.getInstallId()) + .queryParam("network", requestParams.getNetwork()) + .queryParam("os_type", requestParams.getOsType()) + .queryParam("os_version", requestParams.getOsVersion()) + .queryParam("sign", requestParams.getSign()) + .queryParam("sign2", requestParams.getSign2()) + .queryParam("t", requestParams.getT()) + .queryParam("start", requestParams.getStart()) + .queryParam("end", requestParams.getEnd()) + .build()) + .retrieve() + .bodyToMono(ApiResponse.class) + .map(response -> { + log.info("{}", response); + List entities = new ArrayList<>(); + // 遍历groups中的games + for (Group group : response.getData().getGroups()) { + for (Game game : group.getGames()) { + Games entity = new Games(); + entity.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); + entity.setGameId(game.getGameId()); + entity.setHomeTeamName(game.getHomeTeamName()); + entity.setAwayTeamName(game.getAwayTeamName()); + entity.setHomeTeamLogoDark(game.getHomeTeamLogoDark()); + entity.setAwayTeamLogoDark(game.getAwayTeamLogoDark()); + + // 合并日期和时间 + String dateTimeStr = game.getStartDate() + " " + game.getStartTime(); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + entity.setStartTime(format.parse(dateTimeStr)); + } catch (ParseException e) { + // 处理异常或设置默认值 + entity.setStartTime(null); + } + gamesService.insertGames(entity); + entities.add(entity); + } + } + return entities; + }) + .block(); + return gameEntities; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..e20553c --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,16 @@ +server: + port: 9005 +spring: + application: + name: NBA + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://110.42.255.182:3306/NBA?useUnicode=true&characterEncoding=utf-8&useSSL=false + username: root + password: inspur123 +mybatis: + configuration: + map-underscore-to-camel-case: true + log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl + type-aliases-package: com.ping.study.pojo + mapper-locations: classpath:mapper/*.xml \ No newline at end of file diff --git a/src/main/resources/mapper/GamesMapper.xml b/src/main/resources/mapper/GamesMapper.xml new file mode 100644 index 0000000..c5f8fb6 --- /dev/null +++ b/src/main/resources/mapper/GamesMapper.xml @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + id,date,game_id, + start_time,home_team_name,away_team_name, + home_team_logo_dark,away_team_logo_dark + + + + + + delete from games + where id = #{id,jdbcType=INTEGER} + + + insert into games + ( id,date,game_id + ,start_time,home_team_name,away_team_name + ,home_team_logo_dark,away_team_logo_dark) + values (#{id,jdbcType=INTEGER},#{date,jdbcType=VARCHAR},#{gameId,jdbcType=VARCHAR} + ,#{startTime,jdbcType=TIMESTAMP},#{homeTeamName,jdbcType=VARCHAR},#{awayTeamName,jdbcType=VARCHAR} + ,#{homeTeamLogoDark,jdbcType=VARCHAR},#{awayTeamLogoDark,jdbcType=VARCHAR}) + + + insert into games + + id, + date, + game_id, + start_time, + home_team_name, + away_team_name, + home_team_logo_dark, + away_team_logo_dark, + + + #{id,jdbcType=INTEGER}, + #{date,jdbcType=VARCHAR}, + #{gameId,jdbcType=VARCHAR}, + #{startTime,jdbcType=TIMESTAMP}, + #{homeTeamName,jdbcType=VARCHAR}, + #{awayTeamName,jdbcType=VARCHAR}, + #{homeTeamLogoDark,jdbcType=VARCHAR}, + #{awayTeamLogoDark,jdbcType=VARCHAR}, + + + + update games + + + date = #{date,jdbcType=VARCHAR}, + + + game_id = #{gameId,jdbcType=VARCHAR}, + + + start_time = #{startTime,jdbcType=TIMESTAMP}, + + + home_team_name = #{homeTeamName,jdbcType=VARCHAR}, + + + away_team_name = #{awayTeamName,jdbcType=VARCHAR}, + + + home_team_logo_dark = #{homeTeamLogoDark,jdbcType=VARCHAR}, + + + away_team_logo_dark = #{awayTeamLogoDark,jdbcType=VARCHAR}, + + + where id = #{id,jdbcType=INTEGER} + + + update games + set + date = #{date,jdbcType=VARCHAR}, + game_id = #{gameId,jdbcType=VARCHAR}, + start_time = #{startTime,jdbcType=TIMESTAMP}, + home_team_name = #{homeTeamName,jdbcType=VARCHAR}, + away_team_name = #{awayTeamName,jdbcType=VARCHAR}, + home_team_logo_dark = #{homeTeamLogoDark,jdbcType=VARCHAR}, + away_team_logo_dark = #{awayTeamLogoDark,jdbcType=VARCHAR} + where id = #{id,jdbcType=INTEGER} + + + + + + + diff --git a/src/main/resources/mapper/UrlsMapper.xml b/src/main/resources/mapper/UrlsMapper.xml new file mode 100644 index 0000000..be6e8ad --- /dev/null +++ b/src/main/resources/mapper/UrlsMapper.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + id,game_id,url, + type + + + + + + delete from urls + where id = #{id,jdbcType=INTEGER} + + + insert into urls + ( id,game_id,url + ,type) + values (#{id,jdbcType=INTEGER},#{gameId,jdbcType=VARCHAR},#{url,jdbcType=VARCHAR} + ,#{type,jdbcType=VARCHAR}) + + + insert into urls + + id, + game_id, + url, + type, + + + #{id,jdbcType=INTEGER}, + #{gameId,jdbcType=VARCHAR}, + #{url,jdbcType=VARCHAR}, + #{type,jdbcType=VARCHAR}, + + + + update urls + + + game_id = #{gameId,jdbcType=VARCHAR}, + + + url = #{url,jdbcType=VARCHAR}, + + + type = #{type,jdbcType=VARCHAR}, + + + where id = #{id,jdbcType=INTEGER} + + + update urls + set + game_id = #{gameId,jdbcType=VARCHAR}, + url = #{url,jdbcType=VARCHAR}, + type = #{type,jdbcType=VARCHAR} + where id = #{id,jdbcType=INTEGER} + + + + + + + insert into urls + (game_id, url, type) + values + + (#{gameId}, #{item.url}, #{item.type}) + + + diff --git a/src/test/java/com/ping/study/AppTest.java b/src/test/java/com/ping/study/AppTest.java new file mode 100644 index 0000000..ae7c6af --- /dev/null +++ b/src/test/java/com/ping/study/AppTest.java @@ -0,0 +1,38 @@ +package com.ping.study; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}