Commit 06f77153 by lujunyi

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

parent 076e42d0
......@@ -4,6 +4,7 @@
use App\Admin\Repositories\PharmacyRepository;
use App\Models\PharmacistModel;
use App\Models\PharmacyDrugModel;
use App\Models\PharmacyModel;
use App\Models\User;
use Dcat\Admin\Form;
......@@ -12,7 +13,6 @@
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Models\Role;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
// 药店
......@@ -162,42 +162,42 @@ protected function form()
});
$form->saved(function (Form $form, $result) {
DB::beginTransaction();
try {
// 获取店铺管理员角色
$role = Role::where('slug', 'pharmacy')->first();
// 从表单模型获取手机号和其他信息
$mobile = $form->mobile;
$name = $form->name;
$pharmacyId = $form->getKey();
// 查找当前是否已有管理员
$admin = Administrator::where('pharmacy_id', $pharmacyId)->first();
if ($admin) {
if ($mobile && $name) {
$admin->username = $mobile; // 更新账号
$admin->name = $name; // 更新账号姓名
$admin->save();
}
} else {
if ($mobile && $name && $pharmacyId) {
$admin = new Administrator();
$admin->username = $mobile; // 药店手机号作为管理员账号
$admin->name = $name; // 药店名称当做用户的姓名
$admin->password = bcrypt(Str::random(10)); // 设置管理员密码
$admin->pharmacy_id = $pharmacyId; // 药店ID
$admin->save(); // 保存新管理员
// 关联药店管理员角色
$admin->roles()->attach($role->id);
}
$pharmacyId = $form->getKey();
// 从表单模型获取手机号和其他信息
$mobile = $form->mobile;
$name = $form->name;
// 复制药店pharmacy_id=1的药店商品到新建药店
if ($form->isCreating()) {
$originalModels = PharmacyDrugModel::where('pharmacy_id', 1)->get();
foreach ($originalModels as $model) {
$pharmacyModel = $model->replicate();
$pharmacyModel->pharmacy_id = $pharmacyId;
$pharmacyModel->save();
}
}
// 获取店铺管理员角色
$role = Role::where('slug', 'pharmacy')->first();
// 查找当前是否已有管理员
$admin = Administrator::where('pharmacy_id', $pharmacyId)->first();
if ($admin) {
if ($mobile && $name) {
$admin->username = $mobile; // 更新账号
$admin->name = $name; // 更新账号姓名
$admin->save();
}
} else {
if ($mobile && $name && $pharmacyId) {
$admin = new Administrator();
$admin->username = $mobile; // 药店手机号作为管理员账号
$admin->name = $name; // 药店名称当做用户的姓名
$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