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

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 days ago
2 weeks ago
2 days ago
2 weeks ago
2 days ago
2 weeks ago
2 days ago
2 weeks ago
2 weeks ago
2 days ago
2 weeks ago
1 week ago
2 days ago
2 weeks ago
1 week ago
2 days ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
1 week ago
2 weeks ago
2 days ago
2 weeks ago
2 weeks ago
2 weeks ago
2 days ago
2 weeks ago
2 weeks ago
2 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  11. "context"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/olivere/elastic/v7"
  16. "strconv"
  17. "strings"
  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 "具体数据"
  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. userIdInterface := make([]interface{}, len(userIds))
  64. for index, value := range userIds {
  65. userIdInterface[index] = value
  66. }
  67. aliasName := md2.EggFriendCircleEsAlias
  68. boolQuery := elastic.NewBoolQuery()
  69. if userIds != nil {
  70. boolQuery.Filter(elastic.NewTermsQuery("uid", userIdInterface...))
  71. }
  72. if req.BeginAt != "" && req.EndAt != "" {
  73. boolQuery.Filter(elastic.NewRangeQuery("created_at").Gte(req.BeginAt).Lte(req.EndAt).Format("yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"))
  74. }
  75. searchResult, err := es.EsClient.Search().
  76. Index(aliasName).
  77. Query(boolQuery).
  78. From((req.Page-1)*req.Limit).Size(req.Limit).
  79. Sort("created_at", true).
  80. Pretty(true).
  81. Do(context.Background())
  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. doc.IndexId = hit.Id
  97. docs = append(docs, &doc)
  98. }
  99. }
  100. adminDb := implement.NewAdminDb(db.Db)
  101. admins, err := adminDb.AdminFindByParams(map[string]interface{}{
  102. "key": "adm_id",
  103. "value": docUserIds,
  104. })
  105. if err != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. adminMap := make(map[int64]string)
  110. for _, admin := range admins {
  111. adminMap[int64(admin.AdmId)] = admin.Username
  112. }
  113. userDb := implement.NewUserDb(db.Db)
  114. users, err := userDb.UserFindByParams(map[string]interface{}{
  115. "key": "id",
  116. "value": docUserIds,
  117. })
  118. if err != nil {
  119. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  120. return
  121. }
  122. userMap := make(map[int64]string)
  123. for _, user := range users {
  124. userMap[user.Id] = user.Nickname
  125. }
  126. scheme, domain := svc.ImageBucket(db.Db)
  127. list := make([]md.DynamicRespNode, 0, len(docs))
  128. for _, doc := range docs {
  129. switch doc.Kind {
  130. case 1:
  131. doc.Username = userMap[doc.Uid]
  132. case 2:
  133. doc.Username = adminMap[doc.Uid]
  134. }
  135. imageStr := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Image)
  136. videos := svc.ImageFormatWithBucketForDataInfo(scheme, domain, doc.Video)
  137. var images []string
  138. utils.Unserialize([]byte(imageStr), &images)
  139. temp := md.DynamicRespNode{
  140. IndexId: doc.IndexId,
  141. Uid: doc.Uid,
  142. Username: doc.Username,
  143. Kind: doc.Kind,
  144. Content: doc.Content,
  145. Image: images,
  146. Video: videos,
  147. State: doc.State,
  148. IsTopUp: doc.IsTopUp,
  149. IsPraise: doc.IsPraise,
  150. CreatedAt: doc.CreatedAt,
  151. UpdatedAt: doc.UpdatedAt,
  152. }
  153. list = append(list, temp)
  154. }
  155. resp := md.GetDynamicResp{
  156. List: list,
  157. Paginate: md.Paginate{
  158. Limit: req.Limit,
  159. Page: req.Page,
  160. Total: total,
  161. },
  162. }
  163. e.OutSuc(c, resp, nil)
  164. }
  165. // UpdateDynamic
  166. // @Summary 社交管理-动态设置-动态(更新)
  167. // @Tags 动态设置
  168. // @Description 动态(更新)
  169. // @Accept json
  170. // @Produce json
  171. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  172. // @Param req body md.UpdateDynamicReq true "更新动态id必填"
  173. // @Success 200 {string} "success"
  174. // @Failure 400 {object} md.Response "具体错误"
  175. // @Router /api/friendCircleSettings/dynamic/update [POST]
  176. func UpdateDynamic(c *gin.Context) {
  177. var req md.UpdateDynamicReq
  178. err := c.ShouldBindJSON(&req)
  179. if err != nil {
  180. err = svc.HandleValidateErr(err)
  181. err1 := err.(e.E)
  182. e.OutErr(c, err1.Code, err1.Error())
  183. return
  184. }
  185. updateMap := make(map[string]interface{})
  186. if req.State != 0 {
  187. updateMap["state"] = req.State
  188. }
  189. if req.IsTopUp != 0 {
  190. updateMap["is_top_up"] = req.IsTopUp
  191. }
  192. if req.IsPraise != 0 {
  193. updateMap["is_praise"] = req.IsPraise
  194. }
  195. aliasName := md2.EggFriendCircleEsAlias
  196. _, err = es.EsClient.Update().
  197. Index(aliasName).
  198. Id(req.IndexId).
  199. Doc(updateMap).
  200. Do(context.Background())
  201. if err != nil {
  202. e.OutErr(c, e.ERR, err.Error())
  203. return
  204. }
  205. e.OutSuc(c, "success", nil)
  206. }
  207. // DeleteDynamic
  208. // @Summary 社交管理-动态设置-动态(删除)
  209. // @Tags 动态设置
  210. // @Description 动态(删除)
  211. // @Accept json
  212. // @Produce json
  213. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  214. // @Param req body md.DeleteDynamicReq true "删除动态id必填"
  215. // @Success 200 {string} "success"
  216. // @Failure 400 {object} md.Response "具体错误"
  217. // @Router /api/friendCircleSettings/dynamic/del [DELETE]
  218. func DeleteDynamic(c *gin.Context) {
  219. var req md.DeleteDynamicReq
  220. err := c.ShouldBindJSON(&req)
  221. if err != nil {
  222. err = svc.HandleValidateErr(err)
  223. err1 := err.(e.E)
  224. e.OutErr(c, err1.Code, err1.Error())
  225. return
  226. }
  227. aliasName := md2.EggFriendCircleEsAlias
  228. _, err = es.EsClient.Delete().
  229. Index(aliasName).
  230. Id(req.IndexId).
  231. Do(context.Background())
  232. if err != nil {
  233. e.OutErr(c, e.ERR, err.Error())
  234. return
  235. }
  236. e.OutSuc(c, "success", nil)
  237. }
  238. // ReleaseDynamic
  239. // @Summary 社交管理-动态设置-动态(发布)
  240. // @Tags 动态设置
  241. // @Description 动态(发布)
  242. // @Accept json
  243. // @Produce json
  244. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  245. // @Param req body md.ReleaseDynamicReq true "发布动态信息"
  246. // @Success 200 {string} "success"
  247. // @Failure 400 {object} md.Response "具体错误"
  248. // @Router /api/friendCircleSettings/dynamic/release [POST]
  249. func ReleaseDynamic(c *gin.Context) {
  250. var req md.ReleaseDynamicReq
  251. err := c.ShouldBindJSON(&req)
  252. if err != nil {
  253. err = svc.HandleValidateErr(err)
  254. err1 := err.(e.E)
  255. e.OutErr(c, err1.Code, err1.Error())
  256. return
  257. }
  258. user := svc.GetUser(c)
  259. if req.State == 0 {
  260. req.State = 1
  261. }
  262. if req.Kind == 0 {
  263. req.Kind = 1
  264. }
  265. if req.IsPraise == 0 {
  266. req.IsPraise = 2
  267. }
  268. if req.IsTopUp == 0 {
  269. req.IsTopUp = 2
  270. }
  271. scheme, domain := svc.ImageBucket(db.Db)
  272. var imageStr string
  273. if len(req.Image) > 0 {
  274. imageStr = utils.SerializeStr(req.Image)
  275. imageStr = strings.ReplaceAll(imageStr, fmt.Sprintf("%s://%s/", scheme, domain), "{{tempHost}}")
  276. }
  277. now := time.Now()
  278. m := md2.EggFriendCircleEs{
  279. Uid: int64(user.AdmId),
  280. Kind: req.Kind,
  281. Content: req.Content,
  282. Image: imageStr,
  283. Video: req.Video,
  284. LikesNums: 0,
  285. ShareNums: 0,
  286. CommentNums: 0,
  287. State: req.State,
  288. IsTopUp: req.IsTopUp,
  289. IsPraise: req.IsPraise,
  290. CreatedAt: now.Format("2006-01-02 15:04:05"),
  291. UpdatedAt: now.Format("2006-01-02 15:04:05"),
  292. }
  293. createDocRet, err := es.CreateDoc(md2.EggFriendCircleEsIndex, strconv.FormatInt(int64(user.AdmId), 10)+"_"+utils.Int64ToStr(now.Unix()), m)
  294. fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet)
  295. e.OutSuc(c, "success", nil)
  296. }