tg_red_packet_config.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package app
  2. import (
  3. "go_server/base/core"
  4. model "go_server/model/biz_modules/app"
  5. "go_server/model/common/response"
  6. "go_server/service/base"
  7. "strconv"
  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. // 批量填充群组名称:从 magic_tg_group 表查 title
  92. chatIds := make([]int64, 0, len(resp.List))
  93. for _, item := range resp.List {
  94. if item.GroupId != "" {
  95. if chatId, err := strconv.ParseInt(item.GroupId, 10, 64); err == nil {
  96. chatIds = append(chatIds, chatId)
  97. }
  98. }
  99. }
  100. groupNameMap := make(map[string]string)
  101. if len(chatIds) > 0 {
  102. var groups []model.TgGroup
  103. s.DB().Where("chat_id IN ?", chatIds).Find(&groups)
  104. for _, g := range groups {
  105. groupNameMap[strconv.FormatInt(g.ChatId, 10)] = g.Title
  106. }
  107. }
  108. // 填充 group_name
  109. for i := range resp.List {
  110. if resp.List[i].GroupName == "" {
  111. if title, ok := groupNameMap[resp.List[i].GroupId]; ok {
  112. resp.List[i].GroupName = title
  113. }
  114. }
  115. }
  116. response.Resp(c, map[string]interface{}{
  117. "url": url,
  118. "cols": colInfo,
  119. "list": resp.List,
  120. "paging": resp.Paging,
  121. })
  122. }
  123. // Comment 获取表字段注释
  124. func (s *TgRedPacketConfigService) Comment(c *gin.Context) {
  125. s.SetDbAlias("app")
  126. dbs, err := s.GetColumnComment("app", model.NewTgRedPacketConfig().TableName())
  127. if err != nil {
  128. response.Resp(c, "获取失败")
  129. return
  130. } else {
  131. response.Resp(c, gin.H{"dbs": dbs})
  132. return
  133. }
  134. }
  135. // Create 创建红包配置
  136. func (s *TgRedPacketConfigService) Create(c *gin.Context) {
  137. s.SetDbAlias("app")
  138. var req model.TgRedPacketConfig
  139. if err := c.ShouldBindJSON(&req); err != nil {
  140. response.Resp(c, err.Error())
  141. return
  142. }
  143. // 验证必填字段
  144. if req.ConfigName == "" {
  145. response.Resp(c, "配置名称不能为空")
  146. return
  147. }
  148. if req.GroupId == "" {
  149. response.Resp(c, "群组ID不能为空")
  150. return
  151. }
  152. if req.ConfigType != 1 && req.ConfigType != 2 {
  153. response.Resp(c, "配置类型错误")
  154. return
  155. }
  156. if req.PacketType != 1 && req.PacketType != 2 {
  157. response.Resp(c, "红包类型错误")
  158. return
  159. }
  160. if req.TotalCount <= 0 {
  161. response.Resp(c, "红包个数必须大于0")
  162. return
  163. }
  164. // 定时红包需要 cron 表达式
  165. if req.ConfigType == 1 && req.CronExpr == "" {
  166. response.Resp(c, "定时红包必须配置Cron表达式")
  167. return
  168. }
  169. // 设置默认值
  170. if req.Status == 0 {
  171. req.Status = 1 // 默认启用
  172. }
  173. if req.Symbol == "" {
  174. req.Symbol = "VND"
  175. }
  176. if req.TimeZone == "" {
  177. req.TimeZone = "Asia/Ho_Chi_Minh"
  178. }
  179. if err := s.DB().Create(&req).Error; err != nil {
  180. core.Log.Errorf("创建红包配置失败: %v", err)
  181. response.Resp(c, "创建失败")
  182. return
  183. }
  184. response.Resp(c)
  185. }
  186. // Update 更新红包配置
  187. func (s *TgRedPacketConfigService) Update(c *gin.Context) {
  188. s.SetDbAlias("app")
  189. var req model.TgRedPacketConfig
  190. if err := c.ShouldBindJSON(&req); err != nil {
  191. response.Resp(c, err.Error())
  192. return
  193. }
  194. if req.Id == 0 {
  195. response.Resp(c, "ID不能为空")
  196. return
  197. }
  198. // 验证配置是否存在
  199. var exist model.TgRedPacketConfig
  200. if err := s.DB().Where("id = ?", req.Id).First(&exist).Error; err != nil {
  201. response.Resp(c, "配置不存在")
  202. return
  203. }
  204. // 使用 map 更新,这样 nil 值也能正确清空日期字段
  205. updates := map[string]interface{}{
  206. "config_name": req.ConfigName,
  207. "config_type": req.ConfigType,
  208. "group_id": req.GroupId,
  209. "group_name": req.GroupName,
  210. "packet_type": req.PacketType,
  211. "total_amount": req.TotalAmount,
  212. "total_count": req.TotalCount,
  213. "symbol": req.Symbol,
  214. "expire_minutes": req.ExpireMinutes,
  215. "max_grab_amount": req.MaxGrabAmount,
  216. "lang": req.Lang,
  217. "blessing_words": req.BlessingWords,
  218. "cron_expr": req.CronExpr,
  219. "time_zone": req.TimeZone,
  220. "start_date": req.StartDate,
  221. "end_date": req.EndDate,
  222. "remark": req.Remark,
  223. }
  224. if err := s.DB().Model(&model.TgRedPacketConfig{}).Where("id = ?", req.Id).Updates(updates).Error; err != nil {
  225. core.Log.Errorf("更新红包配置失败: %v", err)
  226. response.Resp(c, "更新失败")
  227. return
  228. }
  229. response.Resp(c)
  230. }
  231. // Delete 删除红包配置(软删除)
  232. func (s *TgRedPacketConfigService) Delete(c *gin.Context) {
  233. s.SetDbAlias("app")
  234. type request struct {
  235. Id int64 `json:"id" binding:"required"`
  236. }
  237. var req request
  238. if err := c.ShouldBindJSON(&req); err != nil {
  239. response.Resp(c, err.Error())
  240. return
  241. }
  242. // 软删除:将状态改为3
  243. if err := s.DB().Model(&model.TgRedPacketConfig{}).Where("id = ?", req.Id).Update("status", 3).Error; err != nil {
  244. core.Log.Errorf("删除红包配置失败: %v", err)
  245. response.Resp(c, "删除失败")
  246. return
  247. }
  248. response.Resp(c)
  249. }
  250. // ToggleStatus 切换状态(启用/禁用)
  251. func (s *TgRedPacketConfigService) ToggleStatus(c *gin.Context) {
  252. s.SetDbAlias("app")
  253. type request struct {
  254. Id int64 `json:"id" binding:"required"`
  255. Status int8 `json:"status" binding:"required"`
  256. }
  257. var req request
  258. if err := c.ShouldBindJSON(&req); err != nil {
  259. response.Resp(c, err.Error())
  260. return
  261. }
  262. if req.Status != 1 && req.Status != 2 {
  263. response.Resp(c, "状态值错误")
  264. return
  265. }
  266. if err := s.DB().Model(&model.TgRedPacketConfig{}).Where("id = ?", req.Id).Update("status", req.Status).Error; err != nil {
  267. core.Log.Errorf("切换状态失败: %v", err)
  268. response.Resp(c, "操作失败")
  269. return
  270. }
  271. response.Resp(c)
  272. }