123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- 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 Bill(r *GinRouter) {
- // 创建单据
- r.POST("/bill", CreateBill)
- // 获取单据详情
- r.GET("/bill/:id", GetBill)
- // 获取单据列表
- r.GET("/bills", GetBills)
- // 更新单据
- r.POST("/bill/update", UpdateBill)
- // 删除单据
- r.POST("/bill/delete/:id", DelBill)
- }
- // 创建单据
- func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var bill model.Bill
- err := c.ShouldBindJSON(&bill)
- if err != nil {
- fmt.Println(err)
- return nil, errors.New("参数错误!")
- }
- ctx := apictx.CreateRepoCtx()
- if bill.PackId.Hex() == "" {
- return nil, errors.New("包装产品id为空")
- }
- if bill.PlanId.Hex() == "" {
- return nil, errors.New("生产计划id为空")
- }
- if bill.Type == "" {
- return nil, errors.New("类型为空")
- }
- bill.Status = "created"
- bill.CreateTime = time.Now()
- bill.UpdateTime = time.Now()
- result, err := repo.RepoAddDoc(ctx, repo.CollectionBill, &bill)
- return result, err
- }
- // 获取单据信息
- func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- billId := c.Param("id")
- id, err := primitive.ObjectIDFromHex(billId)
- if err != nil {
- return nil, errors.New("非法id")
- }
- var bill model.Bill
- option := &repo.DocSearchOptions{
- CollectName: repo.CollectionBill,
- Query: repo.Map{"_id": id},
- }
- found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
- if !found || err != nil {
- log.Info(err)
- return nil, errors.New("数据未找到")
- }
- return bill, nil
- }
- // 获取单据列表
- func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- option := &repo.PageSearchOptions{
- CollectName: repo.CollectionBill,
- Query: query,
- Page: page,
- Size: size,
- Sort: bson.M{"createTime": -1},
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
- }
- // 更新单据
- func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var bill model.Bill
- err := c.ShouldBindJSON(&bill)
- if err != nil {
- return nil, errors.New("参数错误")
- }
- if bill.Id.Hex() == "" {
- return nil, errors.New("id的为空")
- }
- bill.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBill, bill.Id.Hex(), &bill)
- }
- // 删除单据
- func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- billId := c.Param("id")
- if billId == "" {
- return nil, errors.New("id为空")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBill, billId)
- }
|