|
@@ -5,6 +5,7 @@ namespace App\Console\Commands\Statistic;
|
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Support\Arr;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
class PromotionDayCharge extends Command
|
|
|
{
|
|
@@ -53,6 +54,7 @@ class PromotionDayCharge extends Command
|
|
|
$promotionDayCharge = $this->promotionDayCharge($item->promotion_id, $date);
|
|
|
$newUserCharge = $this->newUserCharge($item->promotion_id, $date);
|
|
|
$chargeInfo = $this->getPromotionData($promotionDayCharge, $newUserCharge);
|
|
|
+ $chargeInfo['new_user_num'] = $this->getNewUserNum($item->promotion_id, $date);
|
|
|
$promotionData[] = array_merge( $chargeInfo, [
|
|
|
'promotion_id' => $item->promotion_id, 'day_at' => $date,
|
|
|
'created_at' => $now, 'updated_at' => $now,
|
|
@@ -79,15 +81,80 @@ class PromotionDayCharge extends Command
|
|
|
DB::table('tj_optimizer_day_charge')
|
|
|
->insert($items->values()->toArray());
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ DB::table('promotions')
|
|
|
+ ->leftJoin('users', 'users.id', '=' , 'promotions.uid')
|
|
|
+ ->select('promotions.id', 'promotions.uid', 'promotions.miniprogram_id', 'users.pid as puid')
|
|
|
+ ->orderBy('promotions.id')
|
|
|
+ ->chunk(100, function ($items) use ($date){
|
|
|
+ $promotionIds = $items->pluck('id');
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+ $alreadyExistsPromotionIds = DB::table('tj_promotion_day_charge')
|
|
|
+ ->where('day_at', $date)
|
|
|
+ ->whereIn('promotion_id', $promotionIds)
|
|
|
+ ->select('promotion_id')
|
|
|
+ ->get()->pluck('promotion_id');
|
|
|
+ foreach ($items as $item) {
|
|
|
+ if($alreadyExistsPromotionIds->contains($item->id)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ DB::table('tj_promotion_day_charge')
|
|
|
+ ->insert([
|
|
|
+ 'promotion_id' => $item->id, 'day_at' => $date,
|
|
|
+ 'created_at' => $now, 'updated_at' => $now,
|
|
|
+ 'user_id' => $item->uid, 'puser_id' => $item->puid,
|
|
|
+ 'miniprogram_id' => $item->miniprogram_id
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ DB::table('users')
|
|
|
+ ->join('user_has_roles', 'users.id', 'user_has_roles.user_id')
|
|
|
+ ->where(['user_has_roles.role_id' => 2, 'users.deleted_at' => 0])
|
|
|
+ ->distinct()
|
|
|
+ ->select('users.id', 'users.pid')
|
|
|
+ ->orderBy('users.id')
|
|
|
+ ->chunk(50, function ($items) use($date) {
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $miniprogramIds = DB::table('user_has_miniprograms')
|
|
|
+ ->where('uid', $item->id)
|
|
|
+ ->where('is_enabled', 1)
|
|
|
+ ->select('miniprogram_id')
|
|
|
+ ->get()->pluck('miniprogram_id');
|
|
|
+ foreach ($miniprogramIds as $miniprogramId) {
|
|
|
+ if(DB::table('tj_optimizer_day_charge')
|
|
|
+ ->where([
|
|
|
+ 'day_at' => $date,
|
|
|
+ 'user_id' => $item->id, 'miniprogram_id' => $miniprogramId,
|
|
|
+ ])->exists()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ DB::table('tj_optimizer_day_charge')
|
|
|
+ ->insert([
|
|
|
+ 'day_at' => $date, 'user_id' => $item->id, 'miniprogram_id' => $miniprogramId,
|
|
|
+ 'puser_id' => $item->pid, 'created_at' => $now, 'updated_at' => $now
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- public function getPromotionData($promotionDayCharge, $newUserCharge, ) {
|
|
|
+ /**
|
|
|
+ * 某个推广链接在某天的新用户数量
|
|
|
+ * @param $promotionId 推广链接id
|
|
|
+ * @param $date 日期
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function getNewUserNum($promotionId, $date) {
|
|
|
+ return intval(Redis::hget(sprintf('promotion:newUserCount:%s', $date), $promotionId));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getPromotionData($promotionDayCharge, $newUserCharge) {
|
|
|
return [
|
|
|
'pay_money' => $promotionDayCharge['pay_money'] ?? 0,
|
|
|
'pay_count' => $promotionDayCharge['pay_count'] ?? 0,
|
|
|
- 'new_user_pay_money' => $newUserCharge['new_user_pay_money'] ?? 0,
|
|
|
- 'new_user_common_pay_money' => $newUserCharge['new_user_common_pay_money'] ?? 0,
|
|
|
- 'new_user_vip_pay_money' => $newUserCharge['new_user_vip_pay_money'] ?? 0,
|
|
|
'common_pay_money' => $promotionDayCharge['common_pay_money'] ?? 0,
|
|
|
'common_pay_uv' => $promotionDayCharge['common_pay_uv'] ?? 0,
|
|
|
'common_pay_count' => $promotionDayCharge['common_pay_count'] ?? 0,
|
|
@@ -96,6 +163,12 @@ class PromotionDayCharge extends Command
|
|
|
'vip_pay_uv' => $promotionDayCharge['vip_pay_uv'] ?? 0,
|
|
|
'vip_pay_count' => $promotionDayCharge['vip_pay_count'] ?? 0,
|
|
|
'vip_unpay_count' => $promotionDayCharge['vip_unpay_count'] ?? 0,
|
|
|
+ 'new_user_pay_money' => $newUserCharge['new_user_pay_money'] ?? 0,
|
|
|
+ 'new_user_common_pay_money' => $newUserCharge['new_user_common_pay_money'] ?? 0,
|
|
|
+ 'new_user_vip_pay_money' => $newUserCharge['new_user_vip_pay_money'] ?? 0,
|
|
|
+ 'new_user_pay_uv' => $newUserCharge['new_user_pay_uv'] ?? 0,
|
|
|
+ 'new_user_vip_pay_uv' => $newUserCharge['new_user_vip_pay_uv'] ?? 0,
|
|
|
+ 'new_user_common_pay_uv' =>$newUserCharge['new_user_common_pay_uv'] ?? 0,
|
|
|
];
|
|
|
}
|
|
|
|
|
@@ -109,15 +182,27 @@ class PromotionDayCharge extends Command
|
|
|
DB::raw("sum(if(status <> 'unpaid', price, 0)) as pay_money"),
|
|
|
// 普通支付金额
|
|
|
DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), price, 0)) as common_pay_money"),
|
|
|
+ // 累计充值人数
|
|
|
+ DB::raw("count(distinct if(status <> 'unpaid', concat(uid, ranse_created_at), null)) as pay_uv"),
|
|
|
+ // 普通支付人数
|
|
|
+ DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as common_pay_uv"),
|
|
|
+ // vip支付人数
|
|
|
+ DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as vip_pay_uv"),
|
|
|
)->first();
|
|
|
if($info) {
|
|
|
return [
|
|
|
- // 新用户支付总额
|
|
|
+ // 新增用户支付总额
|
|
|
'new_user_pay_money' => $info->pay_money,
|
|
|
- // 新用户普通支付总额
|
|
|
+ // 新增用户普通支付总额
|
|
|
'new_user_common_pay_money' => $info->common_pay_money,
|
|
|
- // 新用户会员支付总额
|
|
|
- 'new_user_vip_pay_money' => $info->pay_money - $info->common_pay_money
|
|
|
+ // 新增用户会员支付总额
|
|
|
+ 'new_user_vip_pay_money' => $info->pay_money - $info->common_pay_money,
|
|
|
+ // 新增用户在{$date}充值人数
|
|
|
+ 'new_user_pay_uv' => $info->pay_uv,
|
|
|
+ // 新增用户在{$date}vip充值支付人数
|
|
|
+ 'new_user_vip_pay_uv' => $info->vip_pay_uv,
|
|
|
+ // 新增用户在{$date}普通充值支付人数
|
|
|
+ 'new_user_common_pay_uv' => $info->vip_pay_uv,
|
|
|
];
|
|
|
} else {
|
|
|
return null;
|
|
@@ -128,6 +213,7 @@ class PromotionDayCharge extends Command
|
|
|
$info = DB::table('orders')
|
|
|
->where('promotion_id', $promotionId)
|
|
|
->whereBetween('created_at', [$date, $date. ' 23:59:59'])
|
|
|
+ ->where('ranse_created_at', '>', '2000-01-01')
|
|
|
->select(
|
|
|
// 未支付金额
|
|
|
DB::raw("sum(if(status = 'unpaid', price, 0)) as unpay_money"),
|
|
@@ -143,10 +229,13 @@ class PromotionDayCharge extends Command
|
|
|
DB::raw("sum(if(order_type in ('coin', 'first_coin'), 1, 0)) as common_count"),
|
|
|
// 普通支付金额
|
|
|
DB::raw("sum(if(order_type in ('coin', 'first_coin') and status <> 'unpaid', price, 0)) as common_pay_money"),
|
|
|
+ // NOTE!!!,uid, ranse_created_at 唯一确定一个用户
|
|
|
+ // 累计充值人数
|
|
|
+ DB::raw("count(distinct if(status <> 'unpaid', concat(uid, ranse_created_at), null)) as pay_uv"),
|
|
|
// 普通支付人数
|
|
|
- DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as common_pay_uv"),
|
|
|
+ DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as common_pay_uv"),
|
|
|
// vip支付人数
|
|
|
- DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as vip_pay_uv"),
|
|
|
+ DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as vip_pay_uv"),
|
|
|
// vip未支付笔数
|
|
|
DB::raw("sum(if(order_type not in ('coin', 'first_coin') and status = 'unpaid', 1, 0)) as vip_unpay_count"),
|
|
|
)->first();
|