repo.go 13 KB

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