spinach

Controller

@GetMapping("/search/{key}")
public ResponseEntity<List<Match>> search(@PathVariable String key) {
List<Match> matchs = service.getMatchByCondition(key);
ResponseEntity<List<Match>> entity = new ResponseEntity<List<Match>>(matchs, HttpStatus.OK);
return entity;
}
Service
@Service
public class IndexService {
@Autowired
private MatchMapper matchMapper;

@Autowired
private CheckUtils checkUtils;

@Autowired
private EncryptUtils encryptUtils;

@Autowired
private DateTimeUtils dateTimeUtils;

public List<Match> getMatchByCondition(String args) {
String param = args;
Match condition = new Match();
HashMap maps = this.testMatchs();
if (checkUtils.isChinaChar(param)) { /** 判断是否为汉字 */
String key = encryptUtils.convert2Hex(param);
Object value = maps.get(key);
if (value != null) {
param = value.toString();
}
condition.setLeagueMatchesType(param);
condition.setHomeTeam(param);
condition.setAwayTeam(param);
} else if (checkUtils.isDateTime(param)) { /** 判断是否为汉字 */
condition.setMatchDateTime(dateTimeUtils.toDateTime(param));
}
return matchMapper.getMatchsCondition(condition);
}

public List<Bettors> getBettorsByCondition(String args) {
String param = args;
Bettors condition = new Bettors();
HashMap maps = this.testMatchs();
if (checkUtils.isChinaChar(param)) { /** 判断是否为汉字 */
String key = encryptUtils.convert2Hex(param);
Object value = maps.get(key);
if (value != null) {
param = value.toString();
}
condition.setHomeTeam(param);
condition.setAwayTeam(param);
} else if (checkUtils.isDateTime(param)) { /** 判断是否为汉字 */
condition.setMatchDateTime(dateTimeUtils.toDateTime(param));
}
return matchMapper.getBettorsByCondition(condition);
}

public HashMap<String, Object> testMatchs() {
HashMap<String, Object> matchs = new HashMap<String, Object>();
matchs.put("82f18d85", "ThePremierLeague");
matchs.put("82f18d8580548d5b", "ThePremierLeague");
matchs.put("82f1683c51708d857ea780548d5b", "ThePremierLeague");
return matchs;
}
}
Utils

@Service
public class CheckUtils {
/**
* 判断是否为数字
* @param key 查询关键字
* @return boolean
*/
public boolean isNumber(String key) {
Pattern p = Pattern.compile("^-?(\\d+|\\d+\\.\\d+)$");
return p.matcher(key).find();
}
/**
* 判断是否为日期:年的位数等于4位,月01到12,日01到31
* @param key 查询关键字
* @return boolean
*/
public boolean isDateTime(String key) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
try {
LocalDate dateTime = LocalDate.parse(key, formatter);
} catch (DateTimeException e) {
return false;
}
return true;
}

/**
* 判断汉字:中文:[\u4e00-\u9fa5],日文:[\u0800-\u4e00]
* @param key 查询关键字
* @return boolean
*/
public boolean isChinaChar(String key) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
return p.matcher(key).find();
}
}
public class Computing {

/**
* 六场积分比较预测法
*/
public HashMap six(Match match) {
HashMap<Enum, Object> result = new HashMap<Enum, Object>();
String teamName = "";
int diff = Math.abs(match.getHomePoints() - match.getAwayPoints());
if (match.getHomePoints() > match.getAwayPoints()) {
if (diff >= 6) {
result.put(ParamData.COMPUTE, ResultType.HOME_WINS);
result.put(ParamData.NAME, match.getHomeTeam());
}
} else {
if (diff >= 6) {
result.put(ParamData.COMPUTE, ResultType.AWAY_WINS);
result.put(ParamData.NAME, match.getAwayTeam());
} else if (diff == 5) {
result.put(ParamData.COMPUTE, ResultType.HOME_DW);
result.put(ParamData.NAME, match.getAwayTeam());
} else if (diff >=2 && diff <= 4) {
result.put(ParamData.COMPUTE, ResultType.AWAY_WINS);
result.put(ParamData.NAME, match.getAwayTeam());
} else if (diff <= 1) {
result.put(ParamData.COMPUTE, ResultType.HOME_DW);
result.put(ParamData.NAME, match.getAwayTeam());
}
}
return result;
}

/**
* 进球率预测法
* @param match
* @return
*/
public HashMap goals(Match match) {
HashMap<Enum, Object> result = new HashMap<Enum, Object>();
float homeEffective = Float.valueOf(match.getHomePoints())/match.getMatchNumber();
float awayEffective = Float.valueOf(match.getAwayPoints())/match.getMatchNumber();
float diff = Math.abs(homeEffective - awayEffective);
if (homeEffective > awayEffective) {
if (diff > 0.30f) {
result.put(ParamData.COMPUTE, ResultType.HOME_DW);
result.put(ParamData.NAME, match.getHomeTeam());
}
} else {
if (diff > 0.30f) {
result.put(ParamData.COMPUTE, ResultType.AWAY_WINS);
result.put(ParamData.NAME, match.getAwayTeam());
} else if (diff > 0.10f && diff <= 0.30f) {
result.put(ParamData.COMPUTE, ResultType.HOME_DW);
result.put(ParamData.NAME, match.getHomeTeam());
} else if (diff <= 0.10f) {
result.put(ParamData.COMPUTE, ResultType.HOME_DW);
result.put(ParamData.NAME, match.getHomeTeam());
}
}
return result;
}

public HashMap analysis() {
HashMap<Enum, Object> result = new HashMap<Enum, Object>();
return result;
}
}

@Service
public class DateTimeUtils {

public LocalDateTime toDateTime(String args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDate = LocalDate.parse(args, formatter);
LocalTime localTime = LocalTime.of(23,59,59);
LocalDateTime dateTime = LocalDateTime.of(localDate, localTime);
return dateTime;
}
}
@Service
public class EncryptUtils {

/**
* String转换Base64
*
* @param args String类型的字符串
* @return
*/
public static String toBase64(String args) {
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(args.getBytes(StandardCharsets.UTF_8));
}

/**
* Base64转换String
* @param base64 Base64类型的字符串
* @return
*/
public static String toString(String base64) {
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(base64);
String args = new String(bytes, StandardCharsets.UTF_8);
return args;
}

/**
* 将汉字的字符串转换为16进制的字符串
* @param args 汉字String的字符串
* @return
*/
public static String convert2Hex(String args) {
StringBuilder keys = new StringBuilder();
/** 将字符串转换成字符数组*/
char[] chars = args.toCharArray();
for (int i = 0; i < chars.length; i++) {
/** 将汉字转换成16进制,然后拼接成字符串*/
keys.append(Integer.toHexString(chars[i]));
}
return keys.toString();
}
}
SpinachApplication 
@SpringBootApplication
public class SpinachApplication {

public static void main(String[] args) {
SpringApplication.run(SpinachApplication.class, args);
}

@Bean("redisTemplate")
@Scope("prototype")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
/**
* redisTemplate:支持所有redis原生API操作
*/
RedisTemplate <String, Object> redisTemplate = new RedisTemplate<String, Object>();


/**
* 设置Bean和Json字符串转换对象
* 将JavaBean转换Json字符串或者将Json字符转换为JavaBean
*/
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);

/**
* Jackson2JsonRedisSerializer:序列化和反序列化对象
* 1.框架默认采用的是JdkSerializationRedisSerializer,需要替换框架默认值。
* 2.优点:序列化效率,占用内存低,序列化后的内容在redis中可读性高。
* 3.缺点:时间类型的数据以long类型存储,反序列化会出现问题.需要序列化的类class,可以使用Object.class
* 4.反序列化带泛型的数组类会报转换异常
*/
Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
jsonRedisSerializer.setObjectMapper(objectMapper);

/**
* 设置redis的key和value序列化规则
* key为String类型
* values为Object类型
*/
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jsonRedisSerializer);

/**
* redis连接
*/
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
Club
/**
* 序号
*/
public int clubId;
/**
* 俱乐部类型
*/
private String type;
/**
* 全称
*/
private String fullName;
/**
* 昵称、简称
*/
private String nickName;
/**
* 场次
*/
private int gamesPlayed;
/**
* 排名
*/
private int rank;
/**
* 积分
*/
private int points;
/**
* 胜
*/
private int wins;
/**
* 平
*/
private int draws;
/**
* 负
*/
private int losses;
/**
* 进球数
*/
private int goals;

Bettors
/**
* 单注编号
*/
private int bettorsNo;

/**
* 主队ID
*/
private int homeClubId;

/**
* 主队名称
*/
private String homeTeam;

/**
* 客队ID
*/
private int awayClubId;

/**
* 客队名称
*/
private String awayTeam;

/**
* 项目
*/
private float item;

/**
* 系数
*/
private float coefficient;

/**
* 赔率
*/
private float odds;

/**
* 成本
*/
private float cost;

/**
* 返还
*/
private float repay;

Match
/**
* 联赛编号
*/
private int matchNumber;
/**
* 联赛类型
*/
private String leagueMatchesType;
/**
* 联赛轮次
*/
private int leagueMatchesRound;
/**
* 联赛时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
private LocalDateTime matchDateTime;
/**
* 主队
*/
private String homeTeam;
/**
* 客队
*/
private String awayTeam;

/**
* 主队积分
*/
private int homePoints;
/**
* 客队积分
*/
private int awayPoints;

/**
* 主队进球
*/
private int homeGoalsScored;
/**
* 客队进球
*/
private int awayGoalsScored;












posted @ 2023-09-08 16:34  狗狗听话  阅读(52)  评论(0编辑  收藏  举报