generated from liutk/owl-admin-base
162 lines
4.8 KiB
PHP
162 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\StoreFilter;
|
|
use App\Models\Employee;
|
|
use App\Models\Store;
|
|
use App\Models\StoreProfitRatioLog;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
|
|
class StoreService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['category', 'level', 'business', 'master'];
|
|
|
|
protected string $modelName = Store::class;
|
|
|
|
protected string $modelFilterName = StoreFilter::class;
|
|
|
|
public function store($data): bool
|
|
{
|
|
$data = $this->resloveData($data);
|
|
|
|
$validate = $this->validate($data);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
$beforeProfitRatio = 0;
|
|
|
|
$model = $this->modelName::create($data);
|
|
|
|
$afterProfitRatio = $model->profit_ratio;
|
|
|
|
if (bccomp($beforeProfitRatio, $afterProfitRatio, 2) !== 0) {
|
|
$employee = Employee::where('admin_user_id', Admin::user()->id)->first();
|
|
|
|
if (is_null($employee)) {
|
|
admin_abort('账号未找到员工信息');
|
|
}
|
|
|
|
StoreProfitRatioLog::create([
|
|
'store_id' => $model->id,
|
|
'employee_id' => $employee->id,
|
|
'before' => $beforeProfitRatio,
|
|
'after' => $afterProfitRatio,
|
|
]);
|
|
}
|
|
|
|
// 修改店长的所属门店
|
|
Employee::where('id', $data['master_id'])->update(['store_id' => $model->id]);
|
|
|
|
$this->currentModel = $model;
|
|
|
|
return true;
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
|
|
$data = $this->resloveData($data, $model);
|
|
$validate = $this->validate($data, $model);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
$beforeProfitRatio = $model->profit_ratio;
|
|
|
|
$model->update($data);
|
|
|
|
// 修改店长的所属门店
|
|
if (isset($data['master_id'])) {
|
|
Employee::where('id', $data['master_id'])->update(['store_id' => $model->id]);
|
|
}
|
|
|
|
$afterProfitRatio = $model->profit_ratio;
|
|
|
|
if (bccomp($beforeProfitRatio, $afterProfitRatio, 2) !== 0) {
|
|
$employee = Employee::where('admin_user_id', Admin::user()->id)->first();
|
|
|
|
if (is_null($employee)) {
|
|
admin_abort('账号未找到员工信息');
|
|
}
|
|
|
|
StoreProfitRatioLog::create([
|
|
'store_id' => $model->id,
|
|
'employee_id' => $employee->id,
|
|
'before' => $beforeProfitRatio,
|
|
'after' => $afterProfitRatio,
|
|
]);
|
|
}
|
|
|
|
$this->currentModel = $model;
|
|
|
|
return true;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (isset($data['location'])) {
|
|
$data['lon'] = data_get($data['location'], 'lng');
|
|
$data['lat'] = data_get($data['location'], 'lat');
|
|
$data['address'] = data_get($data['location'], 'address');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'title' => ['required'],
|
|
'master_id' => ['required', Rule::unique('stores', 'master_id')],
|
|
'category_id' => ['required'],
|
|
'business_id' => ['required'],
|
|
'level_id' => ['required'],
|
|
'region' => ['required'],
|
|
'lon' => ['required'],
|
|
'lat' => ['required'],
|
|
'profit_ratio' => ['required', 'numeric', 'min:0', 'max:100'],
|
|
];
|
|
$updateRules = [
|
|
'master_id' => [Rule::unique('stores', 'master_id')->ignore($model, 'master_id')],
|
|
'profit_ratio' => ['filled', 'numeric', 'min:0', 'max:100'],
|
|
];
|
|
$validator = Validator::make(
|
|
$data,
|
|
$model ? $updateRules : $createRules,
|
|
[
|
|
'master_id.unique' => '已经是店长了',
|
|
],
|
|
[
|
|
'title' => __('store.title'),
|
|
'master_id' => __('store.master_id'),
|
|
'category_id' => __('store.category_id'),
|
|
'business_id' => __('store.business_id'),
|
|
'level_id' => __('store.level_id'),
|
|
'region' => __('store.region'),
|
|
'lon' => __('store.location'),
|
|
'lat' => __('store.location'),
|
|
'profit_ratio' => __('store.profit_ratio'),
|
|
]
|
|
);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function preDelete(array $ids): void
|
|
{
|
|
// 修改员工关联
|
|
Employee::whereIn('store_id', $ids)->update(['store_id' => 0]);
|
|
}
|
|
}
|