去掉「同日+同渠道」全局唯一,回到按录入人判重;唯一索引补 delete_time 避免软删占键导致 1062;错误提示带冲突记录 ID,迁移脚本兼容多次执行。 Co-authored-by: Cursor <cursoragent@cursor.com>
140 lines
4.8 KiB
PHP
140 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\logic\stats;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\stats\PersonalYeji;
|
|
|
|
class PersonalYejiLogic extends BaseLogic
|
|
{
|
|
use PersonalStatsScopeTrait;
|
|
|
|
public static function add(array $params, int $adminId, string $adminName): bool
|
|
{
|
|
try {
|
|
$yejiDate = (string) $params['yeji_date'];
|
|
$mediaSource = self::normalizeMediaSource((string) ($params['media_source'] ?? ''));
|
|
if ($mediaSource === '') {
|
|
self::setError('请填写自媒体来源');
|
|
|
|
return false;
|
|
}
|
|
|
|
$dupId = self::findYejiDuplicateId($adminId, $yejiDate, $mediaSource);
|
|
if ($dupId > 0) {
|
|
self::setError("您在 {$yejiDate} 已录入过【{$mediaSource}】业绩(记录#{$dupId}),请直接编辑该记录");
|
|
|
|
return false;
|
|
}
|
|
|
|
PersonalYeji::create([
|
|
'yeji_date' => $yejiDate,
|
|
'media_source' => $mediaSource,
|
|
'add_fans_count' => (int) ($params['add_fans_count'] ?? 0),
|
|
'total_open_count' => (int) ($params['total_open_count'] ?? 0),
|
|
'unreplied_count' => (int) ($params['unreplied_count'] ?? 0),
|
|
'paid_appointment_count' => (int) ($params['paid_appointment_count'] ?? 0),
|
|
'free_appointment_count' => (int) ($params['free_appointment_count'] ?? 0),
|
|
'interview_count' => (int) ($params['interview_count'] ?? 0),
|
|
'order_amount' => round((float) ($params['order_amount'] ?? 0), 2),
|
|
'completed_order_count' => (int) ($params['completed_order_count'] ?? 0),
|
|
'remark' => (string) ($params['remark'] ?? ''),
|
|
'creator_id' => $adminId,
|
|
'creator_name' => $adminName,
|
|
'updater_id' => $adminId,
|
|
'updater_name' => $adminName,
|
|
'dept_id' => self::resolvePrimaryDeptId($adminId),
|
|
]);
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
if (self::isUniqueConstraintViolation($e)) {
|
|
self::setError('该日期下该渠道的业绩已存在(唯一索引冲突),请刷新列表后直接编辑');
|
|
} else {
|
|
self::setError($e->getMessage());
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function edit(array $params, int $adminId, string $adminName, array $adminInfo): bool
|
|
{
|
|
try {
|
|
$model = PersonalYeji::find($params['id']);
|
|
if (!$model) {
|
|
self::setError('记录不存在');
|
|
|
|
return false;
|
|
}
|
|
|
|
if (!self::assertRecordVisible($adminId, $adminInfo, (int) $model->creator_id)) {
|
|
self::setError('无权操作该记录');
|
|
|
|
return false;
|
|
}
|
|
|
|
$model->add_fans_count = (int) ($params['add_fans_count'] ?? 0);
|
|
$model->total_open_count = (int) ($params['total_open_count'] ?? 0);
|
|
$model->unreplied_count = (int) ($params['unreplied_count'] ?? 0);
|
|
$model->paid_appointment_count = (int) ($params['paid_appointment_count'] ?? 0);
|
|
$model->free_appointment_count = (int) ($params['free_appointment_count'] ?? 0);
|
|
$model->interview_count = (int) ($params['interview_count'] ?? 0);
|
|
$model->order_amount = round((float) ($params['order_amount'] ?? 0), 2);
|
|
$model->completed_order_count = (int) ($params['completed_order_count'] ?? 0);
|
|
$model->remark = (string) ($params['remark'] ?? '');
|
|
$model->updater_id = $adminId;
|
|
$model->updater_name = $adminName;
|
|
$model->save();
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function delete(int $id, int $adminId, array $adminInfo): bool
|
|
{
|
|
try {
|
|
$model = PersonalYeji::find($id);
|
|
if (!$model) {
|
|
self::setError('记录不存在');
|
|
|
|
return false;
|
|
}
|
|
|
|
if (!self::assertRecordVisible($adminId, $adminInfo, (int) $model->creator_id)) {
|
|
self::setError('无权操作该记录');
|
|
|
|
return false;
|
|
}
|
|
|
|
$model->delete();
|
|
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
self::setError($e->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function detail(int $id, int $adminId, array $adminInfo): array
|
|
{
|
|
$model = PersonalYeji::find($id);
|
|
if (!$model) {
|
|
return [];
|
|
}
|
|
|
|
if (!self::assertRecordVisible($adminId, $adminInfo, (int) $model->creator_id)) {
|
|
return [];
|
|
}
|
|
|
|
return $model->toArray();
|
|
}
|
|
}
|