<?php namespace app\api\controller; use app\common\model\finance\CapitalFlow; use app\common\model\user\User; use app\common\model\vip_flow\VipFlow; class BackController extends BaseApiController { public $notNeedLogin = ['backProfit']; /** * 返利 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function backProfit() { //读取前3天的值 按照用户id和类型分下 加到对应的钱 $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) ->group('user_id, pay_type') ->field('user_id, pay_type, COUNT(*) as transaction_count, SUM(number) as total_amount') ->select()->toArray(); // 遍历查询结果并分类 现金不进入反的逻辑 //3余额 18采购款 7微信小程序 9微信条码 13 支付宝条码支付 $Balance = []; $Procurement = []; $WechatMiniPay = []; $WechatBarcodePay = []; $AliBarcodePay = []; foreach ($result as $row) { $payType = $row['pay_type']; $userId = $row['user_id']; $totalAmount = $row['total_amount']; switch ($payType) { case 3: $user_now_money = User::where( [ 'id' => $userId ] )->withTrashed()->value('now_money'); $Balance[] = [ 'id' => $userId, 'now_money' => bcadd($user_now_money, $totalAmount, 2), ]; break; case 7: $WechatMiniPay[] = [ 'id' => $userId, 'total_amount' => $totalAmount, ]; break; case 9: $WechatBarcodePay[] = [ 'id' => $userId, 'total_amount' => $totalAmount, ]; break; case 13: $AliBarcodePay[] = [ 'id' => $userId, 'total_amount' => $totalAmount, ]; break; case 18: $purchase_funds_money = User::where( [ 'id' => $userId ] )->withTrashed()->value('purchase_funds'); $Procurement[] = [ 'id' => $userId, 'purchase_funds' => bcadd($purchase_funds_money, $totalAmount, 2), ]; break; } } //入记录表的话查询后便利入 3余额 18采购款 if ($Balance) { (new User())->saveAll($Balance); $this->dealCapital($startTime, $endTime, 3); } if ($Procurement) { (new User())->saveAll($Procurement); $this->dealCapital($startTime, $endTime, 18); } //7微信小程序 9微信条码 13 支付宝条码支付 } public function dealCapital($startTime, $endTime, $pay_type) { $vipFrozen = VipFlow::where('create_time', '>=', $startTime) ->where('create_time', '<', $endTime) ->where('pay_type', $pay_type)->select()->toArray(); if ($pay_type == 18) { $category_title = 'system_purchase_add'; $title = '系统增加采购款'; $mark = '系统增加采购款'; $filed = 'purchase_funds'; } else { $category_title = 'system_balance_add'; $title = '系统增加余额'; $mark = '系统反余额冻结'; $filed = 'now_money'; } $newArr = []; foreach ($vipFrozen as $k => $value) { $user_funds = User::where('id', $value['user_id'])->value($filed); $newArr[$k]['uid'] = $value['user_id']; $newArr[$k]['category'] = $category_title; $newArr[$k]['link_type'] = 'order'; $newArr[$k]['link_id'] = $value['order_id']; $newArr[$k]['amount'] = $value['number']; $newArr[$k]['before_balance'] = $user_funds; $newArr[$k]['balance'] = bcadd($user_funds, $value['number'], 2); $newArr[$k]['create_time'] = date('Y-m-d H:i:s'); $newArr[$k]['type'] = 'in'; $newArr[$k]['title'] = $title . "{$value['number']}元"; $newArr[$k]['mark'] = $mark; } (new CapitalFlow())->saveAll($newArr); } }