蛋蛋星球 后台端
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.
 
 
 
 

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