门店端优化

This commit is contained in:
wong
2025-08-30 17:14:57 +08:00
parent b9d88160a2
commit 343c142d87
15 changed files with 147 additions and 76 deletions

View File

@@ -4,7 +4,8 @@ namespace app\store\controller;
use app\store\model\WechatFriendModel;
use app\store\model\WechatMessageModel;
use think\facade\Db;
use think\Db;
/**
* 数据统计控制器
@@ -18,8 +19,23 @@ class StatisticsController extends BaseController
{
try {
$companyId = $this->userInfo['companyId'];
$wechatAccountId = $this->device['wechatAccountId'];
$ownerWechatId = $this->device['wechatId'];
$userId = $this->userInfo['id'];
// 构建查询条件
$deviceIds = Db::name('device_user')->where(['userId' => $userId, 'companyId' => $companyId])->order('id DESC')->column('deviceId');
if (empty($deviceIds)) {
return errorJson('设备不存在');
}
$ownerWechatIds = [];
foreach ($deviceIds as $deviceId) {
$ownerWechatIds[] = Db::name('device_wechat_login')
->where(['deviceId' => $deviceId])
->order('id DESC')
->value('wechatId');
}
$wechatAccountIds = Db::table('s2_wechat_account')->whereIn('wechatId',$ownerWechatIds)->column('id');
// 获取时间范围
$timeRange = $this->getTimeRange();
@@ -33,37 +49,37 @@ class StatisticsController extends BaseController
// 1. 总客户数
$totalCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$totalCustomers = WechatFriendModel::whereIn('ownerWechatId',$ownerWechatIds)
->whereTime('createTime', '>=', $startTime)
->whereTime('createTime', '<', $endTime)
->count();
// 上期总客户数
$lastTotalCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$lastTotalCustomers = WechatFriendModel::whereIn('ownerWechatId',$ownerWechatIds)
->whereTime('createTime', '>=', $lastStartTime)
->whereTime('createTime', '<', $lastEndTime)
->count();
// 2. 新增客户数
$newCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$newCustomers = WechatFriendModel::whereIn('ownerWechatId',$ownerWechatIds)
->whereTime('createTime', '>=', $startTime)
->whereTime('createTime', '<', $endTime)
->count();
// 上期新增客户数
$lastNewCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$lastNewCustomers = WechatFriendModel::whereIn('ownerWechatId',$ownerWechatIds)
->whereTime('createTime', '>=', $lastStartTime)
->whereTime('createTime', '<', $lastEndTime)
->count();
//3. 互动次数
$interactionCount = WechatMessageModel::where(['wechatAccountId'=> $wechatAccountId])
$interactionCount = WechatMessageModel::whereIn('wechatAccountId', $wechatAccountIds)
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->count();
// 上期互动次数
$lastInteractionCount = WechatMessageModel::where(['wechatAccountId'=> $wechatAccountId])
$lastInteractionCount = WechatMessageModel::whereIn('wechatAccountId', $wechatAccountIds)
->where('createTime', '>=', $lastStartTime)
->where('createTime', '<', $lastEndTime)
->count();
@@ -103,10 +119,20 @@ class StatisticsController extends BaseController
{
try {
$companyId = $this->userInfo['companyId'];
$wechatAccountId = $this->device['wechatAccountId'];
$ownerWechatId = $this->device['wechatId'];
$userId = $this->userInfo['id'];
// 构建查询条件
$deviceIds = Db::name('device_user')->where(['userId' => $userId, 'companyId' => $companyId])->order('id DESC')->column('deviceId');
if (empty($deviceIds)) {
return errorJson('设备不存在');
}
$ownerWechatIds = [];
foreach ($deviceIds as $deviceId) {
$ownerWechatIds[] = Db::name('device_wechat_login')
->where(['deviceId' => $deviceId])
->order('id DESC')
->value('wechatId');
}
@@ -116,35 +142,35 @@ class StatisticsController extends BaseController
$endTime = $timeRange['end_time'];
// 1. 客户增长趋势数据
$totalCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$totalCustomers = WechatFriendModel::whereIn('ownerWechatId', $ownerWechatIds)
->whereTime('createTime', '<', $endTime)
->count();
$newCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$newCustomers = WechatFriendModel::whereIn('ownerWechatId', $ownerWechatIds)
->whereTime('createTime', '>=', $startTime)
->whereTime('createTime', '<', $endTime)
->count();
// 计算流失客户数假设超过30天未互动的客户为流失客户
$thirtyDaysAgo = strtotime('-30 days');
$lostCustomers = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$lostCustomers = WechatFriendModel::whereIn('ownerWechatId', $ownerWechatIds)
->where('createTime', '>', 0)
->where('deleteTime', '<', $thirtyDaysAgo)
->count();
// 2. 客户来源分布数据
// 朋友推荐
$friendRecommend = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$friendRecommend = WechatFriendModel::whereIn('ownerWechatId', $ownerWechatIds)
// ->whereIn('addFrom', [17, 1000017])
->count();
// 微信搜索
$wechatSearch = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$wechatSearch = WechatFriendModel::whereIn('ownerWechatId', $ownerWechatIds)
// ->whereIn('addFrom', [3, 15, 1000003, 1000015])
->count();
// 微信群
$wechatGroup = WechatFriendModel::where(['ownerWechatId'=> $ownerWechatId])
$wechatGroup = WechatFriendModel::whereIn('ownerWechatId', $ownerWechatIds)
// ->whereIn('addFrom', [14, 1000014])
->count();
@@ -202,9 +228,23 @@ class StatisticsController extends BaseController
public function getInteractionAnalysis()
{
try {
$companyId = $this->userInfo['companyId'];
$wechatAccountId = $this->device['wechatAccountId'];
$companyId = $this->userInfo['companyId'];
$userId = $this->userInfo['id'];
// 构建查询条件
$deviceIds = Db::name('device_user')->where(['userId' => $userId, 'companyId' => $companyId])->order('id DESC')->column('deviceId');
if (empty($deviceIds)) {
return errorJson('设备不存在');
}
$ownerWechatIds = [];
foreach ($deviceIds as $deviceId) {
$ownerWechatIds[] = Db::name('device_wechat_login')
->where(['deviceId' => $deviceId])
->order('id DESC')
->value('wechatId');
}
$wechatAccountIds = Db::table('s2_wechat_account')->whereIn('wechatId',$ownerWechatIds)->column('id');
// 获取时间范围
$timeRange = $this->getTimeRange();
$startTime = $timeRange['start_time'];
@@ -216,7 +256,7 @@ class StatisticsController extends BaseController
// 1. 互动频率分析
// 高频互动用户数每天3次以上
$highFrequencyUsers = WechatMessageModel::where(['wechatAccountId' => $wechatAccountId])
$highFrequencyUsers = WechatMessageModel::whereIn('wechatAccountId' , $wechatAccountIds)
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->field('wechatFriendId, COUNT(*) as count')
@@ -225,7 +265,7 @@ class StatisticsController extends BaseController
->count();
// 中频互动用户数每天1-3次
$midFrequencyUsers = WechatMessageModel::where(['wechatAccountId' => $wechatAccountId])
$midFrequencyUsers = WechatMessageModel::whereIn('wechatAccountId' , $wechatAccountIds)
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->field('wechatFriendId, COUNT(*) as count')
@@ -234,7 +274,7 @@ class StatisticsController extends BaseController
->count();
// 低频互动用户数仅有1次
$lowFrequencyUsers = WechatMessageModel::where(['wechatAccountId' => $wechatAccountId])
$lowFrequencyUsers = WechatMessageModel::whereIn('wechatAccountId' , $wechatAccountIds)
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->field('wechatFriendId, COUNT(*) as count')
@@ -245,41 +285,39 @@ class StatisticsController extends BaseController
// 2. 互动内容分析
// 文字消息数量
$textMessages = WechatMessageModel::where([
'wechatAccountId' => $wechatAccountId,
'msgType' => 1
])
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->whereIn('wechatAccountId' , $wechatAccountIds)
->count();
// 图片互动数量
$imgInteractions = WechatMessageModel::where([
'wechatAccountId' => $wechatAccountId,
'msgType' => 3
])
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->whereIn('wechatAccountId' , $wechatAccountIds)
->count();
// 群聊互动数量
$groupInteractions = WechatMessageModel::where([
'wechatAccountId' => $wechatAccountId,
'type' => 2
])
->where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->whereIn('wechatAccountId' , $wechatAccountIds)
->count();
// 产品咨询数量 (通过消息内容模糊查询)
$productInquiries = WechatMessageModel::where([
'wechatAccountId' => $wechatAccountId
])
->where('createTime', '>=', $startTime)
$productInquiries = WechatMessageModel::where('createTime', '>=', $startTime)
->where('createTime', '<', $endTime)
->where('content', 'like', '%产品%')
->whereOr('content', 'like', '%价格%')
->whereOr('content', 'like', '%购买%')
->whereOr('content', 'like', '%优惠%')
->whereIn('wechatAccountId' , $wechatAccountIds)
->count();
// 构建返回数据