107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\lists\qywx;
|
|
|
|
use app\adminapi\lists\BaseAdminDataLists;
|
|
use app\adminapi\logic\qywx\CustomerLogic;
|
|
use app\common\lists\ListsSearchInterface;
|
|
use app\common\model\auth\Admin;
|
|
use app\common\model\QywxExternalContact;
|
|
|
|
/**
|
|
* 企业微信客户列表
|
|
*/
|
|
class CustomerLists extends BaseAdminDataLists implements ListsSearchInterface
|
|
{
|
|
/**
|
|
* @notes 搜索条件
|
|
*/
|
|
public function setSearch(): array
|
|
{
|
|
// 表无 follow_user 列,跟进人在 follow_users(JSON);跟进人筛选在 baseQuery() 中处理
|
|
return [
|
|
'%like%' => ['name'],
|
|
];
|
|
}
|
|
|
|
private function baseQuery()
|
|
{
|
|
$query = QywxExternalContact::where($this->searchWhere);
|
|
if (!empty($this->params['follow_user'])) {
|
|
$kw = addcslashes((string) $this->params['follow_user'], '%_\\');
|
|
$query->whereLike('follow_users', '%' . $kw . '%');
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* @notes 获取列表
|
|
*/
|
|
public function lists(): array
|
|
{
|
|
$lists = $this->baseQuery()
|
|
->order('id', 'desc')
|
|
->limit($this->limitOffset, $this->limitLength)
|
|
->select()
|
|
->toArray();
|
|
|
|
$wxUserids = [];
|
|
foreach ($lists as $item) {
|
|
$raw = json_decode($item['follow_users'] ?? '[]', true);
|
|
if (!is_array($raw)) {
|
|
continue;
|
|
}
|
|
foreach ($raw as $fu) {
|
|
if (!is_array($fu)) {
|
|
continue;
|
|
}
|
|
$wx = trim((string) ($fu['userid'] ?? ''));
|
|
if ($wx !== '') {
|
|
$wxUserids[$wx] = true;
|
|
}
|
|
}
|
|
}
|
|
$wxUserids = array_keys($wxUserids);
|
|
$adminNameByWx = [];
|
|
if ($wxUserids !== []) {
|
|
$adminNameByWx = Admin::whereIn('work_wechat_userid', $wxUserids)->column('name', 'work_wechat_userid');
|
|
}
|
|
|
|
foreach ($lists as &$item) {
|
|
$followUsers = json_decode($item['follow_users'] ?? '[]', true);
|
|
$followUsers = is_array($followUsers) ? $followUsers : [];
|
|
foreach ($followUsers as &$fu) {
|
|
if (!is_array($fu)) {
|
|
continue;
|
|
}
|
|
$wx = trim((string) ($fu['userid'] ?? ''));
|
|
if ($wx !== '' && isset($adminNameByWx[$wx]) && $adminNameByWx[$wx] !== '') {
|
|
$fu['admin_name'] = $adminNameByWx[$wx];
|
|
}
|
|
}
|
|
unset($fu);
|
|
$item['follow_users'] = $followUsers;
|
|
$followAdminIds = json_decode($item['follow_admin_ids'] ?? '[]', true);
|
|
$item['follow_admin_ids'] = is_array($followAdminIds) ? $followAdminIds : [];
|
|
|
|
$fromDb = (int) ($item['external_first_add_time'] ?? 0);
|
|
$fromJson = CustomerLogic::minFollowCreatetime($followUsers);
|
|
$item['external_first_add_time'] = $fromDb > 0 ? $fromDb : $fromJson;
|
|
}
|
|
unset($item);
|
|
|
|
return $lists;
|
|
}
|
|
|
|
/**
|
|
* @notes 获取数量
|
|
*/
|
|
public function count(): int
|
|
{
|
|
return $this->baseQuery()->count();
|
|
}
|
|
}
|