439 lines
13 KiB
PHP
439 lines
13 KiB
PHP
<?php
|
|
|
|
namespace app\merchant\service;
|
|
|
|
use app\core\Service;
|
|
use app\merchant\service\OrderService;
|
|
use app\merchant\service\OrderHistoryService;
|
|
|
|
/**
|
|
* 商户账户日志服务
|
|
* Class MerchantLogService
|
|
* @package app\merchant\service
|
|
*/
|
|
class MerchantLogService extends Service
|
|
{
|
|
|
|
public $table = 'merchant_account_log';
|
|
/**
|
|
* 设置类型
|
|
* @var string
|
|
*/
|
|
public $type = array
|
|
(
|
|
1 => '后台增加',
|
|
2 => '后台减少',
|
|
3 => 'API使用',
|
|
4 => 'API恢复',
|
|
5 => '满额返点',
|
|
6 => '账户授信',
|
|
);
|
|
|
|
# 根据订单号查询数据
|
|
public function getOrderId($mid, $order_id)
|
|
{
|
|
$where['mid'] = $mid;
|
|
$where['order_id'] = $order_id;
|
|
return $this->db()->where($where)->order('id desc')->find();
|
|
}
|
|
|
|
# 检测是否有数据
|
|
public function check($mid, $type = 5)
|
|
{
|
|
$where = 'mid = '.$mid.' and type = '.$type.' and to_days(now()) - to_days(create_at) = 0';
|
|
return $this->db()->whereRaw($where)->find();
|
|
}
|
|
|
|
# 验证是否有授信
|
|
public function checkShouxin($mid, $type = 6)
|
|
{
|
|
$where = 'mid = '.$mid.' and type = '.$type;
|
|
$num = $this->db()->whereRaw($where)->sum('num');
|
|
|
|
return $num > 0;
|
|
}
|
|
|
|
# 更新数据
|
|
public function up($id, $data)
|
|
{
|
|
return $this->db()->where(array('id' => $id))->update($data);
|
|
}
|
|
|
|
# 插入LOG数据
|
|
public function add($mid, $num,$account_type = 1, $type = 1, $desc = '', $is_yue = 1, $order_id = false, $product_cash = 0, $profit = 0, $pid = 0, $account = true, $order_date = false, $create_at = false)
|
|
{
|
|
$data = [ 'mid' => $mid, 'num' => $num, 'account_type' => $account_type, 'type' => $type, 'desc' => $desc ];
|
|
|
|
if ($type == 2 || $type == 3) {
|
|
$data['oper'] = 2;
|
|
} else {
|
|
$data['oper'] = 1;
|
|
}
|
|
if ($is_yue) {
|
|
$data['is_yue'] = $is_yue;
|
|
}
|
|
if ($order_id) {
|
|
$data['order_id'] = $order_id;
|
|
}
|
|
if ($product_cash) {
|
|
$data['product_cash'] = $product_cash;
|
|
}
|
|
if ($profit) {
|
|
$data['profit'] = $profit;
|
|
}
|
|
if ($pid) {
|
|
$data['pid'] = $pid;
|
|
}
|
|
if (!$order_date) {
|
|
$data['order_date'] = date('Y-m-d H:i:s');
|
|
} else {
|
|
$data['order_date'] = $order_date;
|
|
}
|
|
if ($create_at) {
|
|
$data['create_at'] = $create_at;
|
|
}
|
|
$id = $this->db()->insertGetId($data);
|
|
if (!$id) {
|
|
return false;
|
|
}
|
|
if ($is_yue == 2) {
|
|
return $id;
|
|
}
|
|
if (!$account) {
|
|
return $id;
|
|
}
|
|
if ($this->upAccountNew($type, $id, $mid, $num, $account_type, $data['oper'])) return true;
|
|
/*
|
|
$account = $this->countAccount($type, $num, $data['mid'], $account_type, $id, $order_date);
|
|
//if ($num > $account['account_surplus'] && $type == 2) return false;
|
|
if ($account && $this->upAccount($account)) return true;
|
|
*/
|
|
return false;
|
|
}
|
|
|
|
# 更新账户
|
|
public function upAccountNew ($type, $logid, $id, $num, $account_type, $oper) {
|
|
$info = $this->app->db->name('MerchantList')->where('id', $id)->find();
|
|
if ($info) {
|
|
if ($account_type == 1) {
|
|
$profix = 'account';
|
|
} else {
|
|
$profix = 'credit';
|
|
}
|
|
if ($oper == 2) {
|
|
$num = $num*-1;
|
|
}
|
|
$data[$profix . '_surplus'] = $info[$profix . '_surplus'] + $num;
|
|
|
|
//$state = $this->app->db->name('MerchantList')->where(array('id' => $id, $profix . '_surplus' => $info[$profix . '_surplus']))->update($data);
|
|
|
|
$state = $this->app->db->name('MerchantList')->where(array('id' => $id))->inc($profix . '_surplus', $num)->update();
|
|
|
|
$update = array();
|
|
if ($type == 1) {
|
|
$update['account_total'] = $info['account_total'] + $num;
|
|
}
|
|
if ($type == 6) {
|
|
$update['credit_total'] = $info['credit_total'] + $num;
|
|
}
|
|
if ($update) {
|
|
$this->app->db->name('MerchantList')->where(array('id' => $id))->update($update);
|
|
}
|
|
|
|
|
|
if (!$state) {
|
|
//return $this->upAccountNew($type, $logid, $id, $num, $account_type, $oper);
|
|
}
|
|
|
|
if ($logid) {
|
|
$this->db()->where(array('id' => $logid))->update(array('yue' => $data[$profix . '_surplus'], 'qian_yue' => $info[$profix . '_surplus']));
|
|
}
|
|
|
|
|
|
return $state;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
# 统计账户
|
|
public function countAccount ($type, $num, $mid, $account_type = 1, $id = false, $order_date = false) {
|
|
$where['mid'] = $mid;
|
|
$where['account_type'] = $account_type;
|
|
$where['oper'] = 1;
|
|
$where['is_yue'] = 1;
|
|
|
|
if ($account_type == 1) {
|
|
$profix = 'account';
|
|
} else {
|
|
$profix = 'credit';
|
|
}
|
|
|
|
$data = [$profix . '_total' => 0 , $profix . '_consum' => 0 , 'id' => $mid];
|
|
|
|
$data[$profix . '_total'] = $this->db()->where($where)->sum('num');
|
|
|
|
$where['oper'] = 2;
|
|
$data[$profix . '_consum'] = $this->db()->where($where)->sum('num');
|
|
|
|
$data[$profix . '_surplus'] = round(($data[$profix . '_total'] - $data[$profix . '_consum']), 2);
|
|
|
|
/*
|
|
if ($type == 1 || $type == 2 || $type == 6) {
|
|
DayService::instance()->add($type, $mid, $order_date, $num, $account_type);
|
|
|
|
if ($type == 6) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if ($type == 4) {
|
|
$num = 0;
|
|
}
|
|
$data[$profix . '_surplus'] = OrderService::instance()->getYue($mid, $num, $account_type, $order_date);
|
|
*/
|
|
if ($id) {
|
|
$this->db()->where(array('id' => $id))->update(array('yue' => $data[$profix . '_surplus']));
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
# 更新账户
|
|
public function upAccount ($data) {
|
|
if ($this->app->db->name('MerchantList')->where('id',$data['id'])->update($data) !== false) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
# 获取账户类型
|
|
public function getAccountType($mid, $order_id)
|
|
{
|
|
$where['mid'] = $mid;
|
|
$where['order_id'] = $order_id;
|
|
$info = $this->db()->where($where)->order('id desc')->find();
|
|
if ($info) {
|
|
return $info['account_type'];
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
# 获取某个商户的返点
|
|
public function get($mid, $day, $type = 5)
|
|
{
|
|
if (!$day) {
|
|
$day = date('Y-m-d', strtotime('-1 day'));
|
|
}
|
|
$begin = $day . ' 00:00:00';
|
|
$num = $this->maketime($begin) + 86400;
|
|
$day = date('Y-m-d', $num);
|
|
|
|
$begin = $day . ' 00:00:00';
|
|
$after = $day . ' 23:59:59';
|
|
$time = [$begin, $after];
|
|
return $this->db()->whereRaw('mid = ' . $mid . ' and type = ' . $type . ' ')->whereBetween('create_at', $time)->sum('num');
|
|
}
|
|
|
|
public function total($mid, $merchant)
|
|
{
|
|
$where = array();
|
|
if ($mid) {
|
|
$where['mid'] = $mid;
|
|
}
|
|
|
|
$type = input('type');
|
|
if ($type) {
|
|
$where['type'] = $type;
|
|
}
|
|
|
|
$create_at = input('create_at');
|
|
$order_date = input('order_date');
|
|
if ($order_date) {
|
|
$create_at = $order_date;
|
|
}
|
|
if ($create_at) {
|
|
$temp = explode(' - ', $create_at);
|
|
$begin = $temp[0] . ' 00:00:00';
|
|
$after = $temp[1] . ' 23:59:59';
|
|
$time = [$begin, $after];
|
|
} else {
|
|
$time = false;
|
|
}
|
|
|
|
$where['account_type'] = 2;
|
|
$where['type'] = 6;
|
|
$result['credit_cash'] = $this->getDb($where, $time);
|
|
|
|
$where['type'] = 1;
|
|
$result['credit_zonghuikuan'] = $this->getDb($where, $time);
|
|
|
|
$where['type'] = 2;
|
|
$result['credit_zonghuifu'] = $this->getDb($where, $time);
|
|
|
|
$result['credit_zonghuikuan'] -= $result['credit_zonghuifu'];
|
|
|
|
$where['type'] = 3;
|
|
$result['credit_kou'] = $this->getDb($where, $time);
|
|
|
|
$where['type'] = 4;
|
|
$result['credit_huifu'] = $this->getDb($where, $time);
|
|
|
|
$result['credit_cg'] = $result['credit_kou'] - $result['credit_huifu'];
|
|
|
|
unset($where['type']);
|
|
$where['oper'] = 1;
|
|
$result['shouru'] = $this->getDb($where, $time);
|
|
|
|
$where['oper'] = 2;
|
|
$result['zhichu'] = $this->getDb($where, $time);
|
|
|
|
$result['credit_zongxiaofei'] = $result['zhichu'];
|
|
|
|
$result['credit_yue'] = round($result['shouru'] - $result['zhichu'], 2);
|
|
|
|
$result['credit_huikuan'] = round($result['credit_kou'] - $result['credit_huifu'] - $result['credit_zonghuikuan'], 2);
|
|
|
|
unset($where['oper']);
|
|
$where['account_type'] = 1;
|
|
$where['type'] = 1;
|
|
$result['chongzhi'] = $this->getDb($where, $time);
|
|
|
|
$where['type'] = 2;
|
|
$result['jianshao'] = $this->getDb($where, $time);
|
|
|
|
$result['chongzhi'] = $result['chongzhi'] - $result['jianshao'];
|
|
|
|
$where['type'] = 3;
|
|
$result['kou'] = $this->getDb($where, $time);
|
|
|
|
$where['type'] = 4;
|
|
$result['huifu'] = $this->getDb($where, $time);
|
|
|
|
$result['kou'] -= $result['huifu'];
|
|
|
|
unset($where['type']);
|
|
$where['oper'] = 1;
|
|
$result['shouru'] = $this->getDb($where, $time);
|
|
|
|
$where['oper'] = 2;
|
|
$result['zhichu'] = $this->getDb($where, $time);
|
|
|
|
$result['yue'] = round($result['shouru'] - $result['zhichu'], 2);
|
|
|
|
$result['baitiao'] = $merchant['account_baitiao'];
|
|
if ($result['yue'] < 0) {
|
|
//$result['baitiao'] += $result['yue'];
|
|
//$result['yue'] = 0;
|
|
}
|
|
|
|
# 冻结额度
|
|
$orderService = OrderService::instance();
|
|
|
|
$result['dong'] = $orderService->getDong($mid, $time);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getDb($where, $time, $col = 'create_at', $otime = false)
|
|
{
|
|
if (!$otime && $col == 'create_at') {
|
|
$order_date = input('order_date');
|
|
if ($order_date) {
|
|
$col = 'order_date';
|
|
}
|
|
}
|
|
|
|
$where['is_yue'] = 1;
|
|
$db = $this->db()->where($where);
|
|
if ($time) {
|
|
$db = $db->whereBetween($col, $time);
|
|
}
|
|
if ($otime) {
|
|
$db = $db->whereBetween('order_date', $otime);
|
|
}
|
|
return $db->sum('num');
|
|
}
|
|
|
|
# 根据order_id 查找 pid
|
|
public function getPidByOrderId()
|
|
{
|
|
$data = $this->db()->whereRaw('order_id is not null and pid = 0')->select()->toArray();
|
|
if ($data) {
|
|
$orderService = OrderService::instance();
|
|
$orderHistoryService = OrderHistoryService::instance();
|
|
$orderLastweekHistoryService = OrderLastweekHistoryService::instance();
|
|
$orderLastHistoryService = OrderLastHistoryService::instance();
|
|
$orderTwoHistoryService = OrderTwoHistoryService::instance();
|
|
|
|
foreach ($data as $k => $v) {
|
|
if ($v['order_id']) {
|
|
$info = $orderService->get($v['order_id']);
|
|
if (!$info) {
|
|
$info = $orderLastweekHistoryService->get($v['order_id']);
|
|
}
|
|
if (!$info) {
|
|
$info = $orderLastHistoryService->get($v['order_id']);
|
|
}
|
|
if (!$info) {
|
|
$info = $orderTwoHistoryService->get($v['order_id']);
|
|
}
|
|
if (!$info) {
|
|
$info = $orderHistoryService->get($v['order_id']);
|
|
}
|
|
|
|
if ($info && $info['pid']) {
|
|
$u['pid'] = $info['pid'];
|
|
$this->db()->where(array('id' => $v['id']))->update($u);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# 获取账目
|
|
public function getTotalData($day, $pid = false)
|
|
{
|
|
if (!$day) {
|
|
$day = date('Y-m-d', strtotime('-1 day'));
|
|
}
|
|
$begin = $day . ' 00:00:00';
|
|
$after = $day . ' 23:59:59';
|
|
$time = [$begin, $after];
|
|
$db = $this->db()->whereRaw('type in(3,4) ');
|
|
$where = array();
|
|
if ($pid) {
|
|
$where['pid'] = $pid;
|
|
}
|
|
if ($where) {
|
|
$db->where($where);
|
|
}
|
|
$data = $db->whereBetween('create_at', $time)->order('id asc')->select()->toArray();
|
|
|
|
$result = array();
|
|
if ($data) {
|
|
$result['success'] = 0;
|
|
$result['fail'] = 0;
|
|
$result['total'] = 0;
|
|
$result['product_success'] = 0;
|
|
$result['profit'] = 0;
|
|
$result['yue'] = 0;
|
|
|
|
foreach ($data as $k => $v) {
|
|
if ($v['type'] == 3) {
|
|
$result['total'] += $v['num'];
|
|
$result['product_success'] += $v['product_cash'];
|
|
$result['profit'] += $v['profit'];
|
|
} else {
|
|
$result['fail'] += $v['num'];
|
|
$result['product_success'] -= $v['product_cash'];
|
|
$result['profit'] -= $v['profit'];
|
|
}
|
|
}
|
|
|
|
$result['success'] = $result['total'] - $result['fail'];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
} |