产品服务包,分账
This commit is contained in:
@@ -18,99 +18,95 @@ class SyncController extends BaseAdminController
|
||||
*/
|
||||
public function actionSyncStore()
|
||||
{
|
||||
$admin = \Yii::$app->user->identity;
|
||||
if ($admin->role != UserRoleEnum::SUPER_ADMIN) {
|
||||
throw new Exception('非超管,您不能同步药品');
|
||||
// 定义药物类型映射
|
||||
$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();
|
||||
}
|
||||
$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();
|
||||
// 获取有效的店铺数据
|
||||
$store = Store::find()
|
||||
->select(['id', 'z_buy_percent', 'z_sale_percent', 'g_bug_percent', 'g_sale_percent'])
|
||||
->where(['is_delete' => 0])
|
||||
->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(['is_delete' => 0])->all();
|
||||
if (empty($store) || empty(array_filter($drugData))) {
|
||||
return ['同步成功']; // 如果没有任何数据需要处理,直接返回
|
||||
}
|
||||
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
foreach ($store as $value) {//中药
|
||||
foreach ($DrugStoreDrug_chinese as $val) {
|
||||
|
||||
$Drug = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $val['drug_id']
|
||||
])->one();
|
||||
if (!$Drug) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['id'];
|
||||
$DrugStoreRelations->drug_id = $val['drug_id'];
|
||||
$DrugStoreRelations->price = bcdiv(bcmul($val['price'], $value['z_sale_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->buy_price = bcdiv(bcmul($val['market_price'], $value['z_buy_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->status = $val['status'];
|
||||
$DrugStoreRelations->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($store as $value) {//西药
|
||||
foreach ($DrugStoreDrug_west as $v) {
|
||||
$DrugStore = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $v['drug_id']
|
||||
])->one();
|
||||
if (!$DrugStore) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['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 ($store as $value) {//配方
|
||||
foreach ($DrugStoreDrug_grain as $vv) {
|
||||
$DrugStoreRelations = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $vv['drug_id']
|
||||
])->one();
|
||||
if (!$DrugStoreRelations) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['id'];
|
||||
$DrugStoreRelations->drug_id = $vv['drug_id'];
|
||||
$DrugStoreRelations->price = bcdiv(bcmul($vv['price'], $value['g_sale_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->buy_price = bcdiv(bcmul($vv['market_price'], $value['g_bug_percent']??100, 4), 100, 4);
|
||||
$DrugStoreRelations->status = $vv['status'];
|
||||
$DrugStoreRelations->saveOrFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($store as $value) {//中成药
|
||||
foreach ($DrugStoreDrug_zongcheng as $vv) {
|
||||
$DrugStoreRelations_z = DrugStoreRelations::find()->where([
|
||||
'store_id' => $value['id'],
|
||||
'drug_id' => $vv['drug_id']
|
||||
])->one();
|
||||
if (!$DrugStoreRelations_z) {
|
||||
$DrugStoreRelations = new DrugStoreRelations();
|
||||
$DrugStoreRelations->store_id = $value['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();
|
||||
}
|
||||
}
|
||||
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());
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
$t->commit();
|
||||
return ['同步成功'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user