新增修改商品
This commit is contained in:
parent
f7387bfd4d
commit
2eb9a7bbe2
@ -190,3 +190,42 @@ func (r *Product) AddProduct(c *gin.Context) {
|
|||||||
|
|
||||||
responses.Ok(c)
|
responses.Ok(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PutProduct 修改商品
|
||||||
|
func (r *Product) PutProduct(c *gin.Context) {
|
||||||
|
productRequest := requests.ProductRequest{}
|
||||||
|
req := productRequest.PutProduct
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(req); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := c.Param("product_id")
|
||||||
|
if id == "" {
|
||||||
|
responses.FailWithMessage("缺少参数", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 id 转换为 int64 类型
|
||||||
|
productId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
responses.Fail(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
productService := service.ProductService{}
|
||||||
|
_, err = productService.PutProduct(productId, req)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ type ProductRequest struct {
|
|||||||
GetPlatformProductPage // 获取平台商品列表-分页
|
GetPlatformProductPage // 获取平台商品列表-分页
|
||||||
GetProductPage // 获取系统商品列表-分页
|
GetProductPage // 获取系统商品列表-分页
|
||||||
AddProduct // 新增商品
|
AddProduct // 新增商品
|
||||||
|
PutProduct // 修改商品
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPlatformProductPage 获取平台商品列表-分页
|
// GetPlatformProductPage 获取平台商品列表-分页
|
||||||
@ -55,3 +56,15 @@ type AddProduct struct {
|
|||||||
AvailableDays float64 `json:"available_days" form:"available_days" label:"可用天数" validate:"required"` // 可用天数(3)
|
AvailableDays float64 `json:"available_days" form:"available_days" label:"可用天数" validate:"required"` // 可用天数(3)
|
||||||
ProductRemarks string `json:"product_remarks" form:"product_remarks" label:"商品备注" validate:"required"` // 商品备注
|
ProductRemarks string `json:"product_remarks" form:"product_remarks" label:"商品备注" validate:"required"` // 商品备注
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PutProduct 修改商品
|
||||||
|
type PutProduct struct {
|
||||||
|
MnemonicCode string `json:"mnemonic_code" form:"mnemonic_code" label:"商品助记码"` // 商品助记码(首字母简拼)
|
||||||
|
ProductCoverImg string `json:"product_cover_img" form:"product_cover_img" label:"商品封面图"` // 商品封面图
|
||||||
|
SingleUnit string `json:"single_unit" form:"single_unit" label:"单次剂量" validate:"required"` // 单次剂量(例:1次1包)
|
||||||
|
SingleUse string `json:"single_use" form:"single_use" label:"单次用法" validate:"required"` // 单次用法(例:口服)
|
||||||
|
PackagingUnit string `json:"packaging_unit" form:"packaging_unit" label:"基本包装单位" validate:"required"` // 基本包装单位(例:盒/瓶)
|
||||||
|
FrequencyUse string `json:"frequency_use" form:"frequency_use" label:"使用频率" validate:"required"` // 使用频率(例:1天3次)
|
||||||
|
AvailableDays float64 `json:"available_days" form:"available_days" label:"可用天数" validate:"required"` // 可用天数(3)
|
||||||
|
ProductRemarks string `json:"product_remarks" form:"product_remarks" label:"商品备注" validate:"required"` // 商品备注
|
||||||
|
}
|
||||||
|
|||||||
@ -664,11 +664,8 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
|||||||
// 新增商品
|
// 新增商品
|
||||||
productGroup.POST("", api.Product.AddProduct)
|
productGroup.POST("", api.Product.AddProduct)
|
||||||
|
|
||||||
// 删除商品
|
|
||||||
productGroup.DELETE("", api.Dept.DeleteDept)
|
|
||||||
|
|
||||||
// 修改商品
|
// 修改商品
|
||||||
productGroup.PUT("/:product_id", api.Dept.PutDept)
|
productGroup.PUT("/:product_id", api.Product.PutProduct)
|
||||||
|
|
||||||
// 平台商品
|
// 平台商品
|
||||||
platformGroup := productGroup.Group("/platform")
|
platformGroup := productGroup.Group("/platform")
|
||||||
|
|||||||
@ -158,3 +158,78 @@ func (r *ProductService) AddProduct(userId string, req requests.AddProduct) (boo
|
|||||||
tx.Commit()
|
tx.Commit()
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PutProduct 修改医生
|
||||||
|
func (r *ProductService) PutProduct(productId int64, req requests.PutProduct) (bool, error) {
|
||||||
|
// 获取商品数据
|
||||||
|
productDao := dao.ProductDao{}
|
||||||
|
product, err := productDao.GetProductById(productId)
|
||||||
|
if err != nil || product == nil {
|
||||||
|
return false, errors.New("商品数据错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
productData := make(map[string]interface{}) // 医生数据
|
||||||
|
|
||||||
|
// 商品助记码
|
||||||
|
if product.MnemonicCode != req.MnemonicCode {
|
||||||
|
productData["mnemonic_code"] = req.MnemonicCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单次剂量
|
||||||
|
if product.SingleUnit != req.SingleUnit {
|
||||||
|
productData["single_unit"] = req.SingleUnit
|
||||||
|
}
|
||||||
|
|
||||||
|
// 单次用法
|
||||||
|
if product.SingleUse != req.SingleUse {
|
||||||
|
productData["single_use"] = req.SingleUse
|
||||||
|
}
|
||||||
|
|
||||||
|
// 基本包装单位
|
||||||
|
if product.PackagingUnit != req.PackagingUnit {
|
||||||
|
productData["packaging_unit"] = req.PackagingUnit
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用频率
|
||||||
|
if product.FrequencyUse != req.FrequencyUse {
|
||||||
|
productData["frequency_use"] = req.FrequencyUse
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可用天数
|
||||||
|
if product.AvailableDays != req.AvailableDays {
|
||||||
|
productData["available_days"] = req.AvailableDays
|
||||||
|
}
|
||||||
|
|
||||||
|
// 商品备注
|
||||||
|
if product.ProductRemarks != req.ProductRemarks {
|
||||||
|
productData["product_remarks"] = req.ProductRemarks
|
||||||
|
}
|
||||||
|
|
||||||
|
// 商品封面
|
||||||
|
if req.ProductCoverImg != "" {
|
||||||
|
productCoverImg := utils.RemoveOssDomain(req.ProductCoverImg)
|
||||||
|
if product.ProductCoverImg != productCoverImg {
|
||||||
|
productData["product_cover_img"] = productCoverImg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
tx := global.Db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 修改商品数据
|
||||||
|
if len(productData) != 0 {
|
||||||
|
err = productDao.EditProductByUserId(tx, productId, productData)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("修改失败")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user