repo.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. package repo
  2. import (
  3. "box-cost/db"
  4. dm "box-cost/db/model"
  5. "box-cost/log"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. "go.mongodb.org/mongo-driver/mongo"
  13. "go.mongodb.org/mongo-driver/mongo/options"
  14. )
  15. type RepoSession struct {
  16. Ctx context.Context
  17. Client *db.MongoDB
  18. }
  19. const (
  20. CollectionMaterial = "material"
  21. CollectionCraft = "craft"
  22. CollectionProduct = "product"
  23. CollectionSupplier = "supplier"
  24. CollectionSupplierPrice = "supplier-price"
  25. CollectionPack = "pack"
  26. CollectionProductPlan = "product-plan"
  27. CollectionBillPurchase = "bill-purchase"
  28. CollectionBillProduce = "bill-produce"
  29. CollectionBillProduct = "bill-product" // 成品采购
  30. CollectionSupplierMatprice = "supplier-mats"
  31. CollectionSupplierCraftprice = "supplier-crafts"
  32. CollectionSupplierProductprice = "supplier-product" // 成品采购
  33. CollectionIncrement = "increment"
  34. CollectionSignature = "signature"
  35. CollectionUsers = "users"
  36. // 更改日志记录
  37. // CollectionLogs = "logs"
  38. CollectionRequestLogs = "request-logs"
  39. CollectionPlanTrack = "plan-track"
  40. CollectionBillHistory = "bill-history"
  41. CollectionPlanHistory = "plan-history"
  42. )
  43. type Map map[string]interface{}
  44. type PageResult struct {
  45. List []map[string]interface{} `json:"list"`
  46. Total int64 `json:"total"`
  47. Page int64 `json:"page"`
  48. Size int64 `json:"size"`
  49. }
  50. type PageSearchOptions struct {
  51. Db string
  52. CollectName string
  53. Page int64
  54. Size int64
  55. Query map[string]interface{}
  56. Project []string
  57. Sort interface{}
  58. }
  59. type DocSearchOptions struct {
  60. Db string
  61. CollectName string
  62. Query Map
  63. Project []string
  64. Sort bson.M
  65. }
  66. type DocFilterOptions struct {
  67. Db string
  68. CollectName string
  69. Query Map
  70. }
  71. func NewDocSearchOptions(filter Map, project []string) *DocSearchOptions {
  72. return &DocSearchOptions{
  73. Query: filter,
  74. Project: project,
  75. }
  76. }
  77. func RepoAddDoc(ctx *RepoSession, collectName string, doc interface{}) (string, error) {
  78. users := ctx.Client.GetCollection(collectName)
  79. result, err := users.InsertOne(ctx.Ctx, doc)
  80. if err != nil {
  81. return "", err
  82. }
  83. return result.InsertedID.(primitive.ObjectID).Hex(), nil
  84. }
  85. func RepoAddDoc1(ctx *RepoSession, collectName string, doc interface{}, recordLogReq *RecordLogReq) (string, error) {
  86. colls := ctx.Client.GetCollection(collectName)
  87. result, err := colls.InsertOne(ctx.Ctx, doc)
  88. if err != nil {
  89. return "", err
  90. }
  91. _id := result.InsertedID.(primitive.ObjectID)
  92. insertId, err := _id.Hex(), nil
  93. if !_id.IsZero() {
  94. if isShouldRecordHistoryByName(collectName) {
  95. var newData Map
  96. colls.FindOne(ctx.Ctx, bson.M{"_id": _id}).Decode(&newData)
  97. newObjByte, err := json.Marshal(newData)
  98. if err != nil {
  99. fmt.Println("记录历史失败:", err)
  100. }
  101. history := &dm.History{
  102. Userinfo: recordLogReq.UserInfo,
  103. TargetId: insertId,
  104. Path: recordLogReq.Path,
  105. Collection: collectName,
  106. Type: recordLogReq.Type,
  107. Content: string(newObjByte),
  108. CreateTime: time.Now(),
  109. }
  110. // 订单
  111. if isBill(collectName) {
  112. RepoAddDoc(ctx, CollectionBillHistory, history)
  113. // 计划
  114. } else {
  115. RepoAddDoc(ctx, CollectionPlanHistory, history)
  116. }
  117. }
  118. }
  119. return insertId, nil
  120. }
  121. func RepoDbAddDoc(ctx *RepoSession, dbName string, collectName string, doc interface{}) (string, error) {
  122. users := ctx.Client.GetDbCollection(dbName, collectName)
  123. result, err := users.InsertOne(ctx.Ctx, doc)
  124. if err != nil {
  125. return "", err
  126. }
  127. return result.InsertedID.(primitive.ObjectID).Hex(), nil
  128. }
  129. func RepoDeleteDoc(ctx *RepoSession, collectName string, id string) (interface{}, error) {
  130. uid, _ := primitive.ObjectIDFromHex(id)
  131. colls := ctx.Client.GetCollection(collectName)
  132. return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
  133. }
  134. func RepoDeleteDbDoc(ctx *RepoSession, dbName string, collectName string, id string) (interface{}, error) {
  135. uid, _ := primitive.ObjectIDFromHex(id)
  136. colls := ctx.Client.GetDbCollection(dbName, collectName)
  137. return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
  138. }
  139. func RepoDeleteDocs(ctx *RepoSession, collectName string, query interface{}) (interface{}, error) {
  140. colls := ctx.Client.GetCollection(collectName)
  141. return colls.DeleteMany(ctx.Ctx, query)
  142. }
  143. func RepoUpdateSetDoc(ctx *RepoSession, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
  144. colls := ctx.Client.GetCollection(collectName)
  145. update := bson.M{"$set": model}
  146. uid, _ := primitive.ObjectIDFromHex(idstr)
  147. return colls.UpdateByID(ctx.Ctx, uid, update)
  148. }
  149. type RecordLogReq struct {
  150. Path string
  151. UserInfo *dm.UserSmaple
  152. TargetId string
  153. Type string
  154. }
  155. func RepoUpdateSetDoc1(ctx *RepoSession, collectName string, idstr string, model interface{}, recordLogReq *RecordLogReq) (*mongo.UpdateResult, error) {
  156. colls := ctx.Client.GetCollection(collectName)
  157. update := bson.M{"$set": model}
  158. uid, _ := primitive.ObjectIDFromHex(idstr)
  159. // 查询更改前的数据,用于历史记录
  160. result, err := colls.UpdateByID(ctx.Ctx, uid, update)
  161. // 更新出错或者没有更新就不进行更新日志记录
  162. if err != nil || result.ModifiedCount == 0 {
  163. return result, err
  164. }
  165. if isShouldRecordHistoryByName(collectName) {
  166. var newData Map
  167. colls.FindOne(ctx.Ctx, bson.M{"_id": uid}).Decode(&newData)
  168. newObjByte, err := json.Marshal(newData)
  169. if err != nil {
  170. fmt.Println("记录历史失败:", err)
  171. }
  172. history := &dm.History{
  173. Userinfo: recordLogReq.UserInfo,
  174. TargetId: recordLogReq.TargetId,
  175. Path: recordLogReq.Path,
  176. Collection: collectName,
  177. Type: recordLogReq.Type,
  178. Content: string(newObjByte),
  179. CreateTime: time.Now(),
  180. }
  181. // 订单
  182. if isBill(collectName) {
  183. RepoAddDoc(ctx, CollectionBillHistory, history)
  184. // 计划
  185. } else {
  186. RepoAddDoc(ctx, CollectionPlanHistory, history)
  187. }
  188. }
  189. return result, err
  190. }
  191. func isBill(collection string) bool {
  192. bills := []string{
  193. CollectionBillPurchase,
  194. CollectionBillProduce,
  195. CollectionBillProduct,
  196. }
  197. for _, bill := range bills {
  198. if bill == collection {
  199. return true
  200. }
  201. }
  202. return false
  203. }
  204. func isShouldRecordHistoryByName(collection string) bool {
  205. shouldRecords := []string{
  206. CollectionBillPurchase,
  207. CollectionBillProduce,
  208. CollectionBillProduct,
  209. CollectionProductPlan,
  210. }
  211. for _, sr := range shouldRecords {
  212. if sr == collection {
  213. return true
  214. }
  215. }
  216. return false
  217. }
  218. func RepoUpdateSeDbDoc(ctx *RepoSession, db string, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
  219. colls := ctx.Client.GetDbCollection(db, collectName)
  220. update := bson.M{"$set": model}
  221. uid, _ := primitive.ObjectIDFromHex(idstr)
  222. return colls.UpdateByID(ctx.Ctx, uid, update)
  223. }
  224. func RepoUpdateSetDocProps(ctx *RepoSession, collectName string, idstr string, update interface{}) (*mongo.UpdateResult, error) {
  225. colls := ctx.Client.GetCollection(collectName)
  226. // update := bson.M{"$set": model}
  227. uid, _ := primitive.ObjectIDFromHex(idstr)
  228. return colls.UpdateByID(ctx.Ctx, uid, update)
  229. }
  230. func RepoUpdateSetDocsProps(ctx *RepoSession, filter *DocFilterOptions, model interface{}) (*mongo.UpdateResult, error) {
  231. colls := ctx.Client.GetCollection(filter.CollectName)
  232. if len(filter.Db) > 0 {
  233. colls = ctx.Client.GetDbCollection(filter.Db, filter.CollectName)
  234. }
  235. update := bson.M{"$set": model}
  236. filterParams := bson.M{}
  237. if len(filter.Query) > 0 {
  238. for k, v := range filter.Query {
  239. if k == "_id" {
  240. if uid, ok := v.(string); ok {
  241. docId, _ := primitive.ObjectIDFromHex(uid)
  242. filterParams["_id"] = docId
  243. continue
  244. }
  245. }
  246. filterParams[k] = v
  247. }
  248. }
  249. return colls.UpdateMany(ctx.Ctx, filterParams, update)
  250. }
  251. func RepoUpdateSetDbDocProps(ctx *RepoSession, db string, collectName string, idstr string, update interface{}) (*mongo.UpdateResult, error) {
  252. colls := ctx.Client.GetDbCollection(db, collectName)
  253. // update := bson.M{"$set": model}
  254. uid, _ := primitive.ObjectIDFromHex(idstr)
  255. return colls.UpdateByID(ctx.Ctx, uid, update)
  256. }
  257. func RepoSeachDoc(ctx *RepoSession, param *DocSearchOptions, v interface{}) (bool, error) {
  258. colls := ctx.Client.GetDbCollection(param.Db, param.CollectName)
  259. opt := &options.FindOneOptions{}
  260. if len(param.Project) > 0 {
  261. prj := bson.M{}
  262. for _, v := range param.Project {
  263. prj[v] = 1
  264. }
  265. opt.SetProjection(prj)
  266. }
  267. if len(param.Sort) > 0 {
  268. opt.Sort = param.Sort
  269. }
  270. filter := bson.M{}
  271. if len(param.Query) > 0 {
  272. for k, v := range param.Query {
  273. if k == "_id" {
  274. if uid, ok := v.(string); ok {
  275. docId, _ := primitive.ObjectIDFromHex(uid)
  276. filter["_id"] = docId
  277. continue
  278. }
  279. }
  280. filter[k] = v
  281. }
  282. }
  283. err := colls.FindOne(ctx.Ctx, filter, opt).Decode(v)
  284. if err == mongo.ErrNoDocuments {
  285. return false, nil
  286. }
  287. if err != nil {
  288. return false, err
  289. }
  290. return true, nil
  291. }
  292. func RepoSeachDocMap(ctx *RepoSession, param *DocSearchOptions) (bool, map[string]interface{}) {
  293. ret := map[string]interface{}{}
  294. ok := true
  295. colls := ctx.Client.GetDbCollection(param.Db, param.CollectName)
  296. opt := &options.FindOneOptions{}
  297. if len(param.Project) > 0 {
  298. prj := bson.M{}
  299. for _, v := range param.Project {
  300. prj[v] = 1
  301. }
  302. opt.SetProjection(prj)
  303. }
  304. if len(param.Sort) > 0 {
  305. opt.Sort = param.Sort
  306. }
  307. filter := bson.M{}
  308. if len(param.Query) > 0 {
  309. for k, v := range param.Query {
  310. if k == "_id" {
  311. if uid, ok := v.(string); ok {
  312. docId, _ := primitive.ObjectIDFromHex(uid)
  313. filter["_id"] = docId
  314. continue
  315. }
  316. }
  317. filter[k] = v
  318. }
  319. }
  320. ok = true
  321. err := colls.FindOne(ctx.Ctx, filter, opt).Decode(ret)
  322. if err == mongo.ErrNoDocuments {
  323. ok = false
  324. }
  325. if err != nil {
  326. ok = false
  327. }
  328. return ok, ret
  329. }
  330. // PageSearch 单表分页查询
  331. func RepoPageSearch(ctx *RepoSession, para *PageSearchOptions) (out *PageResult, err error) {
  332. var colls *mongo.Collection
  333. if len(para.Db) > 0 {
  334. colls = ctx.Client.GetDbCollection(para.Db, para.CollectName)
  335. } else {
  336. colls = ctx.Client.GetCollection(para.CollectName)
  337. }
  338. findoptions := &options.FindOptions{}
  339. if para.Size > 0 {
  340. findoptions.SetLimit(para.Size)
  341. findoptions.SetSkip(para.Size * (para.Page - 1))
  342. }
  343. if para.Sort != nil {
  344. findoptions.SetSort(para.Sort)
  345. }
  346. if len(para.Project) > 0 {
  347. prj := bson.M{}
  348. for _, v := range para.Project {
  349. prj[v] = 1
  350. }
  351. findoptions.SetProjection(prj)
  352. }
  353. filter := bson.M{}
  354. if len(para.Query) > 0 {
  355. for k, v := range para.Query {
  356. if value, ok := v.(string); ok {
  357. if len(value) > 0 {
  358. filter[k] = v
  359. continue
  360. }
  361. } else if v != nil {
  362. filter[k] = v
  363. }
  364. }
  365. }
  366. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  367. out = &PageResult{
  368. List: []map[string]interface{}{},
  369. Total: 0,
  370. Page: para.Page,
  371. Size: para.Size,
  372. }
  373. if err != nil {
  374. return out, err
  375. }
  376. defer cur.Close(ctx.Ctx)
  377. err = cur.All(ctx.Ctx, &out.List)
  378. out.Total, _ = colls.CountDocuments(ctx.Ctx, filter)
  379. return
  380. }
  381. func RepoDbCountDoc(ctx *RepoSession, db string, collectionName string, Query Map) (int64, error) {
  382. colls := ctx.Client.GetDbCollection(db, collectionName)
  383. filter := bson.M{}
  384. if len(Query) > 0 {
  385. for k, v := range Query {
  386. if value, ok := v.(string); ok {
  387. if len(value) > 0 {
  388. filter[k] = v
  389. continue
  390. }
  391. } else {
  392. filter[k] = v
  393. }
  394. }
  395. }
  396. return colls.CountDocuments(ctx.Ctx, filter)
  397. }
  398. func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, error) {
  399. colls := ctx.Client.GetCollection(collectionName)
  400. filter := bson.M{}
  401. if len(Query) > 0 {
  402. for k, v := range Query {
  403. if value, ok := v.(string); ok {
  404. if len(value) > 0 {
  405. filter[k] = v
  406. continue
  407. }
  408. } else {
  409. filter[k] = v
  410. }
  411. }
  412. }
  413. return colls.CountDocuments(ctx.Ctx, filter)
  414. }
  415. // PageSearch 单表分页查询
  416. func RepoDocsSearch(ctx *RepoSession, para *PageSearchOptions, out interface{}) (err error) {
  417. colls := ctx.Client.GetCollection(para.CollectName)
  418. if len(para.Db) > 0 {
  419. colls = ctx.Client.GetDbCollection(para.Db, para.CollectName)
  420. }
  421. findoptions := &options.FindOptions{}
  422. if para.Size > 0 {
  423. findoptions.SetLimit(para.Size)
  424. findoptions.SetSkip(para.Size * (para.Page - 1))
  425. }
  426. if para.Sort != nil {
  427. findoptions.SetSort(para.Sort)
  428. }
  429. if len(para.Project) > 0 {
  430. prj := bson.M{}
  431. for _, v := range para.Project {
  432. prj[v] = 1
  433. }
  434. findoptions.SetProjection(prj)
  435. }
  436. filter := bson.M{}
  437. if len(para.Query) > 0 {
  438. for k, v := range para.Query {
  439. if value, ok := v.(string); ok {
  440. if len(value) > 0 {
  441. filter[k] = v
  442. continue
  443. }
  444. } else {
  445. filter[k] = v
  446. }
  447. }
  448. }
  449. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  450. if err != nil {
  451. return err
  452. }
  453. defer cur.Close(ctx.Ctx)
  454. err = cur.All(ctx.Ctx, out)
  455. return
  456. }
  457. func RepoDocArrayAppend(ctx *RepoSession, collectName string, idstr string, fieldpath string, arrayItem interface{}) (*mongo.UpdateResult, error) {
  458. colls := ctx.Client.GetCollection(collectName)
  459. arrayOp := bson.M{}
  460. arrayOp[fieldpath] = arrayItem
  461. update := bson.M{"$push": arrayOp}
  462. uid, _ := primitive.ObjectIDFromHex(idstr)
  463. return colls.UpdateByID(ctx.Ctx, uid, update)
  464. }
  465. // { _id: 4, "grades.grade": 85 },
  466. // { $set: { "grades.$.std" : 6 } }
  467. type ArrayOneUpdateOption struct {
  468. Query Map
  469. Set Map
  470. CollectName string
  471. Id string
  472. }
  473. // if len(scene.Stickers) > 0 {
  474. // optSet["scenes.$.stickers"] = scene.Stickers
  475. // }
  476. // option := &repo.ArrayOneUpdateOption{
  477. // CollectName: repo.CollectionDesigns,
  478. // Id: id,
  479. // Query: repo.Map{"scenes.id": scene.Id},
  480. // Set: optSet,
  481. // }
  482. func RepoDocArrayOneUpdate(ctx *RepoSession, options *ArrayOneUpdateOption) (*mongo.UpdateResult, error) {
  483. colls := ctx.Client.GetCollection(options.CollectName)
  484. docId, _ := primitive.ObjectIDFromHex(options.Id)
  485. query := bson.M{"_id": docId}
  486. if len(options.Query) > 0 {
  487. for k, v := range options.Query {
  488. query[k] = v
  489. }
  490. }
  491. setOp := bson.M{}
  492. for k, v := range options.Set {
  493. setOp[k] = v
  494. }
  495. update := bson.M{"$set": setOp}
  496. return colls.UpdateOne(ctx.Ctx, query, update)
  497. }
  498. type ArrayOneRemoveOption struct {
  499. ArrayQuery Map
  500. CollectName string
  501. Id string
  502. }
  503. // { $pull: { "items" : { id: 23 } } }
  504. func RepoDocArrayOneRemove(ctx *RepoSession, options *ArrayOneRemoveOption) (*mongo.UpdateResult, error) {
  505. colls := ctx.Client.GetCollection(options.CollectName)
  506. docId, _ := primitive.ObjectIDFromHex(options.Id)
  507. query := bson.M{"_id": docId}
  508. arrayQuery := bson.M{}
  509. if len(options.ArrayQuery) > 0 {
  510. for k, v := range options.ArrayQuery {
  511. arrayQuery[k] = v
  512. }
  513. }
  514. update := bson.M{"$pull": arrayQuery}
  515. return colls.UpdateOne(ctx.Ctx, query, update)
  516. }
  517. type ArrayOneSearchOption struct {
  518. ArrayQuery Map
  519. CollectName string
  520. Id string
  521. Field string
  522. }
  523. func RepoDocArraySearch(ctx *RepoSession, options *ArrayOneSearchOption, entity interface{}) error {
  524. colls := ctx.Client.GetCollection(options.CollectName)
  525. docId, _ := primitive.ObjectIDFromHex(options.Id)
  526. match := []bson.E{}
  527. match = append(match, bson.E{"_id", docId})
  528. matchStage := bson.D{
  529. {"$match", match},
  530. }
  531. unwindStage := bson.D{
  532. {"$unwind", fmt.Sprintf("%s%s", "$", options.Field)},
  533. }
  534. pipe := mongo.Pipeline{matchStage, unwindStage}
  535. if len(options.ArrayQuery) > 0 {
  536. match2 := []bson.E{}
  537. for k, v := range options.ArrayQuery {
  538. match2 = append(match2, bson.E{k, v})
  539. }
  540. match2Stage := bson.D{
  541. {"$match", match2},
  542. }
  543. pipe = append(pipe, match2Stage)
  544. }
  545. curr, err := colls.Aggregate(ctx.Ctx, pipe)
  546. if err != nil {
  547. return err
  548. }
  549. defer curr.Close(ctx.Ctx)
  550. if curr.Next(ctx.Ctx) {
  551. err = curr.Decode(entity)
  552. if err != nil {
  553. return err
  554. }
  555. return nil
  556. }
  557. return nil
  558. }
  559. type DocsSearchOptions struct {
  560. CollectName string
  561. Query map[string]interface{}
  562. Project []string
  563. Sort interface{} //bson.D{ bson.E{"update_time", -1}, bson.E{"goods_id", -1},}
  564. }
  565. func RepoSeachDocsMap(ctx *RepoSession, param *DocsSearchOptions) (ok bool, list []map[string]interface{}) {
  566. colls := ctx.Client.GetCollection(param.CollectName)
  567. findoptions := &options.FindOptions{}
  568. if len(param.Project) > 0 {
  569. prj := bson.M{}
  570. for _, v := range param.Project {
  571. prj[v] = 1
  572. }
  573. findoptions.SetProjection(prj)
  574. }
  575. if param.Sort != nil {
  576. findoptions.SetSort(param.Sort)
  577. }
  578. filter := bson.M{}
  579. if len(param.Query) > 0 {
  580. for k, v := range param.Query {
  581. if value, ok := v.(string); ok {
  582. if len(value) > 0 {
  583. filter[k] = v
  584. continue
  585. }
  586. } else {
  587. filter[k] = v
  588. }
  589. }
  590. }
  591. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  592. if err != nil {
  593. ok = false
  594. return
  595. }
  596. defer cur.Close(ctx.Ctx)
  597. listRes := []map[string]interface{}{}
  598. err = cur.All(ctx.Ctx, &listRes)
  599. if err != nil {
  600. log.Error(err)
  601. ok = false
  602. return
  603. }
  604. list = listRes
  605. ok = true
  606. return
  607. }
  608. type DbDocsSearchOptions struct {
  609. Db string
  610. CollectName string
  611. Query map[string]interface{}
  612. Project []string
  613. Sort interface{} //bson.D{ bson.E{"update_time", -1}, bson.E{"goods_id", -1},}
  614. }
  615. func DbRepoSeachDocsMap(ctx *RepoSession, param *DbDocsSearchOptions) (ok bool, list []map[string]interface{}) {
  616. colls := ctx.Client.GetDbCollection(param.Db, param.CollectName)
  617. findoptions := &options.FindOptions{}
  618. if len(param.Project) > 0 {
  619. prj := bson.M{}
  620. for _, v := range param.Project {
  621. prj[v] = 1
  622. }
  623. findoptions.SetProjection(prj)
  624. }
  625. if param.Sort != nil {
  626. findoptions.SetSort(param.Sort)
  627. }
  628. filter := bson.M{}
  629. if len(param.Query) > 0 {
  630. for k, v := range param.Query {
  631. if value, ok := v.(string); ok {
  632. if len(value) > 0 {
  633. filter[k] = v
  634. continue
  635. }
  636. } else {
  637. filter[k] = v
  638. }
  639. }
  640. }
  641. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  642. if err != nil {
  643. ok = false
  644. return
  645. }
  646. defer cur.Close(ctx.Ctx)
  647. listRes := []map[string]interface{}{}
  648. err = cur.All(ctx.Ctx, &listRes)
  649. if err != nil {
  650. log.Error(err)
  651. ok = false
  652. return
  653. }
  654. list = listRes
  655. ok = true
  656. return
  657. }