bcoin.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package service
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "fmt"
  7. "time"
  8. "go-common/app/job/main/vip/model"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. "github.com/pkg/errors"
  12. )
  13. //HandlerBcoin handler bcoin
  14. func (s *Service) HandlerBcoin() (err error) {
  15. var (
  16. batchSize int64 = 2000
  17. oldMaxID int64
  18. newMaxID int64
  19. exitMap = make(map[string]int)
  20. batch []*model.VipBcoinSalary
  21. )
  22. if oldMaxID, err = s.dao.SelOldBcoinMaxID(context.TODO()); err != nil {
  23. log.Error("s.dao.SelOldBcoinMaxID error(%+v)", err)
  24. return
  25. }
  26. if newMaxID, err = s.dao.SelBcoinMaxID(context.TODO()); err != nil {
  27. log.Error("s.dao.SelBcoinMaxID error(%v)", err)
  28. return
  29. }
  30. page := newMaxID / batchSize
  31. if newMaxID%batchSize != 0 {
  32. page++
  33. }
  34. for i := 0; i < int(page); i++ {
  35. arg := new(model.QueryBcoinSalary)
  36. arg.StartID = int64(i) * batchSize
  37. arg.EndID = int64(i+1) * batchSize
  38. if arg.EndID > newMaxID {
  39. arg.EndID = newMaxID
  40. }
  41. arg.GiveNowStatus = -1
  42. var res []*model.VipBcoinSalary
  43. if res, err = s.dao.SelBcoinSalaryData(context.TODO(), arg.StartID, arg.EndID); err != nil {
  44. log.Error("s.dao.SelBcoinSalary(%+v) error(%+v)", arg, err)
  45. return
  46. }
  47. for _, v := range res {
  48. exitMap[s.makeBcoinMD5(v)] = 1
  49. }
  50. }
  51. page = oldMaxID / batchSize
  52. if oldMaxID%batchSize != 0 {
  53. page++
  54. }
  55. for i := 0; i < int(page); i++ {
  56. startID := int64(i) * batchSize
  57. EndID := int64(i+1) * batchSize
  58. if EndID > oldMaxID {
  59. EndID = oldMaxID
  60. }
  61. var res []*model.VipBcoinSalary
  62. if res, err = s.dao.SelOldBcoinSalary(context.TODO(), startID, EndID); err != nil {
  63. log.Error("sel.OldBcoinSalary(startID:%v endID:%v) error(%+v)", startID, EndID, err)
  64. return
  65. }
  66. for _, v := range res {
  67. if exitMap[s.makeBcoinMD5(v)] == 0 {
  68. batch = append(batch, v)
  69. }
  70. }
  71. if err = s.dao.BatchAddBcoinSalary(batch); err != nil {
  72. log.Error("s.dao.BatchAddBcoinSalary (%+v)", err)
  73. return
  74. }
  75. batch = nil
  76. }
  77. return
  78. }
  79. func (s *Service) handleraddbcoinproc() {
  80. defer func() {
  81. if x := recover(); x != nil {
  82. log.Error("service.handlerupdaterechargeorderproc panic(%v)", x)
  83. go s.handleraddbcoinproc()
  84. log.Info("service.handlerupdaterechargeorderproc recover")
  85. }
  86. }()
  87. for {
  88. msg := <-s.handlerAddBcoinSalary
  89. log.Info("cur bcoin msage:%+v", msg)
  90. for i := 0; i < s.c.Property.Retry; i++ {
  91. if err := s.dao.AddBcoinSalary(context.TODO(), msg); err != nil {
  92. log.Error("s.dao.addbcoinsalary(%+v) error(%+v)", msg, err)
  93. } else if err == nil {
  94. break
  95. }
  96. }
  97. }
  98. }
  99. func (s *Service) handlerdelbcoinproc() {
  100. defer func() {
  101. if x := recover(); x != nil {
  102. log.Error("service.handlerdelbcoinproc panic(%v)", x)
  103. go s.handlerdelbcoinproc()
  104. log.Info("service.handlerdelbcoinproc recover")
  105. }
  106. }()
  107. var err error
  108. for {
  109. msg := <-s.handlerDelBcoinSalary
  110. for i := 0; i < s.c.Property.Retry; i++ {
  111. if err = s.dao.DelBcoinSalary(context.TODO(), msg.Payday, msg.Mid); err == nil {
  112. break
  113. }
  114. log.Error("s.dao.DelBcoinSalary(msg:%+v) error(%+v)", msg, err)
  115. }
  116. }
  117. }
  118. func (s *Service) handlerupdatebcoinproc() {
  119. defer func() {
  120. if x := recover(); x != nil {
  121. log.Error("service.handlerupdaterechargeorderproc panic(%v)", x)
  122. go s.handlerupdatebcoinproc()
  123. log.Info("service.handlerupdaterechargeorderproc recover")
  124. }
  125. }()
  126. var err error
  127. for {
  128. msg := <-s.handlerUpdateBcoinSalary
  129. for i := 0; i < s.c.Property.Retry; i++ {
  130. if err = s.dao.UpdateBcoinSalary(context.TODO(), msg.Payday, msg.Mid, msg.Status); err == nil {
  131. break
  132. }
  133. log.Error("s.dao.UpdateBcoinSalary(msg:%+v) error(%+v)", msg, err)
  134. }
  135. }
  136. }
  137. func (s *Service) sendBcoinJob() {
  138. defer func() {
  139. if r := recover(); r != nil {
  140. r = errors.WithStack(r.(error))
  141. log.Error("recover panic error(%+v)", r)
  142. }
  143. }()
  144. log.Info("sen bcoin job start ........................................ ")
  145. s.sendBcoin()
  146. log.Info("sen bcoin job end ........................................ ")
  147. }
  148. func (s *Service) sendBcoin() {
  149. var (
  150. maxID int64
  151. batchSize int64 = 3000
  152. sendSize = 50
  153. err error
  154. )
  155. if maxID, err = s.dao.SelBcoinMaxID(context.TODO()); err != nil {
  156. log.Error("s.dao.selBcoinMaxID() error(%+v)", err)
  157. return
  158. }
  159. page := maxID / batchSize
  160. if maxID%batchSize != 0 {
  161. page++
  162. }
  163. now := time.Now()
  164. startMonth := now.AddDate(0, 0, 1-now.Day())
  165. endMonth := startMonth.AddDate(0, 1, 0)
  166. sendInfo := s.sendInfo()
  167. for i := 0; i < int(page); i++ {
  168. arg := new(model.QueryBcoinSalary)
  169. arg.StartID = int64(i) * batchSize
  170. arg.EndID = int64(i+1) * batchSize
  171. arg.GiveNowStatus = 0
  172. arg.Status = 0
  173. arg.StartMonth = xtime.Time(startMonth.Unix())
  174. arg.EndMonth = xtime.Time(endMonth.Unix())
  175. var res []*model.VipBcoinSalary
  176. if res, err = s.dao.SelBcoinSalary(context.TODO(), arg); err != nil {
  177. log.Error("s.dao.selBcoinSalary(%+v) error(%+v)", arg, err)
  178. return
  179. }
  180. pageSend := len(res) / sendSize
  181. if len(res)%sendSize != 0 {
  182. pageSend++
  183. }
  184. for j := 0; j < pageSend; j++ {
  185. start := j * sendSize
  186. end := int(j+1) * sendSize
  187. if end > len(res) {
  188. end = len(res)
  189. }
  190. if err = s.sendBocinNow(res[start:end], sendInfo.Amount, sendInfo.DueDate); err != nil {
  191. log.Error("%+v", err)
  192. return
  193. }
  194. }
  195. }
  196. }
  197. func (s *Service) sendInfo() (r *model.BcoinSendInfo) {
  198. var (
  199. c time.Time
  200. day = s.c.Property.AnnualVipBcoinDay
  201. amount = s.c.Property.AnnualVipBcoinCouponMoney
  202. )
  203. r = new(model.BcoinSendInfo)
  204. r.Amount = int32(amount)
  205. r.DayOfMonth = day
  206. c = time.Now()
  207. c = c.AddDate(0, 1, int(day)-1-c.Day())
  208. r.DueDate = xtime.Time(c.Unix())
  209. return
  210. }
  211. func (s *Service) sendBocinNow(res []*model.VipBcoinSalary, amount int32, duTime xtime.Time) (err error) {
  212. var (
  213. mids []int64
  214. ids []int64
  215. )
  216. for _, v := range res {
  217. mids = append(mids, v.Mid)
  218. ids = append(ids, v.ID)
  219. }
  220. if err = s.dao.SendBcoin(context.TODO(), mids, amount, duTime, "127.0.0.1"); err != nil {
  221. err = errors.WithStack(err)
  222. return
  223. }
  224. if err = s.dao.UpdateBcoinSalaryBatch(context.TODO(), ids, 1); err != nil {
  225. err = errors.WithStack(err)
  226. return
  227. }
  228. return
  229. }
  230. func (s *Service) makeBcoinMD5(r *model.VipBcoinSalary) string {
  231. key := fmt.Sprintf("%v,%v,%v,%v,%v,%v", r.Mid, r.Memo, r.Amount, r.Payday.Time().Format("2006-01-02"), r.GiveNowStatus, r.Status)
  232. hash := md5.New()
  233. hash.Write([]byte(key))
  234. sum := hash.Sum(nil)
  235. return hex.EncodeToString(sum)
  236. }