AcPool.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\constants\OptionSetting;
  9. use app\modules\alliance\models\CurrencyForm;
  10. use app\plugins\scanCodePay\models\Order as ScanCodePayOrder;
  11. use app\plugins\scanCodePay\models\Order as ScanOrder;
  12. use yii\db\ActiveRecord;
  13. use yii\behaviors\TimestampBehavior;
  14. use Yii;
  15. use yii\helpers\Json;
  16. /**
  17. * This is the model class for table "{{%ac_pool}}".
  18. *
  19. * @property integer $id
  20. * @property integer $main_currency_id
  21. * @property integer $currency_id
  22. * @property integer $start_time
  23. * @property integer $end_time
  24. * @property float $money
  25. * @property float $rate
  26. * @property double $dividend_amount
  27. * @property double $balance_amount
  28. * @property integer $is_send
  29. * @property integer $is_scan
  30. * @property integer $send_time
  31. * @property integer $created_at
  32. * @property integer $updated_at
  33. */
  34. class AcPool extends \yii\db\ActiveRecord
  35. {
  36. /**
  37. * @inheritdoc
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%ac_pool}}';
  42. }
  43. public function behaviors()
  44. {
  45. return [
  46. [
  47. 'class' => TimestampBehavior::class,
  48. 'attributes' => [
  49. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  50. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  51. ]
  52. ]
  53. ];
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function rules()
  59. {
  60. return [
  61. [['is_send', 'send_time', 'start_time', 'end_time', 'is_scan', 'currency_id', 'main_currency_id'], 'integer'],
  62. [['money', 'balance_amount', 'dividend_amount', 'rate'], 'number'],
  63. [['created_at', 'updated_at'], 'safe']
  64. ];
  65. }
  66. /**
  67. * 增加合伙人分红池
  68. * @param $order
  69. * @param $money
  70. * @param string $desc
  71. * @return false|void
  72. */
  73. public static function poolPush($order, $money, $desc = '', $pay_price = 0, $model = 0)
  74. {
  75. $is_scan = $order instanceof ScanCodePayOrder ? 1 : 0;
  76. //debug_log([__METHOD__, __LINE__, "执行acpoolpush"], "app_debug.log");
  77. $form = new CurrencyForm();
  78. $data = $form->Inertupdate_bonus($order->id,$is_scan);
  79. //debug_log([__METHOD__, __LINE__, "===订单id:".$order->id.',payprice:'.$pay_price], "app_debug.log");
  80. foreach ($data as $i){
  81. $dividend = $i['dividend'];
  82. foreach ($dividend as $j){
  83. $pool = self::find()->where(['and',['<', 'start_time', time()],['>=', 'end_time', time()],['=', 'is_send', 0],['=', 'currency_id', $j['id']]])
  84. ->orderBy(['id' => SORT_DESC])->one();
  85. //debug_log([__METHOD__, __LINE__, "---------------- 开始插入分红池1 -------------------currency_id".$j['id']], "app_debug.log");
  86. // 如果没有奖金池,重新创建奖金池
  87. if (!$pool) {
  88. $time_data = self::getTime($j['id']);
  89. $pool = new self();
  90. $pool->start_time = $time_data['start_time'];
  91. $pool->main_currency_id = $i['main_currency_id'];
  92. $pool->currency_id = $j['id'];
  93. $pool->rate= $j['percent'];
  94. $pool->end_time = $time_data['end_time'];
  95. $pool->is_send = 0;
  96. $pool->send_time = 0;
  97. }
  98. $before_money = $pool->money ? $pool->money : 0;
  99. $add_money = $money*$j['percent']/100;
  100. $pool->money += $add_money;
  101. if (!$pool->save()) {
  102. //debug_log([__METHOD__, __LINE__, "---------------- 合伙人奖金池更新失败 -------------------" . json_encode($pool->errors)], "app_debug.log");
  103. return false;
  104. }
  105. $pool_log = new AcPoolDetail();
  106. $pool_log->order_id = $order->id;
  107. $pool_log->currency_id = $j['id'];
  108. $pool_log->order_no = $order->order_no;
  109. $pool_log->pay_price = $is_scan ? $pay_price : $order->pay_price+$order->take_price;
  110. $pool_log->store_id = $order->store_id;
  111. $pool_log->before_money = $before_money;
  112. $pool_log->after_money = $pool->money;
  113. $pool_log->total_pv = $money;
  114. $pool_log->money = $add_money;
  115. $pool_log->total_price = $order->total_price;
  116. $pool_log->is_scan = $is_scan;
  117. $pool_log->pool_id = $pool->id;
  118. $pool_log->model_type = $model;
  119. $pool_log->desc = '订单号:' . $order->order_no . '追加';
  120. if (!$pool_log->save()) {
  121. //debug_log([__METHOD__, __LINE__, "---------------- 奖金池记录保存失败 -------------------" . json_encode($pool_log->errors)], "app_debug.log");
  122. return false;
  123. }
  124. }
  125. }
  126. //debug_log([__METHOD__, __LINE__, "---------------- 开始插入分红池结束 -------------------"], "app_debug.log");
  127. }
  128. public static function getTime($currency_id)
  129. {
  130. $data = Option::getDecode('currency_dividend_time',$currency_id,'currency');//使用币种id代替store_id
  131. $interval = $data['interval'] ?? 1;
  132. $timeunit = $data['timeunit'];
  133. if (!in_array($timeunit,['minute','hour','day','month'])) $timeunit = 'day';
  134. switch ($timeunit){
  135. case 'minute':
  136. $time = strtotime(date('Y-m-d H:i:00', time()));
  137. break;
  138. case 'hour':
  139. $time = strtotime(date('Y-m-d H:00:00', time()));
  140. break;
  141. case 'month':
  142. $time = strtotime(date('Y-m-1', time()));
  143. break;
  144. default:
  145. $time = strtotime(date('Y-m-d', time()));
  146. break;
  147. }
  148. return [
  149. 'start_time' => $time,
  150. 'end_time' => strtotime('+'.$interval.' '.$timeunit,$time)
  151. ];
  152. }
  153. }