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/gate_utils" "time" ) func checkGateCardDelParam(req *pb_v1.GateCardDelRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.Id == 0: return status.Error(10003, "记录id不能为空") } return nil } func CheckUserCardDel(recordId int64, db *gorm.DB) error { gp := dbmodel.TGateCard{} up := dbmodel.TUserCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", recordId) count, err := gp.Count(database.DB(), where, nil) if err != nil { return errors.DataBaseError } if count > 0 { return nil } where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", recordId) err = up.Delete(database.DB(), where) if err != nil { return errors.DataBaseError } return nil } func WhiteCardDelFromDb(up dbmodel.TUserCard, db *gorm.DB) ([]dbmodel.TGateCard, error) { gp := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", up.ID) where = dbmodel.WhereAdd(where, "status >", gate_utils.WhiteAddStatusAllSuc) err := gp.Delete(db, where) if err != nil { return nil, errors.DataBaseError } t := time.Now().Add(-60 * time.Second) where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", up.ID) where = dbmodel.WhereAdd(where, "status", gate_utils.WhiteAddStatusWait) where = dbmodel.WhereAdd(where, "created_at <", t.Format("2006-01-02 15:04:05")) err = gp.Delete(db, where) if err != nil { return nil, errors.DataBaseError } where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", up.ID) where = dbmodel.WhereAdd(where, "protocol", gate_utils.GateProtocolSaiboMqttV1) err = gp.Delete(db, where) if err != nil { return nil, errors.DataBaseError } where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", up.ID) glist, err := gp.List(db, where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } if len(glist) > 0 { return glist, nil } where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", up.ID) err = up.Delete(db, where) if err != nil { return nil, errors.DataBaseError } return nil, nil } func WhiteCardDelFromDevice(up dbmodel.TUserCard, devices []dbmodel.TGate, gm map[int64]dbmodel.TGateCard) { for _, v := range devices { device := v gcard := gm[v.ID] whiter := NewCardWhiter(&up, &device, &gcard) if whiter == nil { continue } whiter.PersonDel() if whiter.Command() { gate_utils.CommandCacheIncrease(device.Sn, device.Protocol) } } } func WhiteCardDel(up dbmodel.TUserCard, db *gorm.DB) error { gplist, err := WhiteCardDelFromDb(up, db) if err != nil { return err } if len(gplist) == 0 { return nil } deviceIds := make([]int64, len(gplist)) gm := map[int64]dbmodel.TGateCard{} for i, v := range gplist { deviceIds[i] = v.DeviceId gm[v.DeviceId] = v } devices, err := GetDevicesByIds(deviceIds) if err != nil { return err } WhiteCardDelFromDevice(up, devices, gm) return nil } func GateCardDel(ctx context.Context, req *pb_v1.GateCardDelRequest) (reply *pb_v1.GateCardDelReply, err error) { reply = &pb_v1.GateCardDelReply{} // 捕获各个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 = checkGateCardDelParam(req) if err != nil { return nil, err } up := dbmodel.TUserCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", req.Id) where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) err = up.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if up.ID == 0 { return nil, errors.ErrRecordNotFound } db := database.DB().Begin() err = WhiteCardDel(up, db) if err != nil { db.Rollback() return nil, errors.DataBaseError } db.Commit() return reply, nil }