173 lines
7.1 KiB
PHP
173 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace app\setting\service;
|
|
|
|
use app\core\Service;
|
|
use app\channel\service\ProductService;
|
|
use app\merchant\service\MerchantService;
|
|
use app\merchant\service\MerchantLogService;
|
|
use app\merchant\service\PercentService;
|
|
|
|
|
|
/**
|
|
* 统计服务
|
|
* Class ChannelService
|
|
* @package app\channel\service
|
|
*/
|
|
class StatService extends Service
|
|
{
|
|
/**
|
|
* 设置默认操作表
|
|
* @var string
|
|
*/
|
|
public $table = 'stat';
|
|
|
|
# 更新统计
|
|
public function up($day, $success, $fail, $total, $profit, $product_cash, $actual_cash, $data)
|
|
{
|
|
$where['day'] = $day;
|
|
$info = $this->db()->where($where)->find();
|
|
$update['success'] = $success;
|
|
$update['fail'] = $fail;
|
|
$update['total'] = $total;
|
|
$update['profit'] = $profit;
|
|
$update['product_cash'] = $product_cash;
|
|
$update['actual_cash'] = $actual_cash;
|
|
$update['data'] = json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
if (!$info) {
|
|
$update['day'] = $day;
|
|
$this->db()->insert($update);
|
|
} else {
|
|
unset($where['day']);
|
|
$where['id'] = $info['id'];
|
|
$update['create_at'] = date('Y-m-d H:i:s');
|
|
$this->db()->where($where)->update($update);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function total()
|
|
{
|
|
$where = array();
|
|
$date = input('date');
|
|
$result['total'] = $this->getDb('total', false, $date);
|
|
$result['success'] = $this->getDb('success', false, $date);
|
|
$result['profit'] = $this->getDb('profit', false, $date);
|
|
return $result;
|
|
}
|
|
|
|
public function getDb($col, $time, $date)
|
|
{
|
|
$db = $this->db();
|
|
if ($time) {
|
|
$db = $db->whereBetween('create_at', $time);
|
|
}
|
|
if ($date) {
|
|
$date = str_replace('-', '', $date);
|
|
$temp = explode('~', $date);
|
|
$db->whereRaw('(replace(day, "-", "")+0) >= '. $temp[0] . ' and (replace(day, "-", "")+0) <= ' . $temp[1]);
|
|
}
|
|
return $db->sum($col);
|
|
}
|
|
|
|
# 处理数据
|
|
public function handle($day, $data)
|
|
{
|
|
/*
|
|
$update = array();
|
|
$update['profit'] = 0;
|
|
# 计算渠道产品的利润
|
|
$productService = ProductService::instance();
|
|
$product_key = $rebate = array();
|
|
if ($data['product']) {
|
|
foreach ($data['product'] as $k => $v) {
|
|
$update['product'][$k]['num'] = (string) $v;
|
|
$update['product'][$k]['profit'] = 0;
|
|
if (isset($data['product_info'][$k]) && $data['product_info'][$k]) {
|
|
$info = $data['product_info'][$k];
|
|
} else {
|
|
$info = $productService->getInfo($k);
|
|
}
|
|
|
|
if ($info && $info['percent']) {
|
|
$product_key[$k] = $info['key'];
|
|
|
|
if (isset($info['percent_type']) && $info['percent_type'] == 2) {
|
|
$update['product'][$k]['percent'] = $info['percent'];
|
|
$update['product'][$k]['percent_type'] = $info['percent_type'];
|
|
$update['product'][$k]['profit'] = $info['percent']*$data['product_num'][$k];
|
|
} else {
|
|
$percent = 1-$info['percent'];
|
|
$update['product'][$k]['percent'] = $percent;
|
|
$update['product'][$k]['percent_type'] = 1;
|
|
$update['product'][$k]['profit'] = $v*$percent;
|
|
}
|
|
|
|
if ($update['product'][$k]['percent_type'] == 2) {
|
|
$update['profit'] -= $update['product'][$k]['profit'];
|
|
} else {
|
|
$update['profit'] += $update['product'][$k]['profit'];
|
|
}
|
|
|
|
|
|
$update['product'][$k]['percent'] = (string) $update['product'][$k]['percent'];
|
|
$update['product'][$k]['profit'] = (string) $update['product'][$k]['profit'];
|
|
}
|
|
}
|
|
}
|
|
|
|
# 计算商户的利润
|
|
$merchantService = MerchantService::instance();
|
|
$merchantLogService = MerchantLogService::instance();
|
|
$percentService = PercentService::instance();
|
|
if ($data['merchant']) {
|
|
foreach ($data['merchant'] as $k => $v) {
|
|
foreach ($v as $k1 => $v1) {
|
|
if (isset($product_key[$k1]) && $product_key[$k1]) {
|
|
$update['merchant'][$k][$k1]['num'] = (string) $v1;
|
|
$update['merchant'][$k][$k1]['profit'] = 0;
|
|
$info = $percentService->get($k, $product_key[$k1]);
|
|
if (!$info) {
|
|
$info = $merchantService->getInfo($k);
|
|
}
|
|
if ($info && $info['percent']) {
|
|
|
|
if (isset($info['percent_type']) && $info['percent_type'] == 2) {
|
|
$update['merchant'][$k][$k1]['percent'] = $info['percent'];
|
|
$update['merchant'][$k][$k1]['percent_type'] = $info['percent_type'];
|
|
$update['merchant'][$k][$k1]['profit'] = $info['percent']*$data['merchant_num'][$k][$k1];
|
|
} else {
|
|
$percent = 1-$info['percent'];
|
|
$update['merchant'][$k][$k1]['percent'] = $percent;
|
|
$update['merchant'][$k][$k1]['percent_type'] = 1;
|
|
$update['merchant'][$k][$k1]['profit'] = $v1*$percent;
|
|
}
|
|
|
|
} else {
|
|
continue;
|
|
}
|
|
# 获取返利
|
|
if (!isset($rebate[$k])) {
|
|
$rebate[$k] = $update['merchant'][$k][$k1]['rebate'] = $merchantLogService->get($k, $day);
|
|
$update['merchant'][$k][$k1]['profit'] += $update['merchant'][$k][$k1]['rebate'];
|
|
} else {
|
|
$update['merchant'][$k][$k1]['rebate'] = 0;
|
|
}
|
|
if ($update['merchant'][$k][$k1]['percent_type'] == 2) {
|
|
$update['profit'] += $update['merchant'][$k][$k1]['profit'];
|
|
} else {
|
|
$update['profit'] -= $update['merchant'][$k][$k1]['profit'];
|
|
}
|
|
|
|
$update['merchant'][$k][$k1]['percent'] = (string) $update['merchant'][$k][$k1]['percent'];
|
|
$update['merchant'][$k][$k1]['profit'] = (string) $update['merchant'][$k][$k1]['profit'];
|
|
$update['merchant'][$k][$k1]['rebate'] = (string) $update['merchant'][$k][$k1]['rebate'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
$this->up($day, $data['success'], $data['fail'], $data['total'], $data['profit'], $data['product_cash'], $data['actual_cash'], $data);
|
|
}
|
|
} |