blog_backend_api/controller/plan.go

301 lines
5.6 KiB
Go
Raw Normal View History

2021-02-16 06:05:21 +00:00
package controller
import (
"background/db"
2021-03-05 02:18:59 +00:00
"background/logs"
2021-02-16 06:05:21 +00:00
"background/model"
2021-02-16 07:44:26 +00:00
"fmt"
2021-02-16 06:05:21 +00:00
"log"
"strconv"
"time"
2021-02-16 06:05:21 +00:00
"github.com/gin-gonic/gin"
)
type PlanController struct {
}
func (this *PlanController) GetPlan(c *gin.Context) {
}
func (this *PlanController) GetPlanDates(c *gin.Context) {
type Req struct {
Date string `json:"date"`
}
var req Req
resp := RespBase{
Msg: "ERROR",
Status: 23,
}
defer c.JSON(200, &resp)
e := c.BindJSON(&req)
if nil != e {
log.Print(e.Error())
return
}
plan := []model.Plan{}
e = db.GetOrm().Model(&plan).Where("date = ?", req.Date).Find(&plan).Error
if nil != e {
log.Print(e.Error())
}
resp.Status = 0
resp.Msg = "OK"
resp.Data = plan
return
}
func (this *PlanController) AddPlan(c *gin.Context) {
var req model.Plan
resp := RespBase{Msg: "r", Status: 0, Data: nil}
defer c.JSON(200, &resp)
e := c.BindJSON(&req)
if nil != e {
log.Print(e.Error())
return
}
e = db.GetOrm().Create(&req).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Msg = "OK"
resp.Status = 0
}
func (this *PlanController) DelPlan(c *gin.Context) {
resp := RespBase{Msg: "r", Status: 0, Data: nil}
defer c.JSON(200, &resp)
var e error
iid := 0
id := c.Query("id")
if "" != id {
iid, e = strconv.Atoi(id)
if nil != e {
log.Print(e.Error())
return
}
} else {
return
}
plan := model.Plan{}
plan.ID = uint32(iid)
e = db.GetOrm().Delete(&plan).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Msg = "OK"
resp.Status = 0
}
2021-02-16 07:44:26 +00:00
type ReqDate struct {
Date string `gorm:"date" json:"date"`
}
func (m *ReqDate) TableName() string {
return "plan"
}
func (this *PlanController) PlanDay(c *gin.Context) {
var req ReqDate
resp := RespBase{
Msg: "ERROR",
Status: 23,
}
defer c.JSON(200, &resp)
e := c.BindJSON(&req)
if nil != e {
log.Print(e.Error())
return
}
ret := make([]ReqDate, 1)
sql := fmt.Sprintf(`SELECT distinct plan.date FROM plan where DATE_FORMAT(plan.date,'%%Y-%%m')=DATE_FORMAT('%s','%%Y-%%m')`, req.Date)
log.Print(sql)
e = db.GetOrm().Raw(sql).Find(&ret).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Data = ret
resp.Msg = "OK"
resp.Status = 0
}
2021-02-21 13:39:37 +00:00
func (this *PlanController) Types(c *gin.Context) {
resp := RespBase{
Msg: "ERROR",
Status: 23,
}
defer c.JSON(200, &resp)
rets := []model.PlanType{}
e := db.GetOrm().Model(&model.PlanType{}).Find(&rets).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Data = rets
resp.Msg = "OK"
resp.Status = 0
}
2021-03-05 02:18:59 +00:00
func (this *PlanController) CreateUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
var req model.Undo
e := c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
resp.Msg = "wrong input"
return
}
req.CreateTime = time.Now().Format("2006-01-02 15:04:05")
2021-03-05 02:18:59 +00:00
e = db.GetOrm().Model(&model.Undo{}).Create(&req).Error
if nil != e {
log.Print(e.Error())
return
}
2021-03-09 15:21:12 +00:00
log.Print(req.ID)
resp.Data = req.ID
2021-03-05 02:18:59 +00:00
resp.Msg = "OK"
resp.Status = 0
}
2021-06-11 17:26:37 +00:00
func (this *PlanController) UnFinishUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
id := c.Param("id")
e := db.GetOrm().Exec(fmt.Sprintf("update background.`undo` set done = '0' where id = '%s'",id )).Error
if nil != e {
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
func (this *PlanController) FinishUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
id := c.Param("id")
log.Print("finish undo id",id)
2021-07-20 16:14:05 +00:00
e := db.GetOrm().Exec(fmt.Sprintf("update background.`undo` set done = 1,finish_time='%s' where id = %s",
time.Now().Format("2006-01-02 15:04:05"),id)).Error
log.Print(fmt.Sprintf("update background.`undo` set done = 1,finish_time='%s' where id = %s",time.Now().Format("2006-01-02 15:04:05"),id))
2021-06-11 17:26:37 +00:00
if nil != e {
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
2021-03-05 02:18:59 +00:00
func (this *PlanController) UpdateUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
2021-06-11 17:26:37 +00:00
var req model.Undo
2021-03-05 02:18:59 +00:00
e := c.BindJSON(&req)
if nil != e {
resp.Msg = "wrong input"
return
}
2021-06-11 17:26:37 +00:00
e = db.GetOrm().Model(&model.Undo{}).Update(&req).Error
2021-03-05 02:18:59 +00:00
if nil != e {
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
2021-06-11 17:26:37 +00:00
func (this *PlanController) GetDone(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
undos := []model.Undo{}
e := db.GetOrm().Model(&model.Undo{}).Where("done = 1").Find(&undos).Error
if nil != e {
log.Print(e.Error())
return
}
resp.Data = undos
resp.Msg = "OK"
resp.Status = 0
}
2021-03-05 02:18:59 +00:00
func (this *PlanController) GetUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
2021-03-09 15:21:12 +00:00
undos := []model.Undo{}
2021-06-12 12:32:42 +00:00
e := db.GetOrm().Model(&model.Undo{}).Find(&undos).Error
2021-03-22 04:10:37 +00:00
if nil != e {
2021-03-09 15:21:12 +00:00
log.Print(e.Error())
2021-03-05 02:18:59 +00:00
return
}
2021-03-09 15:21:12 +00:00
resp.Data = undos
resp.Msg = "OK"
resp.Status = 0
2021-03-05 02:18:59 +00:00
}
func (this *PlanController) GetPageUndo(c *gin.Context) {
type ReqGetPageUndo struct {
}
var req ReqGetPageUndo
resp := RespBase{}
// limit := c.Query("limit")
// offset := c.Query("offset")
// iLmit, e := strconv.Atoi(limit)
// if nil != e {
// return
// }
// iOffset, e := strconv.Atoi(offset)
// if nil != e {
// return
// }
defer func() {
c.JSON(200, resp)
}()
e := c.BindJSON(&req)
if nil != e {
logs.Error(e.Error())
return
}
}
2021-03-09 15:21:12 +00:00
func (this *PlanController) DeleteUndo(c *gin.Context) {
2021-03-05 02:18:59 +00:00
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
2021-03-09 15:21:12 +00:00
sid := c.Param("id")
id := db.Atoi(sid)
e := db.GetOrm().Model(&model.Undo{}).Delete(&model.Undo{ID: int32(id)}).Error
2021-03-22 04:10:37 +00:00
if nil != e {
2021-03-09 15:21:12 +00:00
log.Print(e.Error())
return
2021-03-05 02:18:59 +00:00
}
2021-03-09 15:21:12 +00:00
resp.Status = 0
resp.Msg = "OK"
2021-03-05 02:18:59 +00:00
}