123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 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 checkGateCardSyncParam(req *pb_v1.GateCardSyncRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Id == 0:
- return status.Error(10003, "记录id不能为空")
- }
- return nil
- }
- func getNeedUpdateDevice(recordId int64) ([]dbmodel.TGateCard, error) {
- gcard := dbmodel.TGateCard{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "record_id", recordId)
- where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
- glist, err := gcard.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- return glist, nil
- }
- func GateCardSync(ctx context.Context, req *pb_v1.GateCardSyncRequest) (reply *pb_v1.GateCardSyncReply, err error) {
- reply = &pb_v1.GateCardSyncReply{}
- // 捕获各个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 = checkGateCardSyncParam(req)
- if err != nil {
- return nil, err
- }
- up := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", req.Id)
- err = up.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if up.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if time.Now().Unix()-up.UpdatedAt.Unix() < 60 {
- return nil, status.Error(10003, "下发操作间隔时间不能小于1分钟")
- }
- gcards, err := getNeedUpdateDevice(up.ID)
- if err != nil {
- return nil, err
- }
- if len(gcards) == 0 {
- return reply, nil
- }
- deviceIds := make([]int64, len(gcards))
- for i, v := range gcards {
- deviceIds[i] = v.DeviceId
- }
- devices, err := GetDevicesByIds(deviceIds)
- if err != nil {
- return nil, err
- }
- deviceM := map[int64]dbmodel.TGate{}
- for _, v := range devices {
- deviceM[v.ID] = v
- }
- for _, v := range gcards {
- gcard := v
- device := deviceM[v.DeviceId]
- if device.ID == 0 {
- continue
- }
- whiter := NewCardWhiter(&up, &device, &gcard)
- if whiter == nil {
- continue
- }
- whiter.PersonAdd()
- if whiter.Command() {
- gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
- }
- }
- values := map[string]interface{}{
- "updated_at": time.Now(),
- }
- _ = up.Update(database.DB(), where, values)
- return reply, nil
- }
|