official.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/member/model"
  6. "github.com/jinzhu/gorm"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _officialName = "user_official"
  11. _officialDocName = "user_official_doc"
  12. _officialDocAdditName = "user_official_doc_addit"
  13. )
  14. // Official is.
  15. func (d *Dao) Official(ctx context.Context, mid int64) (off *model.Official, err error) {
  16. off = &model.Official{}
  17. if err = d.member.Table(_officialName).Where("mid=?", mid).Find(off).Error; err != nil {
  18. err = errors.Wrap(err, "official docs")
  19. }
  20. return
  21. }
  22. // OfficialEdit is.
  23. func (d *Dao) OfficialEdit(ctx context.Context, mid int64, role int8, title, desc string) (off *model.Official, err error) {
  24. off = &model.Official{
  25. Mid: mid,
  26. }
  27. attrs := map[string]interface{}{
  28. "role": role,
  29. "title": title,
  30. "description": desc,
  31. }
  32. if err = d.member.Table(_officialName).Where("mid=?", mid).Assign(attrs).FirstOrCreate(off).Error; err != nil {
  33. err = errors.Wrap(err, "official edit")
  34. }
  35. return
  36. }
  37. // Officials is.
  38. func (d *Dao) Officials(ctx context.Context, mid int64, roles []int8, stime, etime time.Time, pn, ps int) (offs []*model.Official, total int, err error) {
  39. where := "role in (?) AND ctime>? AND ctime<?"
  40. if mid > 0 {
  41. where = "mid=? AND " + where
  42. err = d.member.Table(_officialName).Order("ctime DESC").Offset((pn-1)*ps).Limit(ps).Where(where, mid, roles, stime, etime).Find(&offs).Error
  43. d.member.Table(_officialName).Where(where, mid, roles, stime, etime).Count(&total)
  44. } else {
  45. err = d.member.Table(_officialName).Order("ctime DESC").Offset((pn-1)*ps).Limit(ps).Where(where, roles, stime, etime).Find(&offs).Error
  46. d.member.Table(_officialName).Where(where, roles, stime, etime).Count(&total)
  47. }
  48. if err != nil {
  49. err = errors.Wrap(err, "official docs")
  50. }
  51. return
  52. }
  53. // OfficialDoc is.
  54. func (d *Dao) OfficialDoc(ctx context.Context, mid int64) (off *model.OfficialDoc, err error) {
  55. off = &model.OfficialDoc{OfficialExtra: &model.OfficialExtra{}}
  56. if err = d.member.Table(_officialDocName).Where("mid=?", mid).First(off).Error; err != nil {
  57. if err == gorm.ErrRecordNotFound {
  58. return nil, nil
  59. }
  60. err = errors.Wrap(err, "official doc")
  61. off = nil
  62. return
  63. }
  64. off.ParseExtra()
  65. return
  66. }
  67. // OfficialDocs is.
  68. func (d *Dao) OfficialDocs(ctx context.Context, mid int64, roles, states []int8, uname string, stime, etime time.Time, pn, ps int) (offs []*model.OfficialDoc, total int, err error) {
  69. query := d.member.Table(_officialDocName).Order("submit_time DESC")
  70. if uname != "" {
  71. query = query.Where("uname=?", uname)
  72. }
  73. if mid > 0 {
  74. query = query.Where("mid=?", mid)
  75. }
  76. query = query.Where("role IN (?)", roles).
  77. Where("state IN (?)", states).
  78. Where("ctime > ?", stime).
  79. Where("ctime < ?", etime)
  80. query.Count(&total)
  81. if err = query.Offset((pn - 1) * ps).Limit(ps).Find(&offs).Error; err != nil {
  82. err = errors.Wrap(err, "official docs")
  83. return
  84. }
  85. for _, od := range offs {
  86. od.ParseExtra()
  87. }
  88. return
  89. }
  90. // OfficialDocsByMids is.
  91. func (d *Dao) OfficialDocsByMids(ctx context.Context, mids []int64) (map[int64]*model.OfficialDoc, error) {
  92. ofl := make([]*model.OfficialDoc, 0, len(mids))
  93. offs := make(map[int64]*model.OfficialDoc, len(mids))
  94. if err := d.member.Table(_officialDocName).Where("mid IN (?)", mids).Find(&ofl).Error; err != nil {
  95. err = errors.Wrap(err, "official docs")
  96. return nil, err
  97. }
  98. for _, of := range ofl {
  99. offs[of.Mid] = of
  100. }
  101. for _, od := range offs {
  102. od.ParseExtra()
  103. }
  104. return offs, nil
  105. }
  106. // OfficialDocAudit is.
  107. func (d *Dao) OfficialDocAudit(ctx context.Context, mid int64, state int8, uname string, isInternal bool, rejectReason string) (err error) {
  108. ups := map[string]interface{}{
  109. "state": state,
  110. "uname": uname,
  111. "is_internal": isInternal,
  112. "reject_reason": rejectReason,
  113. }
  114. if err = d.member.Table(_officialDocName).Where("mid=?", mid).Updates(ups).Error; err != nil {
  115. err = errors.Wrap(err, "official doc audit")
  116. }
  117. return
  118. }
  119. // OfficialDocEdit is.
  120. func (d *Dao) OfficialDocEdit(ctx context.Context, mid int64, name string, role, state int8, title, desc, extra string, uname string, isInternal bool) (err error) {
  121. off := &model.OfficialDoc{
  122. Mid: mid,
  123. SubmitSource: "admin",
  124. }
  125. ups := map[string]interface{}{
  126. "state": state,
  127. "role": role,
  128. "name": name,
  129. "title": title,
  130. "description": desc,
  131. "extra": extra,
  132. "uname": uname,
  133. "is_internal": isInternal,
  134. }
  135. if err = d.member.Table(_officialDocName).Where("mid=?", mid).Assign(ups).FirstOrCreate(off).Error; err != nil {
  136. err = errors.Wrap(err, "official doc audit")
  137. }
  138. return
  139. }
  140. // OfficialDocSubmit is.
  141. func (d *Dao) OfficialDocSubmit(ctx context.Context, mid int64, name string, role, state int8, title, desc, extra string, uname string, isInternal bool, submitSource string) (err error) {
  142. off := &model.OfficialDoc{
  143. Mid: mid,
  144. }
  145. ups := map[string]interface{}{
  146. "state": state,
  147. "role": role,
  148. "name": name,
  149. "title": title,
  150. "description": desc,
  151. "extra": extra,
  152. "uname": uname,
  153. "is_internal": isInternal,
  154. "submit_source": submitSource,
  155. }
  156. if err = d.member.Table(_officialDocName).Where("mid=?", mid).Assign(ups).FirstOrCreate(off).Error; err != nil {
  157. err = errors.Wrap(err, "official doc audit")
  158. }
  159. return
  160. }
  161. // OfficialDocAddits .
  162. func (d *Dao) OfficialDocAddits(ctx context.Context, property string, vstring string) ([]*model.OfficialDocAddit, error) {
  163. addits := make([]*model.OfficialDocAddit, 0)
  164. err := d.member.Table(_officialDocAdditName).Where("property=? and vstring=?", property, vstring).Find(&addits).Error
  165. if err != nil {
  166. err = errors.Wrap(err, "find official doc addit error")
  167. return nil, err
  168. }
  169. return addits, nil
  170. }