日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

记录一个需求:折线图,要求指定年份每一天的记录

發(fā)布時間:2023/12/31 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录一个需求:折线图,要求指定年份每一天的记录 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

根據(jù)自己的業(yè)務去修改,本文提供參考思路
首先:需求是要求指定年份每一天的價格,沒有則補全并賦值日期之前最近的一次價格

有一個主表(主表中有id和價格)
子表中有主表的id和價格
主表價格更改,就需要向字表中添加一條數(shù)據(jù)
如下表:

解決方案1:設置定時任務,在每晚的12點去主表查詢價格,然后向子表添加一條記錄,如果當天已經(jīng)存在記錄就不在添加
問題:如果一天更改兩次價格,子表中將會存在兩個記錄?
這個問題是這樣解決的,更改的時候去邏輯刪除子表中的這一天的記錄,再去添加,保證,一天只有一條數(shù)據(jù),邏輯刪除的意義在于客戶需要所有的記錄可以都拿的出來

這樣也是一個實現(xiàn)的思路,查詢折線圖的時候,直接獲取未被邏輯刪除的就是每一天的數(shù)據(jù),
但我是另一種方式
解決方案二:
思路如下:

1.先去子表查詢 指定年份的價格 2.得到指定年份的每一天存到一個集合中 3.遍歷查詢的指定年份的價格集合 拿到創(chuàng)建時間 和價格 (我這里查詢是ASE升序,保證日期先后,比如119號和1113號都有記錄,則查詢的集合中9號在13號之前) 4.在查詢的指定年份的價格集合中遍歷得到年份的每一天 ,拿到每一天 5. 判斷這一天是否在 價格的日期與下一個日期之間,獲取(get(i)和get(i+1)) (比如:119號和1113號有記錄,然后每一天回去比較是否在這兩個日期區(qū)域,在的話就賦值119號的價格) 6.如果不在則跳出這輪循環(huán),進行下一輪 continue

按年查詢升序

public ReportResultVo getPrice(Long id, int years) {ReportResultVo reportResultVo = new ReportResultVo();ArrayList<String> xData = new ArrayList<>();ArrayList<Long> yData = new ArrayList<>();// 1.指定年份中的歷史價格(子表數(shù)據(jù))List<GasSourcePrice> gasSourcePrices = gasSourcePriceMapper.selectGasSourcePriceByGasSourceId(id, years);List<String> daysByYears = getDaysByYear(years);for (int x = 0; x < gasSourcePrices.size(); x++) {//next為下一條數(shù)據(jù)int next = 0;if (x + 1 < gasSourcePrices.size()) {next = x + 1;} else {next = x;}GasSourcePrice gasSourcePrice = gasSourcePrices.get(x);Date date1 = gasSourcePrice.getCreateTime();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(date1);Date beginTime = strToDate(format1);GasSourcePrice gasSourcePriceNext = gasSourcePrices.get(next);Date date2 = gasSourcePriceNext.getCreateTime();String format2 = sdf.format(date2);Date endTime = strToDate(format2);for (int i = 0; i < daysByYears.size(); i++) {log.info(daysByYears.get(i));Date date = strToDate(daysByYears.get(i));if (belongCalendar(date, beginTime, endTime)) {xData.add(sdf.format(date));yData.add(gasSourcePrice.getPrice().longValue());} else {continue;}}}reportResultVo.setXData(xData);reportResultVo.setYData(yData);return reportResultVo;}

其中用到的方法:
1.判斷時間是否在時間段內(nèi)

/*** 判斷指定日期是否在兩個日期內(nèi)** @param nowTime* @param beginTime* @param endTime* @return*/public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {Calendar date = Calendar.getInstance();date.setTime(nowTime);Calendar begin = Calendar.getInstance();begin.setTime(beginTime);Calendar end = Calendar.getInstance();end.setTime(endTime);if (date.after(begin) && date.before(end)) {return true;} else if (nowTime.compareTo(beginTime) == 0 ) {return true;} else {return false;}}

2.生成指定年份的每一天

/*** 生成指定年的每一天* @param year* @return*/public static List<String> getDaysByYear(int year){Calendar c=Calendar.getInstance();List<String> dates=new ArrayList<String>();for(int i=0;i<12;i++){c.set(year,i,1);int lastDay=c.getActualMaximum(Calendar.DATE);for(int j=1;j<=lastDay;j++){String month="";String day="";if(i<9) month="-0"+(i+1);else month="-"+(i+1);if(j<10) day="-0"+j;else day="-"+j;String date=year+month+day;dates.add(date);}}return dates;}

3.字符串轉(zhuǎn)日期格式

// 字符串 轉(zhuǎn) 日期public static Date strToDate(String str) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date date = null;try {date = sdf.parse(str);} catch (ParseException e) {}return date;}

效果如下

出現(xiàn)了新的問題
如果遇到上一年有記錄,但這一年的記錄是11月9號的,那就會存在一個問題,
返回的數(shù)據(jù)只是這一年11月9號之后的,之前并沒有數(shù)據(jù),所以需要改進
解決方案:
查詢到這一年記錄后判斷這一年是否有記錄
沒有則查詢這一年第一天之前數(shù)據(jù)庫最近的一條記錄,并將給一年賦值
查詢的sql語句如下:

select * from 表 where date_format(時間字段,'%y%m%d') < date_format(#{指定日期},'%y%m%d')ORDER BY create_time DESC LIMIT 1

其中指定日期就是指定年份的第一天(可以通過如下方法獲取,傳年,傳月,返回第一天)

/*** 獲取該月的第一天** @param year 年* @param month 月* @return*/public static Date getFirstDay(int year, int month) {Calendar c = Calendar.getInstance();c.set(Calendar.YEAR, year);// 設置月份,因為月份從0開始,所以用month - 1c.set(Calendar.MONTH, month - 1);c.set(Calendar.DAY_OF_MONTH, 1);Date time = c.getTime();SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM-dd");String format = slf.format(time);try {return slf.parse(format);} catch (ParseException e) {e.printStackTrace();}return null;}

這是這一年都沒有記錄的情況
下面是這一年中有記錄
分兩步,文章最開始已經(jīng)將存在日期之后的數(shù)據(jù)賦值了為第二步
在之前添加第一步:
拿到指定年份中的歷史數(shù)據(jù)之后 獲取第一條數(shù)據(jù)
因為sql是ASC升序的,所以獲取的第一條就是這一年的第一條數(shù)據(jù),
根據(jù)這一年的第一條數(shù)據(jù)的日期去查詢這之前數(shù)據(jù)庫最近的一條記錄
給這一年存在日期之前的數(shù)據(jù)賦值
這樣就解決了
查詢的sql還是上面那條,哪里的指定日期改為這里第一條數(shù)據(jù)的日期就可以了
成品如下:

public ReportResultVo getPrice(Long id, int years) {//每一天List<String> daysByYears = DateUtils.getDaysByYear(years);ReportResultVo reportResultVo = new ReportResultVo();ArrayList<String> xData = new ArrayList<>();ArrayList<Long> yData = new ArrayList<>();// 指定年份中的歷史價格List<GasSourcePrice> gasSourcePrices = gasSourcePriceMapper.selectGasSourcePriceByGasSourceId(id, years);if (gasSourcePrices.size() > 0) {//查詢該年份之前的最近一條記錄,賦值給該年份第一條記錄之前的日期//獲取該年份第一條數(shù)據(jù)GasSourcePrice gasSourcePriceOne = gasSourcePrices.get(0);//查詢該年份之前最近的一條記錄GasSourcePrice gasSourcePrice = gasSourcePriceMapper.selectGasSourcePriceByDate(id, gasSourcePriceOne.getCreateTime().toString());if (StringUtils.isNotNull(gasSourcePrice)) {Date date1 = gasSourcePrice.getCreateTime();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(date1);Date beginTime = DateUtils.strToDate(format1);Date date2 = gasSourcePriceOne.getCreateTime();String format2 = sdf.format(date2);Date endTime = DateUtils.strToDate(format2);for (int i = 0; i < daysByYears.size(); i++) {Date date = DateUtils.strToDate(daysByYears.get(i));if (DateUtils.belongCalendar(date, beginTime, endTime)) {if (!xData.contains(sdf.format(date))) {xData.add(sdf.format(date));}yData.add(gasSourcePrice.getPrice().longValue());} else {continue;}}}//賦值給該年份查詢到第一條和最后一條之間的日期for (int x = 0; x < gasSourcePrices.size(); x++) {int next = 0;if (x + 1 < gasSourcePrices.size()) {next = x + 1;} else {next = x;}GasSourcePrice gas = gasSourcePrices.get(x);Date date1 = gas.getCreateTime();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(date1);Date beginTime = DateUtils.strToDate(format1);GasSourcePrice gasSourcePriceNext = gasSourcePrices.get(next);Date date2 = gasSourcePriceNext.getCreateTime();String format2 = sdf.format(date2);Date endTime = DateUtils.strToDate(format2);for (int i = 0; i < daysByYears.size(); i++) {log.info(daysByYears.get(i));Date date = DateUtils.strToDate(daysByYears.get(i));if (DateUtils.belongCalendar(date, beginTime, endTime)) {if (!xData.contains(sdf.format(date))) {xData.add(sdf.format(date));}yData.add(gas.getPrice().longValue());} else {continue;}}}} else if (gasSourcePrices.size() == 0) {//這一年沒有記錄 查之前的記錄 并賦值給這一年Date day = DateUtils.getFirstDay(years, 1);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(day);//查詢該年份之前最近的一條記錄GasSourcePrice gasSourcePrice = gasSourcePriceMapper.selectGasSourcePriceByDate(id, format1);if (StringUtils.isNotNull(gasSourcePrice)) {for (int i = 0; i < daysByYears.size(); i++) {log.info(daysByYears.get(i));yData.add(gasSourcePrice.getPrice().longValue());}}xData.addAll(daysByYears);}reportResultVo.setXData(xData);reportResultVo.setYData(yData);return reportResultVo;}

本以為結(jié)束了,又有新的問題,查詢的數(shù)據(jù)如果是今年的應該查詢的今天,而我所查詢出來的只截止到最后一天
使用到的方法getLastDay,根據(jù)年月獲取這個月第一天,最后一天

/*** 獲取該月的第一天** @param year 年* @param month 月* @return*/public static Date getFirstDay(int year, int month) {Calendar c = Calendar.getInstance();c.set(Calendar.YEAR, year);// 設置月份,因為月份從0開始,所以用month - 1c.set(Calendar.MONTH, month - 1);c.set(Calendar.DAY_OF_MONTH, 1);Date time = c.getTime();SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM-dd");String format = slf.format(time);try {return slf.parse(format);} catch (ParseException e) {e.printStackTrace();}return null;}/*** 獲取該月的最后一天*/public static Date getLastDay ( int year, int month){Calendar c = Calendar.getInstance();c.set(Calendar.YEAR, year);// 設置月份,因為月份從0開始,所以用month - 1c.set(Calendar.MONTH, month - 1);// 獲取當前時間下,該月的最大日期的數(shù)字int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);// 將獲取的最大日期數(shù)設置為Calendar實例的日期數(shù)c.set(Calendar.DAY_OF_MONTH, lastDay);Date time = c.getTime();SimpleDateFormat slf = new SimpleDateFormat("yyyy-MM-dd");String format = slf.format(time);try {return slf.parse(format);} catch (ParseException e) {e.printStackTrace();}return null;}

最終版的代碼如下:

public ReportResultVo getPrice(Long id, int years) {//每一天List<String> daysByYears = DateUtils.getDaysByYear(years);//返回結(jié)果ReportResultVo reportResultVo = new ReportResultVo();ArrayList<String> xData = new ArrayList<>();ArrayList<Long> yData = new ArrayList<>();// 指定年份中的歷史價格List<GasSourcePrice> gasSourcePrices = gasSourcePriceMapper.selectGasSourcePriceByGasSourceId(id, years);if (gasSourcePrices.size() > 0) {//判斷是否是今年if (DateUtils.isNowYear(years)){//是今年則補全最后一條記錄至今的數(shù)據(jù)GasSourcePrice gasSourcePrice = new GasSourcePrice();BeanUtils.copyProperties(gasSourcePrices.get(gasSourcePrices.size()-1),gasSourcePrice);gasSourcePrice.setCreateTime(DateUtils.getNowDate());gasSourcePrices.add(gasSourcePrice);}else {//不是今年,補全到年的最后一天GasSourcePrice gasSourcePrice = new GasSourcePrice();BeanUtils.copyProperties(gasSourcePrices.get(gasSourcePrices.size()-1),gasSourcePrice);gasSourcePrice.setCreateTime(DateUtils.getLastDay(years,12));gasSourcePrices.add(gasSourcePrice);}//查詢該年份之前的最近一條記錄,賦值給該年份第一條記錄之前的日期//獲取該年份第一條數(shù)據(jù)GasSourcePrice gasSourcePriceOne = gasSourcePrices.get(0);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");String dateOne = simpleDateFormat.format(gasSourcePriceOne.getCreateTime());//查詢該年份之前最近的一條記錄GasSourcePrice gasSourcePrice = gasSourcePriceMapper.selectGasSourcePriceByDate(id,dateOne);if (StringUtils.isNotNull(gasSourcePrice)) {Date date1 = gasSourcePrice.getCreateTime();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(date1);Date beginTime = DateUtils.strToDate(format1);Date date2 = gasSourcePriceOne.getCreateTime();String format2 = sdf.format(date2);Date endTime = DateUtils.strToDate(format2);for (int i = 0; i < daysByYears.size(); i++) {Date date = DateUtils.strToDate(daysByYears.get(i));if (DateUtils.belongCalendar(date, beginTime, endTime)) {if (!xData.contains(sdf.format(date))) {xData.add(sdf.format(date));}yData.add(gasSourcePrice.getPrice().longValue());} else {continue;}}}//賦值給該年份查詢到第一條和最后一條之間的日期for (int x = 0; x < gasSourcePrices.size(); x++) {int next = 0;if (x + 1 < gasSourcePrices.size()) {next = x + 1;} else {next = x;}GasSourcePrice gas = gasSourcePrices.get(x);Date date1 = gas.getCreateTime();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(date1);Date beginTime = DateUtils.strToDate(format1);GasSourcePrice gasSourcePriceNext = gasSourcePrices.get(next);Date date2 = gasSourcePriceNext.getCreateTime();String format2 = sdf.format(date2);Date endTime = DateUtils.strToDate(format2);for (int i = 0; i < daysByYears.size(); i++) {log.info(daysByYears.get(i));Date date = DateUtils.strToDate(daysByYears.get(i));if (DateUtils.belongCalendar(date, beginTime, endTime)) {if (!xData.contains(sdf.format(date))) {xData.add(sdf.format(date));}yData.add(gas.getPrice().longValue());} else {continue;}}}} else if (gasSourcePrices.size() == 0) {//這一年沒有記錄 查之前的記錄 并賦值給這一年Date day = DateUtils.getFirstDay(years, 1);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String format1 = sdf.format(day);//查詢該年份之前最近的一條記錄GasSourcePrice gasSourcePrice = gasSourcePriceMapper.selectGasSourcePriceByDate(id, format1);if (StringUtils.isNotNull(gasSourcePrice)) {for (int i = 0; i < daysByYears.size(); i++) {log.info(daysByYears.get(i));yData.add(gasSourcePrice.getPrice().longValue());if (daysByYears.get(i).equals(DateUtils.getDate())){break;}}}xData.addAll(daysByYears);}reportResultVo.setXData(xData);reportResultVo.setYData(yData);return reportResultVo;}

總結(jié)

以上是生活随笔為你收集整理的记录一个需求:折线图,要求指定年份每一天的记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。