material.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. // 材料管理
  13. // 不提供修改
  14. func Material(r *GinRouter) {
  15. // 新增材料
  16. r.POSTJWT("/material/create", CreateMaterial)
  17. // 获取材料信息
  18. r.GETJWT("/material/detail/:id", GetMaterial)
  19. // 获取材料列表
  20. r.GETJWT("/material/list", GetMaterials)
  21. // 材料列表
  22. r.POSTJWT("/material/update", UpdateMaterial)
  23. r.POSTJWT("/material/delete/:id", DelMaterial)
  24. }
  25. func CreateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  26. var material model.Material
  27. err := c.ShouldBindJSON(&material)
  28. if err != nil {
  29. return nil, errors.New("参数错误!")
  30. }
  31. ctx := apictx.CreateRepoCtx()
  32. if len(material.Name) < 1 {
  33. return nil, errors.New("材料名为空")
  34. }
  35. if len(material.Category) < 1 {
  36. return nil, errors.New("材料类型为空")
  37. }
  38. material.CreateTime = time.Now()
  39. material.UpdateTime = time.Now()
  40. result, err := repo.RepoAddDoc(ctx, repo.CollectionMaterial, &material)
  41. return result, err
  42. }
  43. // 获取材料信息
  44. func GetMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  45. materialId := c.Param("id")
  46. id, err := primitive.ObjectIDFromHex(materialId)
  47. if err != nil {
  48. return nil, errors.New("非法id")
  49. }
  50. var material model.Material
  51. option := &repo.DocSearchOptions{
  52. CollectName: repo.CollectionMaterial,
  53. Query: repo.Map{"_id": id},
  54. }
  55. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &material)
  56. if !found || err != nil {
  57. log.Info(err)
  58. return nil, errors.New("数据未找到")
  59. }
  60. return material, nil
  61. }
  62. // 获取材料列表
  63. func GetMaterials(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.CollectionMaterial,
  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. func UpdateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  83. var mat model.Material
  84. err := c.ShouldBindJSON(&mat)
  85. if err != nil {
  86. return nil, errors.New("参数错误")
  87. }
  88. if mat.Id == primitive.NilObjectID {
  89. return nil, errors.New("id的为空")
  90. }
  91. mat.UpdateTime = time.Now()
  92. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMaterial, mat.Id.Hex(), &mat)
  93. return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionMaterial, mat.Id.Hex(), &mat, &repo.RecordLogReq{
  94. Path: c.Request.URL.Path,
  95. TargetId: mat.Id.Hex(),
  96. })
  97. }
  98. // 删除材料
  99. func DelMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  100. materialId := c.Param("id")
  101. if materialId == "" {
  102. return nil, errors.New("id为空")
  103. }
  104. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMaterial, materialId)
  105. }