|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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 BannerList(c *gin.Context) {
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- banners, err := bannerDb.FindBanner(0, 0)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, map[string]interface{}{
- "list": banners,
- }, nil)
- return
- }
-
- func BannerSort(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
- }
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- banners, err := bannerDb.FindBannerById(args.Ids)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- for k, v := range *banners {
- v.Sort = k
- _, err1 := bannerDb.BannerUpdate(&v, "sort")
- if err1 != nil {
- e.OutErr(c, e.ERR_DB_ORM, err1.Error())
- return
- }
- }
- e.OutSuc(c, "success", nil)
- }
-
- func BannerAdd(c *gin.Context) {
- var req md.BannerAddReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- now := time.Now()
- banner := model.Banner{
- Name: req.Name,
- ImgUrl: req.ImgUrl,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- }
- _, err = bannerDb.BannerInsert(&banner)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func BannerUpdate(c *gin.Context) {
- var req md.BannerUpdateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- banner, err := bannerDb.GetBanner(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if banner == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
- now := time.Now()
- banner.Name = req.Name
- banner.ImgUrl = req.ImgUrl
- banner.UpdateAt = now.Format("2006-01-02 15:04:05")
- _, err = bannerDb.BannerUpdate(banner, "name", "content", "update_at")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func BannerDelete(c *gin.Context) {
- id := c.Param("id")
- bannerDb := db.BannerDb{}
- bannerDb.Set()
- company, err := bannerDb.GetBanner(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 = bannerDb.BannerDelete(id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
|