AirdropLogic.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\common\logic;
  3. use app\common\model\Config;
  4. use app\common\model\UserAirdrop;
  5. use app\api\logic\WelfareLoginc;
  6. use app\common\model\ProductPopular;
  7. use app\common\model\LedgerTokenChangeModel;
  8. use app\common\model\LedgerWalletModel;
  9. use app\common\model\TimedTaskLogModel;
  10. use fast\Action;
  11. use fast\Asset;
  12. use fast\MembershipLevel;
  13. use think\Cache;
  14. use think\Db;
  15. use think\Log;
  16. use think\Model;
  17. use think\console\Output;
  18. use fast\Http;
  19. /**
  20. * 团队奖励发放逻辑
  21. * 定时任务,定时发放,每天一次,一般在凌晨12一过开始
  22. */
  23. class AirdropLogic
  24. {
  25. /**
  26. * 奖励名称及类型ID
  27. * @var array
  28. */
  29. private array $rewards_type = [
  30. 1 => 'SMH拨币',
  31. 2 => 'Qubic拨币',
  32. ];
  33. /**
  34. * 空投发放
  35. */
  36. public function setAirdropRewards()
  37. {
  38. //查找会员资产列表
  39. $info_list = UserAirdrop::where('type_id', UserAirdrop::typeRwa)->where('status', UserAirdrop::NORMAL)->find();
  40. if(empty($info_list)){
  41. (new Output())->writeln("本次没有空投发放数据:");
  42. return false;
  43. }
  44. $result = ProductPopular::getPopularByTime($info_list['product_id'], 'zh', $info_list->start_time);
  45. if(!$result || $info_list->total_num > $result->stock) {
  46. (new Output())->writeln("本次执行库存不足:");
  47. return false;
  48. }
  49. WelfareLoginc::setUserExRwaNum(
  50. $info_list['rwa_num'],
  51. $info_list['product_id'],
  52. $result->is_area, $result->id, $result->price,
  53. $info_list['rwa_mod'],
  54. $info_list['num']);
  55. $info_list->status = UserAirdrop::STOP;
  56. $info_list->remark = '总发放'.$info_list->total_num.'套';
  57. return $info_list->save();
  58. }
  59. /**
  60. * 拨币
  61. * 向会员发放qubic
  62. * 每一万算力每天收益14-15万个币
  63. */
  64. public function AllocateQubic()
  65. {
  66. $type_id = 2;
  67. $date = $this->getExeDate($type_id);
  68. if(empty($date)){
  69. (new Output())->writeln("本次没有可用时间:" . $date);
  70. return ;
  71. }
  72. //创建定时任务执行日志
  73. $log_id = (new TimedTaskLogModel())->insertGetId([
  74. 'type_id' => $type_id,
  75. 'type_name' => $this->rewards_type[$type_id],
  76. 'date_time' => $date,
  77. 'create_time' => date('Y-m-d H:i:s'),
  78. ]);
  79. //查找会员资产列表
  80. $info_list = DB::table('ledger_wallet')
  81. ->alias('w')
  82. ->join('user u','u.id = w.user_id')
  83. ->field('user_id,power')
  84. ->where('power', '>', 0)
  85. ->where('effective_time', '>', 0) //报单有收益
  86. ->select();
  87. if(empty($info_list)){
  88. (new Output())->writeln("本次没有可用会员数据:" . $date);
  89. return $log_id;
  90. }
  91. $num =rand(120000,130000);
  92. $power_price = $num/10000;//每算力每日获得SMH的数量,算法为每一万算力每日产15万个Qubic
  93. foreach ($info_list as $info) {
  94. $amount = ($info['power']) * $power_price;
  95. $check = (new LedgerQubicChangeModel())
  96. ->where('user_id', $info['user_id'])
  97. ->where('action', LedgerQubicChangeModel::SysSend)
  98. ->where('from_id', strtotime($date))
  99. ->count();
  100. if($check){
  101. dump('用户ID:' . $info['user_id'] . '已拨过,拨币量为:' . $amount);
  102. }else{
  103. dump(['用户ID:' . $info['user_id'],'拨币量为:' . $amount]);
  104. //发放
  105. (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::QUBIC, $amount, LedgerQubicChangeModel::SysSend);
  106. }
  107. }
  108. return $log_id;
  109. }
  110. /**
  111. * 拨币
  112. * 向会员发放qubic
  113. * 每一万算力每天收益14-15万个币
  114. */
  115. public function AllocateAleo()
  116. {
  117. $type_id = 11;
  118. $date = $this->getExeDate($type_id);
  119. if(empty($date)){
  120. (new Output())->writeln("本次没有可用时间:" . $date);
  121. return ;
  122. }
  123. //创建定时任务执行日志
  124. $log_id = (new TimedTaskLogModel())->insertGetId([
  125. 'type_id' => $type_id,
  126. 'type_name' => $this->rewards_type[$type_id],
  127. 'date_time' => $date,
  128. 'create_time' => date('Y-m-d H:i:s'),
  129. ]);
  130. //查找会员资产列表
  131. $info_list = DB::table('ledger_wallet')
  132. ->alias('w')
  133. ->join('user u','u.id = w.user_id')
  134. ->field('user_id,power')
  135. ->where('power', '>', 0)
  136. ->where('effective_time', '>', 0) //报单有收益
  137. ->select();
  138. if(empty($info_list)){
  139. (new Output())->writeln("本次没有可用会员数据:" . $date);
  140. return $log_id;
  141. }
  142. $num = mt_rand(4030,4080) / 1000;//每一万算力每日获得Aleo的数量,算法为每一万算力每日产6.5个币,并从2024-9-20开始,每天递减5%
  143. // $date1 = '2024-9-26';
  144. // $date2 = date('Y-m-d');
  145. // $diff = strtotime($date2) - strtotime($date1);
  146. // // 将时间戳差转换为天数
  147. // $days = floor($diff / (60 * 60 * 24));
  148. // if($days > 0){
  149. // //每日递减3%
  150. // $num = $num * pow((1-0.03), $days);
  151. // }
  152. // if($num < 4){
  153. // $num = $num = mt_rand(3500,4500) / 1000;//每一万算力保底3个币
  154. // }
  155. $power_price = $num/10000;//每算力每日获得Aleo的数量,算法为每一万算力每日产6.5个币,并从2024-9-20开始,每天递减5%
  156. foreach ($info_list as $info) {
  157. $amount = ($info['power']) * $power_price;
  158. $check = (new LedgerQubicChangeModel())
  159. ->where('user_id', $info['user_id'])
  160. ->where('action', LedgerTokenChangeModel::SysSend)
  161. ->where('from_id', strtotime($date))
  162. ->count();
  163. if($check){
  164. dump('用户ID:' . $info['user_id'] . '已拨过,拨币量为:' . $amount);
  165. }else{
  166. dump(['用户ID:' . $info['user_id'],'拨币量为:' . $amount]);
  167. //发放
  168. (new LedgerWalletModel)->changeWalletAccount($info['user_id'], Asset::TOKEN, $amount, LedgerTokenChangeModel::SysSend);
  169. }
  170. }
  171. return $log_id;
  172. }
  173. /**
  174. * 获取可用日期
  175. * @var array
  176. */
  177. protected function getExeDate($type_id = 1)
  178. {
  179. $today = date('Y-m-d',strtotime("-1day"));//当前时间减一天为最后一次可用时间
  180. $dateInfo = DB::table('timed_task_log')
  181. ->where('type_id' ,$type_id)
  182. ->where('status', 1)
  183. ->order('date_time', 'desc')
  184. ->find();
  185. if(empty($dateInfo)){//首次
  186. return $today;
  187. }
  188. $new_date = date('Y-m-d',strtotime("+1day",strtotime($dateInfo['date_time'])));//最后一次发放日期+1天为本次执行时间
  189. if($new_date > $today){//超过今天
  190. return "";
  191. }
  192. return $new_date;
  193. }
  194. }