AwardLake.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\common\model\user;
  3. use app\common\model\BaseModel;
  4. use app\common\repositories\user\AwardIntegralPriceRepository;
  5. use app\common\repositories\user\UserBillRepository;
  6. use app\common\repositories\user\UserRepository;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Exception;
  11. class AwardLake extends BaseModel
  12. {
  13. public static function tablePk(): ?string
  14. {
  15. return 'id';
  16. }
  17. public static function tableName(): string
  18. {
  19. return 'award_lake';
  20. }
  21. /**
  22. * 奖池累加
  23. * @param int $lake_type 奖池类型 1:节能油,2:礼包
  24. * @param float $order_price 订单价格
  25. * @param int $order_id 订单ID
  26. * @return true
  27. * @throws Exception
  28. * @throws DataNotFoundException
  29. * @throws DbException
  30. * @throws ModelNotFoundException
  31. */
  32. public function addOrderLakes($lake_type, $order_price, $order_id)
  33. {
  34. switch ($lake_type) {
  35. case 1:
  36. $model = new OilLevel();
  37. break;
  38. case 2:
  39. $model = new GiftLevel();
  40. break;
  41. default :
  42. throw new Exception('奖池类型错误');
  43. }
  44. $levels = $model->select();
  45. foreach ($levels as $level) {
  46. $lake = self::where('level', $level['id'])->where('type', $lake_type)
  47. ->find();
  48. if (!$lake) $lake = self::create([
  49. 'level' => $level['id'],
  50. 'type' => $lake_type,
  51. 'award_name' => $level['name'] . '分红',
  52. 'num' => 0,
  53. 'send_time' => time()
  54. ]);
  55. $award = bcdiv(bcmul((string)$order_price, (string)$level['award_ratio']), '100', 2);
  56. AwardLakeLog::income($lake['id'], 'order_add', $award, $order_id, '用户购买商品添加分红池');
  57. }
  58. return true;
  59. }
  60. //30秒执行一次
  61. public function autoSend()
  62. {
  63. $time = strtotime(date('Y-m-d 01:00:00'));
  64. if (time() < $time) {
  65. return true;
  66. }
  67. $lakes = self::where('send_time', '<', $time)->select();
  68. foreach ($lakes as $lake) {
  69. $this->sendAward($lake['id']);
  70. }
  71. return true;
  72. }
  73. public function handSend()
  74. {
  75. // $time = strtotime(date('Y-m-d 01:00:00'));
  76. // if (time() < $time) {
  77. // return true;
  78. // }
  79. $time= time();
  80. $lakes = self::where('send_time', '<', $time)->select();
  81. // return $lakes;
  82. foreach ($lakes as $lake) {
  83. $this->sendAward($lake['id']);
  84. }
  85. return '发送成功';
  86. }
  87. public function sendAward($lake_id)
  88. {
  89. try{
  90. $lake = self::where('id', $lake_id)->find();
  91. if (!$lake) return true;
  92. if ($lake['num'] <= 0) {
  93. self::where('id', $lake['id'])->update(['send_time' => time()]);
  94. return true;
  95. }
  96. switch ($lake['type']) {
  97. case 1:
  98. $model = new OilLevel();
  99. $field = 'oil_level';
  100. $name = '节能油';
  101. $title = '节能油积分分红';
  102. break;
  103. case 2:
  104. $model = new GiftLevel();
  105. $field = 'gift_level';
  106. $name = '礼包';
  107. $title = '礼包积分分红';
  108. break;
  109. default :
  110. $model = new OilLevel();
  111. $field = 'oil_level';
  112. $name = '节能油';
  113. $title = '节能油积分分红';
  114. }
  115. $level = $model->where('id', $lake['level'])->find();
  116. $levels = $model->where('grade', '>=', $level['grade'])->column('id');
  117. $users = User::where($field, 'in', $levels)->where('status', 1)->select();
  118. if (count($users) <= 0){
  119. return true;
  120. }
  121. @file_put_contents('quanju.txt',json_encode($users)."-参加分红的人员\r\n",8);
  122. @file_put_contents('quanju.txt',$lake_id."-分红奖池id\r\n",8);
  123. $every = bcdiv($lake['num'], count($users), 2); //每个用户分得的奖励金额
  124. if ($every <= 0) {
  125. self::where('id', $lake['id'])->update(['send_time' => time()]);
  126. return true;
  127. }
  128. $real_send = 0;
  129. $price = app()->make(AwardIntegralPriceRepository::class)->awardIntegralPrice(); //分红积分价格
  130. @file_put_contents('quanju.txt',json_encode($price)."-分红积分价格\r\n",8);
  131. @file_put_contents('quanju.txt',$every."-每个用户分得的金额\r\n",8);
  132. foreach ($users as $user) {
  133. @file_put_contents('quanju.txt',$user['uid']."-参与分红的uid\r\n",8);
  134. //todo 添加用户积分 用户分的奖励除以积分价格就是积分数量
  135. $integral = bcdiv($every, $price['price'], 2);
  136. $award_integral=User::where('uid', $user['uid'])->value('award_integral');
  137. // if ($award_integral>0){
  138. $after = bcadd($award_integral, $integral, 2);
  139. // 创建用户账单仓库实例
  140. $make = app()->make(UserBillRepository::class);
  141. if ($integral>0){
  142. $make->incBill($user['uid'], 'award_integral', 'oil_integral', [
  143. 'number' => $integral,
  144. 'title' => $title,
  145. 'balance' => $after,
  146. 'status' => 1,
  147. 'link_id' => 0,
  148. 'mark' => $user['nickname'] . '获得' .$name.'分红积分'.$integral ,
  149. ]);
  150. }
  151. User::where('uid', $user['uid'])->update(['award_integral' => $after]);
  152. // }
  153. //todo end
  154. $real_send = bcadd($real_send, $every, 2);
  155. $UserRepository = app()->make(UserRepository::class);
  156. $UserRepository->checkAward($user['uid']);
  157. }
  158. AwardLakeLog::expend($lake['id'], 'send', $real_send, 0, '奖池分红');
  159. self::where('id', $lake['id'])->update(['send_time' => time()]);
  160. }catch (Exception $e){
  161. @file_put_contents('quanju.txt',$e->getMessage()."-错误信息\r\n",8);
  162. @file_put_contents('quanju.txt',$e->getLine()."-错误位置\r\n",8);
  163. }
  164. }
  165. }