pack.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. )
  13. // 包装管理
  14. func Pack(r *GinRouter) {
  15. // 创建包装
  16. r.POST("/pack/create", CreatePack)
  17. // 获取包装详情
  18. r.GET("/pack/detail/:id", GetPack)
  19. // 获取包装列表
  20. r.GET("/pack/list", GetPacks)
  21. // 更新包装
  22. r.POST("/pack/update", UpdatePack)
  23. // 删除包装
  24. r.POST("/pack/delete/:id", DelPack)
  25. }
  26. // 创建包装
  27. func CreatePack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var pack model.Pack
  29. err := c.ShouldBindJSON(&pack)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. if pack.Name == "" {
  36. return nil, errors.New("包装名为空")
  37. }
  38. // 创建时生产 CompCounts,packComponent Id,mat。id craft id
  39. pack.CompCounts = len(pack.Components)
  40. // 生成id
  41. // components id
  42. // if pack.CompCounts > 0 {
  43. // for _, v := range pack.Components {
  44. // v.Id = primitive.NewObjectID().Hex()
  45. // // mats id
  46. // if len(v.Mats) > 0 {
  47. // for _, v := range v.Mats {
  48. // v.Id = primitive.NewObjectID().Hex()
  49. // // crafts id
  50. // if len(v.Crafts) > 0 {
  51. // for _, v := range v.Crafts {
  52. // v.Id = primitive.NewObjectID().Hex()
  53. // }
  54. // }
  55. // }
  56. // }
  57. // }
  58. // }
  59. pack.CreateTime = time.Now()
  60. pack.UpdateTime = time.Now()
  61. result, err := repo.RepoAddDoc(ctx, repo.CollectionPack, &pack)
  62. return result, err
  63. }
  64. // 获取包装信息
  65. func GetPack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  66. packId := c.Param("id")
  67. id, err := primitive.ObjectIDFromHex(packId)
  68. if err != nil {
  69. return nil, errors.New("非法id")
  70. }
  71. var pack model.Pack
  72. option := &repo.DocSearchOptions{
  73. CollectName: repo.CollectionPack,
  74. Query: repo.Map{"_id": id},
  75. }
  76. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &pack)
  77. if !found || err != nil {
  78. log.Info(err)
  79. return nil, errors.New("数据未找到")
  80. }
  81. return pack, nil
  82. }
  83. // 获取包装列表
  84. func GetPacks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  85. page, size, query := UtilQueryPageSize(c)
  86. option := &repo.PageSearchOptions{
  87. CollectName: repo.CollectionPack,
  88. Query: query,
  89. Page: page,
  90. Size: size,
  91. Sort: bson.M{"createTime": -1},
  92. Project: []string{"_id", "name", "thumbnail", "compCounts", "designer", "updateTime"},
  93. }
  94. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  95. }
  96. // 更新包装
  97. func UpdatePack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  98. var pack model.Pack
  99. err := c.ShouldBindJSON(&pack)
  100. if err != nil {
  101. return nil, errors.New("参数错误")
  102. }
  103. if pack.Id.Hex() == "" {
  104. return nil, errors.New("id的为空")
  105. }
  106. pack.UpdateTime = time.Now()
  107. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionPack, pack.Id.Hex(), &pack)
  108. }
  109. // 删除包装
  110. func DelPack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  111. packId := c.Param("id")
  112. if packId == "" {
  113. return nil, errors.New("id为空")
  114. }
  115. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPack, packId)
  116. }