新增系统商品
This commit is contained in:
@@ -4,6 +4,12 @@ import (
|
||||
"errors"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/dto"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/extend/prescription"
|
||||
"hospital-admin-api/global"
|
||||
"hospital-admin-api/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ProductService struct {
|
||||
@@ -47,3 +53,108 @@ func (r *ProductService) GetProduct(productId int64) (g *dto.ProductDto, err err
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// AddProduct 新增商品
|
||||
func (r *ProductService) AddProduct(userId string, req requests.AddProduct) (bool, error) {
|
||||
productDao := dao.ProductDao{}
|
||||
productPlatformAmountDao := dao.ProductPlatformAmountDao{}
|
||||
|
||||
productPlatformId, err := strconv.ParseInt(req.ProductPlatformId, 10, 64)
|
||||
if err != nil {
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
// 检测平台商品是否存在
|
||||
productPlatformDao := dao.ProductPlatformDao{}
|
||||
productPlatform, err := productPlatformDao.GetProductPlatformById(productPlatformId)
|
||||
if err != nil || productPlatform == nil {
|
||||
return false, errors.New("平台商品不存在")
|
||||
}
|
||||
|
||||
// 检测商品是否重复
|
||||
maps := make(map[string]interface{})
|
||||
maps["product_platform_id"] = req.ProductPlatformId
|
||||
products, err := productDao.GetProductList(maps)
|
||||
if err != nil {
|
||||
return false, errors.New("商品重复添加")
|
||||
}
|
||||
|
||||
if len(products) != 0 {
|
||||
return false, errors.New("商品重复添加")
|
||||
}
|
||||
|
||||
// 处理图片
|
||||
productCoverImg := utils.RemoveOssDomain(req.ProductCoverImg)
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 新增商品表
|
||||
product := &model.Product{
|
||||
ProductPlatformId: productPlatform.ProductPlatformId,
|
||||
ProductName: req.ProductName,
|
||||
CommonName: req.CommonName,
|
||||
ProductPrice: req.ProductPrice,
|
||||
MnemonicCode: req.MnemonicCode,
|
||||
ProductType: *req.ProductType,
|
||||
ProductPlatformCode: req.ProductPlatformCode,
|
||||
ProductPharmacyCode: req.ProductPharmacyCode,
|
||||
ProductCoverImg: productCoverImg,
|
||||
ProductSpec: req.ProductSpec,
|
||||
LicenseNumber: req.LicenseNumber,
|
||||
Manufacturer: req.Manufacturer,
|
||||
SingleUnit: req.SingleUnit,
|
||||
SingleUse: req.SingleUse,
|
||||
PackagingUnit: req.PackagingUnit,
|
||||
FrequencyUse: req.FrequencyUse,
|
||||
AvailableDays: req.AvailableDays,
|
||||
ProductRemarks: req.ProductRemarks,
|
||||
}
|
||||
|
||||
product, err = productDao.AddProduct(tx, product)
|
||||
if err != nil || product == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
// 获取商品库存
|
||||
ReportPreRequest := prescription.GetProdStockRequest{
|
||||
DrugCode: product.ProductPharmacyCode,
|
||||
}
|
||||
|
||||
result, err := ReportPreRequest.GetProdStock()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("获取商品库存失败")
|
||||
}
|
||||
|
||||
var quantity int
|
||||
if result.Quantity != "" {
|
||||
quantity, err = strconv.Atoi(result.Quantity)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("获取商品库存失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 新增商品库存
|
||||
productPlatformAmount := &model.ProductPlatformAmount{
|
||||
ProductPlatformId: product.ProductPlatformId,
|
||||
ProductPlatformCode: product.ProductPlatformCode,
|
||||
Stock: uint(quantity),
|
||||
}
|
||||
|
||||
productPlatformAmount, err = productPlatformAmountDao.AddProductPlatformAmount(tx, productPlatformAmount)
|
||||
if err != nil || productPlatformAmount == nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user