prcess.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Process(r *GinRouter) {
  15. // 创建工序
  16. r.POST("/process", CreateProcess)
  17. // 获取工序详情
  18. r.GET("/process/:id", GetProcess)
  19. // 获取工序列表
  20. r.GET("/process/list", GetProcesss)
  21. // 更新工序
  22. r.POST("/process/update", UpdateProcess)
  23. // 删除工序
  24. r.POST("/process/delete/:id", DelProcess)
  25. }
  26. // 创建工序
  27. func CreateProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var process model.Process
  29. err := c.ShouldBindJSON(&process)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. if process.Name == "" {
  36. return nil, errors.New("工序名为空")
  37. }
  38. process.CreateTime = time.Now()
  39. process.UpdateTime = time.Now()
  40. result, err := repo.RepoAddDoc(ctx, repo.CollectionProcess, &process)
  41. return result, err
  42. }
  43. // 获取工序详情
  44. func GetProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  45. processId := c.Param("id")
  46. id, err := primitive.ObjectIDFromHex(processId)
  47. if err != nil {
  48. return nil, errors.New("非法id")
  49. }
  50. var process model.Process
  51. option := &repo.DocSearchOptions{
  52. CollectName: repo.CollectionProcess,
  53. Query: repo.Map{"_id": id},
  54. }
  55. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &process)
  56. if !found || err != nil {
  57. log.Info(err)
  58. return nil, errors.New("数据未找到")
  59. }
  60. return process, nil
  61. }
  62. // 获取工序列表
  63. func GetProcesss(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  64. page, size, query := UtilQueryPageSize(c)
  65. option := &repo.PageSearchOptions{
  66. CollectName: repo.CollectionProcess,
  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. // 更新工序
  75. func UpdateProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  76. var process model.Process
  77. err := c.ShouldBindJSON(&process)
  78. if err != nil {
  79. return nil, errors.New("参数错误")
  80. }
  81. if process.Id.Hex() == "" {
  82. return nil, errors.New("id的为空")
  83. }
  84. process.UpdateTime = time.Now()
  85. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProcess, process.Id.Hex(), &process)
  86. }
  87. // 删除工序
  88. func DelProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  89. processId := c.Param("id")
  90. if processId == "" {
  91. return nil, errors.New("id为空")
  92. }
  93. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProcess, processId)
  94. }