package hdl import ( "applet/app/admin/lib/validate" "applet/app/admin/md" "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/utils" "github.com/gin-gonic/gin" "time" ) func NoticeList(c *gin.Context) { noticeDb := db.NoticeDb{} noticeDb.Set() notices, err := noticeDb.FindNotice(0, 0) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, map[string]interface{}{ "list": notices, "is_pop_to_central_kitchen_list": []map[string]interface{}{ { "name": "弹出", "value": 1, }, { "name": "不弹出", "value": 0, }, }, }, nil) return } func NoticeSort(c *gin.Context) { var args struct { Ids []string `json:"ids" binding:"required"` } if err := c.ShouldBindJSON(&args); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } noticeDb := db.NoticeDb{} noticeDb.Set() notices, err := noticeDb.FindNoticeById(args.Ids) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } for k, v := range *notices { v.Sort = k _, err1 := noticeDb.NoticeUpdate(&v, "sort") if err1 != nil { e.OutErr(c, e.ERR_DB_ORM, err1.Error()) return } } e.OutSuc(c, "success", nil) } func NoticeAdd(c *gin.Context) { var req md.NoticeAddReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } noticeDb := db.NoticeDb{} noticeDb.Set() now := time.Now() notice := model.Notice{ Name: req.Name, Content: req.Content, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } _, err = noticeDb.NoticeInsert(¬ice) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } func NoticeIsPopToCentralKitchen(c *gin.Context) { id := c.DefaultQuery("id", "0") isPopToCentralKitchen := c.DefaultQuery("is_pop_to_central_kitchen", "0") noticeDb := db.NoticeDb{} noticeDb.Set() popToCentralKitchen, err := noticeDb.GetPopToCentralKitchen() if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } now := time.Now() if popToCentralKitchen != nil && utils.StrToInt(id) != popToCentralKitchen.Id { popToCentralKitchen.IsPopToCentralKitchen = 0 popToCentralKitchen.UpdateAt = now.Format("2006-01-02 15:04:05") _, err = noticeDb.NoticeUpdate(popToCentralKitchen, "is_pop_to_central_kitchen", "update_at") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } } notice, err := noticeDb.GetNotice(utils.StrToInt(id)) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if notice == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } notice.IsPopToCentralKitchen = utils.StrToInt(isPopToCentralKitchen) notice.UpdateAt = now.Format("2006-01-02 15:04:05") _, err = noticeDb.NoticeUpdate(notice, "is_pop_to_central_kitchen", "update_at") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) } func NoticeUpdate(c *gin.Context) { var req md.NoticeUpdateReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } noticeDb := db.NoticeDb{} noticeDb.Set() notice, err := noticeDb.GetNotice(req.Id) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if notice == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } now := time.Now() notice.Name = req.Name notice.Content = req.Content notice.UpdateAt = now.Format("2006-01-02 15:04:05") _, err = noticeDb.NoticeUpdate(notice, "name", "content", "update_at") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } func NoticeDelete(c *gin.Context) { id := c.Param("id") noticeDb := db.NoticeDb{} noticeDb.Set() company, err := noticeDb.GetNotice(utils.StrToInt(id)) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if company == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } _, err = noticeDb.NoticeDelete(id) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return }