REAPI/app/setting/service/ChannelAccountService.php

178 lines
7.1 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\setting\service;
use app\core\Service;
use app\channel\service\ProductService;
use app\channel\service\AccountService;
use app\merchant\service\MerchantService;
use app\merchant\service\MerchantLogService;
use app\merchant\service\OrderLastHistoryService;
use app\merchant\service\OrderLastweekHistoryService;
2024-09-29 15:43:18 +08:00
use app\merchant\service\OrderService;
use app\merchant\service\OrderHistoryService;
use dever\Log;
/**
* 渠道统计服务
* Class ChannelService
* @package app\channel\service
*/
class ChannelAccountService extends Service
{
/**
* 设置默认操作表
* @var string
*/
public $table = 'stat_chnanel_account';
# 更新统计
public function up($day, $cid, $pid, $percent, $order_total, $order_success, $order_fail, $order_channel_cash, $account_total = 0, $account_success = 0, $account_fail = 0, $account_product_success = 0, $account_profit = 0, $yue = 0, $channel_yue = 0, $yesterday_channel_yue = 0)
{
if ($cid != 10014) {
//return;
}
$where['cid'] = $cid;
$where['pid'] = $pid;
$where['day'] = $day;
$info = $this->db()->where($where)->find();
$update['percent'] = $percent;
$update['order_total'] = $order_total;
$update['order_success'] = $order_success;
$update['order_fail'] = $order_fail;
$update['order_channel_cash'] = $order_channel_cash;
$update['account_total'] = $account_total;
$update['account_success'] = $account_success;
$update['account_fail'] = $account_fail;
$update['account_product_success'] = $account_product_success;
$update['account_profit'] = $account_profit;
$update['yue'] = $yue;
$update['channel_yue'] = $channel_yue;
$update['yesterday_channel_yue'] = $yesterday_channel_yue;
//print_r($update);
if (!$info) {
$update['cid'] = $cid;
$update['pid'] = $pid;
$update['day'] = $day;
$this->db()->insert($update);
} else {
unset($where['cid']);
unset($where['pid']);
unset($where['day']);
$where['id'] = $info['id'];
$this->db()->where($where)->update($update);
}
return true;
}
# 统计数据
public function stat($day)
{
$product = ProductService::instance()->getAll();
Log::write('gateway', 'channel_stat', json_encode($product));
if ($product) {
$order_data = $account_data = array();
// $orderHistoryService = OrderHistoryService::instance();
$orderHistoryService = OrderLastweekHistoryService::instance();
2024-09-29 15:43:18 +08:00
$merchantLogService = MerchantLogService::instance();
$accountService = AccountService::instance();
foreach ($product as $k => $v) {
$order_data_temp = $orderHistoryService->getTotalData($day, $v['cid'], $v['id'],1,2);
2024-09-29 15:43:18 +08:00
if ($order_data_temp) {
if (isset($order_data[$v['cid']]) && $order_data[$v['cid']]) {
foreach ($order_data_temp as $k1 => $v1) {
$order_data[$v['cid']][$k1] += $order_data_temp[$k1];
}
} else {
$order_data[$v['cid']] = $order_data_temp;
}
$order_data[$v['cid']]['percent'] = $v['percent'];
}
$account_data_temp = $merchantLogService->getTotalData($day, $v['id']);
if ($account_data_temp) {
if (isset($account_data[$v['cid']]) && $account_data[$v['cid']]) {
foreach ($account_data[$v['cid']] as $k1 => $v1) {
$account_data[$v['cid']][$k1] += $account_data_temp[$k1];
}
} else {
$account_data[$v['cid']] = $account_data_temp;
}
}
}
if ($order_data) {
foreach ($order_data as $k => $v) {
if (isset($account_data[$k]) && $account_data[$k]) {
$c = $account_data[$k];
} else {
$c = false;
}
# 获取当天的折扣
$today = $this->db()->where(array('cid' => $k, 'day' => $day))->find();
if ($today && $today['percent']) {
$v['percent'] = $today['percent'];
}
# 计算渠道产品的利润
$v['order_channel_cash'] = $v['success'] * $v['percent'];
$old_day = date('Y-m-d', strtotime($day . '-1 day'));
$old_day = $this->db()->where(array('cid' => $k, 'day' => $old_day))->find();
if (!$old_day) {
$account_info = $accountService->db()->where(array('cid' => $k))->order('id asc')->find();
#todo 未查到记录是否需要写入?目前的逻辑没有写入,导致渠道订单统计永远是空的
if (!$account_info) {
continue;
}
# 得到初始余额
$yesterday_channel_yue = $account_info['num'];
# 得到当天的总充值余额
$yue = 0;
# 得到最新余额
$channel_yue = $yesterday_channel_yue;
} else {
# 得到昨天的余额
$yesterday_channel_yue = $old_day['channel_yue'];
# 得到当天的总充值余额
$yue = $accountService->db()->where(array('cid' => $k, 'day' => $day))->sum('num');
# 得到最新余额
$channel_yue = $yesterday_channel_yue + $yue - $c['product_success'];//$v['order_channel_cash']
}
if (!$channel_yue) {
$channel_yue = 0;
}
if ($channel_yue <= 0) {
//continue;
}
$this->handle($day, $k, 0, $v['percent'], $channel_yue, $v, $c, $yue, $yesterday_channel_yue);
}
}
}
}
# 处理数据
public function handle($day, $cid, $pid, $percent, $channel_yue, $order_data, $account_data, $yue, $yesterday_channel_yue)
{
if (!$account_data) {
$account_data['total'] = 0;
$account_data['success'] = 0;
$account_data['fail'] = 0;
$account_data['product_success'] = 0;
$account_data['profit'] = 0;
}
$this->up($day, $cid, $pid, $percent, $order_data['total'], $order_data['success'], $order_data['fail'], $order_data['order_channel_cash'], $account_data['total'], $account_data['success'], $account_data['fail'], $account_data['product_success'], $account_data['profit'], $yue, $channel_yue, $yesterday_channel_yue);
}
}