写第二阶段的接口,基本完成。
今天完成的主要任务:日期转时间戳
在process层中定义一个日期转时间戳类,在下面这里直接调用。
getStockIndexModel.setSelectDate(DateToStamp.date2Stamp(stockIndexModel.getSelectDate()));
特别注意,在下面要加上对输入字符串的非空判断,否则一旦输入为空,则抛出异常,取不到空的那个值,与场景不符合!!
package com.simnectzbank.lbs.processlayer.stock.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToStamp {
public static String date2Stamp(String s) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
if(s != null && !s.equals("")){
Date date =simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
return s;
}
public static void main(String[] args) throws ParseException {
String t = "";
String s = date2Stamp(t);
System.out.println(s);
}
}
重点问题:在sql里如果不把多余的注释掉,则查不出来结果!!!(卡了很久)
<?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.simnectzbank.lbs.systemlayer.stockinfo.dao.IndexHistoryDao" >
<resultMap id="BaseResultMap" type="com.simnectzbank.lbs.systemlayer.stockinfo.entity.IndexHistoryEntity" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="Last" property="last" jdbcType="DECIMAL" />
<result column="Open" property="open" jdbcType="DECIMAL" />
<result column="High" property="high" jdbcType="DECIMAL" />
<result column="Low" property="low" jdbcType="DECIMAL" />
<result column="CreateDate" property="createDate" jdbcType="VARCHAR" />
<result column="UpdateTime" property="updateTime" jdbcType="DECIMAL" />
<result column="Type" property="type" jdbcType="VARCHAR" />
</resultMap>
<select id="findManyByHk" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.IndexHistoryEntity" >
select * from stockindex_hk
where 1=1
<if test="type != null and type != ''" >
and Type = #{type}
</if>
<!-- <if test="fromDate != null and fromDate != ''">-->
<!-- and UpdateTime >= #{fromDate}-->
<!-- </if>-->
<!-- <if test="toDate != null and toDate != ''">-->
<!-- and UpdateTime <= #{toDate}-->
<!-- </if>-->
<if test="selectDate != null and selectDate !=''">
and UpdateTime = #{selectDate}
</if>
<if test="selectDate == null or selectDate ==''">
order by UpdateTime desc limit 1
</if>
</select>
<select id="findManyByChina" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.IndexHistoryEntity" >
select * from stockindex_china
where 1=1
<if test="type != null and type != ''" >
and Type = #{type}
</if>
<!-- <if test="fromDate != null and fromDate != ''">-->
<!-- and UpdateTime >= #{fromDate}-->
<!-- </if>-->
<!-- <if test="toDate != null and toDate != ''">-->
<!-- and UpdateTime <= #{toDate}-->
<!-- </if>-->
<if test="selectDate != null and selectDate !=''">
and UpdateTime = #{selectDate}
</if>
<if test="selectDate == null or selectDate ==''">
order by UpdateTime desc limit 2
</if>
</select>
<select id="findManyByWorld" resultMap="BaseResultMap" parameterType="com.simnectzbank.lbs.systemlayer.stockinfo.entity.IndexHistoryEntity" >
select * from stockindex_world
where 1=1
<if test="type != null and type != ''" >
and Type = #{type}
</if>
<!-- <if test="fromDate != null and fromDate != ''">-->
<!-- and UpdateTime >= #{fromDate}-->
<!-- </if>-->
<!-- <if test="toDate != null and toDate != ''">-->
<!-- and UpdateTime <= #{toDate}-->
<!-- </if>-->
<if test="selectDate != null and selectDate !=''">
and UpdateTime = #{selectDate}
</if>
<if test="selectDate == null or selectDate ==''">
order by UpdateTime desc limit 2
</if>
</select>
</mapper>
测试结果:
{
"code": "200",
"msg": "Transaction Accepted.",
"data": [{
"id": 1,
"last": 3348.33,
"open": 3314.03,
"high": 3349.05,
"low": 3314.03,
"createDate": "2018年1月2日",
"updateTime": 1514822400000,
"type": "SSEC"
}, {
"id": 793,
"last": 4087.4,
"open": 4045.21,
"high": 4087.78,
"low": 4045.21,
"createDate": "2018年1月2日",
"updateTime": 1514822400000,
"type": "CSI300"
}]
}
刚去找老师,被质疑了三个问题:
1.接口显示太多,写成分层的形式!!方法是写在同一个controller中!(羞愧)已解决
package com.simnectzbank.lbs.experiencelayer.stock.controller;
import com.csi.sbs.common.business.util.ResultUtil;
import com.simnectzbank.lbs.experiencelayer.stock.model.StockIndexModel;
import com.simnectzbank.lbs.experiencelayer.stock.model.front.StockIndexHistoryModel;
import com.simnectzbank.lbs.experiencelayer.stock.service.proxy.stockprocess.StockProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@CrossOrigin // 解决跨域请求
@Controller
@RequestMapping("/stock")
@Api(description = "APIs used in stock list retrieval.")
public class StockByIndex {
@Resource
private StockProcessService stockProcessService;
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/stockByChina", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "This API is designed to retrieve stocks By China.", notes = "Before calling /order/orderPlacing to buy a stock, you may want to check the stocks in the market.")
@ApiResponses({ @ApiResponse(code = 200, message = "Query completed successfully.(Returned By Get)"),
@ApiResponse(code = 404, message = "The requested deposit account does not exist.Action: Please make sure the account number and account type you’re inputting are correct."),
@ApiResponse(code = 201, message = "Normal execution. The request has succeeded. (Returned By Post)"),
@ApiResponse(code = 403, message = "Token has incorrect scope or a security policy was violated. Action: Please check whether you’re using the right token with the legal authorized user account."),
@ApiResponse(code = 500, message = "Something went wrong on the API gateway or micro-service. Action: check your network and try again later."), })
public ResultUtil<List<StockIndexHistoryModel>> stockByChina(@RequestBody @Validated StockIndexModel queryStockByChina) throws Exception {
try{
return stockProcessService.stockByChina(queryStockByChina);
} catch (Exception e) {
throw e;
}
}
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/stockByHk", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "This API is designed to retrieve stocks By Hk.", notes = "Before calling /order/orderPlacing to buy a stock, you may want to check the stocks in the market.")
@ApiResponses({ @ApiResponse(code = 200, message = "Query completed successfully.(Returned By Get)"),
@ApiResponse(code = 404, message = "The requested deposit account does not exist.Action: Please make sure the account number and account type you’re inputting are correct."),
@ApiResponse(code = 201, message = "Normal execution. The request has succeeded. (Returned By Post)"),
@ApiResponse(code = 403, message = "Token has incorrect scope or a security policy was violated. Action: Please check whether you’re using the right token with the legal authorized user account."),
@ApiResponse(code = 500, message = "Something went wrong on the API gateway or micro-service. Action: check your network and try again later."), })
public ResultUtil<List<StockIndexHistoryModel>> stockByHk(@RequestBody @Validated StockIndexModel queryStockByHk) throws Exception {
try{
return stockProcessService.stockByHk(queryStockByHk);
} catch (Exception e) {
throw e;
}
}
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/stockByWorld", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "This API is designed to retrieve stocks By World.", notes = "Before calling /order/orderPlacing to buy a stock, you may want to check the stocks in the market.")
@ApiResponses({ @ApiResponse(code = 200, message = "Query completed successfully.(Returned By Get)"),
@ApiResponse(code = 404, message = "The requested deposit account does not exist.Action: Please make sure the account number and account type you’re inputting are correct."),
@ApiResponse(code = 201, message = "Normal execution. The request has succeeded. (Returned By Post)"),
@ApiResponse(code = 403, message = "Token has incorrect scope or a security policy was violated. Action: Please check whether you’re using the right token with the legal authorized user account."),
@ApiResponse(code = 500, message = "Something went wrong on the API gateway or micro-service. Action: check your network and try again later."), })
public ResultUtil<List<StockIndexHistoryModel>> stockByWorld(@RequestBody @Validated StockIndexModel queryStockByWorld) throws Exception {
try{
return stockProcessService.stockByWorld(queryStockByWorld);
} catch (Exception e) {
throw e;
}
}
}
2.sql语句查询的时候,应该使用分组查询,一共两种类型的数据,分别取每组数据的最新数据(未完成)
3.输入的是时间戳,将时间戳转化为两个时间段,比如输入的是1618826381,日期是2021-04-19 17:59:41 那么对应输入数据库查询的日期为 2021-04-19------2021-04-20之间(未完成)
4.计算那些升跌率呀什么的。