蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

304 lines
7.7 KiB

  1. package friend_circle
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. md "applet/app/md/friend_circle"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  11. svc2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  13. "context"
  14. "encoding/json"
  15. "fmt"
  16. "github.com/gin-gonic/gin"
  17. "github.com/olivere/elastic/v7"
  18. "time"
  19. )
  20. // GetDynamic
  21. // @Summary 社交管理-动态设置-动态(获取)
  22. // @Tags 动态设置
  23. // @Description 动态(获取)
  24. // @Accept json
  25. // @Produce json
  26. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  27. // @Param req body md.GetDynamicReq false "筛选条件"
  28. // @Success 200 {object} md.GetDynamicResp "生成的黑名单id"
  29. // @Failure 400 {object} md.Response "具体错误"
  30. // @Router /api/friendCircleSettings/dynamic/index [POST]
  31. func GetDynamic(c *gin.Context) {
  32. var req md.GetDynamicReq
  33. err := c.ShouldBindJSON(&req)
  34. if err != nil {
  35. err = svc.HandleValidateErr(err)
  36. err1 := err.(e.E)
  37. e.OutErr(c, err1.Code, err1.Error())
  38. return
  39. }
  40. var userIds []int64
  41. if req.Nickname != "" {
  42. var users []md.UserIdsGetFromNickname
  43. nickname := "%" + req.Nickname + "%"
  44. err = db.Db.Table("user").Where("nickname like ?", nickname).Find(&users)
  45. if err != nil {
  46. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  47. return
  48. }
  49. for _, user := range users {
  50. userIds = append(userIds, user.Id)
  51. }
  52. }
  53. if req.Uid != "" {
  54. uid := utils.StrToInt64(req.Uid)
  55. userIds = append(userIds, uid)
  56. }
  57. if req.Page == 0 {
  58. req.Page = 1
  59. }
  60. if req.Limit == 0 {
  61. req.Limit = 10
  62. }
  63. aliasName := md2.EggFriendCircleEsAlias
  64. boolQuery := elastic.NewBoolQuery()
  65. if userIds != nil {
  66. boolQuery.Filter(elastic.NewTermsQuery("uid", userIds))
  67. }
  68. if req.BeginAt != "" && req.EndAt != "" {
  69. boolQuery.Filter(elastic.NewRangeQuery("created_at").Gte(req.BeginAt).Lte(req.EndAt).Format("yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"))
  70. }
  71. searchResult, err := es.EsClient.Search().
  72. Index(aliasName).
  73. Query(boolQuery).
  74. From((req.Page-1)*req.Limit).Size(req.Limit).
  75. Sort("created_at", true).
  76. Pretty(true).
  77. Do(context.Background())
  78. if err != nil {
  79. e.OutErr(c, e.ERR, err.Error())
  80. return
  81. }
  82. var docs []md.EggFriendCircleNode
  83. var docUserIds []int64
  84. var total int64
  85. // 检查是否有结果
  86. if searchResult.Hits.TotalHits.Value != 0 {
  87. // 解析结果
  88. total = searchResult.Hits.TotalHits.Value
  89. for _, hit := range searchResult.Hits.Hits {
  90. var doc md.EggFriendCircleNode
  91. err = json.Unmarshal(hit.Source, &doc)
  92. if err != nil {
  93. return
  94. }
  95. docUserIds = append(docUserIds, doc.Uid)
  96. docs = append(docs, doc)
  97. }
  98. }
  99. adminDb := implement.NewAdminDb(db.Db)
  100. admins, err := adminDb.AdminFindByParams(map[string]interface{}{
  101. "key": "adm_id",
  102. "value": docUserIds,
  103. })
  104. if err != nil {
  105. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  106. return
  107. }
  108. adminMap := make(map[int64]string)
  109. for _, admin := range admins {
  110. adminMap[int64(admin.AdmId)] = admin.Username
  111. }
  112. userDb := implement.NewUserDb(db.Db)
  113. users, err := userDb.UserFindByParams(map[string]interface{}{
  114. "key": "id",
  115. "value": docUserIds,
  116. })
  117. if err != nil {
  118. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  119. return
  120. }
  121. userMap := make(map[int64]string)
  122. for _, user := range users {
  123. userMap[user.Id] = user.Nickname
  124. }
  125. redisConn := cache.GetPool().Get()
  126. scheme, domain := svc2.ImageBucket(db.Db, redisConn)
  127. for _, doc := range docs {
  128. switch doc.Kind {
  129. case 1:
  130. doc.Username = userMap[doc.Uid]
  131. case 2:
  132. doc.Username = adminMap[doc.Uid]
  133. }
  134. doc.Image = svc2.ImageFormatWithBucket(scheme, domain, doc.Image)
  135. doc.Video = svc2.ImageFormatWithBucket(scheme, domain, doc.Video)
  136. }
  137. resp := md.GetDynamicResp{
  138. List: docs,
  139. Paginate: md.Paginate{
  140. Limit: req.Limit,
  141. Page: req.Page,
  142. Total: total,
  143. },
  144. }
  145. e.OutSuc(c, resp, nil)
  146. }
  147. // UpdateDynamic
  148. // @Summary 社交管理-动态设置-动态(更新)
  149. // @Tags 动态设置
  150. // @Description 动态(更新)
  151. // @Accept json
  152. // @Produce json
  153. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  154. // @Param req body md.UpdateDynamicReq true "更新动态id必填"
  155. // @Success 200 {string} "success"
  156. // @Failure 400 {object} md.Response "具体错误"
  157. // @Router /api/friendCircleSettings/dynamic/update [POST]
  158. func UpdateDynamic(c *gin.Context) {
  159. var req md.UpdateDynamicReq
  160. err := c.ShouldBindJSON(&req)
  161. if err != nil {
  162. err = svc.HandleValidateErr(err)
  163. err1 := err.(e.E)
  164. e.OutErr(c, err1.Code, err1.Error())
  165. return
  166. }
  167. updateMap := make(map[string]interface{})
  168. if req.State != 0 {
  169. updateMap["state"] = req.State
  170. }
  171. if req.IsTopUp != 0 {
  172. updateMap["is_top_up"] = req.IsTopUp
  173. }
  174. if req.IsPraise != 0 {
  175. updateMap["is_praise"] = req.IsPraise
  176. }
  177. aliasName := md2.EggFriendCircleEsAlias
  178. _, err = es.EsClient.Update().
  179. Index(aliasName).
  180. Id(req.IndexId).
  181. Doc(updateMap).
  182. Do(context.Background())
  183. if err != nil {
  184. e.OutErr(c, e.ERR, err.Error())
  185. return
  186. }
  187. e.OutSuc(c, "success", nil)
  188. }
  189. // DeleteDynamic
  190. // @Summary 社交管理-动态设置-动态(删除)
  191. // @Tags 动态设置
  192. // @Description 动态(删除)
  193. // @Accept json
  194. // @Produce json
  195. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  196. // @Param req body md.DeleteDynamicReq true "删除动态id必填"
  197. // @Success 200 {string} "success"
  198. // @Failure 400 {object} md.Response "具体错误"
  199. // @Router /api/friendCircleSettings/dynamic/del [DELETE]
  200. func DeleteDynamic(c *gin.Context) {
  201. var req md.DeleteDynamicReq
  202. err := c.ShouldBindJSON(&req)
  203. if err != nil {
  204. err = svc.HandleValidateErr(err)
  205. err1 := err.(e.E)
  206. e.OutErr(c, err1.Code, err1.Error())
  207. return
  208. }
  209. aliasName := md2.EggFriendCircleEsAlias
  210. _, err = es.EsClient.Delete().
  211. Index(aliasName).
  212. Id(req.IndexId).
  213. Do(context.Background())
  214. if err != nil {
  215. e.OutErr(c, e.ERR, err.Error())
  216. return
  217. }
  218. e.OutSuc(c, "success", nil)
  219. }
  220. // ReleaseDynamic
  221. // @Summary 社交管理-动态设置-动态(发布)
  222. // @Tags 动态设置
  223. // @Description 动态(发布)
  224. // @Accept json
  225. // @Produce json
  226. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  227. // @Param req body md.ReleaseDynamicReq true "发布动态信息"
  228. // @Success 200 {string} "success"
  229. // @Failure 400 {object} md.Response "具体错误"
  230. // @Router /api/friendCircleSettings/dynamic/release [POST]
  231. func ReleaseDynamic(c *gin.Context) {
  232. var req md.ReleaseDynamicReq
  233. err := c.ShouldBindJSON(&req)
  234. if err != nil {
  235. err = svc.HandleValidateErr(err)
  236. err1 := err.(e.E)
  237. e.OutErr(c, err1.Code, err1.Error())
  238. return
  239. }
  240. user := svc.GetUser(c)
  241. if req.State == 0 {
  242. req.State = 1
  243. }
  244. if req.Kind == 0 {
  245. req.Kind = 1
  246. }
  247. if req.IsPraise == 0 {
  248. req.IsPraise = 2
  249. }
  250. if req.IsTopUp == 0 {
  251. req.IsTopUp = 2
  252. }
  253. now := time.Now()
  254. m := md2.EggFriendCircleEs{
  255. Uid: int64(user.AdmId),
  256. Kind: req.Kind,
  257. Content: req.Content,
  258. Image: req.Image,
  259. Video: req.Video,
  260. LikesNums: 0,
  261. ShareNums: 0,
  262. CommentNums: 0,
  263. State: req.State,
  264. IsTopUp: req.IsTopUp,
  265. IsPraise: req.IsPraise,
  266. CreatedAt: now.Format("2006-01-02 15:04:05"),
  267. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  268. }
  269. id := fmt.Sprintf("%d-%d", now.Unix(), user.AdmId)
  270. aliasName := md2.EggFriendCircleEsAlias
  271. _, err = es.EsClient.Index().
  272. Index(aliasName).
  273. Id(id).
  274. BodyJson(m).
  275. Do(context.Background())
  276. if err != nil {
  277. e.OutErr(c, e.ERR, err.Error())
  278. return
  279. }
  280. e.OutSuc(c, "success", nil)
  281. }