package gate_card 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" "gorm.io/gorm" "property-device/errors" dbmodel "property-device/model" pb_v1 "property-device/pb/v1" "property-device/utils" "property-device/utils/gate_utils" "time" ) func checkGateCardAddParam(req *pb_v1.GateCardAddRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case len(req.DeviceIds) == 0: return status.Error(10003, "设备不能为空") case req.CardNumber == "": return status.Error(10003, "卡号不能为空") } if req.Name == "" { req.Name = "匿名用户" } return nil } func GetDevicesByIds(ids []int64) ([]dbmodel.TGate, error) { g := dbmodel.TGate{} if len(ids) == 0 { return []dbmodel.TGate{}, nil } where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id in", ids) list, err := g.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } return list, nil } func getNeedAddCardDevices(req *pb_v1.GateCardAddRequest, recordId int64) ([]dbmodel.TGate, error) { gcard := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", recordId) gclist, err := gcard.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } added := map[int64]bool{} for _, v := range gclist { added[v.DeviceId] = true } needAddDeviceIds := []int64{} for _, v := range req.DeviceIds { if added[v] { continue } needAddDeviceIds = append(needAddDeviceIds, v) } if len(needAddDeviceIds) == 0 { return nil, nil } needAddDevices, err := GetDevicesByIds(needAddDeviceIds) if err != nil { return nil, err } return needAddDevices, nil } func gernerateCardUid() string { return fmt.Sprintf("auto-%d-%s", time.Now().Unix(), utils.GenerateRandomStr(6, "mix")) } func checkCardExist(req *pb_v1.GateCardAddRequest) (*dbmodel.TUserCard, error) { ucard := dbmodel.TUserCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) where = dbmodel.WhereAdd(where, "card_number", req.CardNumber) err := ucard.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } now := time.Now() if ucard.ID == 0 { ucard.Uid = gernerateCardUid() ucard.Name = req.Name ucard.CardNumber = req.CardNumber ucard.DownStatus = 1 ucard.HouseId = 0 ucard.HouseName = "" ucard.CreatedAt = now ucard.UpdatedAt = now ucard.GardenId = req.GardenId err = ucard.Insert(database.DB()) if err != nil { return nil, errors.DataBaseError } } return &ucard, nil } func GateCardAdd(ctx context.Context, req *pb_v1.GateCardAddRequest) (reply *pb_v1.GateCardAddReply, err error) { reply = &pb_v1.GateCardAddReply{} // 捕获各个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 = checkGateCardAddParam(req) if err != nil { return nil, err } now := time.Now() ucard, err := checkCardExist(req) if err != nil { return nil, err } // 获取设备 devices, err := getNeedAddCardDevices(req, ucard.ID) if err != nil { return nil, err } if len(devices) == 0 { return reply, nil } // 添加关联记录 db := database.DB().Begin() array := make([]dbmodel.TGateCard, len(devices)) for i, v := range devices { array[i].DeviceId = v.ID array[i].RecordId = ucard.ID array[i].Status = 1 array[i].Sn = v.Sn array[i].Protocol = v.Protocol array[i].CreatedAt = now array[i].UpdatedAt = now if v.Protocol == gate_utils.GateProtocolSaiboMqttV1 { array[i].Status = gate_utils.WhiteAddStatusAllSuc } } gcard := dbmodel.TGateCard{} err = gcard.InsertMulti(db, &array) if err != nil { db.Rollback() return nil, err } db.Commit() for i, v := range array { gcard := v device := devices[i] whiter := NewCardWhiter(ucard, &device, &gcard) if whiter == nil { continue } whiter.PersonAdd() if whiter.Command() { gate_utils.CommandCacheIncrease(v.Sn, v.Protocol) } } return reply, nil }