pack.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.POSTJWT("/pack/create", CreatePack)
  17. // 获取包装详情
  18. r.GETJWT("/pack/detail/:id", GetPack)
  19. // 获取包装列表
  20. r.GETJWT("/pack/list", GetPacks)
  21. // 更新包装
  22. r.POSTJWT("/pack/update", UpdatePack)
  23. // 删除包装
  24. r.POSTJWT("/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. pack.CompCounts = len(pack.Components)
  39. pack.CreateTime = time.Now()
  40. pack.UpdateTime = time.Now()
  41. result, err := repo.RepoAddDoc(ctx, repo.CollectionPack, &pack)
  42. return result, err
  43. }
  44. // 获取包装信息
  45. func GetPack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  46. packId := c.Param("id")
  47. id, err := primitive.ObjectIDFromHex(packId)
  48. if err != nil {
  49. return nil, errors.New("非法id")
  50. }
  51. var pack model.Pack
  52. option := &repo.DocSearchOptions{
  53. CollectName: repo.CollectionPack,
  54. Query: repo.Map{"_id": id},
  55. }
  56. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &pack)
  57. if !found || err != nil {
  58. log.Info(err)
  59. return nil, errors.New("数据未找到")
  60. }
  61. return pack, nil
  62. }
  63. // 获取包装列表
  64. func GetPacks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  65. page, size, query := UtilQueryPageSize(c)
  66. if _name, ok := query["name"]; ok {
  67. delete(query, "name")
  68. query["name"] = bson.M{"$regex": _name.(string)}
  69. }
  70. option := &repo.PageSearchOptions{
  71. CollectName: repo.CollectionPack,
  72. Query: query,
  73. Page: page,
  74. Size: size,
  75. Sort: bson.M{"createTime": -1},
  76. Project: []string{"_id", "name", "thumbnail", "compCounts", "designer", "updateTime"},
  77. }
  78. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  79. }
  80. // 更新包装
  81. func UpdatePack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  82. var pack model.Pack
  83. err := c.ShouldBindJSON(&pack)
  84. if err != nil {
  85. return nil, errors.New("参数错误")
  86. }
  87. if pack.Id.Hex() == "" {
  88. return nil, errors.New("id的为空")
  89. }
  90. pack.UpdateTime = time.Now()
  91. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionPack, pack.Id.Hex(), &pack)
  92. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionPack, pack.Id.Hex(), &pack, &repo.RecordLogReq{
  93. Path: c.Request.URL.Path,
  94. TargetId: pack.Id.Hex(),
  95. })
  96. }
  97. // 删除包装
  98. func DelPack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  99. packId := c.Param("id")
  100. if packId == "" {
  101. return nil, errors.New("id为空")
  102. }
  103. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPack, packId)
  104. }