guest.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/common/openplatform/random"
  7. item "go-common/app/service/openplatform/ticket-item/api/grpc/v1"
  8. "go-common/app/service/openplatform/ticket-item/model"
  9. "go-common/library/database/elastic"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. // AddGuest 添加新嘉宾
  14. func (d *Dao) AddGuest(c context.Context, info *item.GuestInfoRequest) (ret bool, err error) {
  15. // 此处guest_id调用订单号生成器
  16. guestID := random.Uniqid(19)
  17. // 插入新数据
  18. if dbErr := d.db.Create(&model.Guest{
  19. Name: info.Name,
  20. GuestImg: info.GuestImg,
  21. Description: info.Description,
  22. GuestID: guestID,
  23. }).Error; dbErr != nil {
  24. log.Error("insertion failed")
  25. return false, ecode.NothingFound
  26. }
  27. return true, nil
  28. }
  29. // UpdateGuest 编辑嘉宾信息
  30. func (d *Dao) UpdateGuest(c context.Context, info *item.GuestInfoRequest) (res bool, err error) {
  31. // find original guest with id
  32. var oriGuest model.Guest
  33. if dbErr := d.db.First(&oriGuest, info.ID).Error; dbErr != nil {
  34. log.Error("guest(%d) not found", info.ID)
  35. return false, ecode.NothingFound
  36. }
  37. // update guest with new info (using map can update the column with empty string)
  38. updateErr := d.db.Model(&oriGuest).Updates(
  39. map[string]interface{}{
  40. "name": info.Name,
  41. "guest_img": info.GuestImg,
  42. "description": info.Description,
  43. }).Error
  44. if updateErr != nil {
  45. log.Error("UPDATE FAILED")
  46. return false, ecode.NotModified
  47. }
  48. return true, nil
  49. }
  50. // GuestStatus 更新嘉宾状态
  51. func (d *Dao) GuestStatus(c context.Context, id int64, status int8) (res bool, err error) {
  52. // find original guest with id
  53. var oriGuest model.Guest
  54. if dbErr := d.db.First(&oriGuest, id).Error; dbErr != nil {
  55. log.Error("guest (%d) not found", id)
  56. return false, ecode.NothingFound
  57. }
  58. // update guest with new info (using map can update the column with empty string)
  59. updateErr := d.db.Model(&oriGuest).Update("status", status).Error
  60. if updateErr != nil {
  61. log.Error("CHANGE STATUS FAILED")
  62. return false, ecode.NotModified
  63. }
  64. return true, nil
  65. }
  66. // GetGuests 获取项目下所有嘉宾
  67. func (d *Dao) GetGuests(c context.Context, pid int64) (res []*model.Guest, err error) {
  68. // 获取项目下所有guestID
  69. var projectGuests []*model.ProjectGuest
  70. if dbErr := d.db.Select("guest_id").Order("position").Where("project_id = ? and delete_status = 0", pid).Find(&projectGuests).Error; dbErr != nil {
  71. log.Error("获取项目嘉宾id失败: %s", dbErr)
  72. err = ecode.NothingFound
  73. return
  74. }
  75. var guestIDs []int64
  76. for idx := range projectGuests {
  77. guestIDs = append(guestIDs, projectGuests[idx].GuestID)
  78. }
  79. // 获取未禁用嘉宾信息 并用id当键值组成map
  80. var guestInfo []*model.Guest
  81. if dbErr := d.db.Where("status = 1 and id in (?)", guestIDs).Find(&guestInfo).Error; dbErr != nil {
  82. log.Error("获取项目嘉宾信息失败: %s", dbErr)
  83. err = ecode.NothingFound
  84. return
  85. }
  86. guestInfoMap := make(map[int64]*model.Guest)
  87. for _, value := range guestInfo {
  88. guestInfoMap[value.ID] = value
  89. }
  90. // 根据guestIDs重新排序嘉宾信息
  91. for _, value := range guestIDs {
  92. if guestInfoMap[value].Status == 1 {
  93. res = append(res, guestInfoMap[value])
  94. }
  95. }
  96. return
  97. }
  98. // GuestSearch guest es search.
  99. func (d *Dao) GuestSearch(c context.Context, arg *model.GuestSearchParam) (guests *model.GuestSearchList, err error) {
  100. r := d.es.NewRequest("ticket_guest").
  101. Index("ticket_guest").
  102. Order("ctime", elastic.OrderDesc).
  103. Order("id", elastic.OrderAsc).
  104. Ps(arg.Ps).Pn(arg.Pn)
  105. if arg.Keyword != "" {
  106. if id, err1 := strconv.Atoi(arg.Keyword); err1 == nil {
  107. r = r.WhereEq("id", id)
  108. } else {
  109. r = r.WhereLike([]string{"name"}, []string{arg.Keyword}, true, elastic.LikeLevelLow)
  110. }
  111. }
  112. log.Info(fmt.Sprintf("%s/x/admin/search/query?%s", d.c.URL.ElasticHost, r.Params()))
  113. guests = new(model.GuestSearchList)
  114. err = r.Scan(c, guests)
  115. if err != nil {
  116. log.Error("GuestSearch() Scan() error(%v)", err)
  117. }
  118. return
  119. }