123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- package gate_pic
- 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 checkGateUserPicDelParam(req *pb_v1.GateUserPicDelRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Id == 0:
- return status.Error(10003, "记录id不能为空")
- }
- return nil
- }
- func CheckUserPicDel(recordId int64, db *gorm.DB) error {
- gp := dbmodel.TGatePic{}
- up := dbmodel.TUserPic{}
- 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 WhitePicDelFromDb(up dbmodel.TUserPic, db *gorm.DB) ([]dbmodel.TGatePic, error) {
- gp := dbmodel.TGatePic{}
- 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)
- 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 WhitePicDelFromDevice(up dbmodel.TUserPic, devices []dbmodel.TGate) {
- // 白名单添加到设备
- for _, v := range devices {
- device := v
- whiter := NewPicWhiter(&up, &device, nil)
- if whiter == nil {
- continue
- }
- whiter.PersonDel()
- if whiter.Command() {
- gate_utils.CommandCacheIncrease(device.Sn, device.Protocol)
- }
- }
- }
- func WhitePicDel(up dbmodel.TUserPic, db *gorm.DB) error {
- gplist, err := WhitePicDelFromDb(up, db)
- if err != nil {
- return err
- }
- if len(gplist) == 0 {
- return nil
- }
- deviceIds := make([]int64, len(gplist))
- for i, v := range gplist {
- deviceIds[i] = v.DeviceId
- }
- devices, err := GetDevicesByIds(deviceIds)
- if err != nil {
- return err
- }
- WhitePicDelFromDevice(up, devices)
- return nil
- }
- func GateUserPicDel(ctx context.Context, req *pb_v1.GateUserPicDelRequest) (reply *pb_v1.GateUserPicDelReply, err error) {
- reply = &pb_v1.GateUserPicDelReply{}
- // 捕获各个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 = checkGateUserPicDelParam(req)
- if err != nil {
- return nil, err
- }
- up := dbmodel.TUserPic{}
- 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 = WhitePicDel(up, db)
- if err != nil {
- db.Rollback()
- return nil, err
- }
- db.Commit()
- return reply, nil
- }
|