zhangqian
2023-08-28 e517b1a99b6edfa24c8cc4e109a6a10488f23b6a
api/v1/purchase/purchase.go
New file
@@ -0,0 +1,194 @@
package purchase
import (
   "github.com/gin-gonic/gin"
   "go.uber.org/zap"
   "srm/global"
   "srm/model/common/request"
   "srm/model/common/response"
   purchaserequest "srm/model/purchase/request"
   "strconv"
   //"srm/model/purchase"
   //"srm/model/purchase"
   purchaseRes "srm/model/purchase/response"
   service "srm/service/purchase"
   "srm/utils"
)
type PurchaseApi struct{}
// CreatePurchase
// @Tags      Purchase
// @Summary   创建采购单
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body     purchaserequest.AddPurchase   true  "采购单用户名, 采购单手机号码"
// @Success   200   {object}  response.Response{msg=string}  "创建采购单"
// @Router    /purchase/purchase [post]
func (e *PurchaseApi) CreatePurchase(c *gin.Context) {
   var params purchaserequest.AddPurchase
   err := c.ShouldBindJSON(&params)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   err = service.NewPurchaseService().CreatePurchase(params)
   if err != nil {
      global.GVA_LOG.Error("创建失败!", zap.Error(err))
      response.FailWithMessage("创建失败", c)
      return
   }
   response.OkWithMessage("创建成功", c)
}
// DeletePurchase
// @Tags      Purchase
// @Summary   删除采购单
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param      id   path      int   true   "采购单ID"
// @Success   200   {object}  response.Response{msg=string}  "删除采购单"
// @Router    /purchase/purchase/{id} [delete]
func (e *PurchaseApi) DeletePurchase(c *gin.Context) {
   id, _ := strconv.Atoi(c.Param("id"))
   if id == 0 {
      response.FailWithMessage("参数缺失", c)
      return
   }
   err := service.NewPurchaseService().DeletePurchase(uint(id))
   if err != nil {
      global.GVA_LOG.Error("删除失败!", zap.Error(err))
      response.FailWithMessage("删除失败", c)
      return
   }
   response.OkWithMessage("删除成功", c)
}
// UpdatePurchase
// @Tags      Purchase
// @Summary   更新采购单信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      purchaserequest.AddPurchase            true  "采购单ID, 采购单信息"
// @Success   200   {object}  response.Response{msg=string}  "更新采购单信息"
// @Router    /purchase/purchase [put]
func (e *PurchaseApi) UpdatePurchase(c *gin.Context) {
   var params purchaserequest.AddPurchase
   err := c.ShouldBindJSON(&params)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   err = utils.Verify(params.Purchase.GVA_MODEL, utils.IdVerify)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   err = service.NewPurchaseService().UpdatePurchase(&params)
   if err != nil {
      global.GVA_LOG.Error("更新失败!", zap.Error(err))
      response.FailWithMessage("更新失败", c)
      return
   }
   response.OkWithMessage("更新成功", c)
}
// GetPurchase
// @Tags      Purchase
// @Summary   获取单一采购单信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param      id   path      int   true   "采购单ID"                                           true  "采购单ID"
// @Success   200   {object}  response.Response{data=purchaseRes.PurchaseResponse,msg=string}  "获取单一采购单信息,返回包括采购单详情"
// @Router    /purchase/purchase/{id} [get]
func (e *PurchaseApi) GetPurchase(c *gin.Context) {
   id, _ := strconv.Atoi(c.Param("id"))
   if id == 0 {
      response.FailWithMessage("参数缺失", c)
      return
   }
   data, err := service.NewPurchaseService().GetPurchase(uint(id))
   if err != nil {
      global.GVA_LOG.Error("获取失败!", zap.Error(err))
      response.FailWithMessage("获取失败", c)
      return
   }
   productList, err := service.NewPurchaseService().GetPurchaseProductList(uint(id))
   if err != nil {
      global.GVA_LOG.Error("获取失败!", zap.Error(err))
      response.FailWithMessage("获取失败", c)
      return
   }
   response.OkWithDetailed(purchaseRes.PurchaseResponse{Purchase: data, ProductList: productList}, "获取成功", c)
}
// GetPurchaseList
// @Tags      Purchase
// @Summary   分页获取采购单列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  query     request.PageInfo                                        true  "页码, 每页大小"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取采购单列表,返回包括列表,总数,页码,每页数量"
// @Router    /purchase/purchaseList [get]
func (e *PurchaseApi) GetPurchaseList(c *gin.Context) {
   var pageInfo request.PageInfo
   err := c.ShouldBindQuery(&pageInfo)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   err = utils.Verify(pageInfo, utils.PageInfoVerify)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   PurchaseList, total, err := service.NewPurchaseService().GetPurchaseList(pageInfo)
   if err != nil {
      global.GVA_LOG.Error("获取失败!", zap.Error(err))
      response.FailWithMessage("获取失败"+err.Error(), c)
      return
   }
   response.OkWithDetailed(response.PageResult{
      List:     PurchaseList,
      Total:    total,
      Page:     pageInfo.Page,
      PageSize: pageInfo.PageSize,
   }, "获取成功", c)
}
// Submit
// @Tags      Purchase
// @Summary   提交采购单
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param      id   path      int   true   "采购单ID"
// @Success   200   {object}  response.Response{msg=string}  "提交采购单"
// @Router    /purchase/submit/{id} [post]
func (e *PurchaseApi) Submit(c *gin.Context) {
   var params purchaserequest.AddPurchase
   err := c.ShouldBindJSON(&params)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   err = utils.Verify(params.Purchase.GVA_MODEL, utils.IdVerify)
   if err != nil {
      response.FailWithMessage(err.Error(), c)
      return
   }
   err = service.NewPurchaseService().UpdatePurchase(&params)
   if err != nil {
      global.GVA_LOG.Error("更新失败!", zap.Error(err))
      response.FailWithMessage("更新失败", c)
      return
   }
   response.OkWithMessage("更新成功", c)
}