package gate_record import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-device/model" pb_v1 "property-device/pb/v1" "time" ) func checkGateRecordAddParam(req *pb_v1.GateRecordAddRequest) error { switch { case req.DeviceId == 0: return status.Error(10003, "设备id不能为空") case req.HouseholdUid == 0 && req.CardNumber == "": return status.Error(10003, "用户id和ic卡号不能同时为空") case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.OpenTime == 0: return status.Error(10003, "开门时间不能为空") case req.OpenType == 0: return status.Error(10003, "识别方式不能为空") } return nil } func GateRecordAdd(ctx context.Context, req *pb_v1.GateRecordAddRequest) (reply *pb_v1.GateRecordAddReply, err error) { reply = &pb_v1.GateRecordAddReply{} // 捕获各个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 = checkGateRecordAddParam(req) if err != nil { return nil, err } tags := map[string]string{"device_id": fmt.Sprintf("%d", req.DeviceId)} fields := map[string]interface{}{ "location": req.Location, "direction": req.Direction, "household_user": req.HouseholdUser, "household_id_number": req.HouseholdIdNumber, "household_housename": req.HouseholdHousename, "household_uid": req.HouseholdUid, "card_number": req.CardNumber, "card_owner": req.CardOwner, "online": req.Online, "garden_id": req.GardenId, "is_visitor": req.IsVisitor, "open_time": req.OpenTime, "visitor_name": req.VisitorName, "visitor_phone": req.VisitorPhone, "sn": req.Sn, "protocol": req.Protocol, "open_type": req.OpenType, } err = model.WriteGateData(fmt.Sprintf("%d", req.DeviceId), tags, fields, time.Now()) if err != nil { return nil, err } return reply, nil }