welfare.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "net/url"
  7. "strings"
  8. "go-common/app/admin/main/vip/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/time"
  12. )
  13. const (
  14. _delete = 1
  15. _maxRows = 20000
  16. )
  17. // WelfareTypeSave save welfare type
  18. func (s *Service) WelfareTypeSave(id int, name, username string) (err error) {
  19. wt := new(model.WelfareType)
  20. wt.ID = id
  21. wt.Name = name
  22. wt.OperName = username
  23. if id == 0 {
  24. if err = s.dao.WelfareTypeAdd(wt); err != nil {
  25. log.Error("WelfareTypeAdd(%v) Error(%v)", wt, err)
  26. }
  27. } else {
  28. if err = s.dao.WelfareTypeUpd(wt); err != nil {
  29. log.Error("WelfareTypeUpd(%v) Error(%v)", wt, err)
  30. }
  31. }
  32. return
  33. }
  34. // WelfareTypeState delete welfare type
  35. func (s *Service) WelfareTypeState(c context.Context, id int, username string) (err error) {
  36. tx := s.dao.BeginGormTran(c)
  37. if err = s.dao.WelfareTypeState(tx, id, _delete, 0, username); err != nil {
  38. log.Error("WelfareTypeState id(%v) Error(%v)", id, err)
  39. tx.Rollback()
  40. return
  41. }
  42. if err = s.dao.ResetWelfareTid(tx, id); err != nil {
  43. log.Error("ResetWelfareTid tid(%v) Error(%v)", id, err)
  44. tx.Rollback()
  45. return
  46. }
  47. return tx.Commit().Error
  48. }
  49. // WelfareTypeList get welfare type list
  50. func (s *Service) WelfareTypeList() (wts []*model.WelfareTypeRes, err error) {
  51. if wts, err = s.dao.WelfareTypeList(); err != nil {
  52. log.Error("WelfareTypeList Error(%v)", err)
  53. }
  54. return
  55. }
  56. // WelfareSave save welfare
  57. func (s *Service) WelfareSave(username string, req *model.WelfareReq) (err error) {
  58. var (
  59. burl string
  60. hurl string
  61. )
  62. wf := new(model.Welfare)
  63. copyFiled(wf, req)
  64. wf.OperName = username
  65. if burl, err = getRelativePath(wf.BackdropUri); err != nil {
  66. return
  67. }
  68. wf.BackdropUri = burl
  69. if hurl, err = getRelativePath(wf.HomepageUri); err != nil {
  70. return
  71. }
  72. wf.HomepageUri = hurl
  73. if req.ID == 0 {
  74. if err = s.dao.WelfareAdd(wf); err != nil {
  75. log.Error("WelfareAdd(%v) Error(%v)", wf, err)
  76. }
  77. } else {
  78. req.BackdropUri = burl
  79. req.HomepageUri = hurl
  80. if err = s.dao.WelfareUpd(req); err != nil {
  81. log.Error("WelfareUpd(%v) Error(%v)", wf, err)
  82. }
  83. }
  84. return
  85. }
  86. // WelfareState delete welfare
  87. func (s *Service) WelfareState(id int, username string) (err error) {
  88. if err = s.dao.WelfareState(id, _delete, 0, username); err != nil {
  89. log.Error("WelfareState(%v) Error(%v)", id, err)
  90. }
  91. return
  92. }
  93. // WelfareList get welfare list
  94. func (s *Service) WelfareList(tid int) (ws []*model.WelfareRes, err error) {
  95. if ws, err = s.dao.WelfareList(tid); err != nil {
  96. log.Error("WelfareList tid(%v) Error(%v)", tid, err)
  97. return
  98. }
  99. randomBFSHost := fmt.Sprintf(s.c.Property.WelfareBgHost, rand.Intn(3))
  100. for _, w := range ws {
  101. wbs, err := s.dao.WelfareBatchList(w.ID)
  102. if err != nil {
  103. log.Error("WelfareBatchList wid(%v) Error(%v)", w.ID, err)
  104. return ws, err
  105. }
  106. w.HomepageUri = fmt.Sprintf("%v%v", randomBFSHost, w.HomepageUri)
  107. w.BackdropUri = fmt.Sprintf("%v%v", randomBFSHost, w.BackdropUri)
  108. for _, wb := range wbs {
  109. w.ReceivedCount += wb.ReceivedCount
  110. w.Count += wb.Count
  111. }
  112. }
  113. return
  114. }
  115. // WelfareBatchUpload save upload welfare code
  116. func (s *Service) WelfareBatchUpload(body []byte, name, username string, wid, vtime int) (err error) {
  117. wcb := new(model.WelfareCodeBatch)
  118. wcb.BatchName = name
  119. wcb.Wid = wid
  120. wcb.Vtime = time.Time(vtime)
  121. wcb.OperName = username
  122. wcs := make([]*model.WelfareCode, 0)
  123. str := string(body)
  124. for _, lineStr := range strings.Split(str, "\n") {
  125. lineStr = strings.TrimSpace(lineStr)
  126. if lineStr == "" {
  127. continue
  128. }
  129. wc := new(model.WelfareCode)
  130. wc.Wid = wid
  131. wc.Code = lineStr
  132. wcs = append(wcs, wc)
  133. }
  134. wcb.Count = len(wcs)
  135. if wcb.Count > _maxRows {
  136. err = ecode.VipWelfareUploadMaxErr
  137. return
  138. }
  139. if err = s.dao.WelfareBatchSave(wcb); err != nil {
  140. log.Error("WelfareBatchSave (%v) Error(%v)", wcb, err)
  141. return
  142. }
  143. for _, wc := range wcs {
  144. wc.Bid = wcb.ID
  145. }
  146. if err = s.dao.WelfareCodeBatchInsert(wcs); err != nil {
  147. log.Error("WelfareBatchSave Error(%v)", err)
  148. }
  149. return
  150. }
  151. // WelfareBatchList get welfare batch list
  152. func (s *Service) WelfareBatchList(wid int) (wbs []*model.WelfareBatchRes, err error) {
  153. if wbs, err = s.dao.WelfareBatchList(wid); err != nil {
  154. log.Error("WelfareBatchList wid(%v) Error(%v)", wid, err)
  155. }
  156. return
  157. }
  158. // WelfareBatchState delete welfare batch
  159. func (s *Service) WelfareBatchState(c context.Context, id int, username string) (err error) {
  160. tx := s.dao.BeginGormTran(c)
  161. if err = s.dao.WelfareBatchState(tx, id, _delete, 0, username); err != nil {
  162. log.Error("WelfareBatchState(%v) Error(%v)", id, err)
  163. tx.Rollback()
  164. return
  165. }
  166. if err = s.dao.WelfareCodeStatus(tx, id, _delete); err != nil {
  167. log.Error("WelfareCodeStatus bid(%v) Error(%v)", id, err)
  168. tx.Rollback()
  169. return
  170. }
  171. return tx.Commit().Error
  172. }
  173. func copyFiled(wf *model.Welfare, req *model.WelfareReq) {
  174. wf.ID = req.ID
  175. wf.WelfareName = req.WelfareName
  176. wf.WelfareDesc = req.WelfareDesc
  177. wf.HomepageUri = req.HomepageUri
  178. wf.BackdropUri = req.BackdropUri
  179. wf.Recommend = req.Recommend
  180. wf.Rank = req.Rank
  181. wf.Tid = req.Tid
  182. wf.Stime = req.Stime
  183. wf.Etime = req.Etime
  184. wf.UsageForm = req.UsageForm
  185. wf.ReceiveRate = req.ReceiveRate
  186. wf.VipType = req.VipType
  187. }
  188. // getRelativePath get relative path
  189. func getRelativePath(absolutePath string) (relativePath string, err error) {
  190. u, err := url.Parse(absolutePath)
  191. if err != nil {
  192. err = ecode.VipWelfareUrlUnvalid
  193. log.Error("hostChange ParseURL(%v) error (%v)", absolutePath, err)
  194. return
  195. }
  196. relativePath = u.Path
  197. return
  198. }