kpi.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/job/main/credit/model"
  7. "go-common/library/log"
  8. )
  9. // KPIReward send reward to user by kpi info.
  10. func (s *Service) KPIReward(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
  11. var (
  12. mr = &model.Kpi{}
  13. res model.Kpi
  14. nameplentID int64
  15. coins float64
  16. )
  17. if err = json.Unmarshal(nwMsg, mr); err != nil {
  18. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  19. return
  20. }
  21. if res, err = s.dao.KPIInfo(c, mr.ID); err != nil {
  22. log.Error("json.KPIInf(%d) error(%v)", mr.ID, err)
  23. return
  24. }
  25. if res.HandlerStatus == 1 {
  26. return
  27. }
  28. s.dao.SendMsg(c, mr.Mid, _msgTitle, _msgContext)
  29. coins = model.KpiCoinsRate(mr.Rate)
  30. if coins > 0 {
  31. s.dao.AddMoney(c, mr.Mid, coins, model.KPICoinsReason)
  32. }
  33. s.dao.UpdateKPIHandlerStatus(c, mr.ID)
  34. if pend, ok := model.LevelPendantByKPI(int8(mr.Rate)); ok {
  35. expired := time.Now().AddDate(0, 0, 30)
  36. if err = s.dao.UpdateJuryExpired(c, mr.Mid, expired); err != nil {
  37. return
  38. }
  39. if len(pend) > 0 {
  40. if err = s.dao.SendPendant(c, mr.Mid, pend, model.KPIDefealtPendSendDays); err != nil {
  41. log.Error("s.dao.SendPendant err(%v)", err)
  42. }
  43. if err == nil {
  44. s.dao.UpdateKPIPendentStatus(c, mr.ID)
  45. }
  46. if mr.Mid%50 == 1 {
  47. time.Sleep(time.Second)
  48. }
  49. }
  50. }
  51. num, err := s.dao.CountKPIRate(c, mr.Mid)
  52. if err != nil {
  53. log.Error("s.dao.CountKPIRate(mid:%d) err(%v)", mr.Mid, err)
  54. return
  55. }
  56. nameplentID = model.KpiPlateIDRateTimes(num)
  57. if nameplentID != 0 {
  58. for i := 0; i <= 5; i++ {
  59. if err = s.dao.SendMedal(c, mr.Mid, nameplentID); err != nil {
  60. log.Error("s.dao.SendMedal(mid:%d nameNameplatid:%d) err(%v)", mr.Mid, nameplentID, err)
  61. continue
  62. }
  63. break
  64. }
  65. }
  66. return
  67. }