|
|
@@ -5,8 +5,8 @@ namespace app\common\logic;
|
|
|
|
|
|
use app\common\model\EtcWithdrawRecordModel;
|
|
|
use app\common\model\LedgerWalletModel;
|
|
|
-use app\common\model\OfflineRechargeRecordModel;
|
|
|
-use app\common\model\ParametersModel;
|
|
|
+use app\common\model\ProductOrder;
|
|
|
+use app\common\model\UserModel;
|
|
|
use app\common\model\TimedTaskLogModel;
|
|
|
use app\common\model\UserPathModel;
|
|
|
use fast\Action;
|
|
|
@@ -30,105 +30,63 @@ class BonusRewards
|
|
|
* @var array
|
|
|
*/
|
|
|
private array $rewards_type = [
|
|
|
- 'community' => [
|
|
|
- 'type_id' => 1,
|
|
|
- 'type_name' => "社区建设津贴"
|
|
|
- ],
|
|
|
'service' => [
|
|
|
- 'type_id' => 2,
|
|
|
- 'type_name' => "服务津贴"
|
|
|
+ 'type_id' => 1,
|
|
|
+ 'type_name' => "服务共创津贴"
|
|
|
],
|
|
|
'together' => [
|
|
|
- 'type_id' => 3,
|
|
|
+ 'type_id' => 2,
|
|
|
'type_name' => "共创津贴"
|
|
|
]
|
|
|
];
|
|
|
|
|
|
/**
|
|
|
- * 发放团队算力奖励
|
|
|
- * 1.先判断最后一次发放时间,把该时间至当前时间的所有日期内的数据都发放了
|
|
|
- * 2.从算力明细表,取当日算力租赁数据,写入团队统计表,统计每人当日团队新增算力
|
|
|
- * 3.遍历当日统计表,计算每人理论应得算力奖励
|
|
|
- * 4.发放奖励,理论奖励-团队已发放=自身奖励金额
|
|
|
+ * 社区建设津贴
|
|
|
+ * 1.直推几个有效用户,拿几层,最高10层;每次收益3.5%
|
|
|
+ * 2.举例:会员A直推了三个有效用户,则会员A下三代内,每个用户购买RWA茶时,A都会收益对应PV的3.5%;
|
|
|
*/
|
|
|
- public function powerRewards()
|
|
|
+ public function allowanceRewards()
|
|
|
{
|
|
|
- $date = $this->getExeDate($this->rewards_type['community']['type_id']);
|
|
|
+ $date = $this->getExeDate($this->rewards_type['service']['type_id']);
|
|
|
if(empty($date)) return "本次没有可用时间";
|
|
|
|
|
|
-
|
|
|
+
|
|
|
//创建定时任务执行日志
|
|
|
$TimedTaskLog = new TimedTaskLogModel();
|
|
|
$log_id = $TimedTaskLog->insertGetId([
|
|
|
- 'type_id' => $this->rewards_type['community']['type_id'],
|
|
|
- 'type_name' => $this->rewards_type['community']['type_name'],
|
|
|
+ 'type_id' => $this->rewards_type['service']['type_id'],
|
|
|
+ 'type_name' => $this->rewards_type['service']['type_name'],
|
|
|
'date_time' => $date,
|
|
|
'create_time' => date('Y-m-d H:i:s'),
|
|
|
|
|
|
]);
|
|
|
|
|
|
- $between_time = [strtotime($date), strtotime($date) + 86400];
|
|
|
-
|
|
|
- //读取当日新增数据,按用户分组统计
|
|
|
- $recharge_record = DB::table('offline_recharge_record')
|
|
|
- ->field('user_id, SUM(amount) as amount')
|
|
|
- ->whereBetween('create_time', $between_time)
|
|
|
- ->where('order_type', 1)
|
|
|
- ->where('status', 1)
|
|
|
- ->group('user_id')
|
|
|
- ->select();
|
|
|
-
|
|
|
- if(empty($recharge_record)){
|
|
|
+ //读取当日新增数据
|
|
|
+ $bonus = self::getDayEarnings($date);
|
|
|
+ if(empty($bonus)){
|
|
|
(new Output())->writeln("本次没有可用新增数据:" . $date);
|
|
|
-
|
|
|
return $log_id;
|
|
|
}
|
|
|
-
|
|
|
- //1.统计团队新增业绩
|
|
|
+ $ledgerWalletModel = new LedgerWalletModel();
|
|
|
+ //1.服务津贴
|
|
|
+ $recharge_record = UserModel::alias('a')
|
|
|
+ ->join('user b', 'a.parent_id = b.id', 'left')
|
|
|
+ ->field('a.id,sum(a.rwa_num) as total_amount')->having('total_amount >= 10')->group("a.id")
|
|
|
+ ->select();
|
|
|
+ $recharge_count = count($recharge_record);//总人数
|
|
|
+ $pv = bcdiv(bcmul($bonus, config('service_ratio')), $recharge_count, 2); //收益
|
|
|
foreach ($recharge_record as $info) {
|
|
|
- $path = (new UserPathModel())
|
|
|
- ->where('user_id', $info['user_id'])
|
|
|
- ->column('parent_id');
|
|
|
- $path[] = $info['user_id'];
|
|
|
-
|
|
|
- $this->createData($path, $info['amount'], $date);
|
|
|
+ $ledgerWalletModel->changeWalletAccount($info->id, Asset::TOKEN, $pv, $ledgerWalletModel::Service, 0);
|
|
|
}
|
|
|
-
|
|
|
- //2.计算每人理论收益
|
|
|
- $team_level_config = DB::table('team_level')
|
|
|
- ->field('level_id, compute_require, compute_incentive')
|
|
|
- ->select();
|
|
|
-
|
|
|
- $rewards_record = DB::table('team_rewards')
|
|
|
- ->field('id, user_id, today_power, date_time')
|
|
|
- ->where('date_time', $date)
|
|
|
- ->where('status', 0)
|
|
|
- ->select();
|
|
|
-
|
|
|
- foreach ($rewards_record as $info) {
|
|
|
- $this->calculateRewards($info, $team_level_config);
|
|
|
- }
|
|
|
-
|
|
|
- //3.发放收益
|
|
|
- $rewards_send = DB::table('team_rewards')
|
|
|
- ->field('id, user_id, commission, team_commission, date_time')
|
|
|
- ->where('date_time', $date)
|
|
|
- ->where('status', 0)
|
|
|
- ->select();
|
|
|
- foreach ($rewards_send as $info) {
|
|
|
- $amount = $info['commission'] - $info['team_commission'];
|
|
|
- if($amount > 0){
|
|
|
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::POWER, $amount / 8, Action::PowerProfit);
|
|
|
+ //2.共享津贴收益
|
|
|
+ $rewards_record = UserModel::where('rwa_num', '>', 4)->column('id');
|
|
|
+ if(!empty($rewards_record )){
|
|
|
+ $rewards_count = count($rewards_record);//总人数
|
|
|
+ $pvs = bcdiv(bcmul($bonus, config('together_ratio')), $rewards_count, 2); //收益
|
|
|
+ foreach ($rewards_record as $info) {
|
|
|
+ $ledgerWalletModel->changeWalletAccount($info, Asset::TOKEN, $pvs, $ledgerWalletModel::Together, 0);
|
|
|
}
|
|
|
- DB::table('team_rewards')
|
|
|
- ->where('id', $info['id'])
|
|
|
- ->update([
|
|
|
- 'send_commission' => $amount,
|
|
|
- 'status' => 1,
|
|
|
- 'update_time' => date('Y-m-d H:i:s')
|
|
|
- ]);
|
|
|
}
|
|
|
-
|
|
|
return $log_id;
|
|
|
}
|
|
|
|
|
|
@@ -336,187 +294,20 @@ class BonusRewards
|
|
|
return $log_id;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 团队业绩分红
|
|
|
- * 1.提取社区长等级的会员列表
|
|
|
- * 2.再计算当日新增业绩 * 0.08
|
|
|
- * 3.取20%发放算力,剩余发放U
|
|
|
- */
|
|
|
- public function teamRewards()
|
|
|
- {
|
|
|
- $date = $this->getExeDate($this->rewards_type['team']['type_id']);
|
|
|
-
|
|
|
- if(empty($date)){
|
|
|
- return "本次没有可用时间";
|
|
|
- }
|
|
|
-
|
|
|
- //创建定时任务执行日志
|
|
|
- $TimedTaskLog = new TimedTaskLogModel();
|
|
|
- $log_id = $TimedTaskLog->insertGetId([
|
|
|
- 'type_id' => $this->rewards_type['team']['type_id'],
|
|
|
- 'type_name' => $this->rewards_type['team']['type_name'],
|
|
|
- 'date_time' => $date,
|
|
|
- 'create_time' => date('Y-m-d H:i:s'),
|
|
|
- ]);
|
|
|
-
|
|
|
- $between_time = [strtotime($date), strtotime($date) + 86400];
|
|
|
-
|
|
|
- //查找有团队长等级的会员
|
|
|
- $community_user_list = DB::table('user')
|
|
|
- ->field('id,market_level_id')
|
|
|
- ->where('market_level_id', MembershipLevel::CommunityLeader)
|
|
|
- ->select();
|
|
|
-
|
|
|
- if(empty($community_user_list)){
|
|
|
- (new Output())->writeln("本次没有可用会员数据:" . $date);
|
|
|
- return $log_id;
|
|
|
- }
|
|
|
-
|
|
|
- $market_level_arr = [];//所有社区长,以user_id 为键名的数组
|
|
|
- $market_user_id_arr = [];//所有社区长,user_id 数组
|
|
|
- foreach ($community_user_list as $info) {
|
|
|
- $market_level_arr[$info['id']] = [
|
|
|
- 'user_id' => $info['id'],//自身ID
|
|
|
- 'today_usdt' => 0,//当日团队新增业绩USDT
|
|
|
- 'user_power' => 0,//自身收益
|
|
|
- 'user_usdt' => 0,//自身收益
|
|
|
- 'team_power' => 0,//下级团队收益
|
|
|
- 'team_usdt' => 0,//下级团队收益
|
|
|
- 'same_power' => 0,//同级奖收益
|
|
|
- 'same_usdt' => 0,//同级奖收益
|
|
|
- ];
|
|
|
- $market_user_id_arr[] = $info['id'];
|
|
|
- }
|
|
|
-
|
|
|
- $community_rewards = (new ParametersModel())->getValue('communityTeamRewards'); //社区长分红参数 数据格式为:0.08:0.2:0.8
|
|
|
- $community_rewards_arr = explode(':', $community_rewards);
|
|
|
- dump('团队分红参数配置');
|
|
|
- dump($community_rewards_arr);
|
|
|
-
|
|
|
- $performance_list = (new \app\admin\model\TeamRewards())
|
|
|
- ->where('user_id', 'in', $market_user_id_arr)
|
|
|
- ->where('date_time', $date)
|
|
|
- ->select();
|
|
|
- foreach ($performance_list as $item){
|
|
|
- if($item['today_power'] > 0){
|
|
|
- $amount = $community_rewards_arr[0] * $item['today_power'];//取新增业绩的8%
|
|
|
- if($amount < 1){
|
|
|
- dump("会员:" . $item['user_id'] . ",本次没有可用新增数据:" . $date);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- $market_level_arr[$item['user_id']]['today_usdt'] = $item['today_power'];//u折成算力
|
|
|
- $market_level_arr[$item['user_id']]['user_power'] = $amount * $community_rewards_arr[1] / 8;//u折成算力
|
|
|
- $market_level_arr[$item['user_id']]['user_usdt'] = $amount * $community_rewards_arr[2];
|
|
|
-
|
|
|
- $parent_ids = (new UserPathModel())
|
|
|
- ->where('user_id', $item['user_id'])
|
|
|
- ->order('distance')
|
|
|
- ->select();
|
|
|
- $i = 0;
|
|
|
- dump($item['user_id'] . '的上级');
|
|
|
- foreach ($parent_ids as $uid){
|
|
|
- if(isset($market_level_arr[$uid['parent_id']])){//向上级的团队收益中累计本次收益,方便后面对应的上级减掉
|
|
|
- dump($uid['parent_id']);
|
|
|
-
|
|
|
- if($i == 0){//只累计给首个上级
|
|
|
- dump($uid['parent_id'] . '累计上级');
|
|
|
-
|
|
|
- $market_level_arr[$uid['parent_id']]['team_power'] += $market_level_arr[$item['user_id']]['user_power'];
|
|
|
- $market_level_arr[$uid['parent_id']]['team_usdt'] += $market_level_arr[$item['user_id']]['user_usdt'];
|
|
|
- $i++;
|
|
|
- }
|
|
|
- dump($uid['parent_id'] . '累计平级');
|
|
|
- //每个同级上级都有平级奖
|
|
|
- $market_level_arr[$uid['parent_id']]['same_power'] += $market_level_arr[$item['user_id']]['user_power'] * 0.05;//5%的平级奖
|
|
|
- $market_level_arr[$uid['parent_id']]['same_usdt'] += $market_level_arr[$item['user_id']]['user_usdt'] * 0.05;//5%的平级奖
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- dump($market_level_arr[$item['user_id']]);
|
|
|
- //dump(['用户ID:' . $info['id'],'新增业绩:' . $recharge_power, '算力收益:' . $reward_power, 'U收益:' . $reward_usdt]);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- dump('进展过半');
|
|
|
- Log::info($date . ' - 社区长分红');
|
|
|
- foreach ($market_level_arr as $info) {
|
|
|
- Log::info(json_encode($info));
|
|
|
- $power = $info['user_power'] - $info['team_power'];
|
|
|
- $usdt = $info['user_usdt'] - $info['team_usdt'];
|
|
|
- //社区长团队分红
|
|
|
- if($power > 0){
|
|
|
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::POWER, $power, Action::PowerCommunityBonusAward);
|
|
|
- }
|
|
|
- if($usdt > 0){
|
|
|
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::USDT, $usdt, Action::UsdtCmmunityBonus);
|
|
|
- }
|
|
|
- //平级奖励
|
|
|
- if($info['same_power'] > 0){
|
|
|
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::POWER, $info['same_power'], Action::PowerSameBonus);
|
|
|
- }
|
|
|
- if($info['same_usdt'] > 0){
|
|
|
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::USDT, $info['same_usdt'], Action::UsdtSameBonus);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $log_id;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
- * 拨币
|
|
|
- * 向会员发放ETC
|
|
|
- * 每8000算力每天收益93,即每算力每天收益 93/8000=0.011625人民币
|
|
|
- * 实际发放的时候,发放ETH,根据当日价格,折算相应ETH,比如1ETH=12300RMB,则每算力为:0.011625/12300=0.000000945122ETH
|
|
|
+ * 获取当日收益 Daily earnings
|
|
|
*/
|
|
|
- public function AllocateEtc()
|
|
|
- {
|
|
|
- $date = $this->getExeDate($this->rewards_type['allocate']['type_id']);
|
|
|
-
|
|
|
- if(empty($date)){
|
|
|
- return "本次没有可用时间";
|
|
|
- }
|
|
|
-
|
|
|
- //创建定时任务执行日志
|
|
|
- $TimedTaskLog = new TimedTaskLogModel();
|
|
|
- $log_id = $TimedTaskLog->insertGetId([
|
|
|
- 'type_id' => $this->rewards_type['allocate']['type_id'],
|
|
|
- 'type_name' => $this->rewards_type['allocate']['type_name'],
|
|
|
- 'date_time' => $date,
|
|
|
- 'create_time' => date('Y-m-d H:i:s'),
|
|
|
- ]);
|
|
|
-
|
|
|
+ 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');
|
|
|
+ }
|
|
|
|
|
|
- //查找会员资产列表
|
|
|
- $info_list = DB::table('ledger_wallet')
|
|
|
- ->alias('w')
|
|
|
- ->join('user u','u.id = w.user_id')
|
|
|
- ->field('user_id,power,server_power')
|
|
|
- ->where('power', '>', 0)
|
|
|
- ->where('effective_time', '<', strtotime($date) - 1209600) //报单十五天后才有收益
|
|
|
- ->select();
|
|
|
-
|
|
|
- if(empty($info_list)){
|
|
|
- (new Output())->writeln("本次没有可用会员数据:" . $date);
|
|
|
- return $log_id;
|
|
|
- }
|
|
|
-
|
|
|
- $etc_usdt_price = (new EtcWithdrawRecordModel)->getEtcPrice();
|
|
|
-
|
|
|
- $power_usdt_price = 0.0038;//每算力每日获得Usdt的数量 没8000算力13U除过来的
|
|
|
- $power_etc_price = round($power_usdt_price / $etc_usdt_price, 6);//每算力每日获得ETC的数量
|
|
|
-
|
|
|
- foreach ($info_list as $info) {
|
|
|
- $amount = ($info['power'] + $info['server_power']) * $power_etc_price;
|
|
|
- dump(['用户ID:' . $info['user_id'],'拨币量为:' . $amount]);
|
|
|
-
|
|
|
- //发放
|
|
|
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::TOKEN, $amount, Action::TokenAllocateEtc);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
- return $log_id;
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 获取可用日期
|
|
|
@@ -525,7 +316,6 @@ class BonusRewards
|
|
|
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)
|