121 lines
4.1 KiB
PHP
121 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace app\common\logic;
|
|
|
|
use app\common\model\finance\CapitalFlow;
|
|
use app\common\model\store_order\StoreOrder;
|
|
use app\common\model\user\User;
|
|
use app\common\model\vip_flow\VipFlow;
|
|
use app\common\service\pay\PayService;
|
|
use support\Log;
|
|
|
|
class BackLogic extends BaseLogic
|
|
{
|
|
public $payService=new PayService();
|
|
|
|
/**
|
|
* 返利
|
|
* @return void
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function backProfit()
|
|
{
|
|
//读取前3天的值
|
|
$startTime = strtotime(date('Y-m-d', strtotime('-3 days')));
|
|
$endTime = strtotime(date('Y-m-d'));
|
|
|
|
$result = VipFlow::where('create_time', '>=', $startTime)
|
|
->where('create_time', '<', $endTime)
|
|
->where('status', 0)
|
|
->where('type', 0)
|
|
->select();
|
|
//3余额 18采购款 7微信小程序 9微信条码 13 支付宝条码支付
|
|
foreach ($result as $row) {
|
|
switch ($row['pay_type']) {
|
|
case 3:
|
|
case 18:
|
|
$this->dealCapital($row['pay_type'],$row);
|
|
break;
|
|
case 7:
|
|
case 9:
|
|
$this->wechat_refund($row['pay_type'],$row);
|
|
break;
|
|
case 13:
|
|
$this->ali_pay_refund($row['pay_type'],$row);
|
|
break;
|
|
}
|
|
}
|
|
//7微信小程序 9微信条码 13 支付宝条码支付
|
|
|
|
|
|
}
|
|
|
|
public function dealCapital($pay_type, $order)
|
|
{
|
|
|
|
$user = User::where('id', $order['user_id'])->find();
|
|
if ($pay_type == 18) {
|
|
$category_title = 'system_purchase_add';
|
|
$title = '系统增加采购款';
|
|
$mark = '系统增加采购款';
|
|
$filed = 'purchase_funds';
|
|
$user->purchase_funds = bcadd($user['purchase_funds'], $order['number'], 2);
|
|
} else {
|
|
$category_title = 'system_balance_add';
|
|
$title = '系统增加余额';
|
|
$mark = '系统反余额冻结';
|
|
$filed = 'now_money';
|
|
$user->now_money = bcadd($user['now_money'], $order['number'], 2);
|
|
}
|
|
$user->save();
|
|
$newArr['uid'] = $order['user_id'];
|
|
$newArr['category'] = $category_title;
|
|
$newArr['link_type'] = 'order';
|
|
$newArr['link_id'] = $order['order_id'];
|
|
$newArr['amount'] = $order['number'];
|
|
$newArr['before_balance'] = $user[$filed];
|
|
$newArr['balance'] = bcadd($user[$filed], $order['number'], 2);
|
|
$newArr['create_time'] = date('Y-m-d H:i:s');
|
|
$newArr['type'] = 'in';
|
|
$newArr['title'] = $title . "{$order['number']}元";
|
|
$newArr['mark'] = $mark;
|
|
CapitalFlow::create($newArr);
|
|
}
|
|
|
|
public function wechat_refund($pay_type, $row){
|
|
$pay_price=StoreOrder::where('id',$row['order_id'])->value('pay_price');
|
|
$order = [
|
|
'out_trade_no' => (string)$row['order_sn'],
|
|
'out_refund_no' => time(),
|
|
'reason' => '待返还金返还',
|
|
'amount' => [
|
|
'refund' => bcmul($row['number'], 100),
|
|
'total' => bcmul($pay_price, 100),
|
|
'currency' => 'CNY',
|
|
],
|
|
];
|
|
try{
|
|
$this->payService->wechat->refund($order);
|
|
VipFlow::where('id',$row['id'])->update(['status'=>1]);
|
|
}catch (\Exception $e){
|
|
Log::error('微信退款失败'.$e->getMessage(),$row->toArray());
|
|
}
|
|
}
|
|
|
|
public function ali_pay_refund($pay_type, $row){
|
|
$order = [
|
|
'out_trade_no' => (string)$row['order_sn'],
|
|
'refund_reason' => '待返还金返还',
|
|
'refund_amount' => $row['number'],
|
|
];
|
|
try{
|
|
$this->payService->alipay->refund($order);
|
|
VipFlow::where('id',$row['id'])->update(['status'=>1]);
|
|
}catch (\Exception $e){
|
|
Log::error('支付宝退款失败'.$e->getMessage(),$row->toArray());
|
|
}
|
|
}
|
|
}
|