push.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/vip/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. "github.com/pkg/errors"
  12. )
  13. // const .
  14. const (
  15. _linkTypeH5 = 7
  16. _linkTypeApp = 10
  17. )
  18. // SavePushData save push data
  19. func (s *Service) SavePushData(c context.Context, arg *model.VipPushData) (err error) {
  20. if err = s.checkPushData(arg); err != nil {
  21. err = errors.WithStack(err)
  22. return
  23. }
  24. if arg.ID == 0 {
  25. arg.ProgressStatus = model.NotStart
  26. arg.Status = model.Normal
  27. if _, err = s.dao.AddPushData(c, arg); err != nil {
  28. err = errors.WithStack(err)
  29. }
  30. return
  31. }
  32. var pushData *model.VipPushData
  33. if pushData, err = s.dao.GetPushData(c, arg.ID); err != nil {
  34. err = errors.WithStack(err)
  35. return
  36. }
  37. if pushData == nil {
  38. err = ecode.VipPushDataNotExitErr
  39. return
  40. }
  41. if pushData.Status == model.Fail {
  42. err = ecode.VipPushDataUpdateErr
  43. return
  44. }
  45. if pushData.ProgressStatus == model.Started {
  46. err = ecode.VipPushDataUpdateErr
  47. return
  48. }
  49. if pushData.GroupName != arg.GroupName || !pushData.EffectStartDate.Time().Equal(arg.EffectStartDate.Time()) {
  50. if !(pushData.Status == model.Normal && pushData.ProgressStatus == model.NotStart) {
  51. err = ecode.VipPushDataUpdateErr
  52. return
  53. }
  54. }
  55. arg.ProgressStatus = pushData.ProgressStatus
  56. if pushData.PushedCount == arg.PushTotalCount {
  57. arg.ProgressStatus = model.Started
  58. }
  59. if _, err = s.dao.UpdatePushData(c, arg); err != nil {
  60. err = errors.WithStack(err)
  61. }
  62. return
  63. }
  64. func (s *Service) checkPushData(arg *model.VipPushData) (err error) {
  65. if len(arg.GroupName) > 10 {
  66. err = ecode.VipPushGroupLenErr
  67. return
  68. }
  69. if len(arg.Title) > 30 {
  70. err = ecode.VipPushTitleLenErr
  71. return
  72. }
  73. if len(arg.Content) > 200 {
  74. err = ecode.VipPushContentLenErr
  75. return
  76. }
  77. if arg.LinkType != _linkTypeH5 && arg.LinkType != _linkTypeApp {
  78. err = ecode.VipPushLinkTypeErr
  79. return
  80. }
  81. if arg.EffectEndDate.Time().Before(arg.EffectStartDate.Time()) {
  82. err = ecode.VipPushEffectTimeErr
  83. return
  84. }
  85. duration := arg.EffectEndDate.Time().Sub(arg.EffectStartDate.Time())
  86. day := duration.Hours() / 24
  87. arg.PushTotalCount = int32(day) + 1
  88. if _, err = time.Parse("15:04:05", arg.PushStartTime); err != nil {
  89. err = ecode.VipPushFmtTimeErr
  90. return
  91. }
  92. if _, err = time.Parse("15:04:05", arg.PushEndTime); err != nil {
  93. err = ecode.VipPushFmtTimeErr
  94. return
  95. }
  96. platformMap := make(map[string]*model.PushDataPlatform)
  97. platformArr := make([]*model.PushDataPlatform, 0)
  98. var (
  99. data []byte
  100. key string
  101. condition string
  102. ok bool
  103. )
  104. if err = json.Unmarshal([]byte(arg.Platform), &platformArr); err != nil {
  105. log.Error("error(%+v)", err)
  106. err = ecode.VipPushPlatformErr
  107. return
  108. }
  109. for _, v := range platformArr {
  110. if key, ok = model.PushPlatformNameMap[v.Name]; !ok {
  111. err = ecode.VipPushPlatformErr
  112. return
  113. }
  114. if condition, ok = model.ConditionNameMap[v.Condition]; !ok {
  115. err = ecode.VipPushPlatformErr
  116. return
  117. }
  118. v.Condition = condition
  119. platformMap[key] = v
  120. }
  121. if data, err = json.Marshal(platformMap); err != nil {
  122. err = errors.WithStack(err)
  123. return
  124. }
  125. arg.Platform = string(data)
  126. return
  127. }
  128. // GetPushData get push data
  129. func (s *Service) GetPushData(c context.Context, id int64) (res *model.VipPushData, err error) {
  130. if res, err = s.dao.GetPushData(c, id); err != nil {
  131. err = errors.WithStack(err)
  132. return
  133. }
  134. if res == nil {
  135. return
  136. }
  137. res.PushProgress = fmt.Sprintf("%v/%v", res.PushedCount, res.PushTotalCount)
  138. if err = s.fmtPushDataPlatform(res); err != nil {
  139. err = errors.WithStack(err)
  140. }
  141. return
  142. }
  143. // DisablePushData .
  144. func (s *Service) DisablePushData(c context.Context, id int64) (err error) {
  145. var (
  146. res *model.VipPushData
  147. now = time.Now()
  148. )
  149. if res, err = s.dao.GetPushData(c, id); err != nil {
  150. err = errors.WithStack(err)
  151. return
  152. }
  153. if res == nil {
  154. err = ecode.VipPushDataNotExitErr
  155. return
  156. }
  157. if !(res.ProgressStatus == model.Starting && res.Status == model.Normal && res.DisableType == model.UnDisable) {
  158. err = ecode.VipPushDataDisableErr
  159. return
  160. }
  161. duration := now.Sub(res.EffectStartDate.Time())
  162. day := duration.Hours() / 24
  163. res.PushTotalCount = int32(day) + 1
  164. res.EffectEndDate = xtime.Time(now.Unix())
  165. if res.PushTotalCount > res.PushedCount {
  166. res.PushTotalCount--
  167. }
  168. if res.PushTotalCount == res.PushedCount {
  169. res.ProgressStatus = model.Started
  170. }
  171. if err = s.dao.DisablePushData(c, res); err != nil {
  172. err = errors.WithStack(err)
  173. }
  174. return
  175. }
  176. // DelPushData .
  177. func (s *Service) DelPushData(c context.Context, id int64) (err error) {
  178. var res *model.VipPushData
  179. if res, err = s.dao.GetPushData(c, id); err != nil {
  180. err = errors.WithStack(err)
  181. return
  182. }
  183. if res == nil {
  184. err = ecode.VipPushDataNotExitErr
  185. return
  186. }
  187. if !(res.ProgressStatus == model.NotStart && res.Status == model.Normal) {
  188. err = ecode.VipPushDataDelErr
  189. return
  190. }
  191. if err = s.dao.DelPushData(c, id); err != nil {
  192. err = errors.WithStack(err)
  193. }
  194. return
  195. }
  196. // PushDatas get push datas
  197. func (s *Service) PushDatas(c context.Context, arg *model.ArgPushData) (res []*model.VipPushData, count int64, err error) {
  198. if count, err = s.dao.PushDataCount(c, arg); err != nil {
  199. err = errors.WithStack(err)
  200. return
  201. }
  202. if res, err = s.dao.PushDatas(c, arg); err != nil {
  203. err = errors.WithStack(err)
  204. return
  205. }
  206. for _, v := range res {
  207. v.PushProgress = fmt.Sprintf("%v/%v", v.PushedCount, v.PushTotalCount)
  208. }
  209. return
  210. }
  211. func (s *Service) fmtPushDataPlatform(res *model.VipPushData) (err error) {
  212. var platformArr []*model.PushDataPlatform
  213. platform := make(map[string]*model.PushDataPlatform)
  214. if err = json.Unmarshal([]byte(res.Platform), &platform); err != nil {
  215. err = errors.WithStack(err)
  216. return
  217. }
  218. for k, v := range platform {
  219. r := new(model.PushDataPlatform)
  220. r.Name = model.PushPlatformMap[k]
  221. r.Build = v.Build
  222. r.Condition = model.ConditionMap[v.Condition]
  223. platformArr = append(platformArr, r)
  224. }
  225. res.PlatformArr = platformArr
  226. return
  227. }