package api import ( "box-cost/db/model" "box-cost/db/repo" "box-cost/log" "errors" "fmt" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) // 生产计划管理 func ProductPlan(r *GinRouter) { // 创建生产计划 r.POST("/plan/create", CreateProductPlan) // 获取生产计划详情 r.GET("/plan/detail/:id", GetProductPlan) // 获取生产计划列表 r.GET("/plan/list", GetProductPlans) // 更新生产计划 r.POST("/plan/update", UpdateProductPlan) // 删除生产计划 r.POST("/plan/delete/:id", DelProductPlan) } // 创建生产计划 func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) { var plan model.ProductPlan err := c.ShouldBindJSON(&plan) if err != nil { fmt.Println(err) return nil, errors.New("参数错误!") } ctx := apictx.CreateRepoCtx() if plan.Name == "" { return nil, errors.New("生产计划名为空") } if plan.Total == 0 { return nil, errors.New("生产计划数应不为0") } plan.Status = "process" // 进行中 plan.CreateTime = time.Now() plan.UpdateTime = time.Now() result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan) return result, err } // 获取生产计划信息 func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) { planId := c.Param("id") id, err := primitive.ObjectIDFromHex(planId) if err != nil { return nil, errors.New("非法id") } var plan model.ProductPlan option := &repo.DocSearchOptions{ CollectName: repo.CollectionProductPlan, Query: repo.Map{"_id": id}, } found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan) if !found || err != nil { log.Info(err) return nil, errors.New("数据未找到") } return plan, nil } // 获取生产计划列表 func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) { page, size, query := UtilQueryPageSize(c) option := &repo.PageSearchOptions{ CollectName: repo.CollectionProductPlan, Query: query, Page: page, Size: size, Sort: bson.M{"createTime": -1}, Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"}, } return repo.RepoPageSearch(apictx.CreateRepoCtx(), option) } // 更新生产计划 func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) { var plan model.ProductPlan err := c.ShouldBindJSON(&plan) if err != nil { return nil, errors.New("参数错误") } if plan.Id.Hex() == "" { return nil, errors.New("id的为空") } plan.UpdateTime = time.Now() return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan) } // 删除生产计划 func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) { planId := c.Param("id") if planId == "" { return nil, errors.New("id为空") } return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId) }