Files
xk-api-yii/admin/controllers/base/SyncController.php
2024-12-23 16:48:36 +08:00

219 lines
9.0 KiB
PHP

<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
use common\enums\UserRoleEnum;
use common\jobs\SyncHospitalJob;
use common\models\oldDb\DrugStoreDrug;
use common\models\oldDb\DrugStoreRelations;
use common\models\oldDb\Store;
use yii\db\Exception;
class SyncController extends BaseAdminController
{
/**
* @doc-name 同步所有的门店门店----总后台同步
*/
public function actionSyncStore()
{
// 定义药物类型映射
$drugTypes = [
'chinese' => 1, // 假设中药类型为1
'west' => 2,
'grain' => 3,
'zongcheng' => 4,
'service_package' => 5
];
// 获取所有类型的药物数据
$drugData = [];
foreach ($drugTypes as $typeKey => $typeValue) {
$drugData[$typeKey] = DrugStoreDrug::find()
->select(['drug_id', 'price', 'market_price', 'status'])
->where(['type' => $typeValue])
->asArray()
->all();
}
// 获取有效的店铺数据
$store = Store::find()
->select(['id', 'z_buy_percent', 'z_sale_percent', 'g_bug_percent', 'g_sale_percent'])
->where(['is_delete' => 0])
->all();
if (empty($store) || empty(array_filter($drugData))) {
return ['同步成功']; // 如果没有任何数据需要处理,直接返回
}
$t = \Yii::$app->db->beginTransaction();
try {
foreach ($drugTypes as $typeKey => $typeValue) {
$this->processDrugType($typeKey, $drugData[$typeKey], $store, $drugTypes);
}
$t->commit();
return ['同步成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception("事务回滚: " . $e->getMessage(), 0, $e);
}
}
/**
* 处理特定类型的药物
*
* @param string $typeKey 药物类型键
* @param array $drugList 药物列表
* @param array $store 店铺列表
* @throws \yii\base\Exception
*/
protected function processDrugType($typeKey, $drugList, $store, $drugTypes) {
// 批量获取所有需要插入的记录
$existingRelations = DrugStoreRelations::find()
->select(['store_id', 'drug_id'])
->indexBy(function ($row) {
return "{$row['store_id']}_{$row['drug_id']}";
})
->column();
foreach ($store as $storeItem) {
foreach ($drugList as $drugItem) {
$key = "{$storeItem['id']}_{$drugItem['drug_id']}";
if (!isset($existingRelations[$key])) {
$relation = new DrugStoreRelations();
$relation->store_id = $storeItem['id'];
$relation->drug_id = $drugItem['drug_id'];
$relation->status = $drugItem['status'];
switch ($typeKey) {
case 'chinese':
$relation->price = bcdiv(bcmul($drugItem['price'], $storeItem['z_sale_percent'] ?? 100, 4), 100, 4);
$relation->buy_price = bcdiv(bcmul($drugItem['market_price'], $storeItem['z_buy_percent'] ?? 100, 4), 100, 4);
break;
case 'grain':
$relation->price = bcdiv(bcmul($drugItem['price'], $storeItem['g_sale_percent'] ?? 100, 4), 100, 4);
$relation->buy_price = bcdiv(bcmul($drugItem['market_price'], $storeItem['g_bug_percent'] ?? 100, 4), 100, 4);
break;
default:
$relation->price = $drugItem['price'];
$relation->buy_price = $drugItem['market_price'] ?? $drugItem['price'];
break;
}
$relation->saveOrFail();
}
}
}
}
/**
* @doc-name 同步互医
*/
public function actionSyncHospital()
{
$type=4;
$admin=\Yii::$app->user->identity;
\Yii::$app->queue->delay(0)->push(new SyncHospitalJob([
'admin_id'=>$admin->uid,
'type'=>$type,
]));
}
/**
* @doc-name 同步单个门店
* @doc-param int store_id 门店ID
*/
public function actionSyncUnitStore()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['store_id', 'required']
]);
$DrugStoreDrug_chinese = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 1])->asArray()->all();
$DrugStoreDrug_west = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 2])->asArray()->all();
$DrugStoreDrug_grain = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 3])->asArray()->all();
$DrugStoreDrug_zongcheng = DrugStoreDrug::find()->select(['drug_id', 'price', 'market_price', 'status'])->where(['type' => 4])->asArray()->all();
$store = Store::find()->select(['id', 'z_buy_percent', 'z_sale_percent', 'g_bug_percent', 'g_sale_percent'])->where(['id' => $get['store_id'], 'is_delete' => 0])->one();
if (!$store) throw new Exception('门店不存在');
$t = \Yii::$app->db->beginTransaction();
try {
//中药
foreach ($DrugStoreDrug_chinese as $val) {
$Drug = DrugStoreRelations::find()->where([
'store_id' => $get['store_id'],
'drug_id' => $val['drug_id']
])->one();
if (!$Drug) {
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->store_id = $get['store_id'];
$DrugStoreRelations->drug_id = $val['drug_id'];
$DrugStoreRelations->price = bcdiv(bcmul($val['price'], $store['z_sale_percent']??100, 4), 100, 4);
$DrugStoreRelations->buy_price = bcdiv(bcmul($val['market_price'], $store['z_buy_percent']??100, 4), 100, 4);
$DrugStoreRelations->status = $val['status'];
$DrugStoreRelations->saveOrFail();
}
}
//西药
foreach ($DrugStoreDrug_west as $v) {
$DrugStore = DrugStoreRelations::find()->where([
'store_id' => $get['store_id'],
'drug_id' => $v['drug_id']
])->one();
if (!$DrugStore) {
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->store_id = $get['store_id'];
$DrugStoreRelations->drug_id = $v['drug_id'];
$DrugStoreRelations->price = $v['price'];
$DrugStoreRelations->buy_price = $v['market_price'] ?? $v['price'];
$DrugStoreRelations->status = $v['status'];
$DrugStoreRelations->saveOrFail();
}
}
//配方
foreach ($DrugStoreDrug_grain as $vv) {
$DrugStoreRelations = DrugStoreRelations::find()->where([
'store_id' => $get['store_id'],
'drug_id' => $vv['drug_id']
])->one();
if (!$DrugStoreRelations) {
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->store_id = $get['store_id'];
$DrugStoreRelations->drug_id = $vv['drug_id'];
$DrugStoreRelations->price = bcdiv(bcmul($vv['price'], $store['g_sale_percent']??100, 4), 100, 4);
$DrugStoreRelations->buy_price = bcdiv(bcmul($vv['market_price'], $store['g_bug_percent']??100, 4), 100, 4);
$DrugStoreRelations->status = $vv['status'];
$DrugStoreRelations->saveOrFail();
}
}
//中成药
foreach ($DrugStoreDrug_zongcheng as $vv) {
$DrugStoreRelations_z = DrugStoreRelations::find()->where([
'store_id' => $get['store_id'],
'drug_id' => $vv['drug_id']
])->one();
if (!$DrugStoreRelations_z) {
$DrugStoreRelations = new DrugStoreRelations();
$DrugStoreRelations->store_id = $get['store_id'];
$DrugStoreRelations->drug_id = $vv['drug_id'];
$DrugStoreRelations->price = $vv['price'];
$DrugStoreRelations->buy_price = $vv['market_price'] ?? $vv['price'];
$DrugStoreRelations->status = $vv['status'];
$DrugStoreRelations->saveOrFail();
}
}
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
$t->commit();
return ['同步成功'];
}
}