REAPI/app/merchant/service/MerchantLogHistoryService.php
2024-09-29 15:43:18 +08:00

365 lines
11 KiB
PHP

<?php
namespace app\merchant\service;
use think\facade\Db;
use app\core\Service;
use app\merchant\service\MerchantLogService;
use app\merchant\service\OrderService;
use app\merchant\service\OrderHistoryService;
/**
* 商户账户日志历史服务
* Class MerchantLogService
* @package app\merchant\service
*/
class MerchantLogHistoryService extends Service
{
public $table = 'merchant_account_log_history';
/**
* 设置类型
* @var string
*/
public $type = array
(
1 => '后台增加',
2 => '后台减少',
3 => 'API使用',
4 => 'API恢复',
5 => '满额返点',
6 => '账户授信',
);
# 迁移数据 可以每个月执行一次 处理历史数据
public function handle()
{
/*
$data = $this->db()->select()->toArray();
foreach ($data as $k => $v) {
unset($v['log_id']);
MerchantLogService::instance()->up($v['mid'], $v['account_type'], $v['type'], $v['num'], $v['desc']);
}
echo 11;die;
*/
$type = input('type', 2);
if ($type == 1) {
$day = date('Y-m-t', strtotime('-1 month'));
$temp = explode('-', $day);
$start = $temp[0] . '-' . $temp[1] . '-01';
$begin = $start . ' 00:00:00';
} else {
$day = date('Y-m-t', strtotime('-2 month'));
$begin = '2019-01-01 00:00:00';
}
$after = $day . ' 23:59:59';
$time = [$begin, $after];
$test = input('test', 2);
if ($test == 1) {
print_r($time);die;
}
# 暂停提单
//sysconf('settingStatus', 1);
$state = Db::execute('insert into merchant_account_log_history select distinct * from merchant_account_log where type != 6 and create_at >= "' . $begin . '" and create_at <= "' . $after . '"');
if ($state) {
$merchant = MerchantService::instance()->getAll();
foreach ($merchant as $k => $v) {
$this->upData($time, $v['id'], 1, 1);
$this->upData($time, $v['id'], 1, 2);
$this->upData($time, $v['id'], 1, 3);
$this->upData($time, $v['id'], 1, 4);
$this->upData($time, $v['id'], 1, 5);
$account = MerchantLogService::instance()->countAccount('','',$v['id'], 1);
MerchantLogService::instance()->upAccount($account);
$this->upData($time, $v['id'], 2, 1);
$this->upData($time, $v['id'], 2, 2);
$this->upData($time, $v['id'], 2, 3);
$this->upData($time, $v['id'], 2, 4);
$this->upData($time, $v['id'], 2, 5);
$account = MerchantLogService::instance()->countAccount('','',$v['id'], 2);
MerchantLogService::instance()->upAccount($account);
}
}
sysconf('settingStatus', 2);
echo 'ok';die;
}
public function upData($time, $mid, $account_type = 1, $type = 1)
{
# 获取当前类型下所有余额
$num = MerchantLogService::instance()->db()->whereRaw('mid = '.$mid.' and account_type = '.$account_type.' and type =' . $type)->whereBetween('create_at', $time)->sum('num');
if ($num != 0) {
$id = MerchantLogService::instance()->add($mid, $num, $account_type, $type, $desc = '优化合并余额', 1, false, 0, 0, 0, false);
if ($id) {
$state = MerchantLogService::instance()->db()->whereRaw('mid = '.$mid.' and account_type = '.$account_type.' and type =' . $type)->whereBetween('create_at', $time)->delete();
/*
if ($state) {
MerchantLogService::instance()->up($id, array('is_yue' => 1));
}
*/
}
}
}
/*
public function up($data, $mid, $account_type, $type)
{
Db::startTrans();
try {
$state = false;
$one = $this->db()->where(array('log_id' => $data['id']))->find();
if (!$one) {
$data['log_id'] = $data['id'];
unset($data['id']);
unset($data['is_type']);
$state = $this->db()->insert($data);
if ($state) {
$state = MerchantLogService::instance()->db()->where(array('id' => $data['log_id']))->delete();
if ($state) {
$state = MerchantLogService::instance()->up($mid, $account_type, $type, $data['num'], $desc = '优化合并余额');
}
}
if ($state) {
Db::commit();
} else {
Db::rollback();
}
}
return true;
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return false;
}
}
public function move()
{
$data = $this->db()->select()->toArray();
foreach ($data as $k => $v) {
unset($v['log_id']);
unset($v['id']);
$id = MerchantLogService::instance()->db()->insert($v);
}
return true;
}
*/
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();
$orderLastHistoryService = OrderLastHistoryService::instance();
$orderLastweekHistoryService = OrderLastweekHistoryService::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;
}
}