396 lines
14 KiB
PHP
396 lines
14 KiB
PHP
<?php
|
||
|
||
namespace app\setting\service;
|
||
|
||
use AllowDynamicProperties;
|
||
use app\core\Service;
|
||
use app\channel\service\ProductService;
|
||
use app\channel\service\AccountService;
|
||
use app\channel\service\ChannelService;
|
||
use app\merchant\service\MerchantService;
|
||
use app\merchant\service\MerchantLogService;
|
||
use app\merchant\service\OrderLastHistoryService;
|
||
use app\merchant\service\OrderService;
|
||
use app\merchant\service\OrderHistoryService;
|
||
|
||
use app\setting\service\AccountService as TotalService;
|
||
use app\setting\service\MerchantAccountService;
|
||
|
||
|
||
/**
|
||
* 账户对账日志
|
||
* Class ChannelService
|
||
* @package app\channel\service
|
||
*/
|
||
#[AllowDynamicProperties] class AccountLogService extends Service
|
||
{
|
||
/**
|
||
* 设置默认操作表
|
||
* @var string
|
||
*/
|
||
public $table = 'stat_account_log';
|
||
|
||
public $type = array
|
||
(
|
||
1 => '渠道',
|
||
2 => '商户',
|
||
3 => '公户',
|
||
);
|
||
|
||
# 获取渠道的余额
|
||
public function getYue($cid, $day)
|
||
{
|
||
if (!$cid) {
|
||
$cid = 10040;
|
||
}
|
||
$channelService = ChannelService::instance();
|
||
$accountService = AccountService::instance();
|
||
|
||
$account_data = $channelService->call('account', $cid, $day);
|
||
if ($account_data) {
|
||
$v['add'] = $account_data['add'];
|
||
$v['dong'] = $account_data['dong'];
|
||
$v['yue'] = $account_data['account'];
|
||
$update = array();
|
||
$update['add'] = $v['add'];
|
||
$update['yue'] = $v['yue'];
|
||
if ($v['dong'] > 0) {
|
||
$update['dong'] = $v['dong'];
|
||
}
|
||
if (isset($account_data['zhang']) && $account_data['zhang']) {
|
||
$update['zhang'] = $account_data['zhang'];
|
||
}
|
||
|
||
$info = $accountService->db()->where(array('day' => $day, 'cid' => $cid))->find();
|
||
if ($info) {
|
||
$accountService->db()->where(array('id' => $info['id']))->update($update);
|
||
} else {
|
||
$update['cid'] = $cid;
|
||
$update['day'] = $day;
|
||
$accountService->db()->insert($update);
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
# 计算每日日志 每天凌晨12点10分执行
|
||
public function handle($day)
|
||
{
|
||
if (!$day) {
|
||
$day = date('Y-m-d', strtotime('-1 day'));
|
||
}
|
||
|
||
$this->merchantLogService = MerchantLogService::instance();
|
||
$accountService = AccountService::instance();
|
||
$merchantAccountService = MerchantAccountService::instance();
|
||
$channelService = ChannelService::instance();
|
||
$merchantService = MerchantService::instance();
|
||
$this->curOrderService = OrderService::instance();
|
||
|
||
$c = date('H');
|
||
/*
|
||
if ($c >= 2) {
|
||
$this->history = true;
|
||
$this->orderService = OrderHistoryService::instance();
|
||
} else {
|
||
$this->history = false;
|
||
$this->orderService = $this->curOrderService;
|
||
}
|
||
*/
|
||
|
||
$this->history = true;
|
||
// $this->orderService = OrderHistoryService::instance();
|
||
$this->orderService = OrderLastHistoryService::instance();
|
||
|
||
$begin = $day . ' 00:00:00';
|
||
$after = $day . ' 23:59:59';
|
||
$time = [$begin, $after];
|
||
|
||
# 获取昨天
|
||
$yday = $day . ' 00:00:00';
|
||
$yday = $this->maketime($yday);
|
||
$yday = date('Y-m-d', strtotime('-1 day', $yday));
|
||
|
||
$begin = $yday . ' 00:00:00';
|
||
$after = $yday . ' 23:59:59';
|
||
$stime = [$begin, $after];
|
||
|
||
# 获取明天
|
||
$mday = $day . ' 00:00:00';
|
||
$mday = $this->maketime($mday);
|
||
$mday = date('Y-m-d', strtotime('+1 day', $mday));
|
||
|
||
$begin = $mday . ' 00:00:00';
|
||
$after = $mday . ' 23:59:59';
|
||
$mtime = [$begin, $after];
|
||
|
||
# 先清空数据
|
||
$this->db()->where(array('day' => $day))->whereRaw('type < 3')->delete();
|
||
|
||
# 统计渠道的,直接从channel_account里取
|
||
|
||
$data = $accountService->db()->where(array('day' => $day))->select();
|
||
|
||
# 合计
|
||
$channel_shouru = $channel_zhichu = $channel_total = 0;
|
||
|
||
$type = 1;
|
||
if ($data) {
|
||
foreach ($data as $k => $v) {
|
||
$channel = $channelService->getInfo($v['cid']);
|
||
if ($channel) {
|
||
$yesterday = $accountService->db()->where(array('day' => $yday, 'cid' => $v['cid']))->find();
|
||
if (!$yesterday) {
|
||
$yesterday['yue'] = 0;
|
||
}
|
||
$this->up($day, '昨日余额', $type, $v['cid'], $channel['name'], 3, $yesterday['yue']);
|
||
$this->up($day, '当日加款', $type, $v['cid'], $channel['name'], 2, $v['add']);
|
||
|
||
$yes = $this->orderService->db()->where(array('cid' => $v['cid']))->whereRaw('status in(2,5)')->whereBetween('create_at', $time)->sum('cash');
|
||
$this->up($day, '成功金额', $type, $v['cid'], $channel['name'], 3, $yes);
|
||
|
||
$this->up($day, '冻结金额', $type, $v['cid'], $channel['name'], 3, $v['dong']);
|
||
$this->up($day, '当日余额', $type, $v['cid'], $channel['name'], 1, $v['yue']);
|
||
//$this->up($day, '待回款', $type, $v['cid'], $channel['name'], 2, $v['hui']);
|
||
|
||
if (isset($v['zhang']) && $v['zhang']) {
|
||
$zhang = $v['zhang'];
|
||
} else {
|
||
$zhang = $this->zhang(array('cid' => $v['cid']), 'product_cash', $time, $mtime, $stime, $yesterday['yue'], $v['add'], $v['dong']);
|
||
}
|
||
|
||
$this->up($day, '对账', $type, $v['cid'], $channel['name'], 3, $zhang);
|
||
|
||
$channel_shouru += $v['yue'];
|
||
//$channel_zhichu += $v['add'];
|
||
}
|
||
}
|
||
}
|
||
|
||
$channel_total = $channel_shouru - $channel_zhichu;
|
||
|
||
# 统计商户
|
||
|
||
# 从订单中分析出今天的商户
|
||
$merchant = array();
|
||
|
||
# 获取所有有余额的商户
|
||
//$merchant = $merchantService->db()->where(array('status' => 1, 'is_deleted' => 0))->whereRaw('account_surplus+credit_surplus+account_baitiao > 0 and id not in(2,6,26,34,42)')->order('status desc,id desc')->select();
|
||
//2,6,26,34,42
|
||
$no_account_merchant = sysconf('no_account_merchant');
|
||
$merchant = $merchantService->db()->where(array('status' => 1, 'is_deleted' => 0))->whereRaw('id not in('.$no_account_merchant.')')->order('status desc,id desc')->select();
|
||
|
||
# 合计
|
||
$merchant_shouru = $merchant_zhichu = $merchant_total = 0;
|
||
|
||
$type = 2;
|
||
|
||
if ($merchant) {
|
||
foreach ($merchant as $k => $v) {
|
||
$syue = $this->merchant_yue($v['id'], $stime);
|
||
$this->up($day, '昨日余额', $type, $v['id'], $v['name'], 3, $syue);
|
||
|
||
$add = $this->merchant_add($v['id'], $time);
|
||
|
||
$this->up($day, '当日加款', $type, $v['id'], $v['name'], 1, $add);
|
||
|
||
$yes = $this->orderService->db()->where(array('mid' => $v['id']))->whereRaw('status in(2,5)')->whereBetween('create_at', $time)->sum('cash');
|
||
|
||
$this->up($day, '成功金额', $type, $v['id'], $v['name'], 3, $yes);
|
||
|
||
$dong = $this->curOrderService->getDong($v['id'], $time);
|
||
$dong = $this->up($day, '冻结金额', $type, $v['id'], $v['name'], 3, $dong);
|
||
|
||
$yue = $this->merchant_yue($v['id'], $time);
|
||
|
||
$this->up($day, '当日余额', $type, $v['id'], $v['name'], 2, $yue);
|
||
|
||
$zhang = $this->zhang(array('mid' => $v['id']), 'actual_cash', $time, $mtime, $stime, $syue, $add, $dong);
|
||
|
||
$this->up($day, '对账', $type, $v['id'], $v['name'], 3, $zhang);
|
||
|
||
//$merchant_shouru += $add;
|
||
$merchant_zhichu += $yue;
|
||
}
|
||
}
|
||
|
||
$merchant_data = $merchantAccountService->db()->where(array('day' => $day))->select();
|
||
if ($merchant_data) {
|
||
foreach ($merchant_data as $k => $v) {
|
||
$merchant = $merchantService->getInfo($v['mid']);
|
||
if ($merchant) {
|
||
$yesterday = $merchantAccountService->db()->where(array('day' => $yday, 'mid' => $v['mid']))->find();
|
||
if (!$yesterday) {
|
||
$yesterday['yue'] = 0;
|
||
}
|
||
$this->up($day, '昨日余额', $type, $v['mid'], $merchant['name'], 3, $yesterday['yue']);
|
||
$this->up($day, '当日加款', $type, $v['mid'], $merchant['name'], 2, $v['add']);
|
||
|
||
$yes = $this->orderService->db()->where(array('mid' => $v['mid']))->whereRaw('status in(2,5)')->whereBetween('create_at', $time)->sum('cash');
|
||
$this->up($day, '成功金额', $type, $v['mid'], $merchant['name'], 3, $yes);
|
||
|
||
$dong = $v['dong'] ?: $this->curOrderService->getDong($v['mid'], $time);
|
||
$dong = $this->up($day, '冻结金额', $type, $v['mid'], $merchant['name'], 3, $dong);
|
||
$this->up($day, '当日余额', $type, $v['mid'], $merchant['name'], 1, $v['yue'] - $v['hui']);
|
||
|
||
$zhang = $this->zhang(array('mid' => $v['mid']), 'actual_cash', $time, $mtime, $stime, $yesterday['yue'], $v['add'], $dong);
|
||
|
||
$this->up($day, '对账', $type, $v['mid'], $merchant['name'], 3, $zhang);
|
||
|
||
$merchant_zhichu += $v['yue'];
|
||
}
|
||
}
|
||
}
|
||
|
||
$merchant_total = $merchant_shouru - $merchant_zhichu;
|
||
|
||
# 统计账户的总量
|
||
$account_yue = $this->db()->where(array('day' => $day, 'type' => 3, 'oper' => -1))->sum('num');
|
||
|
||
//$lday = str_replace('-', '', $day);
|
||
//$account_shouru = $this->db()->where(array('type' => 3, 'oper' => 1))->whereRaw('(replace(day, "-", "")+0) <=' . $lday)->sum('num');
|
||
|
||
//$account_zhichu = $this->db()->where(array('type' => 3, 'oper' => 2))->whereRaw('(replace(day, "-", "")+0) <=' . $lday)->sum('num');
|
||
|
||
$account_total = $account_yue;
|
||
$account_shouru = $this->db()->where(array('day' => $day, 'type' => 3, 'oper' => 1))->sum('num');
|
||
|
||
$account_zhichu = $this->db()->where(array('day' => $day, 'type' => 3, 'oper' => 2))->sum('num');
|
||
|
||
TotalService::instance()->up($day, $channel_shouru, $channel_zhichu, $channel_total, $merchant_shouru, $merchant_zhichu, $merchant_total, $account_shouru, $account_zhichu, $account_total);
|
||
|
||
}
|
||
|
||
# 获取对账数据
|
||
public function zhang($where, $col, $time, $mtime, $stime, $yue, $add, $dong)
|
||
{
|
||
if (isset($where['mid']) && $where['mid'] && $where['mid'] != 6 && $where['mid'] != 42) {
|
||
$zhang = $this->merchant_yes($where, $time);
|
||
} else {
|
||
$zhang = $this->orderService->db()->where($where)->whereRaw('status in(2,5)')->whereBetween('create_at', $time)->sum($col);
|
||
|
||
$mzhang = 0;
|
||
if ($this->history) {
|
||
$mzhang = $this->curOrderService->db()->where($where)->whereRaw('status in(2,5)')->whereBetween('create_at', $time)->sum($col);
|
||
if ($mzhang) {
|
||
$zhang += $mzhang;
|
||
}
|
||
/*
|
||
$mzhang = $this->curOrderService->db()->where($where)->whereRaw('status in(2,5)')->whereBetween('create_at', $time)->sum($col);
|
||
if ($mzhang) {
|
||
$zhang += $mzhang;
|
||
}
|
||
if (isset($where['mid']) && $where['mid']) {
|
||
$shouru = $this->merchant_error($where, $mtime, $time);
|
||
$zhichu = $this->merchant_error($where, $time, $stime);
|
||
$shouru -= $zhichu;
|
||
$zhang += $shouru;
|
||
}
|
||
*/
|
||
|
||
} else {
|
||
$zhang -= $dong;
|
||
}
|
||
}
|
||
|
||
$zhang = round($yue + $add - $zhang, 2);
|
||
|
||
return $zhang;
|
||
}
|
||
|
||
# 计算商户的的一天的余额
|
||
public function merchant_yue($mid, $time)
|
||
{
|
||
$time[0] = '2020-01-01 00:00:00';
|
||
$where['mid'] = $mid;
|
||
$where['account_type'] = 1;
|
||
$where['oper'] = 1;
|
||
$shouru = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$where['oper'] = 2;
|
||
$zhichu = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$yue = round($shouru - $zhichu, 2);
|
||
|
||
unset($where['oper']);
|
||
$where['account_type'] = 2;
|
||
$where['type'] = 3;
|
||
$kou = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$where['type'] = 4;
|
||
$huifu = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$where['type'] = 1;
|
||
$zonghuikuan = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$where['type'] = 2;
|
||
$zonghuifu = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$zonghuikuan -= $zonghuifu;
|
||
|
||
$hui = round($kou - $huifu - $zonghuikuan, 2);
|
||
|
||
return $yue - $hui;
|
||
}
|
||
|
||
# 计算商户隔天的失败数据
|
||
public function merchant_error($where, $mtime, $time)
|
||
{
|
||
$where['type'] = 4;
|
||
$shouru = $this->merchantLogService->getDb($where, $mtime, 'create_at', $time);
|
||
|
||
return $shouru;
|
||
}
|
||
|
||
# 计算商户当天的总成功
|
||
public function merchant_yes($where, $time)
|
||
{
|
||
$where['type'] = 3;
|
||
$kou = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$where['type'] = 4;
|
||
$huifu = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$kou -= $huifu;
|
||
|
||
return $kou;
|
||
}
|
||
|
||
# 计算商户的一天的加款
|
||
public function merchant_add($mid, $time)
|
||
{
|
||
$where['mid'] = $mid;
|
||
$where['type'] = 1;
|
||
$chongzhi = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$where['type'] = 2;
|
||
$jianshao = $this->merchantLogService->getDb($where, $time);
|
||
|
||
$add = round($chongzhi - $jianshao, 2);
|
||
|
||
return $add;
|
||
}
|
||
|
||
# 记录日志
|
||
public function up($day, $name, $type, $type_id, $type_name, $oper, $num, $desc = '')
|
||
{
|
||
if ($name == '冻结金额' && $type == 2) {
|
||
$state = DongService::instance()->up($day, $type_id, $num);
|
||
$num = 0;
|
||
}
|
||
$data['day'] = $day;
|
||
$data['type'] = $type;
|
||
$data['type_id'] = $type_id;
|
||
|
||
$data['type_name'] = $type_name;
|
||
$data['name'] = $name;
|
||
$data['oper'] = $oper;
|
||
$data['num'] = $num;
|
||
$data['desc'] = $desc;
|
||
|
||
$this->db()->insert($data);
|
||
|
||
return $num;
|
||
}
|
||
} |