banner.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package cygx
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "time"
  6. )
  7. type CygxBanner struct {
  8. BannerId int `orm:"column(banner_id);pk"`
  9. ImgId int `description:"cygx_banner_img主键ID"`
  10. ListType string `description:"ABC哪一列"`
  11. BannerType int `description:"1:现有报告、2:自拟报告、3:视频、4:音频、5:活动"`
  12. Title string `description:"标题"`
  13. Subtitle string `description:"副标题"`
  14. Sort int `description:"排序"`
  15. Link string `description:"链接地址"`
  16. AdminId int `description:"管理员ID"`
  17. Status int `description:"0:未发布,1:已发布"`
  18. CreateTime time.Time `description:"创建时间"`
  19. ModifyTime time.Time `description:"更改时间"`
  20. }
  21. type CygxBannerReq struct {
  22. BannerId int `description:"BannerId等于0新增,大于零修改"`
  23. ImgId int `description:"cygx_banner_img主键ID"`
  24. ListType string `description:"ABC哪一列"`
  25. BannerType int `description:"1:现有报告、2:自拟报告、3:视频、4:音频、5:活动"`
  26. Title string `description:"标题"`
  27. Subtitle string `description:"副标题"`
  28. Sort int `description:"排序"`
  29. Link string `description:"链接地址"`
  30. }
  31. // 添加
  32. func AddCygxBanner(item *CygxBanner) (err error) {
  33. o := orm.NewOrmUsingDB("hz_cygx")
  34. _, err = o.Insert(item)
  35. return
  36. }
  37. // 修改
  38. func UpdateCygxBanner(item *CygxBanner) (err error) {
  39. to := orm.NewOrmUsingDB("hz_cygx")
  40. updateParams := make(map[string]interface{})
  41. updateParams["ImgId"] = item.ImgId
  42. updateParams["ListType"] = item.ListType
  43. updateParams["BannerType"] = item.BannerType
  44. updateParams["Title"] = item.Title
  45. updateParams["Subtitle"] = item.Subtitle
  46. updateParams["Sort"] = item.Sort
  47. updateParams["Link"] = item.Link
  48. updateParams["Status"] = item.Status
  49. updateParams["ModifyTime"] = time.Now()
  50. ptrStructOrTableName := "cygx_banner"
  51. whereParam := map[string]interface{}{"banner_id": item.BannerId}
  52. qs := to.QueryTable(ptrStructOrTableName)
  53. for expr, exprV := range whereParam {
  54. qs = qs.Filter(expr, exprV)
  55. }
  56. _, err = qs.Update(updateParams)
  57. return
  58. }
  59. type CygxBannerResp struct {
  60. BannerId int `description:"BannerId等于0新增,大于零修改"`
  61. ImgId int `description:"cygx_banner_img主键ID"`
  62. IndexImg string `description:"小程序封面图"`
  63. ListType string `description:"ABC哪一列"`
  64. BannerType int `description:"添加类型,1:现有报告、2:自拟报告、3:视频、4:音频、5:活动"`
  65. BannerTypeName string `description:"添加类型名称"`
  66. Title string `description:"标题"`
  67. Subtitle string `description:"副标题"`
  68. Sort int `description:"排序"`
  69. Status int `description:"0:未发布,1:已发布"`
  70. Link string `description:"链接地址"`
  71. ModifyTime string `description:"更改时间"`
  72. Pv int `description:"Pv"`
  73. Uv int `description:"Uv"`
  74. }
  75. type CygxBannerIdReq struct {
  76. BannerId int `description:"BannerId"`
  77. }
  78. type CygxBannerMoveReq struct {
  79. BannerId int `description:"BannerId"`
  80. PreviousBannerId int `description:"上一个BannerId"`
  81. NextBannerId int `description:"下一个BannerId"`
  82. ListType string `description:"ABC哪一列"`
  83. }
  84. type GetCygxBannerImgRespDetailResp struct {
  85. Detail *CygxBannerResp
  86. }
  87. // 通过ID获取详情
  88. func GetCygxBannerDetail(banneId int) (item *CygxBannerResp, err error) {
  89. o := orm.NewOrmUsingDB("hz_cygx")
  90. sql := `SELECT * FROM cygx_banner WHERE banner_id=? `
  91. err = o.Raw(sql, banneId).QueryRow(&item)
  92. return
  93. }
  94. // 获取某一列,最大的排序值
  95. func GetCygxBannerDetailByListTypeMaxSort(listType string) (item *CygxBanner, err error) {
  96. o := orm.NewOrmUsingDB("hz_cygx")
  97. sql := `SELECT * FROM cygx_banner WHERE list_type = ? ORDER BY sort DESC LIMIT 1 `
  98. err = o.Raw(sql, listType).QueryRow(&item)
  99. return
  100. }
  101. // UpdateCygxBannerRespMulti 批量修改banner排序
  102. func UpdateCygxBannerMulti(items []*CygxBanner) (err error) {
  103. o := orm.NewOrmUsingDB("hz_cygx")
  104. p, err := o.Raw(` UPDATE cygx_banner SET sort = ? WHERE banner_id = ? `).Prepare()
  105. if err != nil {
  106. return
  107. }
  108. defer func() {
  109. _ = p.Close() // 别忘记关闭 statement
  110. }()
  111. for _, v := range items {
  112. _, err = p.Exec(
  113. v.Sort,
  114. v.BannerId)
  115. if err != nil {
  116. return
  117. }
  118. }
  119. return
  120. }
  121. // 修改是否展示
  122. func EditCygxBannerStatus(status, banneId int) (err error) {
  123. o := orm.NewOrmUsingDB("hz_cygx")
  124. sql := `UPDATE cygx_banner SET status=?, modify_time=NOW() WHERE banner_id=? `
  125. _, err = o.Raw(sql, status, banneId).Exec()
  126. return
  127. }
  128. // 获取数量
  129. func GetCygxBannerCount(condition string, pars []interface{}) (count int, err error) {
  130. sqlCount := ` SELECT COUNT(1) AS count FROM cygx_banner as art WHERE 1= 1 `
  131. if condition != "" {
  132. sqlCount += condition
  133. }
  134. o := orm.NewOrmUsingDB("hz_cygx")
  135. err = o.Raw(sqlCount, pars).QueryRow(&count)
  136. return
  137. }
  138. // 列表
  139. func GetCygxBannerList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxBannerResp, err error) {
  140. o := orm.NewOrmUsingDB("hz_cygx")
  141. sql := `SELECT * FROM cygx_banner as art WHERE 1= 1 `
  142. if condition != "" {
  143. sql += condition
  144. }
  145. sql += ` LIMIT ?,? `
  146. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  147. return
  148. }
  149. type CygxBannerListResp struct {
  150. Paging *paging.PagingItem `description:"分页数据"`
  151. List []*CygxBannerResp
  152. }
  153. // 移动列数并修改排序
  154. func EditCygxBannerMove(sort, banneId int, listType string) (err error) {
  155. o := orm.NewOrmUsingDB("hz_cygx")
  156. to, err := o.Begin()
  157. if err != nil {
  158. return
  159. }
  160. defer func() {
  161. if err != nil {
  162. _ = to.Rollback()
  163. } else {
  164. _ = to.Commit()
  165. }
  166. }()
  167. //更改上一个节点的排序
  168. sql := `UPDATE cygx_banner SET sort = sort +1 WHERE status = 1 AND sort <=? AND list_type =? `
  169. _, err = to.Raw(sql, sort, listType).Exec()
  170. if err != nil {
  171. return
  172. }
  173. //更改下一个节点的排序
  174. sql = `UPDATE cygx_banner SET sort = sort +3 WHERE status = 1 AND sort >? AND list_type =? `
  175. _, err = to.Raw(sql, sort, listType).Exec()
  176. if err != nil {
  177. return
  178. }
  179. //更改移动对象的排序
  180. sql = `UPDATE cygx_banner SET sort = ?, list_type =?, modify_time=NOW() WHERE banner_id=? `
  181. _, err = to.Raw(sql, sort+2, listType, banneId).Exec()
  182. if err != nil {
  183. return
  184. }
  185. return
  186. }
  187. // 移动列数并修改排序
  188. func UpdateCygxBannerlistType(sort, banneId int, listType string) (err error) {
  189. o := orm.NewOrmUsingDB("hz_cygx")
  190. sql := `UPDATE cygx_banner SET sort = ?, list_type =?, modify_time=NOW() WHERE banner_id=? `
  191. _, err = o.Raw(sql, sort, listType, banneId).Exec()
  192. return
  193. }