repo.go 13 KB

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