123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package charge
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "property-garden/errors"
- "property-garden/impl/v1/charge_utils"
- dbmodel "property-garden/model"
- "property-garden/parser"
- "property-garden/pb"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- )
- func checkChargeUrgeParam(req *pb_v1.ChargeUrgeRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.ObjId == 0:
- return status.Error(10003, "对象不能为空")
- case req.ObjType != charge_utils.ObjTypeHouse &&
- req.ObjType != charge_utils.ObjTypeSpace &&
- req.ObjType != charge_utils.ObjTypeVehicle:
- return status.Error(10003, "不支持的对象类型")
- }
- return nil
- }
- func getPublicOpenId(objId int64, objType int32, dbname string) ([]string, error) {
- houseId := int64(0)
- houseUid := int64(0)
- if objType == charge_utils.ObjTypeVehicle {
- vehicle := dbmodel.NewVehicle(dbname)
- where := map[string]interface{}{
- "id": objId,
- }
- err := vehicle.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- houseUid = vehicle.HouseholdUid
- } else {
- bind := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "obj_id": objId,
- "obj_type": objType,
- }
- err := bind.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- houseId = bind.HouseId
- }
- p := dbmodel.NewHouseApprovedGarden(dbname)
- where := map[string]interface{}{}
- if houseId > 0 {
- where["house_id"] = houseId
- }
- if houseUid > 0 {
- where["uid"] = houseUid
- }
- if len(where) == 0 {
- return nil, status.Error(10003, "未找到催缴对象")
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- ret := []string{}
- for _, v := range list {
- if objType != charge_utils.ObjTypeHouse && v.UserType != 1 {
- continue
- }
- ret = append(ret, v.PublicOpenId)
- }
- if len(ret) == 0 {
- return nil, status.Error(10003, "住户未关注公众号,无法推送消息")
- }
- return ret, nil
- }
- func getPhoneAndPublicOpenId(objId int64, objType int32, dbname string) ([]string, []string, error) {
- houseId := int64(0)
- houseUid := int64(0)
- if objType == charge_utils.ObjTypeVehicle {
- vehicle := dbmodel.NewVehicle(dbname)
- where := map[string]interface{}{
- "id": objId,
- }
- err := vehicle.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, nil, errors.DataBaseError
- }
- houseUid = vehicle.HouseholdUid
- } else {
- bind := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "obj_id": objId,
- "obj_type": objType,
- }
- err := bind.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, nil, errors.DataBaseError
- }
- houseId = bind.HouseId
- }
- p := dbmodel.NewHouseApprovedGarden(dbname)
- where := map[string]interface{}{}
- if houseId > 0 {
- where["house_id"] = houseId
- }
- if houseUid > 0 {
- where["uid"] = houseUid
- }
- if len(where) == 0 {
- return nil, nil, status.Error(10003, "未找到催缴对象")
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, nil, errors.DataBaseError
- }
- popenIds := []string{}
- phones := []string{}
- popenIdM := map[string]bool{}
- phoneM := map[string]bool{}
- for _, v := range list {
- if objType != charge_utils.ObjTypeHouse && v.UserType != 1 {
- continue
- }
- if _, ok := phoneM[v.Phone]; !ok && v.Phone != "" {
- phoneM[v.Phone] = true
- phones = append(phones, v.Phone)
- }
- if _, ok := popenIdM[v.PublicOpenId]; !ok && v.PublicOpenId != "" {
- popenIdM[v.PublicOpenId] = true
- popenIds = append(popenIds, v.PublicOpenId)
- }
- }
- return popenIds, phones, nil
- }
- var UrgeTitleM = map[int32]string{
- charge_utils.ObjTypeHouse: "您的房屋有欠费账单",
- charge_utils.ObjTypeSpace: "您的车位有欠费账单",
- charge_utils.ObjTypeVehicle: "您的车辆有欠费账单",
- }
- 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 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 ChargeUrge(ctx context.Context, req *pb_v1.ChargeUrgeRequest) (reply *pb_v1.ChargeUrgeReply, err error) {
- reply = &pb_v1.ChargeUrgeReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // 参数检查
- err = checkChargeUrgeParam(req)
- if err != nil {
- return nil, err
- }
- gardenInfo, err := charge_utils.GetGardenInfos([]int64{req.GardenId})
- if err != nil {
- return nil, err
- }
- gardenName := gardenInfo[req.GardenId].GardenName
- dbname := utils.GetGardenDbName(req.GardenId)
- openIds, phones, err := getPhoneAndPublicOpenId(req.ObjId, req.ObjType, dbname)
- if err != nil {
- return nil, err
- }
- title := UrgeTitleM[req.ObjType]
- remark := "详情请进入小程序查看"
- content := []*pb_v1.WxPublicMsgKeyValue{
- &pb_v1.WxPublicMsgKeyValue{Key: "first", Value: fmt.Sprintf("%s(%s)", title, gardenName)},
- &pb_v1.WxPublicMsgKeyValue{Key: "remark", Value: remark},
- }
- oneSuccess := false
- for i, v := range openIds {
- mreq := pb_v1.WxPublicMsgSendRequest{
- PublicOpenId: v,
- Content: content,
- TemplateId: parser.Conf.ThirdParty.Wx.PublicMsgTmpId,
- }
- _, err := pb.Thirdparty.WxPublicMsgSend(ctx, &mreq)
- if err == nil {
- oneSuccess = true
- continue
- }
- if !oneSuccess && err != nil && i == len(openIds)-1 {
- return nil, err
- }
- }
- // 短信消息
- if len(phones) > 0 {
- msgArray := getGardenAvailableMsgCount(req.GardenId)
- sendMsgPhone(phones, gardenName, msgArray)
- gardenAvailableMsgDecrease(msgArray, req.GardenId)
- }
- return reply, nil
- }
|