123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package application
- 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-system/errors"
- dbmodel "property-system/model"
- pb_v1 "property-system/pb/v1"
- )
- func checkGardenApplicationListParam(req *pb_v1.GardenApplicationListRequest) error {
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- if req.Page == 0 {
- req.Page = 1
- }
- return nil
- }
- //
- func GardenApplicationList(ctx context.Context, req *pb_v1.GardenApplicationListRequest) (reply *pb_v1.GardenApplicationListReply, err error) {
- reply = &pb_v1.GardenApplicationListReply{}
- // 捕获各个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 = checkGardenApplicationListParam(req)
- if err != nil {
- return nil, err
- }
- p := dbmodel.TApplicationOrder{}
- where := map[string]interface{}{}
- if req.GardenId > 0 {
- where["garden_id"] = req.GardenId
- }
- if req.Status > 0 {
- where["status"] = req.Status
- }
- 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
- }
- gardenIds := []int64{}
- garden := dbmodel.TGardenApproved{}
- for _, v := range list {
- gardenIds = append(gardenIds, v.GardenId)
- }
- where = map[string]interface{}{
- "id in": gardenIds,
- }
- glist, err := garden.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- gardenM := map[int64]string{}
- for _, v := range glist {
- gardenM[v.ID] = v.GardenName
- }
- reply.List = make([]*pb_v1.GardenApplicationItem, len(list))
- for i, v := range list {
- application := pb_v1.ApplicationInfo{}
- json.Unmarshal([]byte(v.ApplicationContent), &application)
- item := &pb_v1.GardenApplicationItem{
- Icon: application.Icon,
- Desc: application.Desc,
- Name: application.Name,
- Content: application.Content,
- Amount: v.Amount,
- Enable: application.Enable,
- ApplicationId: v.ApplicationId,
- OrderId: v.ID,
- Status: v.Status,
- PayType: v.PayType,
- Feedback: v.Feedback,
- GardenName: gardenM[v.GardenId],
- CreatedAt: v.CreatedAt.Unix(),
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|