comm.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "moutai/db/repo"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. var clickMap = map[string]struct{}{
  12. repo.CollectionImages: {},
  13. repo.CollectionVideos: {},
  14. repo.CollectionRelics: {},
  15. repo.CollectionVr: {},
  16. repo.CollectionAr: {},
  17. }
  18. func Clicks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  19. ClickType := c.Query("type")
  20. id := c.Query("id")
  21. if _, ok := clickMap[ClickType]; ok {
  22. _, err := repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), ClickType, id, bson.M{"$inc": bson.M{"clicks": 1}})
  23. if err != nil {
  24. return false, err
  25. }
  26. return true, nil
  27. }
  28. return false, errors.New("参数错误!")
  29. }
  30. // sort自增
  31. // 查询最大sort值
  32. func getSortInt(apictx *ApiSession, collection string) int {
  33. out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  34. CollectName: collection,
  35. Page: 1,
  36. Size: 1,
  37. // 降序,越大的在前面
  38. Sort: repo.Map{"sort": -1},
  39. })
  40. if err != nil {
  41. return 0
  42. }
  43. if len(out.List) < 1 {
  44. return 0
  45. }
  46. sort := out.List[0]["sort"].(int32)
  47. fmt.Println("getSortInt: ", sort)
  48. return int(sort)
  49. }
  50. // 排序置顶
  51. func SortTop(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  52. // 根据id,type查询获取数据
  53. ClickType := c.Query("type")
  54. id := c.Query("id")
  55. if _, ok := clickMap[ClickType]; ok {
  56. out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  57. CollectName: ClickType,
  58. Page: 1,
  59. Size: 1,
  60. // 降序,越大的在前面
  61. Sort: repo.Map{"sort": -1},
  62. })
  63. // 出错false
  64. if err != nil {
  65. return false, err
  66. }
  67. // 第一次插入没有数据
  68. if len(out.List) < 1 {
  69. return true, nil
  70. }
  71. // 有数据
  72. // 如果是自己
  73. if id == out.List[0]["_id"].(primitive.ObjectID).Hex() {
  74. return true, nil
  75. } else {
  76. //如果不是自己
  77. // 更新sort值为: 最大sort的值+1
  78. sort := out.List[0]["sort"].(int32) + 1
  79. _, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": int(sort)})
  80. if err != nil {
  81. return false, err
  82. }
  83. return true, nil
  84. }
  85. }
  86. return false, errors.New("参数错误")
  87. }
  88. // sort 上移
  89. func SortPrev(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  90. // 根据id,type查询获取数据
  91. ClickType := c.Query("type")
  92. id := c.Query("id")
  93. if _, ok := clickMap[ClickType]; ok {
  94. // 获取当前数据的sort
  95. _, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  96. CollectName: ClickType,
  97. Query: repo.Map{"_id": id},
  98. })
  99. currentSort := result["sort"].(int32)
  100. currentType := ""
  101. if v, ok := result["type"]; ok {
  102. currentType = v.(string)
  103. }
  104. query := repo.Map{"sort": repo.Map{"$gt": int(currentSort)}}
  105. if len(currentType) > 0 {
  106. query["type"] = currentType
  107. }
  108. // 获取当前数据的上一条数据的sort
  109. out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  110. CollectName: ClickType,
  111. Query: query,
  112. Page: 1,
  113. Size: 1,
  114. // 升序,越小的在前面
  115. Sort: repo.Map{"sort": 1},
  116. })
  117. // 出错false
  118. if err != nil {
  119. return false, err
  120. }
  121. // 没有数据
  122. if len(out.List) < 1 {
  123. return true, nil
  124. }
  125. // 有数据
  126. prevSort := out.List[0]["sort"].(int32)
  127. prevId := out.List[0]["_id"].(primitive.ObjectID)
  128. // 交换id
  129. _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": int(prevSort)})
  130. if err != nil {
  131. return false, err
  132. }
  133. _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, prevId.Hex(), bson.M{"sort": int(currentSort)})
  134. if err != nil {
  135. return false, err
  136. }
  137. return true, nil
  138. }
  139. return false, errors.New("参数错误")
  140. }
  141. // sort 下移
  142. func SortNext(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  143. // 根据id,type查询获取数据
  144. ClickType := c.Query("type")
  145. id := c.Query("id")
  146. if _, ok := clickMap[ClickType]; ok {
  147. // 获取当前数据的sort
  148. _, result := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  149. CollectName: ClickType,
  150. Query: repo.Map{"_id": id},
  151. })
  152. currentSort := result["sort"].(int32)
  153. currentType := ""
  154. if v, ok := result["type"]; ok {
  155. currentType = v.(string)
  156. }
  157. query := repo.Map{"sort": repo.Map{"$lt": int(currentSort)}}
  158. if len(currentType) > 0 {
  159. query["type"] = currentType
  160. }
  161. // 获取当前数据的上一条数据的sort
  162. out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  163. CollectName: ClickType,
  164. Query: query,
  165. Page: 1,
  166. Size: 1,
  167. // 降序,越大的在前面
  168. Sort: repo.Map{"sort": -1},
  169. })
  170. // 出错false
  171. if err != nil {
  172. return false, err
  173. }
  174. // 没有数据
  175. if len(out.List) < 1 {
  176. return true, nil
  177. }
  178. // 有数据
  179. nextSort := out.List[0]["sort"].(int32)
  180. nextId := out.List[0]["_id"].(primitive.ObjectID)
  181. // 交换id
  182. _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, id, bson.M{"sort": int(nextSort)})
  183. if err != nil {
  184. return false, err
  185. }
  186. _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), ClickType, nextId.Hex(), bson.M{"sort": int(currentSort)})
  187. if err != nil {
  188. return false, err
  189. }
  190. return true, nil
  191. }
  192. return false, errors.New("参数错误")
  193. }
  194. func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]interface{}) {
  195. if _cid, ok := query["cid"]; ok {
  196. delete(query, "cid")
  197. cid, _ := primitive.ObjectIDFromHex(_cid.(string))
  198. if !cid.IsZero() {
  199. query["cid"] = cid
  200. }
  201. }
  202. // query name查询
  203. if _name, ok := query["name"]; ok {
  204. name := _name.(string)
  205. delete(query, "name")
  206. if len(name) > 0 {
  207. query["name"] = bson.M{"$regex": name, "$options": "$i"}
  208. }
  209. }
  210. // query uploadTime查询
  211. if _uploadTime, ok := query["uploadTime"]; ok {
  212. uploadTime := _uploadTime.(string)
  213. delete(query, "uploadTime")
  214. if len(uploadTime) > 0 {
  215. uploadTime, err := time.Parse("2006-01-02", uploadTime)
  216. if err == nil {
  217. startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
  218. endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
  219. query["createTime"] = bson.M{
  220. "$gte": startTime,
  221. "$lte": endTime,
  222. }
  223. }
  224. }
  225. }
  226. sortMap := repo.Map{}
  227. // query sort查询
  228. if _sort, ok := query["sort"]; ok {
  229. // clicks 排序
  230. _dclicks := -1
  231. if _sort.(string) == "clicks" {
  232. sortMap = repo.Map{"clicks": &_dclicks}
  233. }
  234. // sort int自定义排序
  235. if _sort.(string) == "sort" {
  236. sortMap = repo.Map{"sort": -1}
  237. }
  238. delete(query, "sort")
  239. }
  240. fmt.Printf("查询条件:\nquery:%#v\nsortMap: %#v\n", query, sortMap)
  241. return query, sortMap
  242. }