Files
xk-api-yii/admin/controllers/base/SyncController.php

223 lines
10 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()
{
$admin = \Yii::$app->user->identity;
if ($admin->role != UserRoleEnum::SUPER_ADMIN) {
throw new Exception('非超管,您不能同步药品');
}
$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(['is_delete' => 0])->all();
$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();
}
}
}
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
$t->commit();
return ['同步成功'];
}
/**
* @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 ['同步成功'];
}
}