view.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package service
  2. import (
  3. "context"
  4. "mime/multipart"
  5. "time"
  6. "go-common/app/admin/main/coupon/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. )
  12. //CouponViewBatchAdd view batch add.
  13. func (s *Service) CouponViewBatchAdd(c context.Context, arg *model.ArgCouponViewBatch) (err error) {
  14. if arg.StartTime > arg.ExpireTime {
  15. err = ecode.CouPonBatchTimeErr
  16. return
  17. }
  18. arg.BatchToken = s.token()
  19. arg.CouponType = model.CouponVideo
  20. if err = s.dao.AddViewBatch(c, arg); err != nil {
  21. err = errors.WithStack(err)
  22. }
  23. return
  24. }
  25. //CouponViewbatchSave view batch save.
  26. func (s *Service) CouponViewbatchSave(c context.Context, arg *model.ArgCouponViewBatch) (err error) {
  27. if err = s.dao.UpdateViewBatch(c, arg); err != nil {
  28. err = errors.WithStack(err)
  29. }
  30. return
  31. }
  32. //CouponViewBlock view block
  33. func (s *Service) CouponViewBlock(c context.Context, mid int64, couponToken string) (err error) {
  34. var (
  35. r *model.CouponInfo
  36. tx *sql.Tx
  37. )
  38. if tx, err = s.dao.BeginTran(c); err != nil {
  39. err = errors.WithStack(err)
  40. return
  41. }
  42. defer func() {
  43. if err != nil {
  44. tx.Rollback()
  45. return
  46. }
  47. tx.Commit()
  48. s.dao.DelCouponTypeCache(context.Background(), mid, model.CouponVideo)
  49. }()
  50. if r, err = s.dao.CouponViewInfo(c, couponToken, mid); err != nil {
  51. err = errors.WithStack(err)
  52. return
  53. }
  54. if r.State != model.NotUsed {
  55. err = ecode.CouponInfoStateBlockErr
  56. return
  57. }
  58. if err = s.dao.TxUpdateViewInfo(tx, model.Block, couponToken, mid); err != nil {
  59. err = errors.WithStack(err)
  60. return
  61. }
  62. olog := new(model.CouponChangeLog)
  63. olog.Mid = mid
  64. olog.CouponToken = couponToken
  65. olog.State = model.Block
  66. if err = s.dao.TxCouponViewLog(tx, olog); err != nil {
  67. err = errors.WithStack(err)
  68. }
  69. return
  70. }
  71. //CouponViewUnblock view unblock
  72. func (s *Service) CouponViewUnblock(c context.Context, mid int64, couponToken string) (err error) {
  73. var (
  74. r *model.CouponInfo
  75. tx *sql.Tx
  76. )
  77. if tx, err = s.dao.BeginTran(c); err != nil {
  78. err = errors.WithStack(err)
  79. return
  80. }
  81. defer func() {
  82. if err != nil {
  83. tx.Rollback()
  84. return
  85. }
  86. tx.Commit()
  87. s.dao.DelCouponTypeCache(context.Background(), mid, model.CouponVideo)
  88. }()
  89. if r, err = s.dao.CouponViewInfo(c, couponToken, mid); err != nil {
  90. err = errors.WithStack(err)
  91. return
  92. }
  93. if r.State != model.Block {
  94. err = ecode.CouponInfoStateUnblockErr
  95. return
  96. }
  97. if err = s.dao.TxUpdateViewInfo(tx, model.NotUsed, couponToken, mid); err != nil {
  98. err = errors.WithStack(err)
  99. return
  100. }
  101. olog := new(model.CouponChangeLog)
  102. olog.Mid = mid
  103. olog.CouponToken = couponToken
  104. olog.State = model.NotUsed
  105. if err = s.dao.TxCouponViewLog(tx, olog); err != nil {
  106. err = errors.WithStack(err)
  107. }
  108. return
  109. }
  110. //CouponViewList view list.
  111. func (s *Service) CouponViewList(c context.Context, arg *model.ArgSearchCouponView) (res []*model.CouponInfo, count int64, err error) {
  112. var (
  113. bls []*model.CouponBatchInfo
  114. )
  115. if arg.AppID > 0 || len(arg.BatchToken) > 0 {
  116. if bls, err = s.dao.BatchViewList(c, arg.AppID, arg.BatchToken, model.CouponVideo); err != nil {
  117. err = errors.WithStack(err)
  118. return
  119. }
  120. if len(bls) == 0 {
  121. return
  122. }
  123. for _, v := range bls {
  124. arg.BatchTokens = append(arg.BatchTokens, v.BatchToken)
  125. }
  126. }
  127. if count, err = s.dao.SearchViewCouponCount(c, arg); err != nil {
  128. err = errors.WithStack(err)
  129. return
  130. }
  131. if count <= 0 {
  132. return
  133. }
  134. if res, err = s.dao.SearchViewCouponInfo(c, arg); err != nil {
  135. err = errors.WithStack(err)
  136. return
  137. }
  138. now := time.Now()
  139. for _, v := range res {
  140. if v.OID == 0 {
  141. v.Mtime = 0
  142. } else {
  143. var pgc *model.PGCInfoResq
  144. if pgc, err = s.dao.GetPGCInfo(c, v.OID); err != nil {
  145. log.Error("get pgc info coupon%+v error:%+v", v, err)
  146. continue
  147. }
  148. if pgc != nil {
  149. v.Title = pgc.Title
  150. }
  151. }
  152. if len(v.BatchToken) > 0 {
  153. var cbi *model.CouponBatchInfo
  154. if cbi, err = s.dao.BatchInfo(c, v.BatchToken); err != nil {
  155. err = errors.WithStack(err)
  156. return
  157. }
  158. v.BatchName = cbi.Name
  159. }
  160. if v.ExpireTime < now.Unix() && v.State == model.NotUsed {
  161. v.State = model.Expire
  162. }
  163. }
  164. return
  165. }
  166. //CouponViewSalary view salary.
  167. func (s *Service) CouponViewSalary(c context.Context, f multipart.File, h *multipart.FileHeader, mids []int64, token string) (count int, err error) {
  168. var bi *model.CouponBatchInfo
  169. if len(mids) == 0 {
  170. if mids, err = s.ReadCsv(f, h); err != nil {
  171. err = errors.WithStack(err)
  172. return
  173. }
  174. }
  175. if len(mids) == 0 {
  176. err = ecode.CouponBatchSalaryCountZeroErr
  177. return
  178. }
  179. if len(mids) > _maxSalaryCount {
  180. err = ecode.CouponBatchSalaryLimitErr
  181. return
  182. }
  183. if bi, err = s.dao.BatchInfo(c, token); err != nil {
  184. err = errors.WithStack(err)
  185. return
  186. }
  187. if bi == nil {
  188. err = ecode.CouPonTokenNotFoundErr
  189. return
  190. }
  191. if bi.State != model.BatchStateNormal {
  192. err = ecode.CouPonHadBlockErr
  193. return
  194. }
  195. if bi.MaxCount != _notLimitSalary && len(mids) > int(bi.MaxCount-bi.CurrentCount) {
  196. err = ecode.CouponBatchSalaryLimitErr
  197. return
  198. }
  199. s.RunSalaryCoupon(c, mids, token, bi.AppID, model.CouponVideo, model.SystemAdminSalary, "")
  200. count = len(mids)
  201. return
  202. }