diff --git a/app/command/CustomerCodeCommand.php b/app/command/CustomerCodeCommand.php index 2d3e447..a82ec0f 100644 --- a/app/command/CustomerCodeCommand.php +++ b/app/command/CustomerCodeCommand.php @@ -44,7 +44,8 @@ class CustomerCodeCommand extends SymfonyCommand $code = 'CM' . $unique; } - $obtain = $customer->created_at>strtotime('2025-07-29')?$customer->created_at:strtotime('2025-07-29'); + $created_at = $customer->created_at->getTimestamp(); + $obtain = $created_at > strtotime('2025-07-29')?$created_at:strtotime('2025-07-29'); $customer->code = $code; $customer->obtain = $obtain; diff --git a/app/common/model/CrmCustomer.php b/app/common/model/CrmCustomer.php index 49446e6..f050186 100644 --- a/app/common/model/CrmCustomer.php +++ b/app/common/model/CrmCustomer.php @@ -46,4 +46,31 @@ class CrmCustomer extends Model * 不允许自动填充模型字段 */ protected $guarded = []; + + protected static function booted() + { + static::saved(function ($customer) { + if (empty($customer->code)) { + $customer->code = self::generateCustomerCode($customer); + // 使用saveQuietly避免递归调用 + $customer->saveQuietly(); + } + }); + } + + public static function generateCustomerCode($customer) + { + // 生成唯一16位code,CM+14位 + $unique = strtoupper(substr(md5($customer->id . microtime(true) . mt_rand(1000, 9999)), 8, 14)); + $code = 'CM' . date('Ymd') .$unique; + return $code; + } + + // 静默保存方法,不触发事件 + public function saveQuietly(array $options = []) + { + return static::withoutEvents(function () use ($options) { + return $this->save($options); + }); + } } \ No newline at end of file diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index ad7aa5f..3712f20 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -9,6 +9,7 @@ use app\common\traits\crud\CrudLogicTrait; use app\common\traits\crud\CrudServiceTrait; use app\common\utils\CrudUtil; use app\crm\model\Country; +use app\crm\model\Customer; use support\exception\BusinessException; use support\Request; @@ -136,6 +137,11 @@ class UserService $page = (int)$request->get('current', 1); $result = $this->logicList($config, $limit, $page); } + + foreach ($result as &$data){ + $data['customer_count'] = Customer::where('user_id', $data['id'])->count(); + } + return $this->postProcess('List', $result); } diff --git a/app/crmAdmin/service/CustomerService.php b/app/crmAdmin/service/CustomerService.php index 1bb22f4..dc08f0e 100644 --- a/app/crmAdmin/service/CustomerService.php +++ b/app/crmAdmin/service/CustomerService.php @@ -105,7 +105,9 @@ class CustomerService if (!Util::isEmpty($keyword)) { $query->where(function ($query) use ($keyword) { $query->where('contacts', 'like', "%{$keyword}%") - ->orWhere('phone', 'like', "%{$keyword}%"); + ->orWhere('phone', 'like', "%{$keyword}%") + ->orWhere('WhatsApp', 'like', "%{$keyword}%") + ->orWhere('code', 'like', "%{$keyword}%"); }); } if (!Util::isEmpty($grade)) {