AwardLake.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\common\model\user;
  3. use app\common\model\BaseModel;
  4. use app\common\repositories\user\AwardIntegralPriceRepository;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\Exception;
  9. class Awardlake extends BaseModel
  10. {
  11. public static function tablePk(): ?string
  12. {
  13. return 'id';
  14. }
  15. public static function tableName(): string
  16. {
  17. return 'award_integral_price';
  18. }
  19. /**
  20. * @param int $lake_type 奖池类型 1:节能油,2:礼包
  21. * @param float $order_price 订单价格
  22. * @param int $order_id 订单ID
  23. * @return true
  24. * @throws Exception
  25. * @throws DataNotFoundException
  26. * @throws DbException
  27. * @throws ModelNotFoundException
  28. */
  29. public function addOrderLakes($lake_type, $order_price, $order_id)
  30. {
  31. switch ($lake_type) {
  32. case 1:
  33. $model = new OilLevel();
  34. break;
  35. case 2:
  36. $model = new GiftLevel();
  37. break;
  38. default :
  39. throw new Exception('奖池类型错误');
  40. }
  41. $levels = $model->select();
  42. foreach ($levels as $level) {
  43. $lake = self::where('level', $level['id'])->where('type', $lake_type)
  44. ->find();
  45. if (!$lake) $lake = self::create([
  46. 'level' => $level['id'],
  47. 'type' => $lake_type,
  48. 'award_name' => $level['name'] . '分红',
  49. 'num' => 0,
  50. 'send_time' => time()
  51. ]);
  52. $award = bcdiv(bcmul((string)$order_price, (string)$level['award_ratio']), '100', 2);
  53. AwardLakeLog::income($lake['id'], 'order_add', $award, $order_id, '用户购买商品添加分红池');
  54. }
  55. return true;
  56. }
  57. public function autoSend()
  58. {
  59. $time = strtotime(date('Y-m-d 01:00:00'));
  60. if (time() < $time) {
  61. return true;
  62. }
  63. $lakes = self::where('send_time', '<', $time)->select();
  64. foreach ($lakes as $lake) {
  65. $this->sendAward($lake['id']);
  66. }
  67. }
  68. public function sendAward($lake_id)
  69. {
  70. $lake = self::where('id', $lake_id)->find();
  71. if (!$lake) return true;
  72. if ($lake['num'] <= 0) {
  73. self::where('id', $lake['id'])->update(['send_time' => time()]);
  74. return true;
  75. }
  76. switch ($lake['type']) {
  77. case 1:
  78. $model = new OilLevel();
  79. $field = 'oil_level';
  80. break;
  81. case 2:
  82. $model = new GiftLevel();
  83. $field = 'gift_level';
  84. break;
  85. default :
  86. $model = new OilLevel();
  87. $field = 'oil_level';
  88. }
  89. $level = $model->where('id', $lake['level'])->find();
  90. $levels = $model->where('grade', '>=', $level['grade'])->column('id');
  91. $users = User::where($field, 'in', $levels)->where('status', 1)->select();
  92. $every = bcdiv($lake['num'], count($users), 2);
  93. if ($every <= 0) {
  94. self::where('id', $lake['id'])->update(['send_time' => time()]);
  95. return true;
  96. }
  97. $real_send = 0;
  98. $price = app()->make(AwardIntegralPriceRepository::class)->awardIntegralPrice();
  99. foreach ($users as $user) {
  100. //todo 添加用户积分
  101. //todo end
  102. $real_send = bcadd($real_send, $every, 2);
  103. }
  104. AwardLakeLog::expend($lake['id'], 'send', $real_send, 0, '奖池分红');
  105. self::where('id', $lake['id'])->update(['send_time' => time()]);
  106. }
  107. }