package purchase import ( "github.com/gin-gonic/gin" "github.com/mitchellh/mapstructure" "go.uber.org/zap" "gorm.io/gorm" "srm/global" "srm/model/common/request" "srm/model/common/response" "srm/model/purchase" purchaserequest "srm/model/purchase/request" "strconv" "strings" //"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(¶ms) if err != nil { response.FailWithMessage(err.Error(), c) return } var purchaseRecord purchase.Purchase if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil { response.FailWithMessage(err.Error(), c) return } purchaseRecord.ID = 0 purchaseRecord.Status = purchase.OrderStatusConfirmed purchaseRecord.HandledBy = "admin" purchaseRecord.Creator = "admin" err = service.NewPurchaseService().CreatePurchase(&purchaseRecord, params.ProductList) if err != nil { if err == gorm.ErrDuplicatedKey || strings.Contains(err.Error(), "Duplicate entry") { response.FailWithMessage("编号重复", c) return } 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.UpdatePurchase true "采购单ID, 采购单信息" // @Success 200 {object} response.Response{msg=string} "更新采购单信息" // @Router /purchase/purchase [put] func (e *PurchaseApi) UpdatePurchase(c *gin.Context) { var params purchaserequest.UpdatePurchase err := c.ShouldBindJSON(¶ms) if err != nil { response.FailWithMessage(err.Error(), c) return } var purchaseRecord purchase.Purchase if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil { response.FailWithMessage(err.Error(), c) return } purchaseRecord.HandledBy = "admin" purchaseRecord.Creator = "admin" err = service.NewPurchaseService().UpdatePurchase(&purchaseRecord, params.ProductList) 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) { id, _ := strconv.Atoi(c.Param("id")) if id == 0 { response.FailWithMessage("参数缺失", c) return } err := service.NewPurchaseService().Submit(uint(id)) if err != nil { global.GVA_LOG.Error("更新失败!", zap.Error(err)) response.FailWithMessage("更新失败", c) return } response.OkWithMessage("更新成功", c) } // SavePurchaseType // @Tags Purchase // @Summary 创建采购类型 // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Param data body []purchaserequest.PurchaseType true "采购类型list" // @Success 200 {object} response.Response{msg=string} "创建采购类型" // @Router /purchase/purchaseType [post] func (e *PurchaseApi) SavePurchaseType(c *gin.Context) { var params []*purchaserequest.PurchaseType err := c.ShouldBindJSON(¶ms) if err != nil { response.FailWithMessage(err.Error(), c) return } purchaseTypeList := make([]*purchase.PurchaseType, 0, len(params)) if err := mapstructure.Decode(params, &purchaseTypeList); err != nil { response.FailWithMessage(err.Error(), c) return } err = service.NewPurchaseService().SavePurchaseType(purchaseTypeList) if err != nil { global.GVA_LOG.Error("创建失败!", zap.Error(err)) response.FailWithMessage("创建失败", c) return } response.OkWithMessage("创建成功", c) } // GetPurchaseTypeList // @Tags Purchase // @Summary 获取采购类型列表 // @Security ApiKeyAuth // @accept application/json // @Produce application/json // @Success 200 {object} response.Response{data=[]purchase.PurchaseType,msg=string} "获取采购类型列表" // @Router /purchase/purchaseTypeList [get] func (e *PurchaseApi) GetPurchaseTypeList(c *gin.Context) { list, err := service.NewPurchaseService().GetPurchaseTypeList() if err != nil { global.GVA_LOG.Error("获取失败!", zap.Error(err)) response.FailWithMessage("获取失败"+err.Error(), c) return } response.OkWithDetailed(list, "获取成功", c) }