background/controller/plan.go

246 lines
4.3 KiB
Go

package controller
import (
"background/db"
"background/logs"
"background/model"
"fmt"
"log"
"strconv"
"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
}
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
}
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
}
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
}
e = db.GetOrm().Model(&model.Undo{}).Create(&req).Error
if nil != e {
log.Print(e.Error())
return
}
log.Print(req.ID)
resp.Data = req.ID
resp.Msg = "OK"
resp.Status = 0
}
func (this *PlanController) UpdateUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
var req model.Book
e := c.BindJSON(&req)
if nil != e {
resp.Msg = "wrong input"
return
}
e = db.GetOrm().Model(&model.Book{}).Update(&req).Error
if nil != e {
logs.Error(e.Error())
return
}
resp.Data = nil
resp.Msg = "OK"
resp.Status = 0
}
func (this *PlanController) GetUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
undos := []model.Undo{}
e := db.GetOrm().Model(&model.Undo{}).Find(&undos).Error
if nil != e{
log.Print(e.Error())
return
}
resp.Data = undos
resp.Msg = "OK"
resp.Status = 0
}
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
}
}
func (this *PlanController) DeleteUndo(c *gin.Context) {
resp := RespBase{}
defer func() {
c.JSON(200, resp)
}()
sid := c.Param("id")
id := db.Atoi(sid)
e := db.GetOrm().Model(&model.Undo{}).Delete(&model.Undo{ID: int32(id)}).Error
if nil != e{
log.Print(e.Error())
return
}
resp.Status = 0
resp.Msg = "OK"
}