pic_info.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package gate_pic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "git.getensh.com/common/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "gorm.io/gorm"
  11. "property-device/errors"
  12. dbmodel "property-device/model"
  13. pb_v1 "property-device/pb/v1"
  14. "property-device/utils/gate_utils"
  15. )
  16. func checkGateUserPicInfoParam(req *pb_v1.GateUserPicInfoRequest) error {
  17. switch {
  18. case req.GardenId == 0:
  19. return status.Error(10003, "小区不能为空")
  20. case req.Uid == 0:
  21. return status.Error(10003, "人员不能为空")
  22. }
  23. return nil
  24. }
  25. func GateUserPicCheck(originStatus int32, id int64) (int32, error) {
  26. up := dbmodel.TUserPic{}
  27. gp := dbmodel.TGatePic{}
  28. where := [][2]interface{}{}
  29. where = dbmodel.WhereAdd(where, "record_id", id)
  30. where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
  31. count, err := gp.Count(database.DB(), where, nil)
  32. if err != nil {
  33. return 0, errors.DataBaseError
  34. }
  35. // 全部成功
  36. if count == 0 {
  37. where := [][2]interface{}{}
  38. where = dbmodel.WhereAdd(where, "id", id)
  39. values := map[string]interface{}{"down_status": 2}
  40. err = up.Update(database.DB(), where, values)
  41. if err != nil {
  42. return 0, errors.DataBaseError
  43. }
  44. return 2, nil
  45. }
  46. if originStatus == 1 {
  47. // 查看是否有下发失败的
  48. where = [][2]interface{}{}
  49. where = dbmodel.WhereAdd(where, "record_id", id)
  50. where = dbmodel.WhereAdd(where, "status >", gate_utils.WhiteAddStatusAllSuc)
  51. count, err = gp.Count(database.DB(), where, nil)
  52. if err != nil {
  53. return 0, errors.DataBaseError
  54. }
  55. if count > 0 {
  56. where := [][2]interface{}{}
  57. where = dbmodel.WhereAdd(where, "id", id)
  58. values := map[string]interface{}{"down_status": 3}
  59. err = up.Update(database.DB(), where, values)
  60. if err != nil {
  61. return 3, errors.DataBaseError
  62. }
  63. return 3, nil
  64. }
  65. }
  66. return 1, nil
  67. }
  68. func GateUserPicInfo(ctx context.Context, req *pb_v1.GateUserPicInfoRequest) (reply *pb_v1.GateUserPicInfoReply, err error) {
  69. reply = &pb_v1.GateUserPicInfoReply{}
  70. // 捕获各个task中的异常并返回给调用者
  71. defer func() {
  72. if r := recover(); r != nil {
  73. err = fmt.Errorf("%+v", r)
  74. e := &status.Status{}
  75. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  76. logger.Error("err",
  77. zap.String("system_err", err.Error()),
  78. zap.Stack("stacktrace"))
  79. }
  80. }
  81. }()
  82. err = checkGateUserPicInfoParam(req)
  83. if err != nil {
  84. return nil, err
  85. }
  86. up := dbmodel.TUserPic{}
  87. where := [][2]interface{}{}
  88. where = dbmodel.WhereAdd(where, "garden_id", req.GardenId)
  89. where = dbmodel.WhereAdd(where, "uid", fmt.Sprintf("%d", req.Uid))
  90. err = up.Find(database.DB(), where)
  91. if err != nil && err != gorm.ErrRecordNotFound {
  92. return nil, errors.DataBaseError
  93. }
  94. reply.PicUrl = up.PicUrl
  95. reply.ApproveStatus = up.ApproveStatus
  96. reply.Feedback = up.Feedback
  97. reply.DownStatus = up.DownStatus
  98. if up.DownStatus == 2 || up.ApproveStatus != 2 {
  99. return reply, nil
  100. }
  101. //等待状态需再次检查
  102. status, err := GateUserPicCheck(up.DownStatus, up.ID)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if status > 1 {
  107. reply.DownStatus = status
  108. }
  109. return reply, nil
  110. }