123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package reminder
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "property-task/parser"
- "property-task/pb"
- pb_v1 "property-task/pb/v1"
- "time"
- )
- // 发送公众号消息
- func sendMsgWx(publicOpenids []string, gardenName string) {
- remark := "详情请进入小程序查看"
- for _, popenId := range publicOpenids {
- content := []*pb_v1.WxPublicMsgKeyValue{
- &pb_v1.WxPublicMsgKeyValue{Key: "first", Value: "您在" + gardenName + "的房屋/车位有欠费账单"},
- &pb_v1.WxPublicMsgKeyValue{Key: "remark", Value: remark},
- }
- mreq := pb_v1.WxPublicMsgSendRequest{
- PublicOpenId: popenId,
- Content: content,
- TemplateId: parser.Conf.ThirdParty.Wx.PublicMsgTmpId,
- }
- _, err := pb.Thirdparty.WxPublicMsgSend(context.Background(), &mreq)
- if err != nil {
- logger.Error("func",
- zap.String("call", "pb.Thirdparty.WxPublicMsgSend "),
- zap.String("error", err.Error()))
- continue
- }
- }
- }
- func getOneAvailable(msgArray [][3]int64, index int) int {
- if index >= len(msgArray) || index < 0 {
- return -1
- }
- if msgArray[index][1] > 0 {
- return index
- }
- if index+1 >= len(msgArray) {
- return -1
- }
- return index + 1
- }
- // 发送短信消息
- func sendMsgPhone(phones []string, gardenName string, msgArray [][3]int64) {
- index := 0
- for _, phone := range phones {
- index = getOneAvailable(msgArray, index)
- if index < 0 {
- break
- }
- mreq := pb_v1.TencentMsgSendRequest{
- Phone: phone,
- MsgType: 1,
- Datas: []string{gardenName},
- }
- _, err := pb.Thirdparty.TencentMsgSend(context.Background(), &mreq)
- if err != nil {
- logger.Error("func",
- zap.String("call", "pb.Garden.TencentMsgSend "),
- zap.String("error", err.Error()))
- continue
- }
- msgArray[index][1] = msgArray[index][1] - 1
- }
- }
- func sendMsgPhoneTest(phones []string, gardenName string, msgArray [][3]int64) {
- index := 0
- for _, phone := range phones {
- index = getOneAvailable(msgArray, index)
- if index < 0 {
- break
- }
- if len(phone) < 4 {
- continue
- }
- msgArray[index][1] = msgArray[index][1] - 1
- }
- }
- func getGardenAvailableMsgCount(gardenId int64) [][3]int64 {
- // 0:存套餐订单id
- // 1:存套餐订单剩余量(发一条短信自减)
- // 2:存套餐订单原始剩余量
- ret := [][3]int64{}
- mreq := pb_v1.MsgPackageOrderListRequest{
- Available: true,
- Status: 2,
- PageSize: -1,
- Page: -1,
- GardenId: gardenId,
- }
- mreply, err := pb.System.MsgPackageOrderList(context.Background(), &mreq)
- if err != nil {
- logger.Error("func",
- zap.String("call", "pb.System.MsgPackageOrderList"),
- zap.String("error", err.Error()))
- return ret
- }
- for _, v := range mreply.List {
- ret = append(ret, [3]int64{v.OrderId, v.MsgAvailableCount, v.MsgAvailableCount})
- }
- return ret
- }
- func gardenAvailableMsgDecrease(msgArray [][3]int64, gardenId int64) {
- mreq := pb_v1.MsgPackageOrderDecreaseRequest{GardenId: gardenId}
- for _, array := range msgArray {
- if array[2]-array[1] <= 0 {
- continue
- }
- mreq.Count = array[2] - array[1]
- mreq.OrderId = array[0]
- _, err := pb.System.MsgPackageOrderDecrease(context.Background(), &mreq)
- if err != nil {
- logger.Error("func",
- zap.String("call", "pb.System.MsgPackageOrderDecrease"),
- zap.String("error", err.Error()))
- }
- }
- }
- func SendMsgTest() {
- phones := []string{"134", "13540735437", "13540735438"}
- msgArray := getGardenAvailableMsgCount(11)
- fmt.Printf("before send:%v\n", msgArray)
- sendMsgPhoneTest(phones, "华汇", msgArray)
- fmt.Printf("after send:%v\n", msgArray)
- gardenAvailableMsgDecrease(msgArray, 11)
- }
- // 定时发送消息
- func SendUrgeMsg() {
- fmt.Printf("开始发送催缴信息:%v\n", time.Now().Format("2006-01-02 15:04:05"))
- now := time.Now()
- nowDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
- str := getGardenInfo(nowDay.Unix())
- if str == "" {
- return
- }
- gardens := []pb_v1.GardenIdInfo{}
- _ = json.Unmarshal([]byte(str), &gardens)
- for _, garden := range gardens {
- if garden.GardenId == 0 {
- continue
- }
- phones := popAllObjUsersPhones(nowDay.Unix(), garden.GardenId)
- openids := popAllObjUsersOpenids(nowDay.Unix(), garden.GardenId)
- if len(phones) > 0 {
- msgArray := getGardenAvailableMsgCount(garden.GardenId)
- sendMsgPhone(phones, garden.GardenName, msgArray)
- gardenAvailableMsgDecrease(msgArray, garden.GardenId)
- }
- if len(openids) > 0 {
- sendMsgWx(openids, garden.GardenName)
- }
- }
- }
|