默认剂数

This commit is contained in:
李琦
2026-04-29 16:52:58 +08:00
parent 1d81f31135
commit 9852b8534b
2 changed files with 387 additions and 35 deletions

View File

@@ -4,15 +4,21 @@ namespace service\modules\v1\controllers;
use common\core\BaseAppController;
use common\helpers\FuncHelper;
use common\models\oldDb\PackageMethod;
use common\models\oldDb\ChineseRepice;
use common\models\oldDb\Drug;
use common\models\oldDb\GranularRepice;
use common\models\oldDb\ProcessRule;
use common\models\oldDb\ProcessRuleNote;
use common\models\oldDb\Prescription;
use common\models\oldDb\PrescriptionChinese;
use common\models\oldDb\PrescriptionGranular;
use common\models\oldDb\PrescriptionWest;
use common\models\oldDb\WestRepice;
use common\modelsgii\oldDb\ChineseMedicine;
use common\services\PrescriptionService;
use yii\base\Exception;
use yii\helpers\Json;
/**
* @doc-module 处方详情
@@ -103,6 +109,354 @@ class PrescripDetailController extends BaseAppController
* 开处方-常用方
*/
public function actionCommonUse()
{
return $this->commonUseResponseLikeXkApi(\Yii::$app->request->post());
}
/**
* @doc-name 开处方-常用方详情(中药)
* @doc-param int id 处方id
*/
public function actionCommonChinese()
{
return $this->commonChineseDetailLikeXkApi(\Yii::$app->request->post());
}
/**
* @doc-name 开处方-常用方详情(颗粒药)
* @doc-param int id 处方id
*/
public function actionCommonGranular()
{
return $this->commonGranularDetailLikeXkApi(\Yii::$app->request->post());
}
/**
* @doc-name 开处方-常用方详情(西药)
* @doc-param int id 处方id
*/
public function actionCommonWest()
{
return $this->commonWestDetailLikeXkApi(\Yii::$app->request->post());
}
protected function commonUseResponseLikeXkApi(array $post): array
{
$this->requestValidate($post, [
['store_id', 'required'],
]);
$storeId = (int) $post['store_id'];
// $checkPrice = !isset($post['check_price']) || (int) $post['check_price'] === 1;
$checkPrice = 1;
$chinese = PrescriptionChinese::find()->where([
'type' => 2,
'store_id' => $storeId,
])->andWhere(['su_id' => \Yii::$app->user->identity->id])->asArray()->all();
$west = PrescriptionWest::find()->where([
'type' => 2,
'store_id' => $storeId,
])->andWhere(['su_id' => \Yii::$app->user->identity->id])->asArray()->all();
$granular = PrescriptionGranular::find()->where([
'type' => 2,
'store_id' => $storeId,
])->andWhere(['su_id' => \Yii::$app->user->identity->id])->asArray()->all();
if (!$chinese && !$west && !$granular) {
throw new Exception('没有常用方');
}
$chineseRepice = [];
foreach ($chinese as &$chineseRow) {
$ids = array_filter(explode(',', (string) $chineseRow['cr_ids']));
$recipes = ChineseRepice::find()->where(['in', 'id', $ids])->asArray()->all();
$syncResult = $this->syncChineseCommonRecipesWithStorePrices($storeId, $chineseRow, $recipes, $checkPrice);
$recipes = $syncResult['recipes'];
$chineseRow['price_adjusted'] = $syncResult['price_adjusted'] ? 1 : 0;
if (!empty($recipes[0])) {
$this->attachChineseMetaForList($chineseRow, $recipes[0]);
}
foreach ($recipes as &$recipeRow) {
$recipeRow = $this->normalizeChineseRecipeRow($recipeRow);
}
unset($recipeRow);
$chineseRepice[] = $recipes;
}
unset($chineseRow);
$westRepice = [];
foreach ($west as $westRow) {
$ids = array_filter(explode(',', (string) $westRow['wr_ids']));
$recipes = WestRepice::find()->where(['in', 'id', $ids])
->with(['usetime', 'usetype', 'frequency', 'westUnit'])
->asArray()->all();
$westRepice[] = $this->buildCommonWestRecipeRowsLikeXkApi($recipes);
}
$granularRepice = [];
foreach ($granular as $granularRow) {
$ids = array_filter(explode(',', (string) $granularRow['gr_ids']));
$recipes = GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all();
$granularRepice[] = $this->buildCommonGranularRecipeRowsLikeXkApi($recipes);
}
return [
'chin_prescription' => $chinese,
'chinese' => $chineseRepice,
'west_prescription' => $west,
'west' => $westRepice,
'granular_prescription' => $granular,
'granular' => $granularRepice,
];
}
protected function commonChineseDetailLikeXkApi(array $post): array
{
$this->requestValidate($post, [
['id', 'required'],
['store_id', 'required'],
]);
$chinese = PrescriptionChinese::find()->where([
'id' => $post['id'],
'store_id' => $post['store_id'],
])->andWhere(['type' => 2])->asArray()->one();
if (!$chinese) {
throw new Exception('没有该处方');
}
$ids = array_filter(explode(',', (string) $chinese['cr_ids']));
$recipes = ChineseRepice::find()->where(['in', 'id', $ids])->asArray()->all();
$syncResult = $this->syncChineseCommonRecipesWithStorePrices((int) $chinese['store_id'], $chinese, $recipes, true);
$recipes = $syncResult['recipes'];
foreach ($recipes as &$recipeRow) {
$recipeRow = $this->normalizeChineseRecipeRow($recipeRow);
}
unset($recipeRow);
return $recipes;
}
protected function commonGranularDetailLikeXkApi(array $post): array
{
$this->requestValidate($post, [
['id', 'required'],
['store_id', 'required'],
]);
$granular = PrescriptionGranular::find()->where([
'id' => $post['id'],
'store_id' => $post['store_id'],
])->andWhere(['type' => 2])->asArray()->one();
if (!$granular) {
throw new Exception('没有该处方');
}
$ids = array_filter(explode(',', (string) $granular['gr_ids']));
$recipes = GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all();
return $this->buildCommonGranularRecipeRowsLikeXkApi($recipes);
}
protected function commonWestDetailLikeXkApi(array $post): array
{
$this->requestValidate($post, [
['id', 'required'],
['store_id', 'required'],
]);
$west = PrescriptionWest::find()->where([
'id' => $post['id'],
'store_id' => $post['store_id'],
])->andWhere(['type' => 2])->asArray()->one();
if (!$west) {
throw new Exception('没有该处方');
}
$ids = array_filter(explode(',', (string) $west['wr_ids']));
$recipes = WestRepice::find()->where(['in', 'id', $ids])
->with(['usetime', 'usetype', 'frequency', 'westUnit'])
->asArray()->all();
return $this->buildCommonWestRecipeRowsLikeXkApi($recipes);
}
protected function syncChineseCommonRecipesWithStorePrices(int $storeId, array $prescriptionRow, array $recipeRows, bool $persist): array
{
$priceAdjusted = false;
if (empty($recipeRows)) {
return ['recipes' => $recipeRows, 'price_adjusted' => false];
}
$time = time();
$transaction = \Yii::$app->db->beginTransaction();
try {
foreach ($recipeRows as &$recipeRow) {
$contentList = Json::decode($recipeRow['content'] ?? '', true);
if (!is_array($contentList)) {
continue;
}
$cmIds = array_values(array_filter(explode(',', (string) ($recipeRow['cm_id'] ?? ''))));
$dosage = max(1, (int) ($recipeRow['dosage'] ?? 1));
$drugTotal = 0.0;
$recipeChanged = false;
foreach ($contentList as $idx => &$drugRow) {
$drugRow = $this->normalizeChineseDrugRow($drugRow);
$drugId = (int) ($drugRow['drug_id'] ?? $drugRow['id'] ?? 0);
$price = (float) ($drugRow['price'] ?? 0);
$buyPrice = (float) ($drugRow['buy_price'] ?? 0);
if ($drugId > 0) {
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.id' => $drugId,
])->joinWith(['drugStoreDrug dsd' => function ($q) {
$q->andWhere(['dsd.status' => 2]);
}])->joinWith(['drugStoreRelation dsr' => function ($q) use ($storeId) {
$q->andWhere(['dsr.store_id' => $storeId]);
$q->andWhere(['dsr.status' => 2]);
}])->asArray()->one();
if ($drug && !empty($drug['drugStoreRelation'])) {
$latestPrice = (float) $drug['drugStoreRelation']['price'];
$latestBuyPrice = (float) $drug['drugStoreRelation']['buy_price'];
if ($latestPrice !== $price || $latestBuyPrice !== $buyPrice) {
$priceAdjusted = true;
$recipeChanged = true;
}
$price = $latestPrice;
$buyPrice = $latestBuyPrice;
$drugRow['price'] = $latestPrice;
$drugRow['buy_price'] = $latestBuyPrice;
if ($persist && isset($cmIds[$idx]) && is_numeric($cmIds[$idx])) {
ChineseMedicine::updateAll([
'price' => $latestPrice,
'buy_price' => $latestBuyPrice,
'updated_at' => $time,
], ['id' => (int) $cmIds[$idx]]);
}
}
}
$drugTotal += (float) ($drugRow['number'] ?? 0) * $price * $dosage;
}
unset($drugRow);
if ($persist && $recipeChanged) {
$recipeRow['content'] = Json::encode($contentList);
$recipeRow['total_price'] = round($drugTotal, 4);
ChineseRepice::updateAll([
'content' => $recipeRow['content'],
'total_price' => $recipeRow['total_price'],
'updated_at' => $time,
], ['id' => $recipeRow['id']]);
} else {
$recipeRow['content'] = Json::encode($contentList);
$recipeRow['total_price'] = round($drugTotal, 4);
}
}
unset($recipeRow);
if ($persist && $priceAdjusted && !empty($prescriptionRow['id'])) {
PrescriptionChinese::updateAll(['updated_at' => $time], ['id' => $prescriptionRow['id']]);
}
$transaction->commit();
return ['recipes' => $recipeRows, 'price_adjusted' => $priceAdjusted];
} catch (\Throwable $e) {
$transaction->rollBack();
throw $e;
}
}
protected function normalizeChineseRecipeRow(array $recipeRow): array
{
$contentList = Json::decode($recipeRow['content'] ?? '', true);
if (is_array($contentList)) {
foreach ($contentList as &$drugRow) {
$drugRow = $this->normalizeChineseDrugRow($drugRow);
}
unset($drugRow);
$recipeRow['content'] = Json::encode($contentList);
}
return $recipeRow;
}
protected function normalizeChineseDrugRow(array $drugRow): array
{
$name = $drugRow['drug_name'] ?? ($drugRow['name'] ?? '');
$drugRow['name'] = $name;
$drugRow['drug_name'] = $name;
if (isset($drugRow['way_id']) && !isset($drugRow['order'])) {
$drugRow['order'] = $drugRow['way_id'];
}
if (isset($drugRow['order']) && !isset($drugRow['way_id'])) {
$drugRow['way_id'] = $drugRow['order'];
}
if (!isset($drugRow['unit']) || empty($drugRow['unit'])) {
$drugRow['unit'] = ['name' => 'g'];
}
return $drugRow;
}
protected function attachChineseMetaForList(array &$prescriptionRow, array $firstRecipeRow): void
{
$prescriptionRow['rule_type'] = (int) ($firstRecipeRow['deployment'] ?? 1);
$prescriptionRow['dosage'] = (int) ($firstRecipeRow['dosage'] ?? 7);
$prescriptionRow['day_dosage'] = (int) ($firstRecipeRow['consumption'] ?? 2);
$prescriptionRow['process_rule_id'] = (int) ($firstRecipeRow['process_rule_id'] ?? 0);
$prescriptionRow['process_rule_note'] = $firstRecipeRow['process_rule_note'] ?? '';
$prescriptionRow['package_method_id'] = (int) ($firstRecipeRow['package_method_id'] ?? 0);
if ($prescriptionRow['process_rule_id']) {
$processRule = ProcessRule::findOne($prescriptionRow['process_rule_id']);
$prescriptionRow['process_rule_name'] = $processRule ? $processRule->name : '';
}
if ($prescriptionRow['process_rule_note']) {
$processRuleNote = ProcessRuleNote::find()->where(['note' => $prescriptionRow['process_rule_note']])->one();
$prescriptionRow['process_rule_note_name'] = $processRuleNote ? $processRuleNote->note : $prescriptionRow['process_rule_note'];
}
if ($prescriptionRow['package_method_id']) {
$packageMethod = PackageMethod::findOne($prescriptionRow['package_method_id']);
$prescriptionRow['package_method_name'] = $packageMethod ? $packageMethod->name : '';
}
}
protected function buildCommonWestRecipeRowsLikeXkApi(array $recipeRows): array
{
foreach ($recipeRows as &$recipeRow) {
$content = Json::decode($recipeRow['content'] ?? '', true);
if (is_array($content)) {
$recipeRow['drug_name'] = $content['drug_name'] ?? '';
$recipeRow['drug_id'] = $content['id'] ?? 0;
$recipeRow['image'] = $content['image'] ?? '';
$recipeRow['price'] = $content['price'] ?? ($recipeRow['total_price'] ?? 0);
}
$recipeRow['frequency_id'] = $recipeRow['f_id'] ?? 0;
$recipeRow['unit_id'] = $recipeRow['wu_id'] ?? 0;
$recipeRow['number'] = $recipeRow['grain_number'] ?? ($recipeRow['number'] ?? 1);
}
unset($recipeRow);
return $recipeRows;
}
protected function buildCommonGranularRecipeRowsLikeXkApi(array $recipeRows): array
{
foreach ($recipeRows as &$recipeRow) {
$contentList = Json::decode($recipeRow['content'] ?? '', true);
if (!is_array($contentList)) {
continue;
}
foreach ($contentList as &$drugRow) {
$name = $drugRow['drug_name'] ?? ($drugRow['name'] ?? '');
$drugRow['name'] = $name;
$drugRow['drug_name'] = $name;
if (isset($drugRow['way_id']) && !isset($drugRow['order'])) {
$drugRow['order'] = $drugRow['way_id'];
}
if (isset($drugRow['order']) && !isset($drugRow['way_id'])) {
$drugRow['way_id'] = $drugRow['order'];
}
if (!isset($drugRow['unit']) || empty($drugRow['unit'])) {
$drugRow['unit'] = ['name' => 'g'];
}
}
unset($drugRow);
$recipeRow['content'] = Json::encode($contentList);
}
unset($recipeRow);
return $recipeRows;
}
protected function actionCommonUseLegacy()
{
$post = \YII::$app->request->post();
@@ -134,14 +488,14 @@ class PrescripDetailController extends BaseAppController
$westRepice[] = WestRepice::find()->where(['in', 'id', $ids])->with(['usetime', 'usetype', 'frequency', 'westUnit'])->asArray()->all();
}
}
if($granular){
foreach ($granular as $value) {
$ids = explode(',', $value['gr_ids']);
$granularRepice[] = GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all();
}
}
return [
'chin_prescription' => $chinese,
'chinese' => $chineseRepice,
@@ -152,11 +506,7 @@ class PrescripDetailController extends BaseAppController
];
}
/**
* @doc-name 开处方-常用方详情(中药)
* @doc-param int id 处方id
*/
public function actionCommonChinese()
protected function actionCommonChineseLegacy()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post,[
@@ -170,16 +520,10 @@ class PrescripDetailController extends BaseAppController
throw new Exception('没有该处方');
}
$ids = explode(',', $chinese['cr_ids']);
$recipe = ChineseRepice::find()->where(['in', 'id', $ids])->asArray()->all();
return $recipe;
return ChineseRepice::find()->where(['in', 'id', $ids])->asArray()->all();
}
/**
* @doc-name 开处方-常用方详情(颗粒药)
* @doc-param int id 处方id
*/
public function actionCommonGranular()
protected function actionCommonGranularLegacy()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post,[
@@ -193,17 +537,10 @@ class PrescripDetailController extends BaseAppController
throw new Exception('没有该处方');
}
$ids = explode(',', $granular['gr_ids']);
$recipe = GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all();
return $recipe;
return GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all();
}
/**
* @doc-name 开处方-常用方详情(西药)
* @doc-param int id 处方id
*/
public function actionCommonWest()
protected function actionCommonWestLegacy()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post,[
@@ -217,9 +554,7 @@ class PrescripDetailController extends BaseAppController
throw new Exception('没有该处方');
}
$ids = explode(',', $west['wr_ids']);
$recipe = WestRepice::find()->where(['in', 'id', $ids])->with(['usetime', 'usetype', 'frequency', 'westUnit'])->asArray()->all();
return $recipe;
return WestRepice::find()->where(['in', 'id', $ids])->with(['usetime', 'usetype', 'frequency', 'westUnit'])->asArray()->all();
}
/**

View File

@@ -871,25 +871,42 @@ class PrescriptionController extends BaseAppController
$drug_ids_str=implode(',',$drug_ids);
$totalNum = 0;
foreach ($medicine as $value){
$time = time();
foreach ($medicine as &$value) {
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.id' => $value['drug_id']
])->joinWith(['drugStoreDrug dsd' => function($q){
'd.id' => $value['drug_id'],
])->joinWith(['drugStoreDrug dsd' => function ($q) {
$q->andWhere(['dsd.status' => 2]);
}])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){
}])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id) {
$q->andWhere(['dsr.store_id' => $store_id]);
$q->andWhere(['dsr.status' => 2]);
}])->asArray()->one();
if(!$drug){
if (!$drug) {
throw new Exception('药品库存数据错误');
}
if (empty($drug['drugStoreRelation'])) {
throw new Exception('药品暂不售卖');
}
$salePrice = $drug['drugStoreRelation']['price'];
$buyPrice = $drug['drugStoreRelation']['buy_price'];
ChineseMedicine::updateAll(
[
'price' => $salePrice,
'buy_price' => $buyPrice,
'updated_at' => $time,
],
['id' => $value['id']],
);
$value['price'] = $salePrice;
$value['buy_price'] = $buyPrice;
$drugNumber = (int) $value['number'] * (int) $param['dosage'];
if($drug['drugStoreDrug']['stock'] < $drugNumber){
throw new Exception($value['name'].'库存数量不足');
if ($drug['drugStoreDrug']['stock'] < $drugNumber) {
throw new Exception($value['name'] . '库存数量不足');
}
$totalNum += $drugNumber;
$price+=$value['number'] * $value['price'] * $param['dosage'];
$price += $value['number'] * $salePrice * $param['dosage'];
}
unset($value);
$process_price = 0;
$processRuleContent = '';