客户等级设置
This commit is contained in:
@@ -5,9 +5,13 @@ namespace App\Http\Controllers\Customer;
|
||||
use App\Exceptions\RepositoryException;
|
||||
use App\Http\Requests\Customer\CustomerLevelFormRequest;
|
||||
use App\Models\CustomerLevelModel;
|
||||
use App\Models\NoticeModel;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\UserModel;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\AnnoRoute\Attribute\DeleteRoute;
|
||||
use Modules\AnnoRoute\Attribute\GetRoute;
|
||||
use Modules\AnnoRoute\Attribute\PostRoute;
|
||||
@@ -17,7 +21,7 @@ use Modules\Common\Http\Controllers\BaseController;
|
||||
use Modules\SystemTool\Services\SysFileService;
|
||||
|
||||
/**
|
||||
* 客户等级管理(同一商品按客户等级定价)
|
||||
* 客户等级管理(同一商品按等级上浮比例定价:售价 = 成本价 × (100 + percent) / 100)
|
||||
*/
|
||||
#[RequestAttribute('/customer/level', 'customer.level')]
|
||||
class CustomerLevelController extends BaseController
|
||||
@@ -63,7 +67,7 @@ class CustomerLevelController extends BaseController
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/** 编辑等级 */
|
||||
/** 编辑等级(上浮比例变更时通知该等级下门店用户) */
|
||||
#[PutRoute(route: '/{id}', authorize: 'update', where: ['id' => '[0-9]+'])]
|
||||
public function update(int $id, CustomerLevelFormRequest $request): JsonResponse
|
||||
{
|
||||
@@ -71,7 +75,14 @@ class CustomerLevelController extends BaseController
|
||||
if (empty($model)) {
|
||||
throw new RepositoryException('客户等级不存在');
|
||||
}
|
||||
$model->update($request->validated());
|
||||
$validated = $request->validated();
|
||||
DB::transaction(function () use ($model, $validated) {
|
||||
$model->update($validated);
|
||||
// 上浮比例变化 → 该等级下所有商品的售价联动变化,通知受影响门店用户
|
||||
if ($model->wasChanged('percent')) {
|
||||
$this->notifyPriceChange($model);
|
||||
}
|
||||
});
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
@@ -86,9 +97,6 @@ class CustomerLevelController extends BaseController
|
||||
if ($model->stores()->exists()) {
|
||||
throw new RepositoryException('该等级下存在门店,无法删除');
|
||||
}
|
||||
if ($model->prices()->exists()) {
|
||||
throw new RepositoryException('该等级下存在商品价格,无法删除');
|
||||
}
|
||||
$model->delete();
|
||||
return $this->success();
|
||||
}
|
||||
@@ -100,8 +108,41 @@ class CustomerLevelController extends BaseController
|
||||
$data = CustomerLevelModel::query()
|
||||
->where('status', CustomerLevelModel::STATUS_NORMAL)
|
||||
->orderBy('sort')
|
||||
->get(['id', 'name'])
|
||||
->get(['id', 'name', 'percent'])
|
||||
->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上浮比例变更后,给该等级下正常门店绑定的正常用户生成价格变更通知
|
||||
*/
|
||||
private function notifyPriceChange(CustomerLevelModel $level): void
|
||||
{
|
||||
$userIds = UserModel::query()
|
||||
->where('status', UserModel::STATUS_NORMAL)
|
||||
->whereIn('store_id', function ($q) use ($level) {
|
||||
$q->select('id')
|
||||
->from('store')
|
||||
->where('status', StoreModel::STATUS_NORMAL)
|
||||
->where('level_id', $level->id);
|
||||
})
|
||||
->pluck('id');
|
||||
|
||||
$content = mb_substr(
|
||||
'您所在客户等级「' . $level->name . '」的价格上浮比例已调整为 ' . (float) $level->percent . '%,商品价格将按新比例显示',
|
||||
0,
|
||||
500
|
||||
);
|
||||
|
||||
foreach ($userIds as $userId) {
|
||||
NoticeModel::create([
|
||||
'user_id' => $userId,
|
||||
'type' => NoticeModel::TYPE_PRICE,
|
||||
'title' => '商品价格变更',
|
||||
'content' => $content,
|
||||
'data' => ['level_id' => $level->id],
|
||||
'is_read' => NoticeModel::UNREAD,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user