repo.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. package repo
  2. import (
  3. "assetcenter/db"
  4. "assetcenter/log"
  5. "context"
  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. CollectionUser = "users"
  18. CollectionProject = "projects"
  19. CollectionAdmin = "admins"
  20. CollectionTexture = "textures"
  21. CollectionCategories = "categories"
  22. CollectionDbAssetCategory = "categories-asset"
  23. CollectionLibCategory = "libCategory"
  24. CollectionLib2Category = "lib2Category"
  25. CollectionTeam = "team"
  26. CollectionStickers = "sticker"
  27. CollectionMesh = "meshes"
  28. CollectionEvn3d = "Env3ds"
  29. CollectionTask = "tasks"
  30. CollectionTaskMeshConv = "task-meshconv"
  31. CollectionTaskHdrConv = "task-hdrconv"
  32. CollectionTaskShadowCreator = "task-shadowCreator"
  33. CollectionTaskShadow = "task-shadow"
  34. CollectionProductTpls = "productTpls"
  35. CollectionMaterials = "materials"
  36. CollectionBaseMats = "basemats"
  37. CollectionDecorates = "decorate-mesh"
  38. CollectionInfoDesigns = "infoDesigns"
  39. CollectionInfoTrends = "infoTrends"
  40. CollectionDesignProducts = "designProducts"
  41. CollectionShoes = "shoes"
  42. CollectionImageMat = "imgmats"
  43. CollectionFabric = "fabrics"
  44. CollectionDesigns = "designs"
  45. CollectionBackground = "backgrounds"
  46. CollectionLasts = "last-mesh"
  47. CollectionHeels = "heel-mesh"
  48. CollectionSoles = "sole-mesh"
  49. CollectionPerms = "perms"
  50. CollectionRoles = "roles"
  51. CollectionDatabase = "database"
  52. CollectionAssetCount = "assetcount"
  53. CollectionUserDatabase = "user-databases"
  54. )
  55. type Map map[string]interface{}
  56. type PageResult struct {
  57. List []map[string]interface{} `json:"list"`
  58. Total int64 `json:"total"`
  59. Page int64 `json:"page"`
  60. Size int64 `json:"size"`
  61. }
  62. type PageEnityResult struct {
  63. List interface{} `json:"list"`
  64. Total int64 `json:"total"`
  65. Page int64 `json:"page"`
  66. Size int64 `json:"size"`
  67. }
  68. type PageSearchOptions struct {
  69. Db string
  70. CollectName string
  71. Page int64
  72. Size int64
  73. Query map[string]interface{}
  74. Project []string
  75. Sort interface{}
  76. List interface{}
  77. }
  78. type DocSearchOptions struct {
  79. Db string
  80. CollectName string
  81. Query Map
  82. Project []string
  83. Sort bson.M
  84. }
  85. func NewDocSearchOptions(filter Map, project []string) *DocSearchOptions {
  86. return &DocSearchOptions{
  87. Query: filter,
  88. Project: project,
  89. }
  90. }
  91. func RepoAddDoc(ctx *RepoSession, collectName string, doc interface{}) (string, error) {
  92. users := ctx.Client.GetCollection(collectName)
  93. result, err := users.InsertOne(ctx.Ctx, doc)
  94. if err != nil {
  95. return "", err
  96. }
  97. return result.InsertedID.(primitive.ObjectID).Hex(), nil
  98. }
  99. func RepoDbAddDoc(ctx *RepoSession, dbName string, collectName string, doc interface{}) (string, error) {
  100. users := ctx.Client.GetDbCollection(dbName, collectName)
  101. result, err := users.InsertOne(ctx.Ctx, doc)
  102. if err != nil {
  103. return "", err
  104. }
  105. return result.InsertedID.(primitive.ObjectID).Hex(), nil
  106. }
  107. func RepoDeleteDoc(ctx *RepoSession, collectName string, id string) (interface{}, error) {
  108. uid, _ := primitive.ObjectIDFromHex(id)
  109. colls := ctx.Client.GetCollection(collectName)
  110. return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
  111. }
  112. func RepoDeleteDbDoc(ctx *RepoSession, dbName string, collectName string, id string) (interface{}, error) {
  113. uid, _ := primitive.ObjectIDFromHex(id)
  114. colls := ctx.Client.GetDbCollection(dbName, collectName)
  115. return colls.DeleteOne(ctx.Ctx, &bson.M{"_id": uid})
  116. }
  117. func RepoDeleteDocs(ctx *RepoSession, collectName string, query interface{}) (interface{}, error) {
  118. colls := ctx.Client.GetCollection(collectName)
  119. return colls.DeleteMany(ctx.Ctx, query)
  120. }
  121. func RepoUpdateSetDoc(ctx *RepoSession, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
  122. colls := ctx.Client.GetCollection(collectName)
  123. update := bson.M{"$set": model}
  124. uid, _ := primitive.ObjectIDFromHex(idstr)
  125. return colls.UpdateByID(ctx.Ctx, uid, update)
  126. }
  127. func RepoUpdateSeDbDoc(ctx *RepoSession, db string, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {
  128. colls := ctx.Client.GetDbCollection(db, collectName)
  129. update := bson.M{"$set": model}
  130. uid, _ := primitive.ObjectIDFromHex(idstr)
  131. return colls.UpdateByID(ctx.Ctx, uid, update)
  132. }
  133. func RepoUpdateSetDocProps(ctx *RepoSession, collectName string, idstr string, update interface{}) (*mongo.UpdateResult, error) {
  134. colls := ctx.Client.GetCollection(collectName)
  135. // update := bson.M{"$set": model}
  136. uid, _ := primitive.ObjectIDFromHex(idstr)
  137. return colls.UpdateByID(ctx.Ctx, uid, update)
  138. }
  139. func RepoUpdateSetDbDocProps(ctx *RepoSession, db string, collectName string, idstr string, update interface{}) (*mongo.UpdateResult, error) {
  140. colls := ctx.Client.GetDbCollection(db, collectName)
  141. // update := bson.M{"$set": model}
  142. uid, _ := primitive.ObjectIDFromHex(idstr)
  143. return colls.UpdateByID(ctx.Ctx, uid, update)
  144. }
  145. func RepoSeachDoc(ctx *RepoSession, param *DocSearchOptions, v interface{}) (bool, error) {
  146. colls := ctx.Client.GetDbCollection(param.Db, param.CollectName)
  147. opt := &options.FindOneOptions{
  148. Sort: param.Sort,
  149. }
  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. err := colls.FindOne(ctx.Ctx, filter, opt).Decode(v)
  171. if err == mongo.ErrNoDocuments {
  172. return false, nil
  173. }
  174. if err != nil {
  175. return false, err
  176. }
  177. return true, nil
  178. }
  179. func RepoSeachDocMap(ctx *RepoSession, param *DocSearchOptions) (bool, map[string]interface{}) {
  180. ret := map[string]interface{}{}
  181. ok := true
  182. colls := ctx.Client.GetDbCollection(param.Db, 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. var colls *mongo.Collection
  217. if len(para.Db) > 0 {
  218. colls = ctx.Client.GetDbCollection(para.Db, para.CollectName)
  219. } else {
  220. colls = ctx.Client.GetCollection(para.CollectName)
  221. }
  222. findoptions := &options.FindOptions{}
  223. if para.Size > 0 {
  224. findoptions.SetLimit(para.Size)
  225. findoptions.SetSkip(para.Size * (para.Page - 1))
  226. }
  227. if para.Sort != nil {
  228. findoptions.SetSort(para.Sort)
  229. }
  230. if len(para.Project) > 0 {
  231. prj := bson.M{}
  232. for _, v := range para.Project {
  233. prj[v] = 1
  234. }
  235. findoptions.SetProjection(prj)
  236. }
  237. filter := bson.M{}
  238. if len(para.Query) > 0 {
  239. for k, v := range para.Query {
  240. if value, ok := v.(string); ok {
  241. if len(value) > 0 {
  242. filter[k] = v
  243. continue
  244. }
  245. } else if v != nil {
  246. filter[k] = v
  247. }
  248. }
  249. }
  250. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  251. out = &PageResult{
  252. List: []map[string]interface{}{},
  253. Total: 0,
  254. Page: para.Page,
  255. Size: para.Size,
  256. }
  257. if err != nil {
  258. return out, err
  259. }
  260. defer cur.Close(ctx.Ctx)
  261. err = cur.All(ctx.Ctx, &out.List)
  262. out.Total, _ = colls.CountDocuments(ctx.Ctx, filter)
  263. return
  264. }
  265. // PageSearch 单表分页查询
  266. func RepoPageSearchEntitys(ctx *RepoSession, para *PageSearchOptions) (out *PageEnityResult, err error) {
  267. var colls *mongo.Collection
  268. if len(para.Db) > 0 {
  269. colls = ctx.Client.GetDbCollection(para.Db, para.CollectName)
  270. } else {
  271. colls = ctx.Client.GetCollection(para.CollectName)
  272. }
  273. findoptions := &options.FindOptions{}
  274. if para.Size > 0 {
  275. findoptions.SetLimit(para.Size)
  276. findoptions.SetSkip(para.Size * (para.Page - 1))
  277. }
  278. if para.Sort != nil {
  279. findoptions.SetSort(para.Sort)
  280. }
  281. if len(para.Project) > 0 {
  282. prj := bson.M{}
  283. for _, v := range para.Project {
  284. prj[v] = 1
  285. }
  286. findoptions.SetProjection(prj)
  287. }
  288. filter := bson.M{}
  289. if len(para.Query) > 0 {
  290. for k, v := range para.Query {
  291. if value, ok := v.(string); ok {
  292. if len(value) > 0 {
  293. filter[k] = v
  294. continue
  295. }
  296. } else if v != nil {
  297. filter[k] = v
  298. }
  299. }
  300. }
  301. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  302. out = &PageEnityResult{
  303. Total: 0,
  304. Page: para.Page,
  305. Size: para.Size,
  306. }
  307. if err != nil {
  308. return out, err
  309. }
  310. defer cur.Close(ctx.Ctx)
  311. err = cur.All(ctx.Ctx, para.List)
  312. out.List = para.List
  313. out.Total, _ = colls.CountDocuments(ctx.Ctx, filter)
  314. return
  315. }
  316. // PageSearch 单表分页查询
  317. func RepoCountDoc(ctx *RepoSession, collectionName string, Query Map) (int64, error) {
  318. colls := ctx.Client.GetCollection(collectionName)
  319. filter := bson.M{}
  320. if len(Query) > 0 {
  321. for k, v := range Query {
  322. if value, ok := v.(string); ok {
  323. if len(value) > 0 {
  324. filter[k] = v
  325. continue
  326. }
  327. } else {
  328. filter[k] = v
  329. }
  330. }
  331. }
  332. return colls.CountDocuments(ctx.Ctx, filter)
  333. }
  334. // PageSearch 单表分页查询
  335. func RepoDocsSearch(ctx *RepoSession, para *PageSearchOptions, out interface{}) (err error) {
  336. // colls := ctx.Client.GetCollection(para.CollectName)
  337. colls := ctx.Client.GetCollection(para.CollectName)
  338. if len(para.Db) > 0 {
  339. colls = ctx.Client.GetDbCollection(para.Db, para.CollectName)
  340. }
  341. findoptions := &options.FindOptions{}
  342. if para.Size > 0 {
  343. findoptions.SetLimit(para.Size)
  344. findoptions.SetSkip(para.Size * (para.Page - 1))
  345. }
  346. if para.Sort != nil {
  347. findoptions.SetSort(para.Sort)
  348. }
  349. if len(para.Project) > 0 {
  350. prj := bson.M{}
  351. for _, v := range para.Project {
  352. prj[v] = 1
  353. }
  354. findoptions.SetProjection(prj)
  355. }
  356. filter := bson.M{}
  357. if len(para.Query) > 0 {
  358. for k, v := range para.Query {
  359. if value, ok := v.(string); ok {
  360. if len(value) > 0 {
  361. filter[k] = v
  362. continue
  363. }
  364. } else {
  365. filter[k] = v
  366. }
  367. }
  368. }
  369. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  370. if err != nil {
  371. return err
  372. }
  373. defer cur.Close(ctx.Ctx)
  374. err = cur.All(ctx.Ctx, out)
  375. return
  376. }
  377. func RepoDocArrayAppend(ctx *RepoSession, collectName string, idstr string, fieldpath string, arrayItem interface{}) (*mongo.UpdateResult, error) {
  378. colls := ctx.Client.GetCollection(collectName)
  379. arrayOp := bson.M{}
  380. arrayOp[fieldpath] = arrayItem
  381. update := bson.M{"$push": arrayOp}
  382. uid, _ := primitive.ObjectIDFromHex(idstr)
  383. return colls.UpdateByID(ctx.Ctx, uid, update)
  384. }
  385. // { _id: 4, "grades.grade": 85 },
  386. // { $set: { "grades.$.std" : 6 } }
  387. type ArrayOneUpdateOption struct {
  388. Query Map
  389. Set Map
  390. CollectName string
  391. Id string
  392. }
  393. // if len(scene.Stickers) > 0 {
  394. // optSet["scenes.$.stickers"] = scene.Stickers
  395. // }
  396. // option := &repo.ArrayOneUpdateOption{
  397. // CollectName: repo.CollectionDesigns,
  398. // Id: id,
  399. // Query: repo.Map{"scenes.id": scene.Id},
  400. // Set: optSet,
  401. // }
  402. func RepoDocArrayOneUpdate(ctx *RepoSession, options *ArrayOneUpdateOption) (*mongo.UpdateResult, error) {
  403. colls := ctx.Client.GetCollection(options.CollectName)
  404. docId, _ := primitive.ObjectIDFromHex(options.Id)
  405. query := bson.M{"_id": docId}
  406. if len(options.Query) > 0 {
  407. for k, v := range options.Query {
  408. query[k] = v
  409. }
  410. }
  411. setOp := bson.M{}
  412. for k, v := range options.Set {
  413. setOp[k] = v
  414. }
  415. update := bson.M{"$set": setOp}
  416. return colls.UpdateOne(ctx.Ctx, query, update)
  417. }
  418. type ArrayOneRemoveOption struct {
  419. ArrayQuery Map
  420. CollectName string
  421. Id string
  422. }
  423. // { $pull: { "items" : { id: 23 } } }
  424. func RepoDocArrayOneRemove(ctx *RepoSession, options *ArrayOneRemoveOption) (*mongo.UpdateResult, error) {
  425. colls := ctx.Client.GetCollection(options.CollectName)
  426. docId, _ := primitive.ObjectIDFromHex(options.Id)
  427. query := bson.M{"_id": docId}
  428. arrayQuery := bson.M{}
  429. if len(options.ArrayQuery) > 0 {
  430. for k, v := range options.ArrayQuery {
  431. arrayQuery[k] = v
  432. }
  433. }
  434. update := bson.M{"$pull": arrayQuery}
  435. return colls.UpdateOne(ctx.Ctx, query, update)
  436. }
  437. type ArrayOneSearchOption struct {
  438. ArrayQuery Map
  439. CollectName string
  440. Id string
  441. Field string
  442. }
  443. func RepoDocArraySearch(ctx *RepoSession, options *ArrayOneSearchOption, entity interface{}) error {
  444. colls := ctx.Client.GetCollection(options.CollectName)
  445. docId, _ := primitive.ObjectIDFromHex(options.Id)
  446. match := []bson.E{}
  447. match = append(match, bson.E{"_id", docId})
  448. matchStage := bson.D{
  449. {"$match", match},
  450. }
  451. unwindStage := bson.D{
  452. {"$unwind", fmt.Sprintf("%s%s", "$", options.Field)},
  453. }
  454. pipe := mongo.Pipeline{matchStage, unwindStage}
  455. if len(options.ArrayQuery) > 0 {
  456. match2 := []bson.E{}
  457. for k, v := range options.ArrayQuery {
  458. match2 = append(match2, bson.E{k, v})
  459. }
  460. match2Stage := bson.D{
  461. {"$match", match2},
  462. }
  463. pipe = append(pipe, match2Stage)
  464. }
  465. curr, err := colls.Aggregate(ctx.Ctx, pipe)
  466. if err != nil {
  467. return err
  468. }
  469. defer curr.Close(ctx.Ctx)
  470. if curr.Next(ctx.Ctx) {
  471. err = curr.Decode(entity)
  472. if err != nil {
  473. return err
  474. }
  475. return nil
  476. }
  477. return nil
  478. }
  479. type DocsSearchOptions struct {
  480. Db string
  481. CollectName string
  482. Query map[string]interface{}
  483. Project []string
  484. Sort interface{} //bson.D{ bson.E{"update_time", -1}, bson.E{"goods_id", -1},}
  485. }
  486. func RepoSeachDocsMap(ctx *RepoSession, param *DocsSearchOptions) (ok bool, list []map[string]interface{}) {
  487. colls := ctx.Client.GetCollection(param.CollectName)
  488. if len(param.Db) > 0 {
  489. colls = ctx.Client.GetDbCollection(param.Db, param.CollectName)
  490. }
  491. findoptions := &options.FindOptions{}
  492. if len(param.Project) > 0 {
  493. prj := bson.M{}
  494. for _, v := range param.Project {
  495. prj[v] = 1
  496. }
  497. findoptions.SetProjection(prj)
  498. }
  499. if param.Sort != nil {
  500. findoptions.SetSort(param.Sort)
  501. }
  502. filter := bson.M{}
  503. if len(param.Query) > 0 {
  504. for k, v := range param.Query {
  505. if value, ok := v.(string); ok {
  506. if len(value) > 0 {
  507. filter[k] = v
  508. continue
  509. }
  510. } else {
  511. filter[k] = v
  512. }
  513. }
  514. }
  515. cur, err := colls.Find(ctx.Ctx, filter, findoptions)
  516. if err != nil {
  517. ok = false
  518. return
  519. }
  520. defer cur.Close(ctx.Ctx)
  521. listRes := []map[string]interface{}{}
  522. err = cur.All(ctx.Ctx, &listRes)
  523. if err != nil {
  524. log.Error(err)
  525. ok = false
  526. return
  527. }
  528. list = listRes
  529. ok = true
  530. return
  531. }