123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package house_rent
- 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"
- "property-household/errors"
- dbmodel "property-household/model"
- pb_v1 "property-household/pb/v1"
- "time"
- )
- //
- func HouseRentAppointmentList(ctx context.Context, req *pb_v1.HouseRentAppointmentListRequest) (reply *pb_v1.HouseRentAppointmentListReply, err error) {
- reply = &pb_v1.HouseRentAppointmentListReply{}
- // 捕获各个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"))
- }
- }
- }()
- if req.GardenId < 1 && req.Uid < 1 {
- return nil, errors.ParamsError
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- p := dbmodel.THouseRentAppointment{}
- where := map[string]interface{}{}
- if req.GardenId > 0 {
- where["garden_id"] = req.GardenId
- }
- if req.RentId > 0 {
- where["rent_id"] = req.RentId
- }
- if req.Uid > 0 {
- where["uid"] = req.Uid
- }
- if req.HouseName != "" {
- where["house_name"] = req.HouseName
- }
- if req.Name != "" {
- where["name like"] = "%" + req.Name + "%"
- }
- if req.Phone != "" {
- where["phone"] = req.Phone
- }
- reply.Total, err = p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.Page = req.Page
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = make([]*pb_v1.HouseRentAppointmentItem, len(list))
- gardenInfosM := map[int64]pb_v1.GardenItem{}
- // 小程序需获取小区名
- if req.Uid > 0 {
- gardenIdsM := map[int64]bool{}
- gardenIds := []int64{}
- for _, v := range list {
- if gardenIdsM[v.GardenId] {
- continue
- }
- gardenIdsM[v.GardenId] = true
- gardenIds = append(gardenIds, v.GardenId)
- }
- gardenInfosM, err = getGardenInfos(gardenIds)
- if err != nil {
- return nil, err
- }
- }
- now := time.Now()
- nowday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
- for i, v := range list {
- reply.List[i] = &pb_v1.HouseRentAppointmentItem{
- Id: v.ID,
- Name: v.Name,
- Phone: v.Phone,
- RentId: v.RentId,
- Uid: v.Uid,
- AppointmentTime: v.AppointmentTime,
- HouseName: v.HouseName,
- Area: v.Area,
- Layer: v.Layer,
- Status: v.Status,
- GardenId: v.GardenId,
- GardenName: gardenInfosM[v.GardenId].GardenName,
- }
- if v.Status == HouseRentAppointmentWait && v.AppointmentTime < nowday.Unix() && v.AppointmentTime > 0 {
- reply.List[i].Status = HouseRentAppointmentExpired
- }
- }
- return reply, nil
- }
|