123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package event
- 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-garden/errors"
- dbmodel "property-garden/model"
- "property-garden/parser"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- "strings"
- "time"
- )
- func householdIsSign(uid int64, eventIds []int64, dbname string) (map[int64]bool, error) {
- ret := map[int64]bool{}
- if len(eventIds) == 0 {
- return ret, nil
- }
- p := dbmodel.NewEventSign(dbname)
- where := map[string]interface{}{
- "household_uid": uid,
- "event_id in": eventIds,
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return ret, errors.DataBaseError
- }
- for _, v := range list {
- ret[v.EventId] = true
- }
- return ret, nil
- }
- //
- func EventList(ctx context.Context, req *pb_v1.EventListRequest) (reply *pb_v1.EventListReply, err error) {
- reply = &pb_v1.EventListReply{}
- // 捕获各个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 {
- return nil, errors.ParamsError
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- p := dbmodel.NewEvent(dbname)
- where := map[string]interface{}{
- //"garden_id":req.GardenId,
- }
- 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
- }
- isSignM := map[int64]bool{}
- // 小程序查询时,判断住户是否报名该活动
- if req.HouseholdUid > 0 {
- eventIds := []int64{}
- for _, v := range list {
- eventIds = append(eventIds, v.ID)
- }
- isSignM, err = householdIsSign(req.HouseholdUid, eventIds, dbname)
- if err != nil {
- return nil, err
- }
- }
- reply.List = make([]*pb_v1.EventItem, len(list))
- for i, v := range list {
- reply.List[i] = &pb_v1.EventItem{
- Id: v.ID,
- Title: v.Title,
- Content: v.Content,
- //Pic: strings.Split(v.Pic, ";"),
- Addr: v.Addr,
- Start: v.Start,
- End: v.End,
- SignUpEnd: v.SignUpEnd,
- PeopleLimit: v.PeopleLimit,
- PeopleCount: v.PeopleCount,
- Status: 1,
- CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
- Sign: isSignM[v.ID],
- Fee: v.Fee,
- }
- if v.Pic != "" {
- reply.List[i].Pic = strings.Split(v.Pic, ";")
- }
- if len(reply.List[i].Pic) == 0 {
- reply.List[i].Pic = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.RentObj}
- }
- if v.SignUpEnd <= time.Now().Unix() || v.PeopleLimit <= v.PeopleCount {
- reply.List[i].Status = 2
- }
- }
- return reply, nil
- }
|