generate.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2021 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package reminder
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/cache"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "property-task/impl/v1/common"
  12. "property-task/pb"
  13. pb_v1 "property-task/pb/v1"
  14. "time"
  15. )
  16. // 费用对象类型
  17. const (
  18. ObjTypeHouse = 1
  19. ObjTypeSpace = 2
  20. ObjTypeVehicle = 3
  21. )
  22. // 业主类型(业主,家人,租客)
  23. const (
  24. UserTypeHousehold = 1
  25. UserTypeFamily = 2
  26. UserTypeRenter = 3
  27. )
  28. var userTypeM = map[int32][]int32{
  29. 1: []int32{UserTypeHousehold},
  30. 2: []int32{UserTypeHousehold, UserTypeFamily},
  31. 3: []int32{UserTypeHousehold, UserTypeFamily, UserTypeRenter},
  32. }
  33. var UrgeTitleM = map[int32]string{
  34. ObjTypeHouse: "您的房屋有欠费账单",
  35. ObjTypeSpace: "您的车位有欠费账单",
  36. ObjTypeVehicle: "您的车辆有欠费账单",
  37. }
  38. // 获取催缴配置
  39. func getUrgeConf(gardenId int64) *pb_v1.ChargeUrgeInfoReply {
  40. mreq := pb_v1.ChargeUrgeInfoRequest{}
  41. mreq.GardenId = gardenId
  42. reply, err := pb.Garden.ChargeUrgeInfo(context.Background(), &mreq)
  43. if err != nil {
  44. logger.Error("func",
  45. zap.String("call", "pb.Garden.ChargeUrgeInfo "),
  46. zap.String("error", err.Error()))
  47. return nil
  48. }
  49. return reply
  50. }
  51. // 获取催缴用户的联系方式
  52. func getUrgeObjUserContact(gardenId int64, userType []int32) ([]interface{}, []interface{}) {
  53. mreq := pb_v1.GenerateReminderRequest{}
  54. mreq.GardenId = gardenId
  55. mreq.UserType = userType
  56. reply, err := pb.Garden.GenerateReminder(context.Background(), &mreq)
  57. if err != nil {
  58. logger.Error("func",
  59. zap.String("call", "pb.Garden.GenerateReminder "),
  60. zap.String("error", err.Error()))
  61. return nil, nil
  62. }
  63. phones := []interface{}{}
  64. openIds := []interface{}{}
  65. for _, objs := range reply.List {
  66. for _, user := range objs.Users {
  67. if user.Phone != "" {
  68. phones = append(phones, user.Phone)
  69. }
  70. if user.PublicOpenId != "" {
  71. openIds = append(openIds, user.PublicOpenId)
  72. }
  73. }
  74. }
  75. return phones, openIds
  76. }
  77. func isPhoneMsg(msgType int32) bool {
  78. return msgType == 1 || msgType == 3
  79. }
  80. func isWxMsg(msgType int32) bool {
  81. return msgType == 2 || msgType == 3
  82. }
  83. func CanUrg(urgeConf *pb_v1.ChargeUrgeInfoReply, nowDay time.Time) bool {
  84. if urgeConf == nil {
  85. return false
  86. }
  87. // 不催缴
  88. if urgeConf.UrgeDay == 0 {
  89. return false
  90. }
  91. // 判断是否今天生成催缴
  92. if nowDay.Day() != int(urgeConf.UrgeDay) {
  93. return false
  94. }
  95. return true
  96. }
  97. // 定时生成催缴单
  98. func GenerateReminder() {
  99. fmt.Printf("开始生成催缴信息\n")
  100. now := time.Now()
  101. nowDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  102. cache.RedisLock(common.TaskKey)
  103. defer cache.RedisUnlock(common.TaskKey)
  104. // 获取小区列表
  105. gardens := common.GetGardenList()
  106. // 缓存小区信息,发送催缴信息时会用到
  107. if len(gardens) > 0 {
  108. bytes, _ := json.Marshal(gardens)
  109. cacheGardenInfo(nowDay.Unix(), string(bytes))
  110. }
  111. for _, garden := range gardens {
  112. //
  113. delOldObjUsers(nowDay.Unix(), garden.GardenId)
  114. // 获取定时催缴配置
  115. urgeConf := getUrgeConf(garden.GardenId)
  116. // 是否能催缴
  117. if !CanUrg(urgeConf, nowDay) {
  118. continue
  119. }
  120. // 查询小区内所有需要催缴的用户的联系方式
  121. phones, openIds := getUrgeObjUserContact(garden.GardenId, userTypeM[urgeConf.UrgeTarget])
  122. fmt.Printf("xxxxy:%v,%v\n", phones, openIds)
  123. // 缓存手机号或用户openid等到8点30发送(cronb)
  124. if isPhoneMsg(urgeConf.UrgeMsgType) && len(phones) > 0 {
  125. cacheAllObjUsersPhone(nowDay.Unix(), garden.GardenId, phones)
  126. }
  127. if isWxMsg(urgeConf.UrgeMsgType) && len(openIds) > 0 {
  128. cacheAllObjUsersOpenid(nowDay.Unix(), garden.GardenId, openIds)
  129. }
  130. }
  131. }