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" ) func checkGateUserPicInfoParam(req *pb_v1.GateUserPicInfoRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.Uid == 0: return status.Error(10003, "人员不能为空") } return nil } func GateUserPicCheck(originStatus int32, id int64) (int32, error) { up := dbmodel.TUserPic{} gp := dbmodel.TGatePic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", id) where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc) count, err := gp.Count(database.DB(), where, nil) if err != nil { return 0, errors.DataBaseError } // 全部成功 if count == 0 { where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", id) values := map[string]interface{}{"down_status": 2} err = up.Update(database.DB(), where, values) if err != nil { return 0, errors.DataBaseError } return 2, nil } if originStatus == 1 { // 查看是否有下发失败的 where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", id) where = dbmodel.WhereAdd(where, "status >", gate_utils.WhiteAddStatusAllSuc) count, err = gp.Count(database.DB(), where, nil) if err != nil { return 0, errors.DataBaseError } if count > 0 { where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", id) values := map[string]interface{}{"down_status": 3} err = up.Update(database.DB(), where, values) if err != nil { return 3, errors.DataBaseError } return 3, nil } } return 1, nil } func GateUserPicInfo(ctx context.Context, req *pb_v1.GateUserPicInfoRequest) (reply *pb_v1.GateUserPicInfoReply, err error) { reply = &pb_v1.GateUserPicInfoReply{} // 捕获各个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 = checkGateUserPicInfoParam(req) if err != nil { return nil, err } up := dbmodel.TUserPic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) where = dbmodel.WhereAdd(where, "uid", fmt.Sprintf("%d", req.Uid)) err = up.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } reply.PicUrl = up.PicUrl reply.ApproveStatus = up.ApproveStatus reply.Feedback = up.Feedback reply.DownStatus = up.DownStatus if up.DownStatus == 2 || up.ApproveStatus != 2 { return reply, nil } //等待状态需再次检查 status, err := GateUserPicCheck(up.DownStatus, up.ID) if err != nil { return nil, err } if status > 1 { reply.DownStatus = status } return reply, nil }