generate_reminder.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package fee
  4. import (
  5. "context"
  6. "git.getensh.com/common/gopkgs/database"
  7. "property-garden/errors"
  8. "property-garden/impl/v1/charge_utils"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "property-garden/utils"
  12. "time"
  13. )
  14. type BillObj struct {
  15. ObjId int64 `json:"obj_id"`
  16. ObjType int32 `json:"obj_type"`
  17. HouseId int64 `json:"house_id"`
  18. }
  19. type BillUser struct {
  20. HouseId int64 `json:"house_id"`
  21. Phone string `json:"phone"`
  22. PublicOpenId string `json:"public_open_id"`
  23. }
  24. func getAllBills(dbname string, isNew bool) ([]BillObj, error) {
  25. p := dbmodel.TChargeBill{}
  26. p.SetTable(dbname)
  27. where := map[string]interface{}{
  28. "pay_mode": 1,
  29. "house_id >": 0,
  30. "charge_time_type": charge_utils.ChargeTimeTypePeriod,
  31. }
  32. if isNew {
  33. where["created_at >"] = time.Now().Format("2006-01-02")
  34. }
  35. list := []BillObj{}
  36. err := p.SelectGroup(database.DB(), where, nil, "obj_id, obj_type, house_id", "obj_id,obj_type", -1, -1, &list)
  37. if err != nil {
  38. return nil, errors.DataBaseError
  39. }
  40. //return []BillObj{{ObjId:1, ObjType:1, HouseId:1},{ObjId:1, ObjType:2, HouseId:2}}, nil
  41. return list, nil
  42. }
  43. func GetPublicOpenIds(dbname string, houseIds []int64, userType []int32) ([]BillUser, error) {
  44. p := dbmodel.NewHouseApprovedGarden(dbname)
  45. where := map[string]interface{}{
  46. "house_id in": houseIds,
  47. }
  48. if len(userType) > 0 {
  49. where["user_type in"] = userType
  50. }
  51. list := []BillUser{}
  52. err := p.SelectGroup(database.DB(), where, nil, "house_id, phone, public_open_id", "house_id, uid", -1, -1, &list)
  53. if err != nil {
  54. return nil, errors.DataBaseError
  55. }
  56. return list, nil
  57. }
  58. // 生成催缴单
  59. func GenerateReminder(ctx context.Context, req *pb_v1.GenerateReminderRequest) (reply *pb_v1.GenerateReminderReply, err error) {
  60. reply = &pb_v1.GenerateReminderReply{}
  61. if req.GardenId < 0 {
  62. return nil, errors.ParamsError
  63. }
  64. dbname := utils.GetGardenDbName(req.GardenId)
  65. billObjs, err := getAllBills(dbname, req.IsNew)
  66. if err != nil {
  67. return reply, err
  68. }
  69. if len(billObjs) == 0 {
  70. return reply, nil
  71. }
  72. houseIds := make([]int64, len(billObjs))
  73. for i, v := range billObjs {
  74. houseIds[i] = v.HouseId
  75. }
  76. billUsers, err := GetPublicOpenIds(dbname, houseIds, req.UserType)
  77. if err != nil {
  78. return reply, err
  79. }
  80. m := map[int64][]*pb_v1.ReminderUserInfo{}
  81. for _, v := range billUsers {
  82. m[v.HouseId] = append(m[v.HouseId], &pb_v1.ReminderUserInfo{Phone: v.Phone, PublicOpenId: v.PublicOpenId})
  83. }
  84. reply.List = make([]*pb_v1.ReminderItem, len(billObjs))
  85. for i, v := range billObjs {
  86. reply.List[i] = &pb_v1.ReminderItem{
  87. ObjId: v.ObjId,
  88. ObjType: v.ObjType,
  89. }
  90. if len(m[v.HouseId]) == 0 {
  91. m[v.HouseId] = make([]*pb_v1.ReminderUserInfo, 0)
  92. }
  93. reply.List[i].Users = m[v.HouseId]
  94. }
  95. return reply, nil
  96. }