12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package gate_command
- 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 checkGateCommandListParam(req *pb_v1.GateCommandListRequest) error {
- switch {
- case req.DeviceId == 0:
- return status.Error(10003, "设备id不能为空")
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- return nil
- }
- func GateCommandList(ctx context.Context, req *pb_v1.GateCommandListRequest) (reply *pb_v1.GateCommandListReply, err error) {
- reply = &pb_v1.GateCommandListReply{}
- // 捕获各个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 = checkGateCommandListParam(req)
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGateCommand{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
- where = dbmodel.WhereAdd(where, "code in", []int{1, 2})
- if req.Start > 0 {
- where = dbmodel.WhereAdd(where, "created_at >=", req.Start)
- }
- if req.End > 0 {
- where = dbmodel.WhereAdd(where, "created_at <", req.End)
- }
- if req.Status > 0 {
- where = dbmodel.WhereAdd(where, "status", req.Status)
- }
- reply.Page = req.Page
- reply.Total, err = p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- 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.GateCommandItem, len(list))
- for i, v := range list {
- reply.List[i] = &pb_v1.GateCommandItem{
- DeviceId: v.DeviceId,
- Desc: v.Desc,
- // 1 待执行 2 执行中 3 执行完成
- Status: v.Status,
- CreatedAt: v.CreatedAt.Unix(),
- // 1 成功 2 失败
- ResultStatus: v.ResultStatus,
- // 结果描述
- ResultStatusDesc: v.ResultStatusDesc,
- Id: v.ID,
- }
- }
- return reply, nil
- }
|