| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\common\logic;
- use app\common\model\Config;
- use app\common\model\UserAirdrop;
- use app\api\logic\WelfareLoginc;
- use app\common\model\ProductPopular;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\LedgerWalletModel;
- use app\common\model\TimedTaskLogModel;
- use fast\Action;
- use fast\Asset;
- use fast\MembershipLevel;
- use think\Cache;
- use think\Db;
- use think\Log;
- use think\Model;
- use think\console\Output;
- use fast\Http;
- /**
- * 团队奖励发放逻辑
- * 定时任务,定时发放,每天一次,一般在凌晨12一过开始
- */
- class AirdropLogic
- {
- /**
- * 奖励名称及类型ID
- * @var array
- */
- private array $rewards_type = [
- 1 => 'SMH拨币',
- 2 => 'Qubic拨币',
- ];
- /**
- * 空投发放
- */
- public function setAirdropRewards()
- {
- //查找会员资产列表
- $info_list = UserAirdrop::where('type_id', UserAirdrop::typeRwa)->where('status', UserAirdrop::NORMAL)->find();
- if(empty($info_list)){
- (new Output())->writeln("本次没有空投发放数据:");
- return false;
- }
- $result = ProductPopular::getPopularByTime($info_list['product_id'], 'zh', $info_list->start_time);
- if(!$result || $info_list->total_num > $result->stock) {
- (new Output())->writeln("本次执行库存不足:");
- return false;
- }
- WelfareLoginc::setUserExRwaNum(
- $info_list['rwa_num'],
- $info_list['product_id'],
- $result->is_area, $result->id, $result->price,
- $info_list['rwa_mod'],
- $info_list['num']);
- $info_list->status = UserAirdrop::STOP;
- $info_list->remark = '总发放'.$info_list->total_num.'套';
- return $info_list->save();
-
-
- }
- /**
- * 拨币
- * 向会员发放qubic
- * 每一万算力每天收益14-15万个币
- */
- public function AllocateQubic()
- {
- $type_id = 2;
- $date = $this->getExeDate($type_id);
- if(empty($date)){
- (new Output())->writeln("本次没有可用时间:" . $date);
- return ;
- }
- //创建定时任务执行日志
- $log_id = (new TimedTaskLogModel())->insertGetId([
- 'type_id' => $type_id,
- 'type_name' => $this->rewards_type[$type_id],
- 'date_time' => $date,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- //查找会员资产列表
- $info_list = DB::table('ledger_wallet')
- ->alias('w')
- ->join('user u','u.id = w.user_id')
- ->field('user_id,power')
- ->where('power', '>', 0)
- ->where('effective_time', '>', 0) //报单有收益
- ->select();
- if(empty($info_list)){
- (new Output())->writeln("本次没有可用会员数据:" . $date);
- return $log_id;
- }
- $num =rand(120000,130000);
- $power_price = $num/10000;//每算力每日获得SMH的数量,算法为每一万算力每日产15万个Qubic
- foreach ($info_list as $info) {
- $amount = ($info['power']) * $power_price;
- $check = (new LedgerQubicChangeModel())
- ->where('user_id', $info['user_id'])
- ->where('action', LedgerQubicChangeModel::SysSend)
- ->where('from_id', strtotime($date))
- ->count();
- if($check){
- dump('用户ID:' . $info['user_id'] . '已拨过,拨币量为:' . $amount);
- }else{
- dump(['用户ID:' . $info['user_id'],'拨币量为:' . $amount]);
- //发放
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::QUBIC, $amount, LedgerQubicChangeModel::SysSend);
- }
- }
- return $log_id;
- }
- /**
- * 拨币
- * 向会员发放qubic
- * 每一万算力每天收益14-15万个币
- */
- public function AllocateAleo()
- {
- $type_id = 11;
- $date = $this->getExeDate($type_id);
- if(empty($date)){
- (new Output())->writeln("本次没有可用时间:" . $date);
- return ;
- }
- //创建定时任务执行日志
- $log_id = (new TimedTaskLogModel())->insertGetId([
- 'type_id' => $type_id,
- 'type_name' => $this->rewards_type[$type_id],
- 'date_time' => $date,
- 'create_time' => date('Y-m-d H:i:s'),
- ]);
- //查找会员资产列表
- $info_list = DB::table('ledger_wallet')
- ->alias('w')
- ->join('user u','u.id = w.user_id')
- ->field('user_id,power')
- ->where('power', '>', 0)
- ->where('effective_time', '>', 0) //报单有收益
- ->select();
- if(empty($info_list)){
- (new Output())->writeln("本次没有可用会员数据:" . $date);
- return $log_id;
- }
- $num = mt_rand(4030,4080) / 1000;//每一万算力每日获得Aleo的数量,算法为每一万算力每日产6.5个币,并从2024-9-20开始,每天递减5%
- // $date1 = '2024-9-26';
- // $date2 = date('Y-m-d');
- // $diff = strtotime($date2) - strtotime($date1);
- // // 将时间戳差转换为天数
- // $days = floor($diff / (60 * 60 * 24));
- // if($days > 0){
- // //每日递减3%
- // $num = $num * pow((1-0.03), $days);
- // }
- // if($num < 4){
- // $num = $num = mt_rand(3500,4500) / 1000;//每一万算力保底3个币
- // }
- $power_price = $num/10000;//每算力每日获得Aleo的数量,算法为每一万算力每日产6.5个币,并从2024-9-20开始,每天递减5%
- foreach ($info_list as $info) {
- $amount = ($info['power']) * $power_price;
- $check = (new LedgerQubicChangeModel())
- ->where('user_id', $info['user_id'])
- ->where('action', LedgerTokenChangeModel::SysSend)
- ->where('from_id', strtotime($date))
- ->count();
- if($check){
- dump('用户ID:' . $info['user_id'] . '已拨过,拨币量为:' . $amount);
- }else{
- dump(['用户ID:' . $info['user_id'],'拨币量为:' . $amount]);
- //发放
- (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::TOKEN, $amount, LedgerTokenChangeModel::SysSend);
- }
- }
- return $log_id;
- }
- /**
- * 获取可用日期
- * @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;
- }
- }
|