WelfareLoginc.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\api\logic;
  3. use app\api\controller\Product;
  4. use app\common\model\ProductArea;
  5. use app\common\controller\Api;
  6. use app\common\model\UserWelfare;
  7. use app\common\model\ProductPopular;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\UserModel;
  10. use fast\Action;
  11. use fast\Asset;
  12. use fast\MembershipLevel;
  13. use think\Db;
  14. use think\Log;
  15. use think\Exception;
  16. use think\console\Output;
  17. use fast\Http;
  18. /**
  19. * 空投福利
  20. */
  21. class WelfareLoginc
  22. {
  23. /** 添加新人福利
  24. * @param int $uid 用户id
  25. * @param array $productId 产品ID
  26. * @param int $num 数量
  27. * @param int $lan 语言
  28. */
  29. public static function setUserWelfareLos($uid, int $productId, $num, $lan, $typeId)
  30. {
  31. $tim = time();
  32. $result = ProductPopular::getPopularByTime($productId, $lan, $tim);
  33. if(!$result || $num > $result->stock) throw new Exception(__('库存不足'));
  34. if(empty($result->is_area)){
  35. ProductOrder::setPopularNoAreaOrder($num, $result->id, $result->price, $result->product_id, $uid, $typeId);
  36. }else{
  37. $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('RAND()')->limit($num)->column('id');
  38. ProductOrder::setPopularAreaOrder($areaArr, $result->id, $result->price, $result->product_id, $uid, $typeId);
  39. }
  40. return ['start_time'=>$tim, 'name'=>$result->name, 'num'=>$num];
  41. }
  42. /**
  43. * 获取当日收益 Daily earnings
  44. */
  45. private static function getDayEarnings(string $date)
  46. {
  47. $between_time = [strtotime($date), strtotime($date) + 86400];
  48. //读取当日新增数据
  49. return ProductOrder::where('type_id', ProductOrder::Popular)->where('status', ProductOrder::Paid)->whereBetween('create_time', $between_time)->sum('price');
  50. }
  51. /**
  52. * 获取可用日期
  53. * @var array
  54. */
  55. protected function getExeDate($type_id = 1)
  56. {
  57. $today = date('Y-m-d',strtotime("-1day"));//当前时间减一天为最后一次可用时间
  58. $dateInfo = DB::table('timed_task_log')
  59. ->where('type_id' ,$type_id)
  60. ->where('status', 1)
  61. ->order('date_time', 'desc')
  62. ->find();
  63. if(empty($dateInfo)){//首次
  64. return $today;
  65. }
  66. $new_date = date('Y-m-d',strtotime("+1day",strtotime($dateInfo['date_time'])));//最后一次发放日期+1天为本次执行时间
  67. if($new_date > $today){//超过今天
  68. return "";
  69. }
  70. return $new_date;
  71. }
  72. /**
  73. * 插入或更新收益统计信息
  74. * @var array
  75. */
  76. protected function createData($uids, $amount, $date)
  77. {
  78. foreach ($uids as $user_id) {
  79. $check = DB::table('team_rewards')
  80. ->field('id')
  81. ->where('user_id', $user_id)
  82. ->where('date_time', $date)
  83. ->find();
  84. if ($check) {//存在,则更新
  85. DB::table('team_rewards')
  86. ->where('id', $check['id'])
  87. ->update([
  88. 'today_power' => Db::raw('today_power+' . $amount),
  89. 'update_time' => date('Y-m-d H:i:s')
  90. ]);
  91. } else {//新增
  92. DB::table('team_rewards')
  93. ->insert([
  94. 'user_id' => $user_id,
  95. 'date_time' => $date,
  96. 'today_power' => Db::raw('today_power+' . $amount),
  97. 'create_time' => date('Y-m-d H:i:s')
  98. ]);
  99. }
  100. }
  101. }
  102. }