304 lines
7.7 KiB
Go
304 lines
7.7 KiB
Go
package service
|
|
|
|
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 {
|
|
}
|
|
|
|
// GetProductPlatform 平台商品详情
|
|
func (r *ProductService) GetProductPlatform(productPlatformId int64) (g *dto.ProductPlatformDto, err error) {
|
|
// 获取平台商品数据
|
|
productPlatformDao := dao.ProductPlatformDao{}
|
|
productPlatform, err := productPlatformDao.GetProductPlatformById(productPlatformId)
|
|
if err != nil || productPlatform == nil {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
|
|
// 获取商品库存
|
|
var quantity int
|
|
|
|
ReportPreRequest := prescription.GetProdStockRequest{
|
|
DrugCode: productPlatform.ProductPharmacyCode,
|
|
}
|
|
|
|
result, err := ReportPreRequest.GetProdStock()
|
|
if err != nil {
|
|
quantity = 0
|
|
}
|
|
|
|
if result != nil {
|
|
if result.Quantity != "" {
|
|
quantity, err = strconv.Atoi(result.Quantity)
|
|
if err != nil {
|
|
quantity = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
// 处理返回值
|
|
g = dto.GetProductPlatformDto(productPlatform)
|
|
|
|
// 加载商品库存
|
|
g.LoadProductAmount(quantity)
|
|
return g, nil
|
|
}
|
|
|
|
// GetProduct 商品详情
|
|
func (r *ProductService) GetProduct(productId int64) (g *dto.ProductDto, err error) {
|
|
// 获取商品数据
|
|
productDao := dao.ProductDao{}
|
|
product, err := productDao.GetProductById(productId)
|
|
if err != nil || product == nil {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
|
|
// 获取商品库存数据
|
|
productPlatformAmountDao := dao.ProductPlatformAmountDao{}
|
|
productPlatformAmount, err := productPlatformAmountDao.GetProductPlatformAmountByProductPlatformId(product.ProductPlatformId)
|
|
|
|
// 处理返回值
|
|
g = dto.GetProductDto(product)
|
|
|
|
if productPlatformAmount != nil {
|
|
// 加载库存
|
|
g.LoadProductAmount(productPlatformAmount)
|
|
}
|
|
|
|
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,
|
|
ProductStatus: *req.ProductStatus,
|
|
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,
|
|
PrescriptionNum: req.PrescriptionNum,
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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 product.PrescriptionNum != req.PrescriptionNum {
|
|
productData["prescription_num"] = req.PrescriptionNum
|
|
}
|
|
|
|
// 商品封面
|
|
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
|
|
}
|
|
|
|
// PutProductStatus 修改商品状态(上/下架)
|
|
func (r *ProductService) PutProductStatus(productId int64, req requests.PutProductStatus) (bool, error) {
|
|
// 获取商品数据
|
|
productDao := dao.ProductDao{}
|
|
product, err := productDao.GetProductById(productId)
|
|
if err != nil || product == nil {
|
|
return false, errors.New("商品数据错误")
|
|
}
|
|
|
|
if product.ProductStatus == *req.ProductStatus {
|
|
return true, nil
|
|
}
|
|
|
|
// 商品数据
|
|
productData := make(map[string]interface{}) // 医生数据
|
|
productData["product_status"] = req.ProductStatus
|
|
|
|
// 开始事务
|
|
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
|
|
}
|