添加金价走势图
parent
8f3ca8c62a
commit
dd6df84346
@ -0,0 +1,87 @@
|
||||
package me.zhengjie.utils;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.net.url.UrlBuilder;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/30
|
||||
*/
|
||||
public class ResponseUtil {
|
||||
|
||||
private ResponseUtil(){
|
||||
|
||||
}
|
||||
|
||||
public static JSONObject goldRequest(Long days,String goldCode, Date startDate,Date endDate) {
|
||||
// 拼接url请求
|
||||
String buildUrl = UrlBuilder.create()
|
||||
.setScheme("https")
|
||||
.setHost("api.jijinhao.com")
|
||||
.addPath("/quoteCenter").addPath("history.htm")
|
||||
.addQuery("style", String.valueOf(3))
|
||||
.addQuery("pageSize", String.valueOf(days)).addQuery("code", goldCode)
|
||||
.addQuery("startDate", cn.hutool.core.date.DateUtil.format(startDate, DatePattern.NORM_DATE_PATTERN))
|
||||
.addQuery("endDate", DateUtil.format(endDate, DatePattern.NORM_DATE_PATTERN)).build();
|
||||
|
||||
String body = HttpUtil.createGet(buildUrl)
|
||||
.header(HttpHeaders.REFERER,
|
||||
"https://quote.cngold.org/")
|
||||
.execute()
|
||||
.charset(CharsetUtil.UTF_8)
|
||||
.body();
|
||||
// 去除不需要数据替换空格
|
||||
String resultString = StrUtil.trimToEmpty
|
||||
(StrUtil.subAfter(body, "=", Boolean.FALSE));
|
||||
|
||||
if (resultString.contains("true")) {
|
||||
return JSON.parseObject(resultString);
|
||||
}
|
||||
throw new BadRequestException("the request error");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static JSONObject firstGoldRequest(String bindStr,String vidStr,String rowStr,String pageStr,Date startDate,Date endDate) {
|
||||
|
||||
// 拼接url请求
|
||||
String buildUrl = UrlBuilder.create()
|
||||
.setScheme("http")
|
||||
.setHost("www.dyhjw.com")
|
||||
.addPath("/api").addPath("ajax.html")
|
||||
.addQuery("jindian_getPriceScope[bid]", bindStr)
|
||||
.addQuery("jindian_getPriceScope[vid]", vidStr)
|
||||
.addQuery("jindian_getPriceScope[stime]", DateUtil.format(startDate, DatePattern.NORM_DATE_PATTERN))
|
||||
.addQuery("jindian_getPriceScope[etime]", DateUtil.format(endDate, DatePattern.NORM_DATE_PATTERN))
|
||||
.addQuery("jindian_getPriceScope[row]", rowStr)
|
||||
.addQuery("jindian_getPriceScope[page]", pageStr)
|
||||
.build();
|
||||
|
||||
String body = HttpUtil.createGet(buildUrl).execute().charset(CharsetUtil.UTF_8).body();
|
||||
// 去除不需要数据替换空格
|
||||
String resultString = StrUtil.trimToEmpty(body);
|
||||
|
||||
if (resultString.contains("data") &&
|
||||
JSON.parseObject(JSON.parseObject(resultString).get("jindian_getPriceScope").toString()).get("status").equals("1")) {
|
||||
|
||||
String byLength =
|
||||
resultString.substring(
|
||||
resultString.indexOf(StrUtil.DELIM_START,
|
||||
resultString.indexOf(StrUtil.DELIM_START) + 1), resultString.length() - 1);
|
||||
return JSON.parseObject(JSON.parseObject(byLength).get("data").toString());
|
||||
}
|
||||
throw new BadRequestException("the request error");
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package me.zhengjie.utils.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/4/7
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum FirstGoldEnums {
|
||||
|
||||
/**
|
||||
* 周大福
|
||||
*/
|
||||
ZDF("1", "1"),
|
||||
|
||||
/**
|
||||
* 周大生
|
||||
*/
|
||||
ZDS("10", "48"),
|
||||
|
||||
/**
|
||||
* 老凤祥
|
||||
*/
|
||||
LFX("2", "8");
|
||||
|
||||
private final String bid;
|
||||
|
||||
private final String vid;
|
||||
|
||||
static final Map<String,FirstGoldEnums> GOLD_ENUMS_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
static {
|
||||
GOLD_ENUMS_MAP.put("JO_42660", FirstGoldEnums.ZDF);
|
||||
GOLD_ENUMS_MAP.put("JO_42657", FirstGoldEnums.LFX);
|
||||
GOLD_ENUMS_MAP.put("JO_52678", FirstGoldEnums.ZDF);
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package me.zhengjie.utils.enums;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/22
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum GoldCodeEnums {
|
||||
|
||||
/**
|
||||
* 周大福
|
||||
*/
|
||||
ZDF("zdf", "JO_42660"),
|
||||
|
||||
/**
|
||||
* 老凤祥
|
||||
*/
|
||||
LFX("lfx", "JO_42657"),
|
||||
|
||||
/**
|
||||
* 周六福
|
||||
*/
|
||||
ZLF("zlf", "JO_42653"),
|
||||
|
||||
/**
|
||||
* 周生生
|
||||
*/
|
||||
ZSS("zss", "JO_42625"),
|
||||
|
||||
/**
|
||||
* 六福珠宝
|
||||
*/
|
||||
LFZB("lfzb", "JO_42646"),
|
||||
|
||||
/**
|
||||
* 菜百
|
||||
*/
|
||||
CB("cb", "JO_42638"),
|
||||
|
||||
/**
|
||||
* 金至尊
|
||||
*/
|
||||
JZZ("jzz", "JO_42632"),
|
||||
|
||||
/**
|
||||
* 老庙
|
||||
*/
|
||||
LM("lm", "JO_42634"),
|
||||
|
||||
/**
|
||||
* 潮宏基
|
||||
*/
|
||||
CHJ("chj", "JO_52670"),
|
||||
|
||||
/**
|
||||
* 周大生
|
||||
*/
|
||||
ZDS("zds", "JO_52678"),
|
||||
|
||||
/**
|
||||
* 中国黄金
|
||||
*/
|
||||
ZGHJ("zghj", "JO_9753");
|
||||
|
||||
private final String code;
|
||||
|
||||
private final String shorthand;
|
||||
|
||||
public static String findCode(String shorthand) {
|
||||
for (GoldCodeEnums value : GoldCodeEnums.values()) {
|
||||
if (shorthand.equalsIgnoreCase(value.getCode())) {
|
||||
return value.getShorthand();
|
||||
}
|
||||
}
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package me.zhengjie.modules.gold.dto;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/22
|
||||
*/
|
||||
@Data
|
||||
public class AnalyzingGoldResponseDTO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 342180031344936298L;
|
||||
|
||||
private Double q1;
|
||||
|
||||
@JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
|
||||
private Date time;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package me.zhengjie.modules.gold.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/22
|
||||
*/
|
||||
@Data
|
||||
public class ApiGoldPriceDTO {
|
||||
|
||||
private String code;
|
||||
|
||||
private Date endDate;
|
||||
|
||||
private String style;
|
||||
|
||||
private Date startDate;
|
||||
|
||||
private Integer pageSize;
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package me.zhengjie.modules.gold.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/4/7
|
||||
*/
|
||||
@Data
|
||||
public class FirstGoldResponseDTO implements Serializable {
|
||||
private static final long serialVersionUID = -4915817022920042708L;
|
||||
|
||||
private String bname;
|
||||
|
||||
private Double price;
|
||||
|
||||
private Date date;
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package me.zhengjie.modules.gold.dto;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/22
|
||||
*/
|
||||
@Data
|
||||
public class GoldRequestDTO {
|
||||
|
||||
@NotEmpty(message = "编号不能为空")
|
||||
@ApiModelProperty("黄金编号")
|
||||
private List<String> goldCodeList;
|
||||
|
||||
@JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN)
|
||||
@ApiModelProperty("开始时间")
|
||||
private Date startDate;
|
||||
|
||||
@JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN)
|
||||
@ApiModelProperty("结束时间")
|
||||
private Date endDate;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package me.zhengjie.modules.gold.dto;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/30
|
||||
*/
|
||||
@Data
|
||||
public class TodayGoldResponseDTO implements Serializable {
|
||||
|
||||
private Double q1;
|
||||
|
||||
private String goldCode;
|
||||
|
||||
@JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
|
||||
private Date time;
|
||||
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
package me.zhengjie.modules.gold.rest;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.collect.Lists;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.annotation.Log;
|
||||
import me.zhengjie.common.http.CommonResponse;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.constant.DefaultConstant;
|
||||
import me.zhengjie.modules.gold.dto.AnalyzingGoldResponseDTO;
|
||||
import me.zhengjie.modules.gold.dto.GoldRequestDTO;
|
||||
import me.zhengjie.modules.gold.dto.TodayGoldResponseDTO;
|
||||
import me.zhengjie.utils.ResponseUtil;
|
||||
import me.zhengjie.utils.enums.GoldCodeEnums;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/3/22
|
||||
*/
|
||||
@Slf4j
|
||||
@Api("获取咨询类信息")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/gold")
|
||||
public class GoldPriceController {
|
||||
|
||||
|
||||
@Log("获取咨询类信息")
|
||||
@ApiOperation("查询金价信息")
|
||||
@PostMapping("/trend")
|
||||
public CommonResponse<Map<String, Object>> goldTrend
|
||||
(@Validated @RequestBody GoldRequestDTO requestDTO) {
|
||||
if (ObjectUtil.isNull(requestDTO.getStartDate())
|
||||
&& ObjectUtil.isNull(requestDTO.getEndDate())) {
|
||||
DateTime date = DateUtil.date();
|
||||
requestDTO.setStartDate(DateUtil.beginOfDay
|
||||
(DateUtil.offsetDay(date, -DefaultConstant.SEVEN_NUMBER)));
|
||||
requestDTO.setEndDate(DateUtil.beginOfDay(date));
|
||||
}
|
||||
if (requestDTO.getStartDate().after(requestDTO.getEndDate())) {
|
||||
throw new BadRequestException("开始时间必须大于结束时间");
|
||||
}
|
||||
|
||||
Map<String, Object> sortedMap = new LinkedHashMap<>();
|
||||
Map<String, Object> map = new HashMap<>(DefaultConstant.TWO_NUMBER);
|
||||
List<TodayGoldResponseDTO> goldResponseDTOList = new ArrayList<>();
|
||||
Map<String, List<AnalyzingGoldResponseDTO>> historyMap = new HashMap<>(DefaultConstant.TWELVE_NUMBER);
|
||||
|
||||
// 相差多少天用于分页数量
|
||||
long betweenDays = DateUtil.between(requestDTO.getStartDate(), requestDTO.getEndDate(), DateUnit.DAY);
|
||||
DateTime dateTime = DateUtil.beginOfDay(DateUtil.date());
|
||||
for (String code : requestDTO.getGoldCodeList()) {
|
||||
String goldCode = GoldCodeEnums.findCode(code);
|
||||
// 发送请求
|
||||
JSONObject jsonObject = ResponseUtil.goldRequest
|
||||
(betweenDays, goldCode, requestDTO.getStartDate(), requestDTO.getEndDate());
|
||||
// 解析数据
|
||||
List<AnalyzingGoldResponseDTO> dtoArrayList = getAnalyzingGoldResponse(jsonObject);
|
||||
List<AnalyzingGoldResponseDTO> todayGoldList = Lists.newArrayList(dtoArrayList);
|
||||
Iterator<AnalyzingGoldResponseDTO> iterator = todayGoldList.iterator();
|
||||
TodayGoldResponseDTO todayGoldResponseDTO;
|
||||
// 查看周几
|
||||
int dayOfWeek = DateUtil.thisDayOfWeek();
|
||||
if (dayOfWeek == DefaultConstant.SEVEN_NUMBER || dayOfWeek == DefaultConstant.ONE_NUMBER) {
|
||||
dateTime = DateUtil.offsetDay
|
||||
(dateTime, -(dayOfWeek == DefaultConstant.SEVEN_NUMBER
|
||||
? DefaultConstant.ONE_NUMBER : DefaultConstant.TWO_NUMBER));
|
||||
}
|
||||
while (iterator.hasNext()) {
|
||||
AnalyzingGoldResponseDTO next = iterator.next();
|
||||
long betweenDay = DateUtil.betweenDay(dateTime, next.getTime(), Boolean.TRUE);
|
||||
// 查询当天金价
|
||||
if (betweenDay == DefaultConstant.ZERO_NUMBER) {
|
||||
todayGoldResponseDTO = new TodayGoldResponseDTO();
|
||||
BeanUtil.copyProperties(next, todayGoldResponseDTO);
|
||||
todayGoldResponseDTO.setGoldCode(code);
|
||||
goldResponseDTOList.add(todayGoldResponseDTO);
|
||||
break;
|
||||
}
|
||||
}
|
||||
historyMap.put(code, dtoArrayList);
|
||||
}
|
||||
// map排序
|
||||
Stream<Map.Entry<String, List<AnalyzingGoldResponseDTO>>> st =
|
||||
historyMap.entrySet().stream();
|
||||
st.sorted(Comparator.comparing(e -> -(e.getValue().size()))).forEach
|
||||
(e -> sortedMap.put(e.getKey(), e.getValue()));
|
||||
map.put("historyGoldPrice", sortedMap);
|
||||
map.put("todayGoldPrice", goldResponseDTOList);
|
||||
return CommonResponse.createBySuccess(map);
|
||||
}
|
||||
|
||||
|
||||
@Log("查询当天金价")
|
||||
@ApiOperation("查询当天金价")
|
||||
@GetMapping("/dayPrice")
|
||||
public ResponseEntity<Map<String, Object>> goldTrend(String goldCode) {
|
||||
Map<String, Object> map = new HashMap<>(DefaultConstant.TWO_NUMBER);
|
||||
DateTime dateTime = DateUtil.beginOfDay(DateUtil.date());
|
||||
String shortCode = GoldCodeEnums.findCode(goldCode);
|
||||
JSONObject jsonObject = ResponseUtil.goldRequest
|
||||
(Long.parseLong(String.valueOf(DefaultConstant.ONE_NUMBER)), shortCode, dateTime, dateTime);
|
||||
List<AnalyzingGoldResponseDTO> dtoArrayList = getAnalyzingGoldResponse(jsonObject);
|
||||
if (CollUtil.isNotEmpty(dtoArrayList)) {
|
||||
Iterator<AnalyzingGoldResponseDTO> iterator = dtoArrayList.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
AnalyzingGoldResponseDTO next = iterator.next();
|
||||
long betweenDay = DateUtil.betweenDay(dateTime, next.getTime(), Boolean.TRUE);
|
||||
if (betweenDay != DefaultConstant.ZERO_NUMBER) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
map.put(goldCode, dtoArrayList);
|
||||
return new ResponseEntity<>(map, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
private List<AnalyzingGoldResponseDTO> getAnalyzingGoldResponse(JSONObject jsonObject) {
|
||||
// 解析数据
|
||||
List<AnalyzingGoldResponseDTO> dtoList =
|
||||
Convert.toList(AnalyzingGoldResponseDTO.class,
|
||||
jsonObject.get("data"));
|
||||
// 时间去除
|
||||
return dtoList.stream()
|
||||
.collect(Collectors.collectingAndThen(Collectors.toCollection(
|
||||
() -> new TreeSet<>(Comparator.comparing(AnalyzingGoldResponseDTO::getTime))), ArrayList::new));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue