diff --git a/plugins/cultural-space/src/PluginApplication.php b/plugins/cultural-space/src/PluginApplication.php index 313c8a77..948a086d 100644 --- a/plugins/cultural-space/src/PluginApplication.php +++ b/plugins/cultural-space/src/PluginApplication.php @@ -81,6 +81,29 @@ class PluginApplication extends \app\common\services\PluginApplication{ ], ] ], + 'plugin_cultural_space_fund_pool' => [ + 'name' => '基金池', + 'permit' => 1, + 'menu' => 1, + 'icon' => '', + 'url' => 'plugin.cultural-space.admin.fund.index', + 'url_params' => '', + 'item' => 'plugin_cultural_space_fund', + 'parents' => ['cultural-space'], + 'child' => [ + // 权限补充 + 'plugin_cultural_space_fund_index' => [ + 'name' => '基金池', + 'url' => 'plugin.cultural-space.admin.fund.index', + 'url_params' => '', + 'permit' => 1, + 'menu' => 0, + 'icon' => '', + 'item' => 'plugin_cultural_space_fund_index', + 'parents' => ['cultural-space','plugin_cultural_space_fund'] + ], + ] + ], 'plugin_cultural_space_set' => [ 'name' => '基本设置', 'permit' => 1, diff --git a/plugins/cultural-space/src/admin/FundController.php b/plugins/cultural-space/src/admin/FundController.php new file mode 100644 index 00000000..eb3e4197 --- /dev/null +++ b/plugins/cultural-space/src/admin/FundController.php @@ -0,0 +1,49 @@ +GiveGoodsFundMoney(22,93); +// } + +// $result =(new CulturalSpace())->GiveGoodsFundMoney(22,93); + + $set = Setting::get('plugin.cultural_space_set'); + $result =(new CulturalSpace())->capitalIncrease($set); + + var_dump($result); + } + + public function index(){ + return view('Yunshop\CulturalSpace::fund.index')->render(); + } + + + /*** + * 获取基金统计 + * @return void + */ + public function getfund(){ + $result =CulturalFund::getfund(); + return $this->successJson('成功',$result); + } + + /*** + * 获取基金记录 + * @return void + */ + public function getfundRecord(){ + $search = request()->input('search'); + $result =CulturalOrderFundLog::getList($search); + return $this->successJson('成功',$result); + } +} \ No newline at end of file diff --git a/plugins/cultural-space/src/listener/OrderPaidListener.php b/plugins/cultural-space/src/listener/OrderPaidListener.php index 3cafa841..b8e90ce7 100644 --- a/plugins/cultural-space/src/listener/OrderPaidListener.php +++ b/plugins/cultural-space/src/listener/OrderPaidListener.php @@ -7,24 +7,18 @@ use app\common\events\order\AfterOrderPaidEvent; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Foundation\Bus\DispatchesJobs; use Yunshop\CulturalSpace\models\CulturalSpace; - +use Yunshop\CulturalSpace\models\CulturalFund; class OrderPaidListener{ use DispatchesJobs; - public function subscribe(Dispatcher $events){ $events->listen(AfterOrderPaidEvent::class, self::class . '@handle'); } - public function handle(AfterOrderPaidEvent $event){ date_default_timezone_set("PRC"); $model = $event->getOrderModel(); // 订单支付成功奖励贡献值 (new CulturalSpace())->buyGoodsGiveContribution($model->uid,$model->id); + (new CulturalSpace())->GiveGoodsFundMoney($model->uid,$model->id); } - - - - - } diff --git a/plugins/cultural-space/src/models/CulturalFund.php b/plugins/cultural-space/src/models/CulturalFund.php new file mode 100644 index 00000000..71639e03 --- /dev/null +++ b/plugins/cultural-space/src/models/CulturalFund.php @@ -0,0 +1,44 @@ + 'datetime:Y-m-d H:i:s', + 'updated_at' => 'datetime:Y-m-d H:i:s', + ]; + public function getfund(){ + $result = self::uniacid() + ->first(); + if(empty($result)){ + $set = Setting::get('plugin.cultural_space_set'); + $result=[ + 'uniacid'=>\YunShop::app()->uniacid, + 'last_fee'=>$set['fund_start_price'], + 'current_fee'=>$set['fund_start_price'], + 'fund_money'=>0, + 'voucher_number'=>0, + 'history_number'=>0, + 'created_at'=>time(), + 'updated_at'=>time() + ]; + self::insert($result); + } + return $result; + } + + /*** + * 保存数据 + * @param $data + * @return bool + * + */ + public function SaveData($data){ + $result = self::uniacid() + ->update($data); + return $result; + } +} \ No newline at end of file diff --git a/plugins/cultural-space/src/models/CulturalFundIncrease.php b/plugins/cultural-space/src/models/CulturalFundIncrease.php new file mode 100644 index 00000000..5073792d --- /dev/null +++ b/plugins/cultural-space/src/models/CulturalFundIncrease.php @@ -0,0 +1,13 @@ + 'datetime:Y-m-d H:i:s', + 'updated_at' => 'datetime:Y-m-d H:i:s', + ]; +} \ No newline at end of file diff --git a/plugins/cultural-space/src/models/CulturalOrderFundLog.php b/plugins/cultural-space/src/models/CulturalOrderFundLog.php new file mode 100644 index 00000000..7db4bb34 --- /dev/null +++ b/plugins/cultural-space/src/models/CulturalOrderFundLog.php @@ -0,0 +1,52 @@ + 'datetime:Y-m-d H:i:s', + 'updated_at' => 'datetime:Y-m-d H:i:s', + ]; + public function getList($search=[]){ + $where = []; + if($search['member_id'] > 0) $where[] = ['member_id','=',$search['member_id']]; + $result = self::uniacid() + ->where($where) + ->with([ + 'member' => function($query){ + $query->select(['uid','nickname','realname','avatar']); + } + ]) + ->orderBy('created_at','DESC') + ->orderBy('id','DESC') + ->paginate(10); + return $result ? $result->toArray() : []; + } + + + /*** + * 写入日记 + * @param $data + * @return BaseModel + */ + public static function InsertLog($data){ + $data['uniacid'] = \YunShop::app()->uniacid; + $data['created_at'] = time(); + $data['updated_at'] = time(); + return self::insert($data); + } + + /** + * Common: 一对一关联 用户信息 + * Author: wu-hui + * Time: 2023/11/03 9:35 + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function member(){ + return $this->hasOne(Member::class,'uid','member_id'); + } +} \ No newline at end of file diff --git a/plugins/cultural-space/src/models/CulturalSpace.php b/plugins/cultural-space/src/models/CulturalSpace.php index 6143698e..e0c15358 100644 --- a/plugins/cultural-space/src/models/CulturalSpace.php +++ b/plugins/cultural-space/src/models/CulturalSpace.php @@ -1,15 +1,13 @@ 0) $where[] = ['uid','=',$search['uid']]; + if ($search['uid'] > 0) $where[] = ['uid', '=', $search['uid']]; // 查询model $model = self::uniacid() ->where($where) ->with([ - 'member' => function($query){ - $query->select(['uid','nickname','realname','avatar']); + 'member' => function ($query) { + $query->select(['uid', 'nickname', 'realname', 'avatar']); } ]); // 信息获取 $totalContribution = $model->sum('contribution'); - $list = $model->select(['id','uid','contribution']) - ->orderBy('id','DESC') + $list = $model->select(['id', 'uid', 'contribution']) + ->orderBy('id', 'DESC') ->paginate(10) ->toArray(); - foreach($list['data'] as &$item){ - $item['total_proportion'] = (float)sprintf("%.2f",$totalContribution); - $item['ratio'] = (float)sprintf("%.2f",$item['contribution'] / $totalContribution * 100); + foreach ($list['data'] as &$item) { + $item['total_proportion'] = (float)sprintf("%.2f", $totalContribution); + $item['ratio'] = (float)sprintf("%.2f", $item['contribution'] / $totalContribution * 100); } return $list; @@ -61,47 +60,48 @@ class CulturalSpace extends BaseModel{ * @param $uid * @param $orderId */ - public function buyGoodsGiveContribution($uid,$orderId){ - \Log::debug('文创空间 - 购买商品奖励贡献值',['uid' => $uid,'order_id' => $orderId]); + public function buyGoodsGiveContribution($uid, $orderId) + { + \Log::debug('文创空间 - 购买商品奖励贡献值', ['uid' => $uid, 'order_id' => $orderId]); DB::beginTransaction(); - try{ + try { $set = Setting::get('plugin.cultural_space_set'); - if($set['is_give_contribution'] != 1 || (float)$set['contribution_ratio'] <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 未开启贡献值奖励或者奖励比例为0'); + if ($set['is_give_contribution'] != 1 || (float)$set['contribution_ratio'] <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 未开启贡献值奖励或者奖励比例为0'); // 获取直推上级id && 获取订单商品信息 $parentUid = (int)MemberParent::getParentId($uid); $orderGoodsList = (array)$this->getGoodsModel($orderId) - ->where('yz_goods_cultural_space.is_give_contribution',1) + ->where('yz_goods_cultural_space.is_give_contribution', 1) ->get() - ->makeHidden(['buttons','after_sales','order']) + ->makeHidden(['buttons', 'after_sales', 'order']) ->toArray(); - if(count($orderGoodsList) <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 无奖励商品信息'); + if (count($orderGoodsList) <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 无奖励商品信息'); // 获取文创空间用户信息 - $memberList = $parentUid <= 0 ? $this->getCulturalSpace([$uid]) : $this->getCulturalSpace([$uid,$parentUid]); - $userName = Member::getMemberById($uid,['realname','nickname'])->username ?? ''; + $memberList = $parentUid <= 0 ? $this->getCulturalSpace([$uid]) : $this->getCulturalSpace([$uid, $parentUid]); + $userName = Member::getMemberById($uid, ['realname', 'nickname'])->username ?? ''; // 循环商品处理奖励贡献值 $changeList = []; - foreach($orderGoodsList as $goodsInfo){ + foreach ($orderGoodsList as $goodsInfo) { // 计算奖励贡献值 - $contribution = (float)sprintf('%.2f',$goodsInfo['payment_amount'] * (float)$set['contribution_ratio'] / 100); - if($contribution > 0){ - foreach($memberList as $memberInfo){ + $contribution = (float)sprintf('%.2f', $goodsInfo['payment_amount'] * (float)$set['contribution_ratio'] / 100); + if ($contribution > 0) { + foreach ($memberList as $memberInfo) { // 用户当前持有数量 $changeFront = (float)$memberList[$memberInfo['uid']]['contribution']; // 变更后的数量 - $changeAfter = (float)sprintf("%.2f",$changeFront + $contribution); + $changeAfter = (float)sprintf("%.2f", $changeFront + $contribution); // 记录变更信息 $changeList[] = [ - 'uniacid' => $goodsInfo['uniacid'], - 'uid' => $memberInfo['uid'], - 'goods_id' => $goodsInfo['goods_id'], - 'order_id' => $orderId, - 'order_goods_id' => $goodsInfo['id'], - 'change_type' => 1, + 'uniacid' => $goodsInfo['uniacid'], + 'uid' => $memberInfo['uid'], + 'goods_id' => $goodsInfo['goods_id'], + 'order_id' => $orderId, + 'order_goods_id' => $goodsInfo['id'], + 'change_type' => 1, 'change_quantity' => $contribution, - 'change_front' => $changeFront, - 'change_after' => $changeAfter, - 'remark' => ($memberInfo['uid'] == $uid ? '' : "【{$userName}】") . "购买商品【{$goodsInfo['title']}】赠送", - 'created_at' => time(), + 'change_front' => $changeFront, + 'change_after' => $changeAfter, + 'remark' => ($memberInfo['uid'] == $uid ? '' : "【{$userName}】") . "购买商品【{$goodsInfo['title']}】赠送", + 'created_at' => time(), ]; // 刷新持有处理 $memberList[$memberInfo['uid']]['contribution'] = $changeAfter; @@ -109,19 +109,129 @@ class CulturalSpace extends BaseModel{ } } // 数据操作 - if(count($changeList) > 0) { - $this->batchUpdate($memberList,'uid','uid'); + if (count($changeList) > 0) { + $this->batchUpdate($memberList, 'uid', 'uid'); ContributionLog::insert($changeList); } DB::commit(); - }catch(\Exception $e){ - \Log::debug('文创空间 - 购买商品奖励贡献值 - 错误抛出',$e->getMessage()); + } catch (\Exception $e) { + \Log::debug('文创空间 - 购买商品奖励贡献值 - 错误抛出', $e->getMessage()); DB::rollBack(); } - - return; } + + /*** + * 计算基金比例 + * @param $uid + * @param $orderId + * @return void + */ + public function GiveGoodsFundMoney($uid, $orderId) + { + DB::beginTransaction(); + try { + $set = Setting::get('plugin.cultural_space_set'); + if ($set['is_fund_open'] == 1) { + // 获取直推上级id && 获取订单商品信息 + $orderGoodsList = $this->getGoodsModel($orderId) + ->where('yz_goods_cultural_space.is_fund_open', 1) + ->get() + ->makeHidden(['buttons', 'after_sales', 'order']) + ->toArray(); + if (count($orderGoodsList) <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 无奖励商品信息'); + $current_fee = $set['fund_start_price']; //当前价格 + $last_fee = $current_fee; + $fund_money = 0; + $voucher_number = 0; + $history_number = 0; + $history_fund_money = 0; + $fundInfo = CulturalFund::getfund(); + if ($fundInfo) { + $current_fee = $fundInfo['current_fee']; + $last_fee = $current_fee; + $fund_money = $fundInfo['fund_money']; + $voucher_number = $fundInfo['voucher_number']; + $history_number = $fundInfo['history_number']; + $history_fund_money = $fundInfo['history_fund_money']; + } + $user_voucher_number = 0;//用户凭证数量 + $user_voucher_total = 0;//用户总计凭证数量 + foreach ($orderGoodsList as $goodsInfo) { + $user_voucher_number = (($goodsInfo['payment_amount'] * $set['user_fund_ratio']) / 100) / $current_fee; //赠送数量 + $user_fund_money = round(($goodsInfo['payment_amount'] * $set['order_fund_ratio']) / 100, 2);//资金数量 + $fund_money += $user_fund_money; + $user_voucher_total += $user_voucher_number; + $voucher_number += $user_voucher_number; + $history_number += $user_voucher_number; + $history_fund_money += $user_fund_money; + $data_log = [ + 'member_id' => $uid, + 'order_id' => $orderId, + 'goods_id' => $goodsInfo['goods_id'], + 'good_name' => $goodsInfo['title'], + 'pay_money' => $goodsInfo['payment_amount'], + 'fund_money' => $user_fund_money, + 'voucher_number' => $user_voucher_number, + 'current_fee' => $current_fee, + ]; + CulturalOrderFundLog::InsertLog($data_log); //写入日记 + } + $this->where('uid', $uid)->increment('voucher_number', $user_voucher_total);//用户增量 + $next_fee = round($fund_money / $voucher_number, 2); + $FundData = [ + 'last_fee' => $last_fee, + 'current_fee' => $next_fee, + 'fund_money' => $fund_money, + 'voucher_number' => $voucher_number, + 'history_number' => $history_number, + 'history_fund_money' => $history_fund_money, + ]; + $culturalFund = new CulturalFund(); + $culturalFund->SaveData($FundData); + if ($user_voucher_number <= $set['min_number']) { + $this->capitalIncrease($set);//小于数量增加积分倍数 + } + // 获取文创空间用户信息 + // 数据操作 + DB::commit(); + } else { + DB::rollBack(); + } + } catch (\Exception $e) { + DB::rollBack(); + } + } + + /*** + * 增加资本 + * @return void + */ + public function capitalIncrease($config){ + $uniacid=\YunShop::app()->uniacid; + $this->where('uniacid', $uniacid) ->update( + [ + 'voucher_number' => DB::raw('voucher_number * '.$config['fund_multiple']), + ] + ); //增加会员资本 + $culturalFund = new CulturalFund(); + $data=$culturalFund->where('uniacid', $uniacid)->first()->toArray(); + $data['up_voucher_number']=$data['voucher_number']*$config['fund_multiple']; + $data['up_current_fee']=$data['current_fee']/$config['fund_multiple']; + $data['fund_multiple']=$config['fund_multiple']; + $data['created_at']=time(); + $data['updated_at']=time(); + unset($data['id']); + DB::table('yz_cultural_fund_increase_log')->insert($data); + $culturalFund->where('uniacid', $uniacid)->update( //增加资本数量 + [ + 'voucher_number' => DB::raw('voucher_number * '.$config['fund_multiple']), + 'history_number' => DB::raw('history_number * '.$config['fund_multiple']), + 'current_fee' => DB::raw('current_fee /'.$config['fund_multiple']), + ] + ); + } + /** * Common: 获取订单商品查询model * Author: wu-hui @@ -129,7 +239,8 @@ class CulturalSpace extends BaseModel{ * @param $orderId * @return mixed */ - private function getGoodsModel($orderId){ + private function getGoodsModel($orderId) + { return OrderGoods::uniacid() ->select([ 'yz_order_goods.id', @@ -140,9 +251,10 @@ class CulturalSpace extends BaseModel{ 'yz_order_goods.payment_amount', ]) ->leftJoin('yz_goods_cultural_space', 'yz_goods_cultural_space.goods_id', 'yz_order_goods.goods_id') - ->where('yz_order_goods.order_id',$orderId) - ->where('yz_goods_cultural_space.is_open',1); + ->where('yz_order_goods.order_id', $orderId) + ->where('yz_goods_cultural_space.is_open', 1); } + /** * Common: 根据用户ids获取用户文创空间相关信息(如果不存在则添加默认信息并且返回默认信息) * Author: wu-hui @@ -150,51 +262,49 @@ class CulturalSpace extends BaseModel{ * @param $ids * @return array */ - private function getCulturalSpace($ids){ + private function getCulturalSpace($ids) + { // 获取已经存在的信息 $list = self::uniacid() - ->select(['id','uid','contribution']) - ->whereIn('uid',$ids) + ->select(['id', 'uid', 'contribution']) + ->whereIn('uid', $ids) ->get() ->keyBy('uid') ->toArray(); // 循环处理:不存在则添加,并且赋值默认值;存在则使用已经存在的信息 $memberList = []; $insertData = []; - foreach($ids as $userId){ - if($list[$userId]){ + foreach ($ids as $userId) { + if ($list[$userId]) { // 存在 $memberList[$userId] = $list[$userId]; - }else{ + } else { // 不存在 $insertData[] = [ 'uniacid' => \YunShop::app()->uniacid, - 'uid' => $userId, + 'uid' => $userId, ]; $memberList[$userId] = [ - 'uniacid' => \YunShop::app()->uniacid, - 'uid' => $userId, + 'uniacid' => \YunShop::app()->uniacid, + 'uid' => $userId, 'contribution' => 0 ]; } } - if($insertData) self::insert($insertData); + if ($insertData) self::insert($insertData); return $memberList; } - - - - /** * Common: 一对一关联 用户信息 * Author: wu-hui * Time: 2023/11/03 9:35 * @return \Illuminate\Database\Eloquent\Relations\HasOne */ - public function member(){ - return $this->hasOne(Member::class,'uid','uid'); + public function member() + { + return $this->hasOne(Member::class, 'uid', 'uid'); } } diff --git a/plugins/cultural-space/src/models/GoodsCulturalSpace.php b/plugins/cultural-space/src/models/GoodsCulturalSpace.php index e0166616..3cace80a 100644 --- a/plugins/cultural-space/src/models/GoodsCulturalSpace.php +++ b/plugins/cultural-space/src/models/GoodsCulturalSpace.php @@ -22,7 +22,7 @@ class GoodsCulturalSpace extends BaseModel{ $info->goods_id = $goodsId;// 商品ID $info->is_open = $data['is_open'] ?? 0;// 当前商品是否参与文创空间:0=未开启,1=开启 $info->is_give_contribution = $data['is_give_contribution'] ?? 0;// 是否赠送贡献值:0=不赠送,1=赠送 - + $info->is_fund_open = $data['is_fund_open'] ?? 0;// 是否开启基金 return $info->save(); diff --git a/plugins/cultural-space/views/fund/index.blade.php b/plugins/cultural-space/views/fund/index.blade.php new file mode 100644 index 00000000..d80b19aa --- /dev/null +++ b/plugins/cultural-space/views/fund/index.blade.php @@ -0,0 +1,235 @@ +@extends('layouts.base') + +@section('content') +
+
+
+ + + + +
基金总数:[[fundData.fund_money || 0]]
+
+
凭证总数:[[fundData.voucher_number || 0]]
+
当前价值:[[fundData.current_fee || 0]]
+
上次价值:[[fundData.last_fee || 0]]
+
历史基金:[[fundData.history_fund_money || 0]]
+
历史凭证:[[fundData.history_number || 0]]
+
+
+ + {{--搜索--}} + + + + + + 查询 + + + {{--表单--}} + + + + + + + + + + + +
+ {{--分页--}} + + +
+
+
+ + +@endsection + diff --git a/plugins/cultural-space/views/index/set.blade.php b/plugins/cultural-space/views/index/set.blade.php index 986829be..d0e516f3 100644 --- a/plugins/cultural-space/views/index/set.blade.php +++ b/plugins/cultural-space/views/index/set.blade.php @@ -42,11 +42,67 @@
周期销售额的N%用作分红总金额,根据每个人当前贡献值比例进行分红;0或者空则无贡献值奖励
+ +
+
+
基金规则
+
+
+ +
+ 不开启 + 开启 +
+
基金池能力
+
+ + + + + +
基金池基金用户订单成交订单金额比例纳入基金池
+
+ + + + + +
用户订单成交第一笔订单计算价格
+
+ + + + + + +
用户购买商品活动赠送文创豆数量比例 《消费额度ⅹ赠送10%÷价格》
+
+ + + + + +
用户将文创豆兑换余额比例
+
+ + + + + +
当系统赠送积分小于当前数量时,系统将自动增值比例
+
+ + + + + +
当系统赠送积分小于指定数量,用户全局增长总量倍数
+
+ +
- - {{--保存按钮--}} 保存 diff --git a/plugins/cultural-space/views/widget/marketing/cultural_space.js b/plugins/cultural-space/views/widget/marketing/cultural_space.js index 3246c01b..77820927 100644 --- a/plugins/cultural-space/views/widget/marketing/cultural_space.js +++ b/plugins/cultural-space/views/widget/marketing/cultural_space.js @@ -18,6 +18,15 @@ define({ + `, @@ -44,6 +53,7 @@ define({ json: { is_open: 0,// 当前商品是否参与文创空间:0=未开启,1=开启 is_give_contribution: 0,// 是否赠送贡献值:0=不赠送,1=赠送 + is_fund_open: 0,// 是否赠送贡献值:0=不赠送,1=赠送 } } }, @@ -55,6 +65,7 @@ define({ console.log(cultural_space); this.json.is_open = cultural_space.is_open ? cultural_space.is_open : 0; this.json.is_give_contribution = cultural_space.is_give_contribution ? cultural_space.is_give_contribution : 0; + this.json.is_fund_open = cultural_space.is_fund_open ? cultural_space.is_fund_open : 0; } }, methods:{