|
|
@@ -0,0 +1,228 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 版权所有 2017~2021 LARAVEL研发中心
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 官方网站: http://www.laravel.cn
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | Author: laravel开发员 <laravel.qq.com>
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+
|
|
|
+namespace App\Services\Api;
|
|
|
+
|
|
|
+use App\Models\AccountLogModel;
|
|
|
+use App\Models\AgentModel;
|
|
|
+use App\Models\MemberModel;
|
|
|
+use App\Models\StoreCategoryModel;
|
|
|
+use App\Models\StoreModel;
|
|
|
+use App\Services\BaseService;
|
|
|
+use App\Services\ConfigService;
|
|
|
+use App\Services\RedisService;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 结算管理-服务类
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ * @package App\Services\Common
|
|
|
+ */
|
|
|
+class SettleService extends BaseService
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new AccountLogModel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态入口
|
|
|
+ * @return static|null
|
|
|
+ */
|
|
|
+ public static function make()
|
|
|
+ {
|
|
|
+ if (!self::$instance) {
|
|
|
+ self::$instance = (new static());
|
|
|
+ }
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商家收益结算
|
|
|
+ * @param $storeId
|
|
|
+ * @param $money 收益
|
|
|
+ * @param $order 订单数据
|
|
|
+ * @return array|false|int
|
|
|
+ */
|
|
|
+ public function storeBonus($storeId, $money, $order)
|
|
|
+ {
|
|
|
+ $orderNo = isset($order['order_no'])? $order['order_no'] : '';
|
|
|
+ if($money<=0 && $storeId<=0){
|
|
|
+ $this->error = '无商家佣金可结算';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $storeInfo = StoreModel::where(['id'=> $storeId,'mark'=>1])->first();
|
|
|
+ $balance = isset($storeInfo['balance'])? $storeInfo['balance'] : 0;
|
|
|
+ $storeUserId = isset($storeInfo['user_id'])? $storeInfo['user_id'] : 0;
|
|
|
+ if($storeUserId<=0){
|
|
|
+ $this->error = '商家账号错误';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!StoreModel::where(['id'=> $storeId])->update(['balance'=>DB::raw("balance + {$money}"),'income'=>DB::raw("income + {$money}"),'update_time'=>time()])){
|
|
|
+ $this->error = '收货错误,商家结算错误,请联系客服处理';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $log = [
|
|
|
+ 'user_id'=> $storeUserId,
|
|
|
+ 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
|
|
|
+ 'type'=> 7,
|
|
|
+ 'money'=> $money,
|
|
|
+ 'before_money'=> $balance,
|
|
|
+ 'date'=>date('Y-m-d'),
|
|
|
+ 'create_time'=>time(),
|
|
|
+ 'remark'=> '商家收益',
|
|
|
+ 'status'=>1
|
|
|
+ ];
|
|
|
+ if(!$id = $this->model->insertGetId($log)){
|
|
|
+ $this->error = '商家收益结算失败,请联系客服处理';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = ['id'=>$id,'store_id'=>$storeId,'bonus'=>$money];
|
|
|
+ if(env('APP_DEBUG')){
|
|
|
+ RedisService::set("caches:settle:{$orderNo}:store_{$storeId}", $result, 7200);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function agentBonus($userId, $money, $order, $parentId)
|
|
|
+ {
|
|
|
+ $orderNo = isset($order['order_no'])? $order['order_no'] : '';
|
|
|
+ if($money<=0 || $userId<=0 || $parentId<=0){
|
|
|
+ $this->error = '无收益可结算';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $parent = AgentModel::with(['user'])->where(['user_id'=> $parentId,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','balance','income','status'])
|
|
|
+ ->first();
|
|
|
+ $agentId = isset($parent['id'])? $parent['id'] : 0;
|
|
|
+ $balance = isset($parent['balance'])? $parent['balance'] : 0;
|
|
|
+ $parentInfo = isset($parent['user'])? $parent['user'] : [];
|
|
|
+ $parentOneId = isset($parentInfo['parent_id'])? $parentInfo['parent_id'] :0;
|
|
|
+
|
|
|
+
|
|
|
+ // 推荐消费者的佣金
|
|
|
+ $logs = [];
|
|
|
+ $agentDirectBonusRate = ConfigService::make()->getConfigByCode('agent_direct_bonus_rate', 0);
|
|
|
+ $agentDirectBonusRate = $agentDirectBonusRate>0 && $agentDirectBonusRate<100? $agentDirectBonusRate : 0;
|
|
|
+ $bonus = moneyFormat($agentDirectBonusRate * $money/100, 2);
|
|
|
+ if(empty($parent) || $bonus<=0 || $agentId<=0){
|
|
|
+ $this->error = '上级代理无效';
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!AgentModel::where(['id'=> $agentId])->update(['balance'=>DB::raw("balance + {$bonus}"),'income'=>DB::raw("income + {$bonus}"),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
|
|
|
+ $this->error = '推荐消费者收益结算错误,请联系客服处理';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $logs[] = [
|
|
|
+ 'user_id'=> $parentId,
|
|
|
+ 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
|
|
|
+ 'type'=> 9,
|
|
|
+ 'money'=> $bonus,
|
|
|
+ 'before_money'=> $balance,
|
|
|
+ 'date'=>date('Y-m-d'),
|
|
|
+ 'create_time'=>time(),
|
|
|
+ 'remark'=> '推广收益',
|
|
|
+ 'status'=>1
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ // 一级代理收益
|
|
|
+ $parentTwoId = 0;
|
|
|
+ $oneBonus = 0;
|
|
|
+ if($bonus>0 && $parentOneId){
|
|
|
+ $parentOne = AgentModel::with(['user'])->where(['user_id'=> $parentOneId,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','balance','income','status'])
|
|
|
+ ->first();
|
|
|
+ $oneId = isset($parentOne['id'])? $parentOne['id'] : 0;
|
|
|
+ $oneBalance = isset($parentOne['balance'])? $parentOne['balance'] : 0;
|
|
|
+ $oneInfo = isset($parentOne['user'])? $parentOne['user'] : [];
|
|
|
+ $parentTwoId = isset($oneInfo['parent_id'])? $oneInfo['parent_id'] :0;
|
|
|
+
|
|
|
+ $agentBonusLevel1Rate = ConfigService::make()->getConfigByCode('agent_bonus_level1_rate', 0);
|
|
|
+ $agentBonusLevel1Rate = $agentBonusLevel1Rate>0 && $agentBonusLevel1Rate<100? $agentBonusLevel1Rate : 0;
|
|
|
+ $oneBonus = moneyFormat($agentBonusLevel1Rate * $bonus/100, 2);
|
|
|
+ if(!AgentModel::where(['id'=> $oneId])->update(['balance'=>DB::raw("balance + {$oneBonus}"),'income'=>DB::raw("income + {$oneBonus}"),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
|
|
|
+ $this->error = '推荐代理收益结算错误,请联系客服处理';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $logs[] = [
|
|
|
+ 'user_id'=> $parentOneId,
|
|
|
+ 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
|
|
|
+ 'type'=> 8,
|
|
|
+ 'money'=> $oneBonus,
|
|
|
+ 'before_money'=> $oneBalance,
|
|
|
+ 'date'=>date('Y-m-d'),
|
|
|
+ 'create_time'=>time()+1,
|
|
|
+ 'remark'=> '一级代理收益',
|
|
|
+ 'status'=>1
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 二级代理收益
|
|
|
+ $twoBonus = 0;
|
|
|
+ if($bonus>0 && $parentTwoId){
|
|
|
+ $parentTwo = AgentModel::with(['user'])->where(['user_id'=> $parentTwoId,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','balance','income','status'])
|
|
|
+ ->first();
|
|
|
+ $twoId = isset($parentTwo['id'])? $parentTwo['id'] : 0;
|
|
|
+ $twoBalance = isset($parentTwo['balance'])? $parentTwo['balance'] : 0;
|
|
|
+
|
|
|
+ $agentBonusLevel2Rate = ConfigService::make()->getConfigByCode('agent_bonus_level2_rate', 0);
|
|
|
+ $agentBonusLevel2Rate = $agentBonusLevel2Rate>0 && $agentBonusLevel2Rate<100? $agentBonusLevel2Rate : 0;
|
|
|
+ $twoBonus = moneyFormat($agentBonusLevel2Rate * $bonus/100, 2);
|
|
|
+ if(!AgentModel::where(['id'=> $twoId])->update(['balance'=>DB::raw("balance + {$twoBonus}"),'income'=>DB::raw("income + {$twoBonus}"),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
|
|
|
+ $this->error = '推荐代理收益结算错误,请联系客服处理';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $logs[] = [
|
|
|
+ 'user_id'=> $parentTwoId,
|
|
|
+ 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
|
|
|
+ 'type'=> 8,
|
|
|
+ 'money'=> $twoBonus,
|
|
|
+ 'before_money'=> $twoBalance,
|
|
|
+ 'date'=>date('Y-m-d'),
|
|
|
+ 'create_time'=>time()+2,
|
|
|
+ 'remark'=> '二级代理收益',
|
|
|
+ 'status'=>1
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ if($logs && !$this->model->insert($logs)){
|
|
|
+ $this->error = '推荐代理收益结算错误,请联系客服处理';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = ['user_id'=>$userId,'total'=> $money,'bonus'=>$bonus,'parent_id'=>$parentId,'oneId'=>$parentOneId,'oneBonus'=>$oneBonus,'twoId'=>$parentTwoId,'twoBonus'=>$twoBonus];
|
|
|
+ if(env('APP_DEBUG')){
|
|
|
+ RedisService::set("caches:settle:{$orderNo}:agent_{$userId}", $result, 7200);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|