review.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/member/model"
  6. "go-common/library/database/sql"
  7. xtime "go-common/library/time"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _reviewName = "user_property_review"
  12. )
  13. // Reviews is. todo delete
  14. func (d *Dao) Reviews(ctx context.Context, mid int64, property []int8, state []int8, isMonitor, isDesc bool, operator string, stime, etime xtime.Time, pn, ps int) ([]*model.UserPropertyReview, int, error) {
  15. from, to, err := d.prepareReviewRange(ctx, stime, etime)
  16. if err != nil {
  17. return nil, 0, err
  18. }
  19. order := "asc"
  20. if isDesc {
  21. order = "desc"
  22. }
  23. rws := []*model.UserPropertyReview{}
  24. total := 0
  25. query := d.member.Table(_reviewName).
  26. Order("id "+order).
  27. Where("id>=?", from).
  28. Where("id<=?", to).
  29. Where("is_monitor=?", isMonitor)
  30. if mid > 0 {
  31. query = query.Where("mid=?", mid)
  32. }
  33. if len(property) > 0 {
  34. query = query.Where("property IN (?)", property)
  35. }
  36. if operator != "" {
  37. query = query.Where("operator=?", operator)
  38. }
  39. if len(state) > 0 {
  40. query = query.Where("state IN (?)", state)
  41. }
  42. query = query.Count(&total)
  43. query = query.Offset((pn - 1) * ps).Limit(ps)
  44. if err := query.Find(&rws).Error; err != nil {
  45. if err == sql.ErrNoRows {
  46. return []*model.UserPropertyReview{}, 0, nil
  47. }
  48. err = errors.Wrap(err, "reviews")
  49. return nil, 0, err
  50. }
  51. for _, rw := range rws {
  52. if rw.Property == model.ReviewPropertyFace {
  53. rw.BuildFaceURL()
  54. }
  55. }
  56. return rws, total, nil
  57. }
  58. func (d *Dao) prepareReviewRange(ctx context.Context, stime, etime xtime.Time) (int64, int64, error) {
  59. // from id
  60. rw := &model.UserPropertyReview{}
  61. if err := d.member.Table(_reviewName).
  62. Select("id").
  63. Where("ctime>?", stime).
  64. Order("ctime asc").
  65. Limit(1).
  66. Find(rw).Error; err != nil {
  67. return 0, 0, err
  68. }
  69. from := rw.ID
  70. // to id
  71. rw = &model.UserPropertyReview{}
  72. if err := d.member.Table(_reviewName).
  73. Select("id").
  74. Where("ctime<?", etime).
  75. Order("ctime desc").
  76. Limit(1).
  77. Find(rw).Error; err != nil {
  78. return 0, 0, err
  79. }
  80. to := rw.ID
  81. return from, to, nil
  82. }
  83. // ReviewAudit is.
  84. func (d *Dao) ReviewAudit(ctx context.Context, id []int64, state int8, remark, operator string) error {
  85. ups := map[string]interface{}{
  86. "state": state,
  87. "remark": remark,
  88. "operator": operator,
  89. }
  90. if err := d.member.Table(_reviewName).Where("id IN (?)", id).Where("state=?", model.ReviewStateWait).UpdateColumns(ups).Error; err != nil {
  91. return errors.Wrap(err, "review audit")
  92. }
  93. return nil
  94. }
  95. // Review is.
  96. func (d *Dao) Review(ctx context.Context, id int64) (*model.UserPropertyReview, error) {
  97. r := &model.UserPropertyReview{}
  98. if err := d.member.Table(_reviewName).Where("id=?", id).First(r).Error; err != nil {
  99. return nil, err
  100. }
  101. return r, nil
  102. }
  103. // ReviewByIDs is.
  104. func (d *Dao) ReviewByIDs(ctx context.Context, ids []int64, state []int8) ([]*model.UserPropertyReview, error) {
  105. rws := []*model.UserPropertyReview{}
  106. query := d.member.Table(_reviewName).Where("id IN (?)", ids)
  107. if len(state) > 0 {
  108. query = query.Where("state in (?)", state)
  109. }
  110. if err := query.Find(&rws).Error; err != nil {
  111. if err == sql.ErrNoRows {
  112. return []*model.UserPropertyReview{}, nil
  113. }
  114. err = errors.Wrap(err, "review by ids")
  115. return nil, err
  116. }
  117. return rws, nil
  118. }
  119. // UpdateReviewFace is.
  120. func (d *Dao) UpdateReviewFace(ctx context.Context, id int64, face string) error {
  121. ups := map[string]interface{}{
  122. "new": face,
  123. }
  124. if err := d.member.Table(_reviewName).
  125. Where("id=?", id).
  126. Where("property=?", model.ReviewPropertyFace).
  127. Update(ups).Error; err != nil {
  128. return errors.WithStack(err)
  129. }
  130. return nil
  131. }
  132. // MvArchivedFaceToPriv mvArchivedFaceToPriv.
  133. func (d *Dao) MvArchivedFaceToPriv(ctx context.Context, face, privFace, operator, remark string) error {
  134. ups := map[string]interface{}{
  135. "new": privFace,
  136. "remark": fmt.Sprintf("将归档图片移动到新bucket, face: %s,remark: %s,operator: %s", face, remark, operator),
  137. }
  138. if err := d.member.Table(_reviewName).
  139. Where("new=?", face).
  140. Where("property=?", model.ReviewPropertyFace).
  141. Where("state=?", model.ReviewStateArchived).
  142. Where("is_monitor=?", false).
  143. Update(ups).Error; err != nil {
  144. return errors.WithStack(err)
  145. }
  146. return nil
  147. }
  148. // IncrFaceReject incrFaceReject.
  149. func (d *Dao) IncrFaceReject(ctx context.Context, mid int64) error {
  150. if err := d.member.Exec("INSERT INTO user_addit (mid,face_reject)VALUES(?,1) ON DUPLICATE KEY UPDATE face_reject=face_reject+1", mid).Error; err != nil {
  151. return errors.Wrapf(err, "mid: %d", mid)
  152. }
  153. return nil
  154. }
  155. // IncrViolationCount is.
  156. func (d *Dao) IncrViolationCount(ctx context.Context, mid int64) error {
  157. if err := d.member.Exec("INSERT INTO user_addit (mid,violation_count)VALUES(?,1) ON DUPLICATE KEY UPDATE violation_count=violation_count+1", mid).Error; err != nil {
  158. return errors.Wrapf(err, "mid: %d", mid)
  159. }
  160. return nil
  161. }
  162. // FaceAutoPass is.
  163. func (d *Dao) FaceAutoPass(ctx context.Context, ids []int64, etime xtime.Time) (err error) {
  164. ups := map[string]interface{}{
  165. "operator": "system/auto",
  166. "remark": "48 小时未处理自动通过",
  167. "state": model.ReviewStatePass,
  168. }
  169. if err = d.member.Table(_reviewName).
  170. Where("state in (?)", []int8{model.ReviewStateWait, model.ReviewStateQueuing}).
  171. Where("id in (?)", ids).
  172. Where("mtime<?", etime).
  173. Update(ups).Error; err != nil {
  174. err = errors.WithStack(err)
  175. return
  176. }
  177. return
  178. }
  179. // UpdateRemark is.
  180. func (d *Dao) UpdateRemark(ctx context.Context, id int64, remark string) (err error) {
  181. ups := map[string]interface{}{
  182. "remark": remark,
  183. }
  184. if err := d.member.Table(_reviewName).
  185. Where("id=?", id).
  186. Update(ups).Error; err != nil {
  187. return errors.WithStack(err)
  188. }
  189. return nil
  190. }
  191. //QueuingFaceReviewsByTime is.
  192. func (d *Dao) QueuingFaceReviewsByTime(c context.Context, stime, etime xtime.Time) ([]*model.UserPropertyReview, error) {
  193. rws := []*model.UserPropertyReview{}
  194. if err := d.member.Table(_reviewName).
  195. Where("ctime>? ", stime).
  196. Where("ctime<? ", etime).
  197. Where("property=?", model.ReviewPropertyFace).
  198. Where("state=?", model.ReviewStateQueuing).
  199. Find(&rws).Error; err != nil {
  200. return nil, err
  201. }
  202. return rws, nil
  203. }
  204. //AuditQueuingFace is
  205. func (d *Dao) AuditQueuingFace(c context.Context, id int64, remark string, state int8) error {
  206. ups := map[string]interface{}{
  207. "remark": remark,
  208. "state": state,
  209. "operator": "system",
  210. }
  211. if err := d.member.Table(_reviewName).
  212. Where("id=?", id).
  213. Where("state=?", model.ReviewStateQueuing).
  214. Update(ups).Error; err != nil {
  215. return errors.WithStack(err)
  216. }
  217. return nil
  218. }