This commit is contained in:
2025-04-19 14:32:41 +08:00
commit d60df6d712
33 changed files with 1181 additions and 0 deletions

View File

@@ -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!" );
}
}

View File

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

View File

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

View File

@@ -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<Games> addGames() {
log.info("执行定时方法添加当天赛程");
return nbaApi.addGames();
}
@RequestMapping("/games")
public List<Games> getGames() {
log.info("获取所有赛程");
return gamesService.getGames();
}
@RequestMapping("/urls")
public List<HashMap<String, List<HashMap<String, String>>>> 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);
}
}

View File

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

View File

@@ -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<String> selectGameIds();
List<HashMap<String, String>> selectUrlsListByGameId(String gameId);
void insertUrlsWithGameId(@Param("gameId") String gameId, @Param("list") List<LiveUrl> urls);
}

View File

@@ -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); // 当天日期
}

View File

@@ -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<LiveUrl> urls;
}

View File

@@ -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;
}

View File

@@ -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"
}

View File

@@ -0,0 +1,10 @@
package com.ping.study.model.vo;
import lombok.Data;
import java.util.List;
@Data
public class Group {
private List<Game> games;
}

View File

@@ -0,0 +1,10 @@
package com.ping.study.model.vo;
import lombok.Data;
import java.util.List;
@Data
public class ResponseData {
private List<Group> groups;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

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

View File

@@ -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<HashMap<String, List<HashMap<String, String>>>> getUrls();
//添加直播地址到对应赛事
public void addUrls(addUrls addUrls);
}

View File

@@ -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<Games> getGames() {
return gamesMapper.selectAll();
}
}

View File

@@ -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<HashMap<String, List<HashMap<String, String>>>> getUrls() {
List<HashMap<String,List<HashMap<String, String>>>> urlsList = new ArrayList<>();
List<String> gameIds = urlsMapper.selectGameIds();
gameIds.forEach(gameId -> {
List<HashMap<String, String>> maps = urlsMapper.selectUrlsListByGameId(gameId);
HashMap<String, List<HashMap<String, String>>> map = new HashMap<>();
map.put(gameId,maps);
urlsList.add(map);
}); // 添加右括号和分号
return urlsList;
}
@Override
public void addUrls(addUrls addUrls) {
urlsMapper.insertUrlsWithGameId(addUrls.getGameId(), addUrls.getUrls());
}
}

View File

@@ -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<Games> addGames() {
// 创建请求DTO会自动设置当前时间戳和日期
NbaStatsRequestDto requestParams = new NbaStatsRequestDto();
log.info("{}", requestParams);
log.info("进入{},执行{}", this.getClass().getName(), "getGames");
List<Games> 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<Games> 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;
}
}

View File

@@ -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

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ping.study.mapper.GamesMapper">
<resultMap id="BaseResultMap" type="com.ping.study.pojo.Games">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="date" column="date" jdbcType="VARCHAR"/>
<result property="gameId" column="game_id" jdbcType="VARCHAR"/>
<result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
<result property="homeTeamName" column="home_team_name" jdbcType="VARCHAR"/>
<result property="awayTeamName" column="away_team_name" jdbcType="VARCHAR"/>
<result property="homeTeamLogoDark" column="home_team_logo_dark" jdbcType="VARCHAR"/>
<result property="awayTeamLogoDark" column="away_team_logo_dark" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,date,game_id,
start_time,home_team_name,away_team_name,
home_team_logo_dark,away_team_logo_dark
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from games
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from games
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ping.study.pojo.Games" useGeneratedKeys="true">
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>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ping.study.pojo.Games" useGeneratedKeys="true">
insert into games
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="date != null">date,</if>
<if test="gameId != null">game_id,</if>
<if test="startTime != null">start_time,</if>
<if test="homeTeamName != null">home_team_name,</if>
<if test="awayTeamName != null">away_team_name,</if>
<if test="homeTeamLogoDark != null">home_team_logo_dark,</if>
<if test="awayTeamLogoDark != null">away_team_logo_dark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id,jdbcType=INTEGER},</if>
<if test="date != null">#{date,jdbcType=VARCHAR},</if>
<if test="gameId != null">#{gameId,jdbcType=VARCHAR},</if>
<if test="startTime != null">#{startTime,jdbcType=TIMESTAMP},</if>
<if test="homeTeamName != null">#{homeTeamName,jdbcType=VARCHAR},</if>
<if test="awayTeamName != null">#{awayTeamName,jdbcType=VARCHAR},</if>
<if test="homeTeamLogoDark != null">#{homeTeamLogoDark,jdbcType=VARCHAR},</if>
<if test="awayTeamLogoDark != null">#{awayTeamLogoDark,jdbcType=VARCHAR},</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ping.study.pojo.Games">
update games
<set>
<if test="date != null">
date = #{date,jdbcType=VARCHAR},
</if>
<if test="gameId != null">
game_id = #{gameId,jdbcType=VARCHAR},
</if>
<if test="startTime != null">
start_time = #{startTime,jdbcType=TIMESTAMP},
</if>
<if test="homeTeamName != null">
home_team_name = #{homeTeamName,jdbcType=VARCHAR},
</if>
<if test="awayTeamName != null">
away_team_name = #{awayTeamName,jdbcType=VARCHAR},
</if>
<if test="homeTeamLogoDark != null">
home_team_logo_dark = #{homeTeamLogoDark,jdbcType=VARCHAR},
</if>
<if test="awayTeamLogoDark != null">
away_team_logo_dark = #{awayTeamLogoDark,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ping.study.pojo.Games">
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}
</update>
<select id="selectByGameId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from games
where game_id = #{gameId,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from games
</select>
</mapper>

View File

@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ping.study.mapper.UrlsMapper">
<resultMap id="BaseResultMap" type="com.ping.study.pojo.Urls">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="gameId" column="game_id" jdbcType="VARCHAR"/>
<result property="url" column="url" jdbcType="VARCHAR"/>
<result property="type" column="type" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,game_id,url,
type
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from urls
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from urls
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ping.study.pojo.Urls" useGeneratedKeys="true">
insert into urls
( id,game_id,url
,type)
values (#{id,jdbcType=INTEGER},#{gameId,jdbcType=VARCHAR},#{url,jdbcType=VARCHAR}
,#{type,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ping.study.pojo.Urls" useGeneratedKeys="true">
insert into urls
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="gameId != null">game_id,</if>
<if test="url != null">url,</if>
<if test="type != null">type,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id,jdbcType=INTEGER},</if>
<if test="gameId != null">#{gameId,jdbcType=VARCHAR},</if>
<if test="url != null">#{url,jdbcType=VARCHAR},</if>
<if test="type != null">#{type,jdbcType=VARCHAR},</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ping.study.pojo.Urls">
update urls
<set>
<if test="gameId != null">
game_id = #{gameId,jdbcType=VARCHAR},
</if>
<if test="url != null">
url = #{url,jdbcType=VARCHAR},
</if>
<if test="type != null">
type = #{type,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ping.study.pojo.Urls">
update urls
set
game_id = #{gameId,jdbcType=VARCHAR},
url = #{url,jdbcType=VARCHAR},
type = #{type,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectGameIds" resultType="java.lang.String">
select distinct game_id from urls
</select>
<select id="selectUrlsListByGameId" resultType="map">
select urls.type,urls.url from urls where game_id = #{gameId}
</select>
<insert id="insertUrlsWithGameId">
insert into urls
(game_id, url, type)
values
<foreach collection="list" item="item" separator=",">
(#{gameId}, #{item.url}, #{item.type})
</foreach>
</insert>
</mapper>