diff --git a/app/Command/getProductStockCommand.php b/app/Command/getProductStockCommand.php index bf1ec27..235c598 100644 --- a/app/Command/getProductStockCommand.php +++ b/app/Command/getProductStockCommand.php @@ -5,9 +5,11 @@ declare(strict_types=1); namespace App\Command; use App\Model\Product; +use App\Model\ProductPlatformAmount; use Extend\Prescription\Prescription; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; +use Hyperf\DbConnection\Db; use Psr\Container\ContainerInterface; /** @@ -34,7 +36,7 @@ class getProductStockCommand extends HyperfCommand try { // 获取商品 $params = array(); - $product = Product::getPage($params); + $product = Product::getPage($params,['*'],1,10); if (empty($product['data'])){ $this->line("商品库存更新成功,无可更新库存商品"); return; @@ -44,23 +46,85 @@ class getProductStockCommand extends HyperfCommand foreach ($product['data'] as $item){ if (!empty($item['product_pharmacy_code'])){ - + $result = $prescription->getProdStock($item['product_pharmacy_code']); + $this->handleData($item['product_platform_id'],$item['product_platform_code'],$result[0]); } - - $result = $prescription->getProdStock($item['product_pharmacy_code']); - dump($result);die; } + if ($product['last_page'] > 1){ + for ($i = 2; $i <= $product['last_page']; $i++) { + // 获取商品 + $params = array(); + $product = Product::getPage($params,['*'],1,10); + if (empty($product['data'])){ + $this->line("商品库存更新成功,无可更新库存商品"); + return; + } + $prescription = new Prescription(); - - + foreach ($product['data'] as $item){ + if (!empty($item['product_pharmacy_code'])){ + $result = $prescription->getProdStock($item['product_pharmacy_code']); + $this->handleData($item['product_platform_id'],$item['product_platform_code'],$result[0]); + } + } + } + } } catch (\Exception $e) { $this->line("商品库存更新失败:" . $e->getMessage()); } - - $this->line("商品库存更新成功"); } + + /** + * 入库 + * @param string $product_platform_id + * @param string $product_platform_code + * @param array $resultData + * @return bool + */ + public function handleData(string $product_platform_id,string $product_platform_code,array $resultData): bool + { + if (empty($resultData['quantity'])){ + $resultData['quantity'] = 0; + } + try { + Db::beginTransaction(); + + $params = array(); + $params['product_platform_id'] = $product_platform_id; + $params['product_platform_code'] = $product_platform_code; + $product_platform_amount = ProductPlatformAmount::getSharedLockOne($params); + if (empty($product_platform_amount)){ + // 无库存数据,新增 + $data = array(); + $data['product_platform_id'] = $product_platform_id; + $data['product_platform_code'] = $product_platform_code; + $data['stock'] = $resultData['quantity']; + $product_platform_amount = ProductPlatformAmount::addProductPlatformAmount($data); + if (empty($product_platform_amount)){ + Db::rollBack(); + $this->line("商品库存更新失败,无法新增库存数据" . json_encode($data, JSON_UNESCAPED_UNICODE)); + } + }else{ + // 存在库存数据,修改 + $data = array(); + $data['stock'] = $resultData['quantity']; + + $params = array(); + $params['amount_id'] = $product_platform_amount['amount_id']; + ProductPlatformAmount::edit($params,$data); + } + + Db::commit(); + } catch (\Exception $e) { + Db::rollBack(); + $this->line("商品库存更新失败:" . $e->getMessage()); + return false; + } + + return true; + } } diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index f2aaf4f..d5b6c24 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -305,7 +305,7 @@ class CallBackController extends AbstractController $server = $app->getServer(); $message = $server->getRequestMessage(); - dump($message); + if (empty($message)){ return $this->response->withStatus(500)->withBody(new SwooleStream(strval(json_encode(['code' => 'ERROR', 'message' => "药品微信支付回调数据为空"], JSON_UNESCAPED_UNICODE)))); } @@ -391,24 +391,6 @@ class CallBackController extends AbstractController return $server->serve(); } - // 修改库存,去除锁定 - foreach ($order_product_item as $item){ - // 释放锁定库存 - $params = array(); - $params['product_id'] = $item['product_id']; - $product = Product::getWithAmountOne($params); - if (empty($product)){ - Db::rollBack(); - Log::getInstance()->info("药品微信支付回调数据处理失败,未查询到对应订单商品订单列表"); - return $server->serve(); - } - - // 锁定库存-订单商品数量 = 实际锁定库存数量 - $params = array(); - $params['amount_id'] = $product['ProductPlatformAmount']['amount_id']; - ProductPlatformAmount::dec($params, 'lock_stock', (float)$item['amount']); - } - Db::commit(); } catch (\Exception $e) { Db::rollBack(); diff --git a/app/Model/ProductPlatformAmount.php b/app/Model/ProductPlatformAmount.php index b9ea19d..39059b8 100644 --- a/app/Model/ProductPlatformAmount.php +++ b/app/Model/ProductPlatformAmount.php @@ -6,15 +6,14 @@ namespace App\Model; +use Carbon\Carbon; use Hyperf\Snowflake\Concern\Snowflake; /** * @property int $amount_id 主键id - * @property int $product_platform_id 商品id + * @property int $product_platform_id 处方平台商品id * @property string $product_platform_code 处方平台商品编码 - * @property int $real_stock 实际库存 * @property int $stock 现有库存 - * @property int $lock_stock 锁定库存 * @property \Carbon\Carbon $created_at 创建时间 * @property \Carbon\Carbon $updated_at 修改时间 */ @@ -30,12 +29,7 @@ class ProductPlatformAmount extends Model /** * The attributes that are mass assignable. */ - protected array $fillable = ['amount_id', 'product_platform_id', 'product_platform_code', 'real_stock', 'stock', 'lock_stock', 'created_at', 'updated_at']; - - /** - * The attributes that should be cast to native types. - */ - protected array $casts = ['amount_id' => 'integer', 'product_platform_id' => 'integer', 'real_stock' => 'integer', 'stock' => 'integer', 'lock_stock' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; + protected array $fillable = ['amount_id', 'product_platform_id', 'product_platform_code', 'stock', 'created_at', 'updated_at']; protected string $primaryKey = "amount_id"; @@ -62,4 +56,58 @@ class ProductPlatformAmount extends Model { return self::where($params)->decrement($field,$numeral); } + + /** + * 获取信息-单条 + * @param array $params + * @param array $fields + * @return object|null + */ + public static function getOne(array $params, array $fields = ['*']): object|null + { + return self::where($params)->first($fields); + } + + /** + * 获取信息-单条 + * @param array $params + * @param array $fields + * @return object|null + */ + public static function getSharedLockOne(array $params, array $fields = ['*']): object|null + { + return self::where($params)->sharedLock()->first($fields); + } + + /** + * 获取信息-多条 + * @param array $params + * @param array $fields + * @return object|null + */ + public static function getList(array $params, array $fields = ['*']): object|null + { + return self::where($params)->get($fields); + } + + /** + * 新增 + * @param array $data + * @return ProductPlatformAmount|\Hyperf\Database\Model\Model + */ + public static function addProductPlatformAmount(array $data = []): ProductPlatformAmount|\Hyperf\Database\Model\Model + { + return self::create($data); + } + + /** + * 修改 + * @param array $params + * @param array $data + * @return int + */ + public static function edit(array $params = [], array $data = []): int + { + return self::where($params)->update($data); + } } diff --git a/app/Services/OrderProductService.php b/app/Services/OrderProductService.php index f999188..5425447 100644 --- a/app/Services/OrderProductService.php +++ b/app/Services/OrderProductService.php @@ -2,12 +2,14 @@ namespace App\Services; +use App\Constants\HttpEnumCode; use App\Model\OrderPrescription; use App\Model\OrderPrescriptionProduct; use App\Model\OrderProduct; use App\Model\OrderProductItem; use App\Model\Product; use App\Model\ProductPlatformAmount; +use Hyperf\DbConnection\Db; class OrderProductService extends BaseService { @@ -104,20 +106,27 @@ class OrderProductService extends BaseService // 释放锁定库存 $params = array(); $params['product_id'] = $item['product_id']; - $product = Product::getWithAmountOne($params); + $product = Product::getOne($params); if (empty($product)){ $result['status'] = 0; $result['message'] = "取消未支付的问诊订单失败:未查询到对应订单商品订单列表"; return $result; } + $params = array(); + $params['product_platform_code'] = $product['product_platform_code']; + $product_platform_amount = ProductPlatformAmount::getSharedLockOne($params); + if (empty($product_platform_amount)){ + $result['status'] = 0; + $result['message'] = "取消未支付的问诊订单失败:无商品库存数据"; + return $result; + } + + // 库存+订单商品数量 $params = array(); - $params['amount_id'] = $product['ProductPlatformAmount']['amount_id']; + $params['amount_id'] = $product_platform_amount['amount_id']; ProductPlatformAmount::inc($params, 'stock', (float)$item['amount']); - - // 锁定库存-订单商品数量 - ProductPlatformAmount::dec($params, 'lock_stock', (float)$item['amount']); } // 获取处方数据 diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index c762cd5..8cd06e4 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -946,12 +946,20 @@ class PatientOrderService extends BaseService $params = array(); $params['product_id'] = $product_id; - $product = Product::getWithAmountOne($params); + $product = Product::getOne($params); if (empty($product)) { Db::rollBack(); return fail(HttpEnumCode::HTTP_ERROR, "处方存在未知药品"); } + $params = array(); + $params['product_platform_code'] = $product['product_platform_code']; + $product_platform_amount = ProductPlatformAmount::getSharedLockOne($params); + if (empty($product_platform_amount)){ + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR, "无商品库存数据"); + } + $data = array(); $data['order_product_id'] = $order_product['order_product_id']; $data['order_inquiry_id'] = $order_prescription['order_inquiry_id']; @@ -981,11 +989,8 @@ class PatientOrderService extends BaseService // 锁定库存 // 库存-1 $params = array(); - $params['amount_id'] = $product['ProductPlatformAmount']['amount_id']; + $params['amount_id'] = $product_platform_amount['amount_id']; ProductPlatformAmount::dec($params, 'stock', $order_prescription_product['prescription_product_num']); - - // 锁定库存+1 - ProductPlatformAmount::inc($params, 'lock_stock', $order_prescription_product['prescription_product_num']); } // 修改处方为已使用 diff --git a/app/Services/UserDoctorService.php b/app/Services/UserDoctorService.php index d5e7676..c872f1f 100644 --- a/app/Services/UserDoctorService.php +++ b/app/Services/UserDoctorService.php @@ -1188,7 +1188,7 @@ class UserDoctorService extends BaseService } // 检测药品库存数据 - if ($item['prescription_product_num'] > $product['ProductPlatformAmount']['real_stock']) { + if ($item['prescription_product_num'] > $product['ProductPlatformAmount']['stock']) { // 库存不足 Db::rollBack(); return fail(HttpEnumCode::HTTP_ERROR, "药品" . $product['product_name'] . "库存不足"); diff --git a/config/routes.php b/config/routes.php index 05a0bf4..c33e0a3 100644 --- a/config/routes.php +++ b/config/routes.php @@ -378,10 +378,9 @@ Router::addGroup('/area', function () { Router::get('/county', [AreaController::class, 'getCounty']); }); -// oss -Router::addGroup('/oss', function () { +Router::addGroup('/sign', function () { // 获取oss签名数据 - Router::get('/sign', [SafeController::class, 'getOssSign']); + Router::get('/oss', [SafeController::class, 'getOssSign']); }); // 基础数据 diff --git a/extend/Prescription/Prescription.php b/extend/Prescription/Prescription.php index 1b0b3ac..16247b6 100644 --- a/extend/Prescription/Prescription.php +++ b/extend/Prescription/Prescription.php @@ -114,7 +114,12 @@ class Prescription ]; try { - return $this->httpRequest($this->api_url . $this->version . '/pharmacy/pharmacyInventory', $option); + $response = $this->httpRequest($this->api_url . $this->version . '/pharmacy/pharmacyInventory', $option); + if (empty($response)){ + // 返回值为空 + throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); + } + return $response; } catch (GuzzleException $e) { throw new BusinessException($e->getMessage()); }