material.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.POST("/material/create", CreateMaterial)
  17. // 获取材料信息
  18. r.GET("/material/detail/:id", GetMaterial)
  19. // 获取材料列表
  20. r.GET("/material/list", GetMaterials)
  21. // 材料列表
  22. r.POST("/material/update", UpdateMaterial)
  23. r.POST("/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. option := &repo.PageSearchOptions{
  66. CollectName: repo.CollectionMaterial,
  67. Query: query,
  68. Page: page,
  69. Size: size,
  70. Sort: bson.M{"createTime": -1},
  71. }
  72. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  73. }
  74. func UpdateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  75. var mat model.Material
  76. err := c.ShouldBindJSON(&mat)
  77. if err != nil {
  78. return nil, errors.New("参数错误")
  79. }
  80. if mat.Id == primitive.NilObjectID {
  81. return nil, errors.New("id的为空")
  82. }
  83. mat.UpdateTime = time.Now()
  84. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMaterial, mat.Id.Hex(), &mat)
  85. }
  86. // 删除材料
  87. func DelMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. materialId := c.Param("id")
  89. if materialId == "" {
  90. return nil, errors.New("id为空")
  91. }
  92. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMaterial, materialId)
  93. }