组员客户数

This commit is contained in:
2025-08-14 15:28:15 +08:00
parent 312c7706f9
commit d90a8b05ce
4 changed files with 38 additions and 2 deletions
+27
View File
@@ -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);
});
}
}
+6
View File
@@ -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);
}