123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package house
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/cache"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-household/errors"
- dbmodel "property-household/model"
- pb_v1 "property-household/pb/v1"
- )
- const HouseholdHousesPrefix = "household_gardens"
- func checkHouseholdHousesParam(req *pb_v1.HouseholdHousesRequest) error {
- switch {
- case req.Uid < 1:
- return status.Error(10003, "id不能为空")
- }
- return nil
- }
- func getHouseholdHousesKey(uid int64) string {
- return fmt.Sprintf("%s_%d", HouseholdHousesPrefix, uid)
- }
- func householdGardensFromRedis(uid int64) []*pb_v1.HouseholdHouseInfo {
- key := getHouseholdHousesKey(uid)
- str, _ := cache.Redis().Get(key)
- if str == "" {
- return nil
- }
- ret := []*pb_v1.HouseholdHouseInfo{}
- json.Unmarshal([]byte(str), &ret)
- return ret
- }
- func householdGardensToRedis(uid int64, data []*pb_v1.HouseholdHouseInfo) {
- key := getHouseholdHousesKey(uid)
- bytes, _ := json.Marshal(data)
- cache.Redis().SetEx(key, 30, string(bytes))
- }
- func householdGardensDelFromRedis(uid int64) {
- key := getHouseholdHousesKey(uid)
- cache.Redis().Del(key)
- }
- //
- func HouseholdHouses(ctx context.Context, req *pb_v1.HouseholdHousesRequest) (reply *pb_v1.HouseholdHousesReply, err error) {
- reply = &pb_v1.HouseholdHousesReply{}
- // 捕获各个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 = checkHouseholdHousesParam(req)
- if err != nil {
- return nil, err
- }
- reply.List = householdGardensFromRedis(req.Uid)
- if reply.List != nil {
- return reply, nil
- }
- p := dbmodel.THouseApproved{}
- where := map[string]interface{}{}
- where["uid"] = req.Uid
- if req.ForRent {
- where[fmt.Sprintf("id not in(select t1.id from (select * from t_house_approved where uid=%d) t1 inner join t_house_rent as t2 on t1.garden_id = t2.garden_id and t1.house_id=t2.house_id and t2.approve_status < 4)", req.Uid)] = ""
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- gardenIdsM := map[int64]bool{}
- gardenIds := []int64{}
- ghIds := [][2]int64{}
- for _, v := range list {
- ghIds = append(ghIds, [2]int64{v.GardenId, v.HouseId})
- if gardenIdsM[v.GardenId] {
- continue
- }
- gardenIdsM[v.GardenId] = true
- gardenIds = append(gardenIds, v.GardenId)
- }
- gardenInfosM, err := getGardenInfos(gardenIds)
- if err != nil {
- return nil, err
- }
- reply.List = make([]*pb_v1.HouseholdHouseInfo, len(list))
- for i, v := range list {
- ginfo := gardenInfosM[v.GardenId]
- reply.List[i] = &pb_v1.HouseholdHouseInfo{
- Id: v.ID,
- HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
- GardenName: ginfo.GardenName,
- HouseId: v.HouseId,
- GardenId: v.GardenId,
- UserType: v.UserType,
- CanPay: false,
- }
- if ginfo.PayMode == 3 {
- reply.List[i].CanPay = true
- }
- if ginfo.PayMode == 2 && ginfo.MchId != "" {
- reply.List[i].CanPay = true
- }
- }
- householdGardensToRedis(req.Uid, reply.List)
- return reply, nil
- }
|