123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 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 Process(r *GinRouter) {
- // 创建工序
- r.POST("/process", CreateProcess)
- // 获取工序详情
- r.GET("/process/:id", GetProcess)
- // 获取工序列表
- r.GET("/process/list", GetProcesss)
- // 更新工序
- r.POST("/process/update", UpdateProcess)
- // 删除工序
- r.POST("/process/delete/:id", DelProcess)
- }
- // 创建工序
- func CreateProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var process model.Process
- err := c.ShouldBindJSON(&process)
- if err != nil {
- fmt.Println(err)
- return nil, errors.New("参数错误!")
- }
- ctx := apictx.CreateRepoCtx()
- if process.Name == "" {
- return nil, errors.New("工序名为空")
- }
- process.CreateTime = time.Now()
- process.UpdateTime = time.Now()
- result, err := repo.RepoAddDoc(ctx, repo.CollectionProcess, &process)
- return result, err
- }
- // 获取工序详情
- func GetProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- processId := c.Param("id")
- id, err := primitive.ObjectIDFromHex(processId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var process model.Process
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionProcess,
- Query: repo.Map{"_id": id},
- }
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &process)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- return process, nil
- }
- // 获取工序列表
- func GetProcesss(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionProcess,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"createTime": -1},
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- // 更新工序
- func UpdateProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var process model.Process
- err := c.ShouldBindJSON(&process)
- if err != nil {
- return nil, errors.New("参数错误")
- }
- if process.Id.Hex() == "" {
- return nil, errors.New("id的为空")
- }
- process.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProcess, process.Id.Hex(), &process)
- }
- // 删除工序
- func DelProcess(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- processId := c.Param("id")
- if processId == "" {
- return nil, errors.New("id为空")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProcess, processId)
- }
|