123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package gate
- 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"
- "time"
- )
- func checkGateRecoveryParam(req *pb_v1.GateRecoveryRequest) error {
- switch {
- case req.DeviceId == 0:
- return status.Error(10003, "设备id不能为空")
- }
- return nil
- }
- func GateRecovery(ctx context.Context, req *pb_v1.GateRecoveryRequest) (reply *pb_v1.GateRecoveryReply, err error) {
- reply = &pb_v1.GateRecoveryReply{}
- // 捕获各个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 = checkGateRecoveryParam(req)
- if err != nil {
- return nil, err
- }
- p := &dbmodel.TGate{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", req.DeviceId)
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if p.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if p.GardenId == 0 {
- return reply, nil
- }
- if p.Enable == 1 {
- return nil, status.Error(10003, "设备未禁用无法回收")
- }
- now := time.Now()
- values := map[string]interface{}{
- "garden_id": 0,
- "enable": 1,
- "updated_at": now,
- "out_time": 0,
- "out_user": "",
- }
- db := database.DB().Begin()
- err = p.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", req.DeviceId)
- gu := dbmodel.TGateUnit{}
- gc := dbmodel.TGateCommand{}
- gv := dbmodel.TGateVisitor{}
- gp := dbmodel.TGatePic{}
- err = gu.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- err = gc.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- err = gv.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- err = gp.Delete(db, where)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- //todo commandCacheDecrease(req.DeviceId)
- db.Commit()
- return reply, nil
- }
|