|
|
@@ -0,0 +1,326 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+namespace app\api\logic;
|
|
|
+
|
|
|
+use app\api\controller\Product;
|
|
|
+use app\common\model\ProductArea;
|
|
|
+use app\common\controller\Api;
|
|
|
+use app\common\model\UserWelfare;
|
|
|
+use app\common\model\ProductPopular;
|
|
|
+use app\common\model\ProductOrder;
|
|
|
+use fast\Action;
|
|
|
+use fast\Asset;
|
|
|
+use fast\MembershipLevel;
|
|
|
+use think\Db;
|
|
|
+use think\Log;
|
|
|
+use think\Exception;
|
|
|
+use think\console\Output;
|
|
|
+use fast\Http;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 空投福利
|
|
|
+ */
|
|
|
+class WelfareLoginc
|
|
|
+{
|
|
|
+
|
|
|
+ /** 添加新人福利
|
|
|
+ * @param int $orderId 订单id
|
|
|
+ * @param array $orderInfo 订单详情
|
|
|
+ * @param int $typeId 订单类型
|
|
|
+ * @param int $userId 用户id
|
|
|
+ * @param int $fromUser 来源id
|
|
|
+ * @param float $fees 手续费
|
|
|
+ * @param float $fees 抢购价
|
|
|
+ */
|
|
|
+ public static function setUserWelfareLos($uid, int $productId, $num, $lan)
|
|
|
+ {
|
|
|
+ $tim = time();
|
|
|
+ $result = ProductPopular::alias('a')->where('a.start_time', '<=', $tim)->where('a.end_time', '>=', $tim)
|
|
|
+ ->join('product_list b', "a.product_id = b.id", "left")
|
|
|
+ ->field('a.*,b.is_area,'.$lan.'_name')
|
|
|
+ ->where('a.product_id', $productId)->find();
|
|
|
+ if($num > $result->stock) throw new Exception(__('库存不足'));
|
|
|
+ if(empty($result->is_area)){
|
|
|
+ ProductOrder::setPopularNoAreaOrder($num, $result->id, $result->price, $result->product_id, $uid);
|
|
|
+ }else{
|
|
|
+ $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('RAND()')->limit($num)->column('id');
|
|
|
+ ProductOrder::setPopularAreaOrder($areaArr, $result->id, $result->price, $result->product_id, $uid);
|
|
|
+ }
|
|
|
+ return ['start_time'=>$tim, 'name'=>$result->$lan.'_name'];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算理论收益
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected function calculateRewards($info, $team_level_config)
|
|
|
+ {
|
|
|
+ $user = DB::table('user')
|
|
|
+ ->where('id', $info['user_id'])
|
|
|
+ ->find();
|
|
|
+ $level_info = [];
|
|
|
+ foreach ($team_level_config as $level) {
|
|
|
+ if ($user['team_power'] >= $level['compute_require']) {
|
|
|
+ $level_info['id'] = $level['level_id'];
|
|
|
+ $level_info['rate'] = $level['compute_incentive'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(isset($level_info['id']) && $level_info['id'] > 0){
|
|
|
+ //团队等级大于0,才发放收益
|
|
|
+ $amount = $info['today_power'] * $level_info['rate'];
|
|
|
+
|
|
|
+ DB::table('team_rewards')
|
|
|
+ ->where('id', $info['id'])
|
|
|
+ ->update([
|
|
|
+ 'team_power' => $user['team_power'],
|
|
|
+ 'level_id' => $level_info['id'],
|
|
|
+ 'rate' => $level_info['rate'],
|
|
|
+ 'commission' => $amount,
|
|
|
+ 'update_time' => date('Y-m-d H:i:s')
|
|
|
+ ]);
|
|
|
+ //累计上级的团队收益
|
|
|
+ DB::table('team_rewards')
|
|
|
+ ->where('user_id', $user['parent_id'])
|
|
|
+ ->where('date_time', $info['date_time'])
|
|
|
+ ->update([
|
|
|
+ 'team_commission' => DB::raw("team_commission + " . $amount),
|
|
|
+ 'update_time' => date('Y-m-d H:i:s')
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发放团队加权分红
|
|
|
+ * 1.提取各个等级的会员列表
|
|
|
+ * 2.再计算当日新增业绩
|
|
|
+ * 3.根据每个等级现存的人数,按比例平分佣金
|
|
|
+ */
|
|
|
+ public function usdtRewards()
|
|
|
+ {
|
|
|
+ $date = $this->getExeDate($this->rewards_type['usdt']['type_id']);
|
|
|
+
|
|
|
+ if(empty($date)){
|
|
|
+ return "本次没有可用时间";
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建定时任务执行日志
|
|
|
+ $TimedTaskLog = new TimedTaskLogModel();
|
|
|
+ $log_id = $TimedTaskLog->insertGetId([
|
|
|
+ 'type_id' => $this->rewards_type['usdt']['type_id'],
|
|
|
+ 'type_name' => $this->rewards_type['usdt']['type_name'],
|
|
|
+ 'date_time' => $date,
|
|
|
+ 'create_time' => date('Y-m-d H:i:s'),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $between_time = [strtotime($date), strtotime($date) + 86400];
|
|
|
+
|
|
|
+ //读取当日新增数据,按用户分组统计
|
|
|
+ $recharge_amount = DB::table('offline_recharge_record')
|
|
|
+ ->whereBetween('create_time', $between_time)
|
|
|
+ ->where('order_type', 1)
|
|
|
+ ->where('status', 1)
|
|
|
+ ->sum('amount');
|
|
|
+
|
|
|
+ if($recharge_amount < 1){
|
|
|
+ (new Output())->writeln("本次没有可用新增数据:" . $date);
|
|
|
+ return $log_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ //1.查找有等级的会员
|
|
|
+ $level_user_list = DB::table('user')
|
|
|
+ ->field('id,team_level_id')
|
|
|
+ ->where('team_level_id', '>', 0)
|
|
|
+ ->select();
|
|
|
+ $levels_info = [];
|
|
|
+ //整理数据格式,以team_level_id为键名,存放该等级下的会员id数组
|
|
|
+ foreach ($level_user_list as $info) {
|
|
|
+ $levels_info[$info['team_level_id']][] = $info['id'];
|
|
|
+ }
|
|
|
+ //读取团队等级配置
|
|
|
+ $team_level_config = DB::table('team_level')
|
|
|
+ ->field('level_id, weight_dividend')
|
|
|
+ ->select();
|
|
|
+ foreach ($team_level_config as $level) {
|
|
|
+ if(isset($levels_info[$level['level_id']])){//存在当前等级的会员
|
|
|
+ //计算当前等级平分佣金金额:当日新增业绩 * 分红比例 / 当前等级人数
|
|
|
+ $amount = $recharge_amount * $level['weight_dividend'] / count($levels_info[$level['level_id']]);
|
|
|
+ if($amount > 0){
|
|
|
+ foreach ($levels_info[$level['level_id']] as $user_id){
|
|
|
+ //发放佣金
|
|
|
+ (new LedgerWalletModel)->changeWalletAccount($user_id, Asset::USDT, $amount, Action::UsdtWeightDividend);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ Log::info("分红等级" . $level['level_id'] . "在" . $date . "分红金额为0;当日新增业绩:" . $amount . ",分红比例:" . $level['weight_dividend']. ",分红人数" . dump($levels_info[$level['level_id']]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $log_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发放社区长、系统领导人加权分红
|
|
|
+ * 1.提取社区长等级的会员列表
|
|
|
+ * 2.再计算当日新增业绩
|
|
|
+ * 3.根据每个等级现存的人数,按比例平分佣金
|
|
|
+ * 5.提取系统领导人等级的会员列表
|
|
|
+ * 6.再计算当日新增业绩
|
|
|
+ * 7.根据每个等级现存的人数,按比例平分佣金
|
|
|
+ */
|
|
|
+ public function marketRewards()
|
|
|
+ {
|
|
|
+ $date = $this->getExeDate($this->rewards_type['market']['type_id']);
|
|
|
+
|
|
|
+ if(empty($date)){
|
|
|
+ return "本次没有可用时间";
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建定时任务执行日志
|
|
|
+ $TimedTaskLog = new TimedTaskLogModel();
|
|
|
+ $log_id = $TimedTaskLog->insertGetId([
|
|
|
+ 'type_id' => $this->rewards_type['market']['type_id'],
|
|
|
+ 'type_name' => $this->rewards_type['market']['type_name'],
|
|
|
+ 'date_time' => $date,
|
|
|
+ 'create_time' => date('Y-m-d H:i:s'),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $between_time = [strtotime($date), strtotime($date) + 86400];
|
|
|
+
|
|
|
+ //读取当日新增数据
|
|
|
+ $recharge_power = DB::table('offline_recharge_record')
|
|
|
+ ->whereBetween('create_time', $between_time)
|
|
|
+ ->where('status', 1)
|
|
|
+ ->sum('power');
|
|
|
+
|
|
|
+ if($recharge_power < 1){
|
|
|
+ (new Output())->writeln("本次没有可用新增数据:" . $date);
|
|
|
+ return $log_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ //团队长分红
|
|
|
+ //查找有团队长等级的会员
|
|
|
+ $community_user_list = DB::table('user')
|
|
|
+ ->field('id,market_level_id')
|
|
|
+ ->where('market_level_id', MembershipLevel::CommunityLeader)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $community_rewards = (new ParametersModel())->getValue('communityRewards'); //社区长分红参数 数据格式为:0.02:0.5:0.5
|
|
|
+ $usdt_rate = (new ParametersModel())->getValue('usdtToPowerRate');
|
|
|
+ $community_rewards_arr = explode(':', $community_rewards);
|
|
|
+
|
|
|
+ $reward = $recharge_power * $community_rewards_arr[0] / count($community_user_list);//新增业绩 * 分红比例 / 人数
|
|
|
+ if($reward > 0) {
|
|
|
+ $reward_power = $reward * $community_rewards_arr[1];
|
|
|
+ $reward_usdt = $reward * $community_rewards_arr[2] / $usdt_rate;//算力折算成U
|
|
|
+
|
|
|
+ foreach ($community_user_list as $info) {
|
|
|
+ //发放分红
|
|
|
+ (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::POWER, $reward_power, Action::PowerBonusAward);
|
|
|
+
|
|
|
+ (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::USDT, $reward_usdt, Action::UsdtWeightDividend);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ Log::info("社区长分红在" . $date . "分红金额为0;当日新增业绩:" . $recharge_power . ",分红比例:" . $community_rewards_arr[0] . ",分红人数" . count($community_user_list));
|
|
|
+ }
|
|
|
+
|
|
|
+ //领导人分红
|
|
|
+ //查找有团队长等级的会员
|
|
|
+ $sys_leader_user_list = DB::table('user')
|
|
|
+ ->field('id,market_level_id')
|
|
|
+ ->where('market_level_id', MembershipLevel::SystemLeader)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $sys_leade_rewards = (new ParametersModel())->getValue('sysLeaderRewards'); //系统领导人分红参数 数据格式为:0.02:0.5:0.5
|
|
|
+ $sys_leade_rewards_arr = explode(':', $sys_leade_rewards);
|
|
|
+
|
|
|
+ $reward = $recharge_power * $sys_leade_rewards_arr[0] / count($sys_leader_user_list);//新增业绩 * 分红比例 / 人数
|
|
|
+ if($reward > 0) {
|
|
|
+ $reward_power = $reward * $sys_leade_rewards_arr[1];
|
|
|
+ $reward_usdt = $reward * $sys_leade_rewards_arr[2] * 8;//算力折算成U
|
|
|
+
|
|
|
+ foreach ($sys_leader_user_list as $info) {
|
|
|
+ //发放分红
|
|
|
+ (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::POWER, $reward_power, Action::PowerBonusAward);
|
|
|
+
|
|
|
+ (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::USDT, $reward_usdt, Action::UsdtWeightDividend);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ Log::info("系统领导人分红在" . $date . "分红金额为0;当日新增业绩:" . $recharge_power . ",分红比例:" . $community_rewards_arr[0] . ",分红人数" . count($community_user_list));
|
|
|
+ }
|
|
|
+ return $log_id;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当日收益 Daily earnings
|
|
|
+ */
|
|
|
+ private static function getDayEarnings(string $date)
|
|
|
+ {
|
|
|
+ $between_time = [strtotime($date), strtotime($date) + 86400];
|
|
|
+ //读取当日新增数据
|
|
|
+ return ProductOrder::where('type_id', ProductOrder::Popular)->where('status', ProductOrder::Paid)->whereBetween('create_time', $between_time)->sum('price');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取可用日期
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected function getExeDate($type_id = 1)
|
|
|
+ {
|
|
|
+ $today = date('Y-m-d',strtotime("-1day"));//当前时间减一天为最后一次可用时间
|
|
|
+ $dateInfo = DB::table('timed_task_log')
|
|
|
+ ->where('type_id' ,$type_id)
|
|
|
+ ->where('status', 1)
|
|
|
+ ->order('date_time', 'desc')
|
|
|
+ ->find();
|
|
|
+ if(empty($dateInfo)){//首次
|
|
|
+ return $today;
|
|
|
+ }
|
|
|
+ $new_date = date('Y-m-d',strtotime("+1day",strtotime($dateInfo['date_time'])));//最后一次发放日期+1天为本次执行时间
|
|
|
+ if($new_date > $today){//超过今天
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return $new_date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入或更新收益统计信息
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected function createData($uids, $amount, $date)
|
|
|
+ {
|
|
|
+ foreach ($uids as $user_id) {
|
|
|
+ $check = DB::table('team_rewards')
|
|
|
+ ->field('id')
|
|
|
+ ->where('user_id', $user_id)
|
|
|
+ ->where('date_time', $date)
|
|
|
+ ->find();
|
|
|
+ if ($check) {//存在,则更新
|
|
|
+ DB::table('team_rewards')
|
|
|
+ ->where('id', $check['id'])
|
|
|
+ ->update([
|
|
|
+ 'today_power' => Db::raw('today_power+' . $amount),
|
|
|
+ 'update_time' => date('Y-m-d H:i:s')
|
|
|
+ ]);
|
|
|
+ } else {//新增
|
|
|
+ DB::table('team_rewards')
|
|
|
+ ->insert([
|
|
|
+ 'user_id' => $user_id,
|
|
|
+ 'date_time' => $date,
|
|
|
+ 'today_power' => Db::raw('today_power+' . $amount),
|
|
|
+ 'create_time' => date('Y-m-d H:i:s')
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|