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

hdl_dynamic.go 8.7 KiB

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