This commit is contained in:
Yutousama 2022-09-04 11:04:03 +08:00
parent f296d4c819
commit 7885f2bbfb
6 changed files with 331 additions and 93 deletions

View File

@ -60,7 +60,7 @@
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.7</version>
<version>2.0.12</version>
</dependency>
<dependency>

View File

@ -1,11 +1,18 @@
package com.yutou.qqbot.Controllers;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.data.calendar.CalendarTask;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.CalendarTools;
import com.yutou.qqbot.utlis.RedisTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.Set;
@Controller
public class CalendarController {
@ResponseBody
@ -18,9 +25,38 @@ public class CalendarController {
}
@ResponseBody
@RequestMapping("/calendar/set.do")
public JSONObject setCalendar(String startDate,String interval){
public JSONObject setCalendar(String startDate,String title,String content,String type,String model,String day){
JSONObject json = new JSONObject();
String[] dates=startDate.split(" - ");
Date sDate = AppTools.timeToDate(dates[0].split(" ")[0], dates[0].split(" ")[1]);
Date endDate = AppTools.timeToDate(dates[1].split(" ")[0], dates[1].split(" ")[1]);
CalendarTask task=new CalendarTask();
task.setModel(Integer.parseInt(model));
task.setType(Integer.parseInt(type));
task.setTitle(title);
task.setContent(content);
task.setStartTime(sDate);
task.setEndTime(endDate);
task.setTime(Integer.parseInt(day));
CalendarTools.addTask(task);
json.put("code",0);
json.put("msg","add task success!");
json.put("data",task);
return json;
}
@RequestMapping("/calendar/list.do")
@ResponseBody
public JSONObject list(){
JSONObject json=new JSONObject();
JSONArray array=new JSONArray();
Set<String> set = RedisTools.list_get(CalendarTools.REDIS_TAG);
for (String s : set) {
JSONObject task = JSONObject.parseObject(s);
array.add(task);
}
json.put("code",0);
json.put("msg","ok");
json.put("data",array);
return json;
}
}

View File

@ -0,0 +1,51 @@
package com.yutou.qqbot.data.calendar;
import lombok.Data;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
@Data
public class CalendarTask {
public static final int TYPE_DAY=Calendar.DATE;
public static final int TYPE_WEEK=Calendar.WEEK_OF_MONTH;
public static final int TYPE_MONTH=Calendar.MONTH;
public static final int MODEL_ONE=0;
public static final int MODEL_LOOP=1;
private int id;
private int type;
private int model=MODEL_ONE;
private String title;
private String content;
private Date startTime;
private Date endTime;
private int time;
private boolean outQQ;
public Date getEndTime() {
if(endTime==null){
Calendar calendar=Calendar.getInstance(Locale.CHINA);
calendar.setTime(new Date());
calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+1);
endTime= calendar.getTime();
}
return endTime;
}
public Date getStartTime() {
if(startTime==null){
startTime=new Date();
}
return startTime;
}
public int getTime() {
if(time==0){
time = (int) ((getEndTime().getTime() - getStartTime().getTime())/1000/60/60/24);
}
return time;
}
}

View File

@ -21,6 +21,7 @@ import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.jar.JarEntry;
@ -216,4 +217,18 @@ public class AppTools {
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
return saveFile.getAbsolutePath();
}
public static Date timeToDate(String date,String time){
String form;
if(StringUtils.isEmpty(time)){
form="yyyy-MM-dd";
}else{
form="yyyy-MM-dd HH:mm:ss";
}
try {
return new SimpleDateFormat(form).parse(date+" "+time);
} catch (ParseException e) {
return null;
}
}
}

View File

@ -1,61 +1,115 @@
package com.yutou.qqbot.utlis;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.data.calendar.CalendarTask;
import lombok.SneakyThrows;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Set;
import java.text.SimpleDateFormat;
import java.util.*;
@SuppressWarnings("MagicConstant")
public class CalendarTools {
private static final String REDIS_TAG="Calendar";
public static final String REDIS_TAG = "Calendar";
public static JSONObject getHoliday() {
int month = Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA).get(Calendar.MONTH) + 1;
int year = Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA).get(Calendar.YEAR);
return getHoliday(year + "", null);
return getHoliday(year + "");
}
public static JSONObject getHoliday(String year, String month) {
@SneakyThrows
public static JSONObject getHoliday(String year) {
String get = HttpTools.get(String.format("https://timor.tech/api/holiday/year/%s", year));
JSONObject json = JSONObject.parseObject(get);
JSONObject date = new JSONObject();
JSONObject dateHoliday = new JSONObject();
JSONArray dateHoliday = new JSONArray();
JSONObject holiday = json.getJSONObject("holiday");
JSONArray a1=new JSONArray();
JSONArray a2=new JSONArray();
for (CalendarTask task : getTasks()) {
for (String datum : getMouthData(task)) {
date.put(new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("yyyy-M-d").parse(datum.split(" ")[0])), task.getTitle());
a1.add(datum.split(" ")[0]);
a2.add(datum.split(" ")[0]);
}
}
for (String key : holiday.keySet()) {
JSONObject _tmp = holiday.getJSONObject(key);
date.put(year + "-" + key, _tmp.getString("name"));
dateHoliday.put(year + "-" + key, _tmp.getBooleanValue("holiday"));
String _key=new SimpleDateFormat("yyyy-M-d").format(new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + key));
if(_tmp.getBooleanValue("holiday")){
a1.add(_key);
}else{
a2.add(_key);
}
}
dateHoliday.add(a1);
dateHoliday.add(a2);
JSONObject tmp = new JSONObject();
tmp.put("date", date);
tmp.put("holiday", dateHoliday);
return tmp;
}
public static boolean isHoliday() {
Calendar calendar=Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA);
Calendar calendar2=Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA);
Set<String> list = RedisTools.list_get(REDIS_TAG);
for (String s : list) {
public static boolean addTask(CalendarTask task){
task.setId(RedisTools.list_get(REDIS_TAG).size()+1);
String json = JSONObject.toJSONString(task);
RedisTools.list_add(REDIS_TAG,json);
return true;
}
public static List<CalendarTask> getTasks(){
List<CalendarTask> list=new ArrayList<>();
Set<String> set = RedisTools.list_get(REDIS_TAG);
for (String s : set) {
list.add(JSONObject.parseObject(s,CalendarTask.class));
}
return false;
return list;
}
public static boolean removeTask(CalendarTask task){
for (String s : RedisTools.list_get(REDIS_TAG)) {
CalendarTask tmp=JSONObject.parseObject(s,CalendarTask.class);
if(tmp.getId()==task.getId()){
RedisTools.list_remove(REDIS_TAG,s);
break;
}
}
return true;
}
public static List<String> getMouthData(CalendarTask task) {
List<String> list = new ArrayList<>();
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(task.getStartTime());
list.add(new SimpleDateFormat("yyyy-M-d HH:mm:ss",Locale.CHINA).format(calendar.getTime()));
while (true) {
calendar.set(task.getType(),calendar.get(task.getType())+task.getTime());
if(calendar.getTime().getTime()>task.getEndTime().getTime()){
break;
}
list.add(new SimpleDateFormat("yyyy-M-d HH:mm:ss",Locale.CHINA).format(calendar.getTime()));
if(task.getModel()==CalendarTask.MODEL_ONE){
break;
}
}
return list;
}
public static void main(String[] args) {
Calendar calendar=Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA);
Calendar calendar2=Calendar.getInstance(DateFormat.getDateInstance().getTimeZone(), Locale.CHINA);
int start = 6;
int in = 14;
calendar.set(Calendar.MONTH,7);
calendar.set(Calendar.DATE,start);
calendar2.set(Calendar.DATE,13);
long t2=calendar2.getTime().getTime();
long t1=calendar.getTime().getTime();
long l = calendar2.getTime().getTime() - calendar.getTime().getTime();
long interval=l/1000/60/60/24;
System.out.println(interval);
System.out.println("t1 = " + t1);
System.out.println("t2 = " + t2);
System.out.println("interval = " + interval%in);
CalendarTask task = new CalendarTask();
task.setTitle("myTitle");
task.setContent("game");
task.setStartTime(AppTools.timeToDate("2022-9-3",null));
//task.setEndTime(new Date(1669996799999L));
task.setEndTime(AppTools.timeToDate("2032-9-3",null));
task.setType(CalendarTask.TYPE_WEEK);
task.setModel(CalendarTask.MODEL_LOOP);
task.setTime(2);
List<String> list = getMouthData(task);
for (String s : list) {
System.out.println("data = " + s);
}
}
}

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>BiliBili下载器</title>
<title>Calendar</title>
<link rel="stylesheet" href="layui/css/layui.css" media="all">
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
@ -11,47 +11,78 @@
<body>
<div class="myDiy">
<div class="layui-layout layui-layout-admin myDiy">
<blockquote class="layui-elem-quote">日历管理
</blockquote>
<br/><br/><br/>
<div class="layui-bg-gray layui-row layui-col-space15" id="card" style="padding: 30px;">
<div class="layui-bg-gray layui-row layui-col-space15" id="card">
<form class="layui-form" action="">
<form class="layui-form" lay-filter="calendarForm">
<div class="layui-form-item">
<table id="demo" lay-filter="test"></table>
</div>
<div class="layui-form-item">
<label class="layui-form-label">日历</label>
<div class="layui-input-block">
<div id="calendar"/>
<div id="calendarShow" name="calendar" style="width: 45%"/>
</div>
</div>
<br/>
<br/>
<br/>
<div class="layui-form-item">
<label class="layui-form-label">间隔</label>
<div class="layui-input-block">
<select id="interval" lay-verify="required">
<option value=""></option>
<option value="0">相隔X天</option>
<option value="1">每周X</option>
<option value="2">每月X号</option>
</select>
<div class="layui-form-item">
<label class="layui-form-label">addTask</label>
<div class="layui-input-block">
<input type="text" class="layui-input" id="calendar">
</div>
</div>
</div>
<blockquote class="layui-elem-quote"><span id="dateText">日期选择</span>
</blockquote>
<div class="layui-form-item">
<label class="layui-form-label">间隔日期</label>
<div class="layui-input-block">
<input type="text" id="day" required lay-verify="required" placeholder="天数"
autocomplete="off" class="layui-input">
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-block">
<input type="text" name="title" required lay-verify="required" placeholder="请输入标题"
autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<a class="layui-btn" id="setCalendar">新增规则</a>
<div class="layui-form-item">
<label class="layui-form-label">content</label>
<div class="layui-input-block">
<input type="text" name="content" required lay-verify="required" placeholder="请输入content"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">type</label>
<div class="layui-input-block">
<input type="radio" name="model" value="0" title="ONE" checked>
<input type="radio" name="model" value="1" title="LOOP">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">model</label>
<div class="layui-input-block">
<input type="radio" name="type" value="5" title="DAY" checked>
<input type="radio" name="type" value="4" title="WEEK">
<input type="radio" name="type" value="2" title="MONTH">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">间隔日期</label>
<div class="layui-input-block">
<input type="text" name="day" required lay-verify="required" placeholder="天数"
autocomplete="off" class="layui-input">
</div>
</div>
<blockquote class="layui-elem-quote"><span id="dateText">日期选择</span>
</blockquote>
<div class="layui-form-item">
<div class="layui-input-block">
<a class="layui-btn" id="setCalendar">新增规则</a>
</div>
</div>
</div>
</form>
@ -63,53 +94,104 @@
<script src="layui/layui.js"></script>
<script src="layui/jquery-3.2.1.js"></script>
<script>
let marks = []
let laydate;
let datatmp;
layui.use('laydate', function () {
let dayDateObj
laydate = layui.laydate;
datatmp = laydate.render({
elem: '#calendar' //指定元素
, position: 'static'
// , mark: marks
, holidays: marks
, done: function (value, date, endDate) {
console.log(date)
dayDateObj = date;
$('#dateText')[0].innerHTML = "从" + date.date + "号开始计算"
}
});
layui.use(['laydate', 'form','table'], function () {
let dayDateObj
let marks = {}
let holiday = []
let laydate = layui.laydate;
let form = layui.form;
let table=layui.table;
$.get("/calendar/all.do", function (json) {
//执行一个laydate实例
// marks = json.data.date;
showLayDate()
marks = json.data.date;
holiday = json.data.holiday;
showData()
})
$('#setCalendar').click(function () {
let interval = $('#interval')[0].value
let day = $('#day')[0].value
let intervalText = $('#interval').find("option:selected").text();
intervalText = intervalText.replace('X', day)
layer.open({
title: '确认'
, content: '确认从' + dayDateObj.date + '号开始, ' + intervalText + ' 开始提醒?'
, yes: function (index, layero) {
table.render({
elem: '#demo'
,url: '/calendar/list.do' //数据接口
,page: true //开启分页
,cols: [[ //表头
{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
,{field: 'title', title: '标题', width:100}
,{field: 'content', title: 'content', width:200, sort: true}
,{field: 'startTime', title: 'startTime', width:180}
,{field: 'endTime', title: 'endTime', width: 180}
,{field: 'time', title: 'time', width: 80, sort: true}
,{field: 'type', title: 'type', width: 80, sort: true}
,{field: 'model', title: 'model', width: 80}
,{field: 'words', title: '字数', width: 135, sort: true}
]]
});
function showData() {
laydate.render({
elem: '#calendar' //指定元素
, type: 'datetime'
, show:true
, range: true
, holidays: holiday
, done: function (value, date, endDate) {
console.log(date)
console.log(endDate)
dayDateObj = date;
dayDateObj.time = value
$('#dateText')[0].innerHTML = "从" + date.date + "号开始计算"
}, change: function (value, date, endDate) {
}
});
laydate.render({
elem: '#calendarShow' //指定元素
, position: 'static'
, holidays: holiday
,theme: 'molv'
, done: function (value, date, endDate) {
}, change: function (value, date, endDate) {
console.log(marks[value]+" > "+value)
if (marks[value] !== undefined) {
// datatmp.hint(marks[value])
layer.tips(marks[value], '#calendarShow')
}
}
});
}
$('#setCalendar').click(function () {
form.submit('calendarForm', function (obj) {
console.log(obj)
let field = obj.field;
layer.open({
title: '确认'
, content: '确认从' + dayDateObj.date + '号开始,开始提醒?'
, yes: function (index, layero) {
$.post("/calendar/set.do", {
startDate: dayDateObj.time
, title: field.title
, content: field.content
, type: field.type
, model: field.model
, day: field.day
},function (json) {
layer.msg(json.msg)
})
}
});
})
})
});
</script>
<style>
.myDiy {
width: 40%;
height: 300px;
margin-top: 10px;
margin-left: 25%;
/*width: 80%;*/
margin-top: 10%;
margin-left: 15%;
margin-right: 15%;
}
.button {