123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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
- }
|