repo.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. package repo
  2. import (
  3. "context"
  4. "copter-train/db"
  5. "copter-train/log"
  6. "fmt"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. )
  12. type RepoSession struct {
  13. Ctx context.Context
  14. Client *db.MongoDB
  15. }
  16. const (
  17. CollectionCategory = "category"
  18. CollectionUser = "users"
  19. CollectionLearnLog = "learn_logs"
  20. CollectionTest = "tests"
  21. )
  22. type Map map[string]interface{}
  23. type PageResult struct {
  24. List []map[string]interface{} `json:"list"`
  25. Total int64 `json:"total"`
  26. Page int64 `json:"page"`
  27. Size int64 `json:"size"`
  28. }
  29. type PageSearchOptions struct {
  30. Db string
  31. CollectName string
  32. Page int64
  33. Size int64
  34. Query map[string]interface{}
  35. Project []string
  36. Sort interface{}
  37. }
  38. type DocSearchOptions struct {
  39. Db string
  40. CollectName string
  41. Query Map
  42. Project []string
  43. Sort bson.M
  44. }
  45. func NewDocSearchOptions(filter Map, project []string) *DocSearchOptions {
  46. return &DocSearchOptions{
  47. Query: filter,
  48. Project: project,
  49. }
  50. }
  51. func RepoAddDoc(ctx *RepoSession, collectName string, doc interface{}) (string, error) {
  52. users := ctx.Client.GetCollection(collectName)
  53. result, err := users.InsertOne(ctx.Ctx, doc)
  54. if err != nil {
  55. return "", err
  56. }
  57. return result.InsertedID.(primitive.ObjectID).Hex(), nil
  58. }
  59. func RepoAddDbDoc(ctx *RepoSession, Db string, collectName string, doc interface{}) (string, error) {
  60. coll := ctx.Client.GetDbCollection(Db, collectName)
  61. result, err := coll.InsertOne(ctx.Ctx, doc)
  62. if err != nil {
  63. return "", err
  64. }
  65. return result.InsertedID.(primitive.ObjectID).Hex(), nil
  66. }
  67. func RepoDeleteDoc(ctx *RepoSession, collectName string, id string) (interface{}, error) {
  68. uid, _ := primitive.ObjectIDFromHex(id)
  69. colls := ctx.Client.GetCollection(collectName)
  70. return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
  71. }
  72. func RepoDeleteDbDoc(ctx *RepoSession, Db string, collectName string, id string) (interface{}, error) {
  73. uid, _ := primitive.ObjectIDFromHex(id)
  74. colls := ctx.Client.GetDbCollection(Db, collectName)
  75. return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
  76. }
  77. func RepoDeleteDocs(ctx *RepoSession, collectName string, query interface{}) (interface{}, error) {
  78. colls := ctx.Client.GetCollection(collectName)
  79. return colls.DeleteMany(ctx.Ctx, query)
  80. }
  81. func RepoUpdateSetDocs(ctx *RepoSession, collectName string, filter interface{}, model interface{}) (*mongo.UpdateResult, error) {
  82. colls := ctx.Client.GetCollection(collectName)
  83. update := bson.M{"$set": model}
  84. return colls.UpdateOne(ctx.Ctx, filter, update)
  85. }
  86. func RepoUpdateSetDoc(ctx *RepoSession, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
  87. colls := ctx.Client.GetCollection(collectName)
  88. update := bson.M{"$set": model}
  89. uid, _ := primitive.ObjectIDFromHex(idstr)
  90. return colls.UpdateByID(ctx.Ctx, uid, update)
  91. }
  92. func RepoUpdateDbSetDoc(ctx *RepoSession, Db string, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
  93. colls := ctx.Client.GetDbCollection(Db, collectName)
  94. update := bson.M{"$set": model}
  95. uid, _ := primitive.ObjectIDFromHex(idstr)
  96. return colls.UpdateByID(ctx.Ctx, uid, update)
  97. }
  98. func RepoUpsertSetDoc(ctx *RepoSession, collectName string, filter interface{}, model interface{}) (*mongo.UpdateResult, error) {
  99. coll := ctx.Client.GetCollection(collectName)
  100. update := bson.M{"$set": model}
  101. // filter := bson.D{{"type", "Oolong"}}
  102. // update := bson.D{{"$set", bson.D{}}}
  103. opts := options.Update().SetUpsert(true)
  104. return coll.UpdateOne(ctx.Ctx, filter, update, opts)
  105. }
  106. func RepoUpdateSetDocProps(ctx *RepoSession, collectName string, idstr string, update interface{}) (*mongo.UpdateResult, error) {
  107. colls := ctx.Client.GetCollection(collectName)
  108. // update := bson.M{"$set": model}
  109. uid, _ := primitive.ObjectIDFromHex(idstr)
  110. return colls.UpdateByID(ctx.Ctx, uid, update)
  111. }
  112. func RepoSeachDoc(ctx *RepoSession, param *DocSearchOptions, v interface{}) (bool, error) {
  113. colls := ctx.Client.GetDbCollection(param.Db, param.CollectName)
  114. opt := &options.FindOneOptions{}
  115. if param.Sort != nil {
  116. opt.SetSort(param.Sort)
  117. }
  118. if len(param.Project) > 0 {
  119. prj := bson.M{}
  120. for _, v := range param.Project {
  121. prj[v] = 1
  122. }
  123. opt.SetProjection(prj)
  124. }
  125. filter := bson.M{}
  126. if len(param.Query) > 0 {
  127. for k, v := range param.Query {
  128. if k == "_id" {
  129. if uid, ok := v.(string); ok {
  130. docId, _ := primitive.ObjectIDFromHex(uid)
  131. filter["_id"] = docId
  132. continue
  133. }
  134. }
  135. filter[k] = v
  136. }
  137. }
  138. err := colls.FindOne(ctx.Ctx, filter, opt).Decode(v)
  139. if err == mongo.ErrNoDocuments {
  140. return false, nil
  141. }
  142. if err != nil {
  143. return false, err
  144. }
  145. return true, nil
  146. }
  147. func RepoSeachDocs(ctx *RepoSession, param *DocSearchOptions, out interface{}) error {
  148. colls := ctx.Client.GetCollection(param.CollectName)
  149. opt := &options.FindOptions{}
  150. if len(param.Project) > 0 {
  151. prj := bson.M{}
  152. for _, v := range param.Project {
  153. prj[v] = 1
  154. }
  155. opt.SetProjection(prj)
  156. }
  157. filter := bson.M{}
  158. if len(param.Query) > 0 {
  159. for k, v := range param.Query {
  160. if k == "_id" {
  161. if uid, ok := v.(string); ok {
  162. docId, _ := primitive.ObjectIDFromHex(uid)
  163. filter["_id"] = docId
  164. continue
  165. }
  166. }
  167. filter[k] = v
  168. }
  169. }
  170. cur, err := colls.Find(ctx.Ctx, filter, opt)
  171. if err != nil {
  172. return err
  173. }
  174. defer cur.Close(ctx.Ctx)
  175. err = cur.All(ctx.Ctx, out)
  176. return err
  177. }
  178. func RepoSeachDocMap(ctx *RepoSession, param *DocSearchOptions) (bool, map[string]interface{}) {
  179. ret := map[string]interface{}{}
  180. ok := true
  181. colls := ctx.Client.GetCollection(param.CollectName)
  182. opt := &options.FindOneOptions{}
  183. if len(param.Project) > 0 {
  184. prj := bson.M{}
  185. for _, v := range param.Project {
  186. prj[v] = 1
  187. }
  188. opt.SetProjection(prj)
  189. }
  190. filter := bson.M{}
  191. if len(param.Query) > 0 {
  192. for k, v := range param.Query {
  193. if k == "_id" {
  194. if uid, ok := v.(string); ok {
  195. docId, _ := primitive.ObjectIDFromHex(uid)
  196. filter["_id"] = docId
  197. continue
  198. }
  199. }
  200. filter[k] = v
  201. }
  202. }
  203. ok = true
  204. err := colls.FindOne(ctx.Ctx, filter, opt).Decode(ret)
  205. if err == mongo.ErrNoDocuments {
  206. ok = false
  207. }
  208. if err != nil {
  209. ok = false
  210. }
  211. return ok, ret
  212. }
  213. // PageSearch 单表分页查询
  214. func RepoPageSearch(ctx *RepoSession, para *PageSearchOptions) (out *PageResult, err error) {
  215. colls := ctx.Client.GetCollection(para.CollectName)
  216. findoptions := &options.FindOptions{}
  217. if para.Size > 0 {
  218. findoptions.SetLimit(para.Size)
  219. findoptions.SetSkip(para.Size * (para.Page - 1))
  220. }
  221. if para.Sort != nil {
  222. findoptions.SetSort(para.Sort)
  223. }
  224. if len(para.Project) > 0 {
  225. prj := bson.M{}
  226. for _, v := range para.Project {
  227. prj[v] = 1
  228. }
  229. findoptions.SetProjection(prj)
  230. }
  231. filter := bson.M{}
  232. if len(para.Query) > 0 {
  233. for k, v := range para.Query {
  234. if value, ok := v.(string); ok {
  235. if len(value) > 0 {
  236. filter[k] = v
  237. continue
  238. }
  239. } else if v != nil {
  240. filter[k] = v
  241. }
  242. }
  243. }
  244. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  245. out = &PageResult{
  246. List: []map[string]interface{}{},
  247. Total: 0,
  248. Page: para.Page,
  249. Size: para.Size,
  250. }
  251. if err != nil {
  252. return out, err
  253. }
  254. defer cur.Close(ctx.Ctx)
  255. err = cur.All(ctx.Ctx, &out.List)
  256. out.Total, _ = colls.CountDocuments(ctx.Ctx, filter)
  257. return
  258. }
  259. func RepoDbPageSearch(ctx *RepoSession, para *PageSearchOptions) (out *PageResult, err error) {
  260. colls := ctx.Client.GetDbCollection(para.Db, para.CollectName)
  261. findoptions := &options.FindOptions{}
  262. if para.Size > 0 {
  263. findoptions.SetLimit(para.Size)
  264. findoptions.SetSkip(para.Size * (para.Page - 1))
  265. }
  266. if para.Sort != nil {
  267. findoptions.SetSort(para.Sort)
  268. }
  269. if len(para.Project) > 0 {
  270. prj := bson.M{}
  271. for _, v := range para.Project {
  272. prj[v] = 1
  273. }
  274. findoptions.SetProjection(prj)
  275. }
  276. filter := bson.M{}
  277. if len(para.Query) > 0 {
  278. for k, v := range para.Query {
  279. if value, ok := v.(string); ok {
  280. if len(value) > 0 {
  281. filter[k] = v
  282. continue
  283. }
  284. } else if v != nil {
  285. filter[k] = v
  286. }
  287. }
  288. }
  289. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  290. out = &PageResult{
  291. List: []map[string]interface{}{},
  292. Total: 0,
  293. Page: para.Page,
  294. Size: para.Size,
  295. }
  296. if err != nil {
  297. return out, err
  298. }
  299. defer cur.Close(ctx.Ctx)
  300. err = cur.All(ctx.Ctx, &out.List)
  301. out.Total, _ = colls.CountDocuments(ctx.Ctx, filter)
  302. return
  303. }
  304. // PageSearch 单表分页查询
  305. func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, error) {
  306. colls := ctx.Client.GetCollection(collectionName)
  307. filter := bson.M{}
  308. if len(Query) > 0 {
  309. for k, v := range Query {
  310. if value, ok := v.(string); ok {
  311. if len(value) > 0 {
  312. filter[k] = v
  313. continue
  314. }
  315. } else {
  316. filter[k] = v
  317. }
  318. }
  319. }
  320. return colls.CountDocuments(ctx.Ctx, filter)
  321. }
  322. // PageSearch 单表分页查询
  323. func RepoDocsSearch(ctx *RepoSession, para *PageSearchOptions, out interface{}) (err error) {
  324. colls := ctx.Client.GetCollection(para.CollectName)
  325. findoptions := &options.FindOptions{}
  326. if para.Size > 0 {
  327. findoptions.SetLimit(para.Size)
  328. findoptions.SetSkip(para.Size * (para.Page - 1))
  329. }
  330. if para.Sort != nil {
  331. findoptions.SetSort(para.Sort)
  332. }
  333. if len(para.Project) > 0 {
  334. prj := bson.M{}
  335. for _, v := range para.Project {
  336. prj[v] = 1
  337. }
  338. findoptions.SetProjection(prj)
  339. }
  340. filter := bson.M{}
  341. if len(para.Query) > 0 {
  342. for k, v := range para.Query {
  343. if value, ok := v.(string); ok {
  344. if len(value) > 0 {
  345. filter[k] = v
  346. continue
  347. }
  348. } else {
  349. filter[k] = v
  350. }
  351. }
  352. }
  353. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  354. if err != nil {
  355. return err
  356. }
  357. defer cur.Close(ctx.Ctx)
  358. err = cur.All(ctx.Ctx, out)
  359. return
  360. }
  361. func RepoDocArrayAppend(ctx *RepoSession, collectName string, idstr string, fieldpath string, arrayItem interface{}) (*mongo.UpdateResult, error) {
  362. colls := ctx.Client.GetCollection(collectName)
  363. arrayOp := bson.M{}
  364. arrayOp[fieldpath] = arrayItem
  365. update := bson.M{"$push": arrayOp}
  366. uid, _ := primitive.ObjectIDFromHex(idstr)
  367. return colls.UpdateByID(ctx.Ctx, uid, update)
  368. }
  369. // { _id: 4, "grades.grade": 85 },
  370. // { $set: { "grades.$.std" : 6 } }
  371. type ArrayOneUpdateOption struct {
  372. Query Map
  373. Set Map
  374. CollectName string
  375. Id string
  376. }
  377. func RepoDocArrayOneUpdate(ctx *RepoSession, options *ArrayOneUpdateOption) (*mongo.UpdateResult, error) {
  378. colls := ctx.Client.GetCollection(options.CollectName)
  379. docId, _ := primitive.ObjectIDFromHex(options.Id)
  380. query := bson.M{"_id": docId}
  381. if len(options.Query) > 0 {
  382. for k, v := range options.Query {
  383. query[k] = v
  384. }
  385. }
  386. setOp := bson.M{}
  387. for k, v := range options.Set {
  388. setOp[k] = v
  389. }
  390. update := bson.M{"$set": setOp}
  391. return colls.UpdateOne(ctx.Ctx, query, update)
  392. }
  393. type ArrayOneRemoveOption struct {
  394. ArrayQuery Map
  395. CollectName string
  396. Id string
  397. }
  398. // { $pull: { "items" : { id: 23 } } }
  399. func RepoDocArrayOneRemove(ctx *RepoSession, options *ArrayOneRemoveOption) (*mongo.UpdateResult, error) {
  400. colls := ctx.Client.GetCollection(options.CollectName)
  401. docId, _ := primitive.ObjectIDFromHex(options.Id)
  402. query := bson.M{"_id": docId}
  403. arrayQuery := bson.M{}
  404. if len(options.ArrayQuery) > 0 {
  405. for k, v := range options.ArrayQuery {
  406. arrayQuery[k] = v
  407. }
  408. }
  409. update := bson.M{"$pull": arrayQuery}
  410. return colls.UpdateOne(ctx.Ctx, query, update)
  411. }
  412. type ArrayOneSearchOption struct {
  413. ArrayQuery Map
  414. CollectName string
  415. Id string
  416. Field string
  417. }
  418. func RepoDocArraySearch(ctx *RepoSession, options *ArrayOneSearchOption) (ok bool, ret map[string]interface{}) {
  419. colls := ctx.Client.GetCollection(options.CollectName)
  420. docId, _ := primitive.ObjectIDFromHex(options.Id)
  421. match := []bson.E{}
  422. match = append(match, bson.E{Key: "_id", Value: docId})
  423. if len(options.ArrayQuery) > 0 {
  424. for k, v := range options.ArrayQuery {
  425. match = append(match, bson.E{Key: k, Value: v})
  426. }
  427. }
  428. matchStage := bson.D{
  429. {Key: "$match", Value: match},
  430. }
  431. unwindStage := bson.D{
  432. {Key: "$unwind", Value: fmt.Sprintf("%s%s", "$", options.Field)},
  433. }
  434. curr, err := colls.Aggregate(ctx.Ctx, mongo.Pipeline{matchStage, unwindStage})
  435. if err != nil {
  436. ok = false
  437. return
  438. }
  439. defer curr.Close(ctx.Ctx)
  440. var list []map[string]interface{}
  441. err = curr.All(ctx.Ctx, list)
  442. if err != nil {
  443. ok = false
  444. return
  445. }
  446. ok = true
  447. ret = list[0]
  448. return
  449. }
  450. type DocsSearchOptions struct {
  451. CollectName string
  452. Query map[string]interface{}
  453. Project []string
  454. Sort interface{} //bson.D{ bson.E{"update_time", -1}, bson.E{"goods_id", -1},}
  455. }
  456. func RepoSeachDocsMap(ctx *RepoSession, param *DocsSearchOptions) (ok bool, list []map[string]interface{}) {
  457. colls := ctx.Client.GetCollection(param.CollectName)
  458. findoptions := &options.FindOptions{}
  459. if len(param.Project) > 0 {
  460. prj := bson.M{}
  461. for _, v := range param.Project {
  462. prj[v] = 1
  463. }
  464. findoptions.SetProjection(prj)
  465. }
  466. if param.Sort != nil {
  467. findoptions.SetSort(param.Sort)
  468. }
  469. filter := bson.M{}
  470. if len(param.Query) > 0 {
  471. for k, v := range param.Query {
  472. if value, ok := v.(string); ok {
  473. if len(value) > 0 {
  474. filter[k] = v
  475. continue
  476. }
  477. } else {
  478. filter[k] = v
  479. }
  480. }
  481. }
  482. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  483. if err != nil {
  484. ok = false
  485. return
  486. }
  487. defer cur.Close(ctx.Ctx)
  488. listRes := []map[string]interface{}{}
  489. err = cur.All(ctx.Ctx, &listRes)
  490. if err != nil {
  491. log.Error(err)
  492. ok = false
  493. return
  494. }
  495. list = listRes
  496. ok = true
  497. return
  498. }