123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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"
- "property-device/impl/v1/oss_utils"
- dbmodel "property-device/model"
- "property-device/pb"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- "time"
- )
- func checkGateUserPicAddParam(req *pb_v1.GateUserPicAddRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Uid == 0:
- return status.Error(10003, "人员不能为空")
- case req.Name == "":
- return status.Error(10003, "姓名不能为空")
- case req.PicUrl == "":
- return status.Error(10003, "照片不能为空")
- }
- return nil
- }
- const SystemMsgCodeFaceAdd = "8"
- func picMsgAdd(gardenId int64) {
- mreq := pb_v1.SystemMsgAddRequest{
- GardenId: gardenId,
- Content: "有新的人脸申请待审批",
- Code: SystemMsgCodeFaceAdd,
- Uid: 0,
- }
- pb.Garden.SystemMsgAdd(context.Background(), &mreq)
- }
- func GateUserPicAdd(ctx context.Context, req *pb_v1.GateUserPicAddRequest) (reply *pb_v1.GateUserPicAddReply, err error) {
- reply = &pb_v1.GateUserPicAddReply{}
- // 捕获各个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 = checkGateUserPicAddParam(req)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- unitInfo, houseNames, userType, err := gate_utils.GetUserUnitIds(req.GardenId, []int64{req.Uid})
- if err != nil {
- return nil, err
- }
- if len(unitInfo[req.Uid]) == 0 {
- return nil, status.Error(10003, "请先绑定房屋")
- }
- gunit := dbmodel.TGateUnit{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- where = dbmodel.WhereAdd(where, "unit_id in", unitInfo[req.Uid])
- // 支持人脸的设备
- protocols := []int32{}
- for k, v := range gate_utils.GateProtocolFuntionMap {
- if v[1] == 1 {
- protocols = append(protocols, k)
- }
- }
- where = dbmodel.WhereAdd(where, "protocol in", protocols)
- count, err := gunit.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if count == 0 {
- return nil, status.Error(10003, "当前无可用设备")
- }
- p := &dbmodel.TUserPic{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
- where = dbmodel.WhereAdd(where, "uid", req.Uid)
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if p.ID > 0 && p.ApproveStatus == 2 {
- return nil, status.Error(10003, "已存在审核通过的记录,不能重复申请,请联系管理人员")
- }
- if p.ID > 0 && p.ApproveStatus == 1 {
- return nil, status.Error(10003, "已存在待审核的记录,不能重复申请,请联系管理人员")
- }
- if p.ID > 0 {
- values := map[string]interface{}{
- "name": req.Name,
- "id_number": req.IdNumber,
- "pic_url": req.PicUrl,
- "approve_status": 1,
- "updated_at": now,
- "down_status": 1,
- "user_type": userType,
- }
- db := database.DB().Begin()
- err = p.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- if err = oss_utils.OssObjAdd([]string{req.PicUrl}, nil); err != nil {
- db.Rollback()
- return nil, err
- }
- db.Commit()
- picMsgAdd(req.GardenId)
- return reply, nil
- }
- p.PicUrl = req.PicUrl
- p.GardenId = req.GardenId
- p.Name = req.Name
- p.Uid = fmt.Sprintf("%d", req.Uid)
- p.IdNumber = req.IdNumber
- p.ApproveStatus = 1
- p.CreatedAt = now
- p.UpdatedAt = now
- p.DownStatus = 1
- p.Phone = req.Phone
- p.HouseName = houseNames[req.Uid]
- p.UserType = userType
- db := database.DB().Begin()
- err = p.Insert(db)
- if err != nil {
- db.Rollback()
- return nil, err
- }
- if err = oss_utils.OssObjAdd([]string{req.PicUrl}, nil); err != nil {
- db.Rollback()
- return nil, err
- }
- db.Commit()
- picMsgAdd(req.GardenId)
- return reply, nil
- }
|