moral.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/job/main/member/model"
  8. smodel "go-common/app/service/main/member/model"
  9. "go-common/library/log"
  10. )
  11. func (s *Service) recoverMoral(c context.Context, mid int64) (err error) {
  12. var (
  13. moral *smodel.Moral
  14. rowsAffected int64
  15. )
  16. if moral, err = s.dao.SelMoral(c, mid); err != nil {
  17. return
  18. }
  19. if moral == nil || moral.Moral >= smodel.DefaultMoral {
  20. log.Info("recoverMoral ignore, moral(%v)", moral)
  21. return
  22. }
  23. now := time.Now()
  24. deltaDays := deltaDays(now, moral.LastRecoverDate.Time())
  25. if deltaDays <= 0 {
  26. return
  27. }
  28. deltaMoral := deltaDays * 100
  29. if moral.Moral+deltaMoral > smodel.DefaultMoral {
  30. deltaMoral = smodel.DefaultMoral - moral.Moral
  31. }
  32. if rowsAffected, err = s.dao.RecoverMoral(c, mid, deltaMoral, deltaMoral, now.Format("2006-01-02")); err != nil {
  33. return
  34. }
  35. if rowsAffected == 0 {
  36. return
  37. }
  38. // report log
  39. ul := &model.UserLog{
  40. Mid: mid,
  41. IP: "127.0.0.1",
  42. TS: now.Unix(),
  43. LogID: model.UUID4(),
  44. Content: map[string]string{
  45. "from_moral": strconv.FormatInt(moral.Moral, 10),
  46. "to_moral": strconv.FormatInt(deltaMoral+moral.Moral, 10),
  47. "origin": strconv.FormatInt(smodel.ManualRecoveryType, 10),
  48. "status": strconv.FormatInt(smodel.RevocableMoralStatus, 10),
  49. "remark": fmt.Sprintf("自动恢复(%d)天", deltaDays),
  50. "operater": "系统",
  51. "reason": fmt.Sprintf("时间:%d天", deltaDays),
  52. },
  53. }
  54. s.dao.AddMoralLog(c, ul)
  55. // origin log
  56. // content := make(map[string][]byte, 10)
  57. // content["mid"] = []byte(strconv.FormatInt(mid, 10))
  58. // content["ip"] = []byte("127.0.0.1")
  59. // content["from_moral"] = []byte(strconv.FormatInt(moral.Moral, 10))
  60. // content["to_moral"] = []byte(strconv.FormatInt(deltaMoral+moral.Moral, 10))
  61. // content["origin"] = []byte(strconv.FormatInt(smodel.ManualRecoveryType, 10))
  62. // content["status"] = []byte(strconv.FormatInt(smodel.RevocableMoralStatus, 10))
  63. // content["remark"] = []byte(fmt.Sprintf("自动恢复(%d)天", deltaDays))
  64. // content["operater"] = []byte("系统")
  65. // content["reason"] = []byte(fmt.Sprintf("时间:%d天", deltaDays))
  66. // if err = s.dao.AddLog(c, mid, now.Unix(), content, model.TableMoralLog); err != nil {
  67. // log.Error("recoverMoral mid: %v from_moral:%v,to_moral:%v error(%v)", mid, content["from_moral"], content["to_moral"], err)
  68. // } else {
  69. // log.Info("recoverMoral mid: %v from_moral:%v,to_moral:%v error(%v)", mid, content["from_moral"], content["to_moral"], err)
  70. // }
  71. s.dao.DelMoralCache(c, mid)
  72. return
  73. }
  74. func deltaDays(now, old time.Time) int64 {
  75. return int64(now.Sub(old).Hours() / 24)
  76. }