导入测试

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

@@ -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];
}
}
}