package visitor import ( "context" "crypto/rc4" "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" "property-device/impl/v1/gate_match" dbmodel "property-device/model" "property-device/parser" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" "time" ) const ( GateTypeCode = 1 GateTypeFace = 2 ) func checkGateVisitorAddParam(req *pb_v1.GateVisitorAddRequest) error { switch { case req.DeviceId == 0: return status.Error(10003, "设备id不能为空") case req.Uid == 0 || req.Name == "" || req.Phone == "" || req.GardenId == 0: return status.Error(10003, "不支持的门禁类型") case req.VisitorPhone == "" || req.Visitor == "" || req.Comment == "": return status.Error(10003, "访客信息不能为空") } if req.End < 1 || req.Start < 1 { return status.Error(10003, "访客邀约请选择时间段") } if req.End-req.Start > 3600*24 { return status.Error(10003, "访客二维码时间不能超过一天") } return nil } func Rc4Encrypt(plain string, key string) string { dest := make([]byte, len(plain)) cipher2, _ := rc4.NewCipher([]byte(key)) cipher2.XORKeyStream(dest, []byte(plain)) return fmt.Sprintf("%X", dest) } func GateVisitorAdd(ctx context.Context, req *pb_v1.GateVisitorAddRequest) (reply *pb_v1.GateVisitorAddReply, err error) { reply = &pb_v1.GateVisitorAddReply{} // 捕获各个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 = checkGateVisitorAddParam(req) if err != nil { return nil, err } now := time.Now() g := dbmodel.TGate{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", req.DeviceId) err = g.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if g.ID == 0 { return nil, errors.ErrRecordNotFound } whiteMatchInfo, err := gate_match.GetMatchInfoFromUserDb(g.GardenId, fmt.Sprintf("%d", req.Uid), &gate_utils.GateCommonInfo{GardenId: g.GardenId, DeviceId: g.ID}) if err != nil { return nil, err } if whiteMatchInfo.DeviceId == 0 { return nil, status.Error(10003, "您不在白名单中,请联系物业添加白名单") } p := dbmodel.TGateVisitor{ Uid: req.Uid, DeviceId: req.DeviceId, Name: req.Name, Phone: req.Phone, Visitor: req.Visitor, VisitorPhone: req.VisitorPhone, Start: req.Start, End: req.End, OpenTime: 0, CreatedAt: now, UpdatedAt: now, GardenId: req.GardenId, Comment: req.Comment, } db := database.DB().Begin() err = p.Insert(db) if err != nil { db.Rollback() return nil, errors.DataBaseError } if p.ID == 0 { db.Rollback() return nil, errors.DataBaseError } str := fmt.Sprintf("[%d-%s-%s-%d,%d,%s,%s,0,0,A]", req.Uid, req.VisitorPhone, req.Visitor, p.ID, req.GardenId, time.Unix(req.Start, 0).Format("20060102150405"), time.Unix(req.End, 0).Format("20060102150405")) qcode := "CB01" + Rc4Encrypt(str, parser.Conf.GateKey) where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", p.ID) values := map[string]interface{}{ "qcode": qcode, } err = p.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } reply.Id = p.ID reply.Qcode = qcode db.Commit() return reply, nil }