| | |
| | | package purchase |
| | | |
| | | import ( |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/mitchellh/mapstructure" |
| | | "go.uber.org/zap" |
| | | "gorm.io/gorm" |
| | | "srm/global" |
| | |
| | | purchaserequest "srm/model/purchase/request" |
| | | "strconv" |
| | | "strings" |
| | | "time" |
| | | |
| | | //"srm/model/purchase" |
| | | |
| | |
| | | // @Security ApiKeyAuth |
| | | // @accept application/json |
| | | // @Produce application/json |
| | | // @Param data body purchaserequest.AddPurchase true "采购单用户名, 采购单手机号码" |
| | | // @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 { |
| | | global.GVA_LOG.Error("Add Purchase failed", zap.Error(err)) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | |
| | | var purchaseRecord purchase.Purchase |
| | | if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil { |
| | | if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil { |
| | | global.GVA_LOG.Error("Add Purchase failed", zap.Error(err)) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | |
| | | purchaseRecord.Status = purchase.OrderStatusConfirmed |
| | | purchaseRecord.HandledBy = "admin" |
| | | purchaseRecord.Creator = "admin" |
| | | purchaseRecord.Number = fmt.Sprintf("CG%v", time.Now().Unix()) |
| | | purchaseRecord.Principal = "admin" |
| | | |
| | | if !purchaseRecord.WholeDiscountType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.WholeDiscount) { |
| | | response.FailWithMessage("整单折扣数值不正确", c) |
| | | return |
| | | } |
| | | |
| | | if !purchaseRecord.PriceAdjustmentType.IsValid(purchaseRecord.TotalPrice, purchaseRecord.PriceAdjustment) { |
| | | response.FailWithMessage("价格调整数值不正确", c) |
| | | return |
| | | } |
| | | |
| | | err = service.NewPurchaseService().CreatePurchase(&purchaseRecord, params.ProductList) |
| | | |
| | | if err != nil { |
| | |
| | | return |
| | | } |
| | | global.GVA_LOG.Error("创建失败!", zap.Error(err)) |
| | | response.FailWithMessage("创建失败", c) |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | response.OkWithMessage("创建成功", c) |
| | |
| | | } |
| | | |
| | | var purchaseRecord purchase.Purchase |
| | | if err := mapstructure.Decode(params.Purchase, &purchaseRecord); err != nil { |
| | | if err := utils.AssignTo(params.Purchase, &purchaseRecord); err != nil { |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } |
| | | response.OkWithDetailed(purchaseRes.PurchaseResponse{Purchase: data, ProductList: productList}, "获取成功", c) |
| | | respProductList := make([]*purchaseRes.PurchaseProducts, len(productList)) |
| | | err = utils.AssignTo(productList, &respProductList) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } |
| | | for k, item := range productList { |
| | | respProductList[k].Amount = item.Amount |
| | | respProductList[k].Price = item.Price |
| | | respProductList[k].Total = item.Total |
| | | err = utils.AssignTo(item.Product, &respProductList[k]) |
| | | if err != nil { |
| | | global.GVA_LOG.Error("获取失败!", zap.Error(err)) |
| | | response.FailWithMessage("获取失败", c) |
| | | return |
| | | } |
| | | } |
| | | |
| | | response.OkWithDetailed(purchaseRes.PurchaseResponse{Purchase: data, ProductList: respProductList}, "获取成功", c) |
| | | } |
| | | |
| | | // GetPurchaseList |
| | |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |
| | | PurchaseList, total, err := service.NewPurchaseService().GetPurchaseList(pageInfo) |
| | | 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, |
| | | List: purchaseList, |
| | | Total: total, |
| | | Page: pageInfo.Page, |
| | | PageSize: pageInfo.PageSize, |
| | |
| | | } |
| | | |
| | | purchaseTypeList := make([]*purchase.PurchaseType, 0, len(params)) |
| | | if err := mapstructure.Decode(params, &purchaseTypeList); err != nil { |
| | | if err := utils.AssignTo(params, &purchaseTypeList); err != nil { |
| | | response.FailWithMessage(err.Error(), c) |
| | | return |
| | | } |