123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package gate_unit
- 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-device/errors"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- )
- func checkGateUnitListParam(req *pb_v1.GateUnitListRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.DeviceId == 0:
- return status.Error(10003, "设备不能为空")
- }
- return nil
- }
- func GateUnitList(ctx context.Context, req *pb_v1.GateUnitListRequest) (reply *pb_v1.GateUnitListReply, err error) {
- reply = &pb_v1.GateUnitListReply{}
- // 捕获各个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 = checkGateUnitListParam(req)
- if err != nil {
- return nil, err
- }
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGateUnit{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.UnitId = make([]int64, len(list))
- for i, v := range list {
- reply.UnitId[i] = v.UnitId
- }
- return reply, nil
- }
|