send.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package reminder
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "go.uber.org/zap"
  8. "property-task/parser"
  9. "property-task/pb"
  10. pb_v1 "property-task/pb/v1"
  11. "time"
  12. )
  13. // 发送公众号消息
  14. func sendMsgWx(publicOpenids []string, gardenName string) {
  15. remark := "详情请进入小程序查看"
  16. for _, popenId := range publicOpenids {
  17. content := []*pb_v1.WxPublicMsgKeyValue{
  18. &pb_v1.WxPublicMsgKeyValue{Key: "first", Value: "您在" + gardenName + "的房屋/车位有欠费账单"},
  19. &pb_v1.WxPublicMsgKeyValue{Key: "remark", Value: remark},
  20. }
  21. mreq := pb_v1.WxPublicMsgSendRequest{
  22. PublicOpenId: popenId,
  23. Content: content,
  24. TemplateId: parser.Conf.ThirdParty.Wx.PublicMsgTmpId,
  25. }
  26. _, err := pb.Thirdparty.WxPublicMsgSend(context.Background(), &mreq)
  27. if err != nil {
  28. logger.Error("func",
  29. zap.String("call", "pb.Thirdparty.WxPublicMsgSend "),
  30. zap.String("error", err.Error()))
  31. continue
  32. }
  33. }
  34. }
  35. func getOneAvailable(msgArray [][3]int64, index int) int {
  36. if index >= len(msgArray) || index < 0 {
  37. return -1
  38. }
  39. if msgArray[index][1] > 0 {
  40. return index
  41. }
  42. if index+1 >= len(msgArray) {
  43. return -1
  44. }
  45. return index + 1
  46. }
  47. // 发送短信消息
  48. func sendMsgPhone(phones []string, gardenName string, msgArray [][3]int64) {
  49. index := 0
  50. for _, phone := range phones {
  51. index = getOneAvailable(msgArray, index)
  52. if index < 0 {
  53. break
  54. }
  55. mreq := pb_v1.TencentMsgSendRequest{
  56. Phone: phone,
  57. MsgType: 1,
  58. Datas: []string{gardenName},
  59. }
  60. _, err := pb.Thirdparty.TencentMsgSend(context.Background(), &mreq)
  61. if err != nil {
  62. logger.Error("func",
  63. zap.String("call", "pb.Garden.TencentMsgSend "),
  64. zap.String("error", err.Error()))
  65. continue
  66. }
  67. msgArray[index][1] = msgArray[index][1] - 1
  68. }
  69. }
  70. func sendMsgPhoneTest(phones []string, gardenName string, msgArray [][3]int64) {
  71. index := 0
  72. for _, phone := range phones {
  73. index = getOneAvailable(msgArray, index)
  74. if index < 0 {
  75. break
  76. }
  77. if len(phone) < 4 {
  78. continue
  79. }
  80. msgArray[index][1] = msgArray[index][1] - 1
  81. }
  82. }
  83. func getGardenAvailableMsgCount(gardenId int64) [][3]int64 {
  84. // 0:存套餐订单id
  85. // 1:存套餐订单剩余量(发一条短信自减)
  86. // 2:存套餐订单原始剩余量
  87. ret := [][3]int64{}
  88. mreq := pb_v1.MsgPackageOrderListRequest{
  89. Available: true,
  90. Status: 2,
  91. PageSize: -1,
  92. Page: -1,
  93. GardenId: gardenId,
  94. }
  95. mreply, err := pb.System.MsgPackageOrderList(context.Background(), &mreq)
  96. if err != nil {
  97. logger.Error("func",
  98. zap.String("call", "pb.System.MsgPackageOrderList"),
  99. zap.String("error", err.Error()))
  100. return ret
  101. }
  102. for _, v := range mreply.List {
  103. ret = append(ret, [3]int64{v.OrderId, v.MsgAvailableCount, v.MsgAvailableCount})
  104. }
  105. return ret
  106. }
  107. func gardenAvailableMsgDecrease(msgArray [][3]int64, gardenId int64) {
  108. mreq := pb_v1.MsgPackageOrderDecreaseRequest{GardenId: gardenId}
  109. for _, array := range msgArray {
  110. if array[2]-array[1] <= 0 {
  111. continue
  112. }
  113. mreq.Count = array[2] - array[1]
  114. mreq.OrderId = array[0]
  115. _, err := pb.System.MsgPackageOrderDecrease(context.Background(), &mreq)
  116. if err != nil {
  117. logger.Error("func",
  118. zap.String("call", "pb.System.MsgPackageOrderDecrease"),
  119. zap.String("error", err.Error()))
  120. }
  121. }
  122. }
  123. func SendMsgTest() {
  124. phones := []string{"134", "13540735437", "13540735438"}
  125. msgArray := getGardenAvailableMsgCount(11)
  126. fmt.Printf("before send:%v\n", msgArray)
  127. sendMsgPhoneTest(phones, "华汇", msgArray)
  128. fmt.Printf("after send:%v\n", msgArray)
  129. gardenAvailableMsgDecrease(msgArray, 11)
  130. }
  131. // 定时发送消息
  132. func SendUrgeMsg() {
  133. fmt.Printf("开始发送催缴信息:%v\n", time.Now().Format("2006-01-02 15:04:05"))
  134. now := time.Now()
  135. nowDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  136. str := getGardenInfo(nowDay.Unix())
  137. if str == "" {
  138. return
  139. }
  140. gardens := []pb_v1.GardenIdInfo{}
  141. _ = json.Unmarshal([]byte(str), &gardens)
  142. for _, garden := range gardens {
  143. if garden.GardenId == 0 {
  144. continue
  145. }
  146. phones := popAllObjUsersPhones(nowDay.Unix(), garden.GardenId)
  147. openids := popAllObjUsersOpenids(nowDay.Unix(), garden.GardenId)
  148. if len(phones) > 0 {
  149. msgArray := getGardenAvailableMsgCount(garden.GardenId)
  150. sendMsgPhone(phones, garden.GardenName, msgArray)
  151. gardenAvailableMsgDecrease(msgArray, garden.GardenId)
  152. }
  153. if len(openids) > 0 {
  154. sendMsgWx(openids, garden.GardenName)
  155. }
  156. }
  157. }