107 lines
3.3 KiB
PHP
107 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\merchant\service;
|
|
|
|
use app\core\Service;
|
|
use think\admin\extend\CodeExtend;
|
|
use app\merchant\service\MerchantService;
|
|
use app\merchant\service\MerchantLogService;
|
|
|
|
/**
|
|
* 商户返点服务
|
|
* Class MerchantService
|
|
* @package app\merchant\service
|
|
*/
|
|
class RebateService extends Service
|
|
{
|
|
/**
|
|
* 设置默认操作表
|
|
* @var string
|
|
*/
|
|
public $table = 'merchant_rebate';
|
|
private $total = array();
|
|
|
|
# 计算返点
|
|
public function getData()
|
|
{
|
|
# 获取所有的商户信息
|
|
$merchant = $this->app->db->name('merchant_list')->select()->toArray();
|
|
if ($merchant) {
|
|
foreach ($merchant as $k => $v) {
|
|
# 获取是否有分成设置
|
|
$set = $this->app->db->name('merchant_percent')->where(array('mid' => $v['id']))->select()->toArray();
|
|
if ($set) {
|
|
foreach ($set as $k1 => $v1) {
|
|
$this->set($v, $v['id'], $v1['percent'], $v1['product_key']);
|
|
}
|
|
} else {
|
|
$this->set($v, $v['id'], $v['percent']);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function set($merchant, $mid, $percent, $product_key = false)
|
|
{
|
|
$where['mid'] = $mid;
|
|
if ($product_key) {
|
|
$where['product_key'] = $product_key;
|
|
}
|
|
|
|
$data = $this->db()->where($where)->order('num desc')->select()->toArray();
|
|
if ($data) {
|
|
$cash = 0;
|
|
$is_yue = 1;
|
|
$setting = array();
|
|
foreach ($data as $k => $v) {
|
|
$total = $this->getTotal($v['mid'], $v['product_key']);
|
|
$v['num'] = $v['num'] * 10000;
|
|
if ($total >= $v['num']) {
|
|
|
|
//echo '成功金额:' . $total;
|
|
//echo "\r\n";
|
|
//echo '当前档位:' . $v['num'];
|
|
//echo "\r\n";
|
|
$cur = $percent - $v['percent'];
|
|
//echo '分成:' . $percent . " - " . $v['percent'] . ' = ' . $cur;
|
|
//echo "\r\n";
|
|
$cash = $total*$cur;
|
|
//echo '公式:' . $total . " * " . $cur . ' = ' . $cash;
|
|
//echo "\r\n";
|
|
$is_yue = isset($v['is_yue']) && $v['is_yue'] ? $v['is_yue'] : 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//print_r('最终返点:' . $cash);die;
|
|
if ($cash > 0) {
|
|
# 验证当天是否已经返点,如已返点,则无需再返
|
|
$check = MerchantLogService::instance()->check($mid, 5);
|
|
if (!$check) {
|
|
MerchantLogService::instance()->add($mid, $cash,2, 5, '满额返点', $is_yue);
|
|
MerchantService::instance()->get($merchant['appid'], false);
|
|
MerchantService::instance()->get($merchant['id'], false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getTotal($mid, $product_key)
|
|
{
|
|
$key = 't_' . $mid . '_' . $product_key;
|
|
if (!isset($this->total[$key])) {
|
|
$where = array();
|
|
$where['mid'] = $mid;
|
|
$where['product_key'] = $product_key;
|
|
$where['status'] = 2;
|
|
|
|
$whereRaw = 'to_days(now()) - to_days(create_at) = 1';
|
|
|
|
$this->total[$key] = $this->app->db->name('merchant_order_last_history')->where($where)->whereRaw($whereRaw)->sum('cash');
|
|
$this->total[$key] = $this->app->db->name('merchant_order_history')->where($where)->whereRaw($whereRaw)->sum('cash');
|
|
|
|
}
|
|
|
|
return $this->total[$key];
|
|
}
|
|
} |