Commit 06f77153 by lujunyi

新增药店同步id=1的药店的药品

parent 076e42d0
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use App\Admin\Repositories\PharmacyRepository; use App\Admin\Repositories\PharmacyRepository;
use App\Models\PharmacistModel; use App\Models\PharmacistModel;
use App\Models\PharmacyDrugModel;
use App\Models\PharmacyModel; use App\Models\PharmacyModel;
use App\Models\User; use App\Models\User;
use Dcat\Admin\Form; use Dcat\Admin\Form;
...@@ -12,7 +13,6 @@ ...@@ -12,7 +13,6 @@
use Dcat\Admin\Models\Administrator; use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Models\Role; use Dcat\Admin\Models\Role;
use Dcat\Admin\Show; use Dcat\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str; use Illuminate\Support\Str;
// 药店 // 药店
...@@ -162,42 +162,42 @@ protected function form() ...@@ -162,42 +162,42 @@ protected function form()
}); });
$form->saved(function (Form $form, $result) { $form->saved(function (Form $form, $result) {
DB::beginTransaction(); $pharmacyId = $form->getKey();
try { // 从表单模型获取手机号和其他信息
// 获取店铺管理员角色 $mobile = $form->mobile;
$role = Role::where('slug', 'pharmacy')->first(); $name = $form->name;
// 复制药店pharmacy_id=1的药店商品到新建药店
// 从表单模型获取手机号和其他信息 if ($form->isCreating()) {
$mobile = $form->mobile; $originalModels = PharmacyDrugModel::where('pharmacy_id', 1)->get();
$name = $form->name; foreach ($originalModels as $model) {
$pharmacyId = $form->getKey(); $pharmacyModel = $model->replicate();
$pharmacyModel->pharmacy_id = $pharmacyId;
// 查找当前是否已有管理员 $pharmacyModel->save();
$admin = Administrator::where('pharmacy_id', $pharmacyId)->first(); }
}
if ($admin) {
if ($mobile && $name) { // 获取店铺管理员角色
$admin->username = $mobile; // 更新账号 $role = Role::where('slug', 'pharmacy')->first();
$admin->name = $name; // 更新账号姓名 // 查找当前是否已有管理员
$admin->save(); $admin = Administrator::where('pharmacy_id', $pharmacyId)->first();
} if ($admin) {
} else { if ($mobile && $name) {
if ($mobile && $name && $pharmacyId) { $admin->username = $mobile; // 更新账号
$admin = new Administrator(); $admin->name = $name; // 更新账号姓名
$admin->username = $mobile; // 药店手机号作为管理员账号 $admin->save();
$admin->name = $name; // 药店名称当做用户的姓名 }
$admin->password = bcrypt(Str::random(10)); // 设置管理员密码 } else {
$admin->pharmacy_id = $pharmacyId; // 药店ID if ($mobile && $name && $pharmacyId) {
$admin->save(); // 保存新管理员 $admin = new Administrator();
$admin->username = $mobile; // 药店手机号作为管理员账号
// 关联药店管理员角色 $admin->name = $name; // 药店名称当做用户的姓名
$admin->roles()->attach($role->id); $admin->password = bcrypt(Str::random(10)); // 设置管理员密码
} $admin->pharmacy_id = $pharmacyId; // 药店ID
$admin->save(); // 保存新管理员
// 关联药店管理员角色
$admin->roles()->attach($role->id);
} }
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw new \Exception($e->getMessage());
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment