craft.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Craft(r *GinRouter) {
  15. // 创建工艺
  16. r.POSTJWT("/craft/create", CreateCraft)
  17. // 获取工艺详情
  18. r.GETJWT("/craft/detail/:id", GetCraft)
  19. // 获取工艺列表
  20. r.GETJWT("/craft/list", GetCrafts)
  21. // 更新工艺
  22. r.POSTJWT("/craft/update", UpdateCraft)
  23. // 删除工艺
  24. r.POSTJWT("/craft/delete/:id", DelCraft)
  25. }
  26. // 创建工艺
  27. func CreateCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var craft model.Craft
  29. err := c.ShouldBindJSON(&craft)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. if craft.Name == "" {
  36. return nil, errors.New("工艺名为空")
  37. }
  38. craft.CreateTime = time.Now()
  39. craft.UpdateTime = time.Now()
  40. result, err := repo.RepoAddDoc(ctx, repo.CollectionCraft, &craft)
  41. return result, err
  42. }
  43. // 获取工艺信息
  44. func GetCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  45. craftId := c.Param("id")
  46. id, err := primitive.ObjectIDFromHex(craftId)
  47. if err != nil {
  48. return nil, errors.New("非法id")
  49. }
  50. var craft model.Craft
  51. option := &repo.DocSearchOptions{
  52. CollectName: repo.CollectionCraft,
  53. Query: repo.Map{"_id": id},
  54. }
  55. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &craft)
  56. if !found || err != nil {
  57. log.Info(err)
  58. return nil, errors.New("数据未找到")
  59. }
  60. return craft, nil
  61. }
  62. // 获取工艺列表
  63. func GetCrafts(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  64. page, size, query := UtilQueryPageSize(c)
  65. if _name, ok := query["name"]; ok {
  66. delete(query, "name")
  67. query["name"] = bson.M{"$regex": _name.(string)}
  68. }
  69. if _norm, ok := query["norm"]; ok {
  70. delete(query, "norm")
  71. query["norm"] = bson.M{"$regex": _norm.(string)}
  72. }
  73. option := &repo.PageSearchOptions{
  74. CollectName: repo.CollectionCraft,
  75. Query: query,
  76. Page: page,
  77. Size: size,
  78. Sort: bson.M{"createTime": -1},
  79. }
  80. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  81. }
  82. // 更新工艺
  83. func UpdateCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  84. var craft model.Craft
  85. err := c.ShouldBindJSON(&craft)
  86. if err != nil {
  87. return nil, errors.New("参数错误")
  88. }
  89. if craft.Id.Hex() == "" {
  90. return nil, errors.New("id的为空")
  91. }
  92. // if remark = ""
  93. if craft.Remark == "" {
  94. craft.Remark = " "
  95. }
  96. craft.UpdateTime = time.Now()
  97. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCraft, craft.Id.Hex(), &craft)
  98. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionCraft, craft.Id.Hex(), &craft, &repo.RecordLogReq{
  99. Path: c.Request.URL.Path,
  100. TargetId: craft.Id.Hex(),
  101. })
  102. }
  103. // 删除工艺
  104. func DelCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  105. craftId := c.Param("id")
  106. if craftId == "" {
  107. return nil, errors.New("id为空")
  108. }
  109. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionCraft, craftId)
  110. }