导入测试

This commit is contained in:
2024-10-30 16:00:44 +08:00
parent a2e6e79eb1
commit 77b689bb08
2 changed files with 100 additions and 3 deletions

View File

@@ -3,10 +3,10 @@
namespace admin\controllers\store;
use admin\components\BaseAdminController;
use admin\controllers\AuthController;
use common\enums\UserRoleEnum;
use admin\models\forms\drug\ImportForm;
//use common\enums\UserRoleEnum;
use common\jobs\ProductOrderAutoReceivedJob;
use common\jobs\ProductOrderSendJob;
//use common\jobs\ProductOrderSendJob;
use common\jobs\templateMessage\ProductOrderSend;
use common\models\DrugStoreDrug;
use common\models\DrugStoreRelations;
@@ -16,8 +16,10 @@ use common\models\Log;
use common\models\ProductOrder;
use common\models\Store;
use GuzzleHttp\Client;
use moonland\phpexcel\Excel;
use yii\db\Exception;
use yii\helpers\Json;
use yii\web\UploadedFile;
/**
* @doc-module 仓库
@@ -233,6 +235,7 @@ class WareController extends BaseAdminController
*/
public function actionUpdateDrugstorePrice()
{
// TODO 开始修改这部分
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['drug_id', 'type'], 'required']
@@ -320,6 +323,15 @@ class WareController extends BaseAdminController
}
/**
* 根据Excel修改仓库价格
* @return void
*/
public function actionUpdateDrugstorePriceByExcel()
{
}
/**
* @doc-name 仓库修改门店西药价格
* @doc-param int store_id 门店id
@@ -625,4 +637,55 @@ class WareController extends BaseAdminController
]);
return ['修改成功'];
}
// Excel导入批量修改商品价格
public function actionExcelUpdatePrice()
{
ds(base64_encode(''));
try {
$request = \Yii::$app->request;
$storeId = \Yii::$app->store;
if (empty($storeId)) {
throw new Exception('店铺账号获取失败-'. $storeId);
}
if ($request->isPost) {
$post = \Yii::$app->request->post();
//设置最大执行时间
ini_set("max_execution_time", "360");
$file = UploadedFile::getInstanceByName('file');
if ($file->size > 5 * 1024 * 1024) {
return ['excel文件不能超过5M!'];
}
//文件名
$filename = date('His') . md5($file->getBaseName()) . mt_rand(1000, 9999) . '.' . $file->getExtension();
//保存文件
$file->saveAs($filename);
// $fullFileName = $absolute_path . $filename;
$data = Excel::import($filename, [
'setFirstRecordAsKeys' => true,
'getOnlySheet' => 'Sheet1',
]);
ds([
$data,
@unlink($filename)
]);
$ImportForm = new ImportForm();
$ImportForm->attributes = $post;
return $ImportForm->ExportUpdateDrugPrice($data, $storeId);
}
} catch (\Exception $e) {
ds([
$e->getMessage(),
$e->getTrace(),
$e->getLine(),
$e->getFile(),
]);
// 删除文件
@unlink($filename);
return ['error' => $e->getMessage()];
}
}
}

View File

@@ -6,6 +6,7 @@ use common\core\BaseModel;
use common\helpers\ArrayHelper;
use common\models\Drug;
use common\models\DrugStoreDrug;
use common\models\DrugStoreRelations;
use common\models\Platform;
use Overtrue\Pinyin\Pinyin;
use yii\db\Exception;
@@ -347,4 +348,37 @@ class ImportForm extends BaseModel
return ['success' => $success, 'error' => $errors];
}
}
public function ExportUpdateDrugPrice($data, $storeId)
{
if (empty($data)) {
throw new Exception('导入数据为空');
} else {
$t=\Yii::$app->db->beginTransaction();
try {
foreach ($data as $item) {
$DrugStoreDrug = DrugStoreRelations::find()->where(['drug_id' => trim($item['药品ID']), 'store_id' => $storeId])->one();
if ($DrugStoreDrug) {
$model = new DrugStoreRelations();
$model->setAttribute('drug_id', trim($item['药品ID']));
$model->setAttribute('price', trim($item['销售价格']));
if (!$model->save()) {
$errorsMsg = array_values($model->getFirstErrors());
$errors[] = $item['药品ID'] . '导入失败,原因:保存失败;'.$errorsMsg[0];
}else{
$success[] = $item['药品ID'] . '导入成功!';
}
} else {
$errors[] = $item['药品ID'] . '导入失败,原因:未找到对应药品';
}
}
$t->commit();
} catch (\Exception $e) {
$t->rollBack();
$errors[] = $e->getMessage();
}
return ['success' => $success, 'error' => $errors];
}
}
}