tg_red_packet_config.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package app
  2. import (
  3. "fmt"
  4. "go_server/base/core"
  5. model "go_server/model/biz_modules/app"
  6. "go_server/model/common/response"
  7. "go_server/service/base"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. type TgRedPacketConfigService struct {
  12. base.BizCommonService
  13. }
  14. // Get 获取单条记录
  15. func (s *TgRedPacketConfigService) Get(c *gin.Context) {
  16. s.SetDbAlias("app")
  17. base.NewBaseHandler(model.NewTgRedPacketConfig()).Get(c, s.DB())
  18. }
  19. // Find 查询列表
  20. func (s *TgRedPacketConfigService) Find(c *gin.Context) {
  21. s.SetDbAlias("app")
  22. type request[T any] struct {
  23. base.ListRequest[T]
  24. Id *int64 `form:"id"`
  25. IsExport *bool `form:"isExport"`
  26. Fields *string `form:"fields"` // 指定返回字段 , 分割
  27. ConfigName *string `form:"configName"`
  28. ConfigType *int8 `form:"configType"`
  29. GroupId *string `form:"groupId"`
  30. PacketType *int8 `form:"packetType"`
  31. Status *int8 `form:"status"`
  32. BeginTime *int64 `form:"beginTime"`
  33. EndTime *int64 `form:"endTime"`
  34. }
  35. req := new(request[model.TgRedPacketConfig])
  36. if err := c.BindQuery(req); err != nil {
  37. response.Resp(c, err.Error())
  38. return
  39. }
  40. db := s.DB()
  41. if req.Id != nil && *req.Id != 0 {
  42. db = db.Where("id", req.Id)
  43. }
  44. if req.ConfigName != nil && *req.ConfigName != "" {
  45. db = db.Where("config_name LIKE ?", "%"+*req.ConfigName+"%")
  46. }
  47. if req.ConfigType != nil {
  48. db = db.Where("config_type", req.ConfigType)
  49. }
  50. if req.GroupId != nil && *req.GroupId != "" {
  51. db = db.Where("group_id", req.GroupId)
  52. }
  53. if req.PacketType != nil {
  54. db = db.Where("packet_type", req.PacketType)
  55. }
  56. if req.Status != nil {
  57. db = db.Where("status", req.Status)
  58. } else {
  59. // 默认不显示已删除的
  60. db = db.Where("status != ?", 3)
  61. }
  62. if req.BeginTime != nil && *req.BeginTime > 0 && req.EndTime != nil && *req.EndTime > 0 {
  63. db = db.Where("created_at between ? and ?", *req.BeginTime, *req.EndTime)
  64. }
  65. // 导出处理
  66. fields := make([]string, 0)
  67. if req.Fields != nil {
  68. fields = strings.Split(*req.Fields, ",")
  69. }
  70. var url string
  71. colInfo := s.GetColumnCommentFromStruct(model.TgRedPacketConfig{})
  72. var err error
  73. if req.IsExport != nil && *req.IsExport {
  74. if len(fields) == 0 {
  75. for _, col := range colInfo {
  76. fields = append(fields, col.Field)
  77. }
  78. }
  79. core.Log.Infof("导出的字段:%s", fields)
  80. url, err = base.ExportCsv[model.TgRedPacketConfig](db, fields, colInfo)
  81. if err != nil {
  82. response.Resp(c, err.Error())
  83. return
  84. }
  85. }
  86. resp, err := base.NewQueryBaseHandler(model.NewTgRedPacketConfig()).List(db, req)
  87. if err != nil {
  88. response.Resp(c, err.Error())
  89. return
  90. }
  91. response.Resp(c, map[string]interface{}{
  92. "url": url,
  93. "cols": colInfo,
  94. "list": resp.List,
  95. "paging": resp.Paging,
  96. })
  97. }
  98. // Comment 获取表字段注释
  99. func (s *TgRedPacketConfigService) Comment(c *gin.Context) {
  100. s.SetDbAlias("app")
  101. dbs, err := s.GetColumnComment("app", model.NewTgRedPacketConfig().TableName())
  102. if err != nil {
  103. response.Resp(c, "获取失败")
  104. return
  105. } else {
  106. response.Resp(c, gin.H{"dbs": dbs})
  107. return
  108. }
  109. }
  110. // Create 创建红包配置
  111. func (s *TgRedPacketConfigService) Create(c *gin.Context) {
  112. s.SetDbAlias("app")
  113. var req model.TgRedPacketConfig
  114. if err := c.ShouldBindJSON(&req); err != nil {
  115. response.Resp(c, err.Error())
  116. return
  117. }
  118. // 验证必填字段
  119. if req.ConfigName == "" {
  120. response.Resp(c, "配置名称不能为空")
  121. return
  122. }
  123. if req.GroupId == "" {
  124. response.Resp(c, "群组ID不能为空")
  125. return
  126. }
  127. if req.ConfigType != 1 && req.ConfigType != 2 {
  128. response.Resp(c, "配置类型错误")
  129. return
  130. }
  131. if req.PacketType != 1 && req.PacketType != 2 {
  132. response.Resp(c, "红包类型错误")
  133. return
  134. }
  135. if req.TotalCount <= 0 {
  136. response.Resp(c, "红包个数必须大于0")
  137. return
  138. }
  139. // 定时红包需要 cron 表达式
  140. if req.ConfigType == 1 && req.CronExpr == "" {
  141. response.Resp(c, "定时红包必须配置Cron表达式")
  142. return
  143. }
  144. // 设置默认值
  145. if req.Status == 0 {
  146. req.Status = 1 // 默认启用
  147. }
  148. if req.Symbol == "" {
  149. req.Symbol = "VND"
  150. }
  151. if req.TimeZone == "" {
  152. req.TimeZone = "Asia/Ho_Chi_Minh"
  153. }
  154. if err := s.DB().Create(&req).Error; err != nil {
  155. core.Log.Errorf("创建红包配置失败: %v", err)
  156. response.Resp(c, "创建失败")
  157. return
  158. }
  159. response.Resp(c, "创建成功")
  160. }
  161. // Update 更新红包配置
  162. func (s *TgRedPacketConfigService) Update(c *gin.Context) {
  163. s.SetDbAlias("app")
  164. var req model.TgRedPacketConfig
  165. if err := c.ShouldBindJSON(&req); err != nil {
  166. response.Resp(c, err.Error())
  167. return
  168. }
  169. if req.Id == 0 {
  170. response.Resp(c, "ID不能为空")
  171. return
  172. }
  173. // 验证配置是否存在
  174. var exist model.TgRedPacketConfig
  175. if err := s.DB().Where("id = ?", req.Id).First(&exist).Error; err != nil {
  176. response.Resp(c, "配置不存在")
  177. return
  178. }
  179. // 更新
  180. if err := s.DB().Model(&model.TgRedPacketConfig{}).Where("id = ?", req.Id).Updates(&req).Error; err != nil {
  181. core.Log.Errorf("更新红包配置失败: %v", err)
  182. response.Resp(c, "更新失败")
  183. return
  184. }
  185. response.Resp(c, "更新成功")
  186. }
  187. // Delete 删除红包配置(软删除)
  188. func (s *TgRedPacketConfigService) Delete(c *gin.Context) {
  189. s.SetDbAlias("app")
  190. type request struct {
  191. Id int64 `json:"id" binding:"required"`
  192. }
  193. var req request
  194. if err := c.ShouldBindJSON(&req); err != nil {
  195. response.Resp(c, err.Error())
  196. return
  197. }
  198. // 软删除:将状态改为3
  199. if err := s.DB().Model(&model.TgRedPacketConfig{}).Where("id = ?", req.Id).Update("status", 3).Error; err != nil {
  200. core.Log.Errorf("删除红包配置失败: %v", err)
  201. response.Resp(c, "删除失败")
  202. return
  203. }
  204. response.Resp(c, "删除成功")
  205. }
  206. // ToggleStatus 切换状态(启用/禁用)
  207. func (s *TgRedPacketConfigService) ToggleStatus(c *gin.Context) {
  208. s.SetDbAlias("app")
  209. type request struct {
  210. Id int64 `json:"id" binding:"required"`
  211. Status int8 `json:"status" binding:"required"`
  212. }
  213. var req request
  214. if err := c.ShouldBindJSON(&req); err != nil {
  215. response.Resp(c, err.Error())
  216. return
  217. }
  218. if req.Status != 1 && req.Status != 2 {
  219. response.Resp(c, "状态值错误")
  220. return
  221. }
  222. if err := s.DB().Model(&model.TgRedPacketConfig{}).Where("id = ?", req.Id).Update("status", req.Status).Error; err != nil {
  223. core.Log.Errorf("切换状态失败: %v", err)
  224. response.Resp(c, "操作失败")
  225. return
  226. }
  227. statusText := "禁用"
  228. if req.Status == 1 {
  229. statusText = "启用"
  230. }
  231. response.Resp(c, fmt.Sprintf("%s成功", statusText))
  232. }