| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\constants\OptionSetting;
- use app\modules\alliance\models\CurrencyForm;
- use app\plugins\scanCodePay\models\Order as ScanCodePayOrder;
- use app\plugins\scanCodePay\models\Order as ScanOrder;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%ac_pool}}".
- *
- * @property integer $id
- * @property integer $main_currency_id
- * @property integer $currency_id
- * @property integer $start_time
- * @property integer $end_time
- * @property float $money
- * @property float $rate
- * @property double $dividend_amount
- * @property double $balance_amount
- * @property integer $is_send
- * @property integer $is_scan
- * @property integer $send_time
- * @property integer $created_at
- * @property integer $updated_at
- */
- class AcPool extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%ac_pool}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['is_send', 'send_time', 'start_time', 'end_time', 'is_scan', 'currency_id', 'main_currency_id'], 'integer'],
- [['money', 'balance_amount', 'dividend_amount', 'rate'], 'number'],
- [['created_at', 'updated_at'], 'safe']
- ];
- }
- /**
- * 增加合伙人分红池
- * @param $order
- * @param $money
- * @param string $desc
- * @return false|void
- */
- public static function poolPush($order, $money, $desc = '', $pay_price = 0, $model = 0)
- {
- $is_scan = $order instanceof ScanCodePayOrder ? 1 : 0;
- //debug_log([__METHOD__, __LINE__, "执行acpoolpush"], "app_debug.log");
- $form = new CurrencyForm();
- $data = $form->Inertupdate_bonus($order->id,$is_scan);
- //debug_log([__METHOD__, __LINE__, "===订单id:".$order->id.',payprice:'.$pay_price], "app_debug.log");
- foreach ($data as $i){
- $dividend = $i['dividend'];
- foreach ($dividend as $j){
- $pool = self::find()->where(['and',['<', 'start_time', time()],['>=', 'end_time', time()],['=', 'is_send', 0],['=', 'currency_id', $j['id']]])
- ->orderBy(['id' => SORT_DESC])->one();
- //debug_log([__METHOD__, __LINE__, "---------------- 开始插入分红池1 -------------------currency_id".$j['id']], "app_debug.log");
- // 如果没有奖金池,重新创建奖金池
- if (!$pool) {
- $time_data = self::getTime($j['id']);
- $pool = new self();
- $pool->start_time = $time_data['start_time'];
- $pool->main_currency_id = $i['main_currency_id'];
- $pool->currency_id = $j['id'];
- $pool->rate= $j['percent'];
- $pool->end_time = $time_data['end_time'];
- $pool->is_send = 0;
- $pool->send_time = 0;
- }
- $before_money = $pool->money ? $pool->money : 0;
- $add_money = $money*$j['percent']/100;
- $pool->money += $add_money;
- if (!$pool->save()) {
- //debug_log([__METHOD__, __LINE__, "---------------- 合伙人奖金池更新失败 -------------------" . json_encode($pool->errors)], "app_debug.log");
- return false;
- }
- $pool_log = new AcPoolDetail();
- $pool_log->order_id = $order->id;
- $pool_log->currency_id = $j['id'];
- $pool_log->order_no = $order->order_no;
- $pool_log->pay_price = $is_scan ? $pay_price : $order->pay_price+$order->take_price;
- $pool_log->store_id = $order->store_id;
- $pool_log->before_money = $before_money;
- $pool_log->after_money = $pool->money;
- $pool_log->total_pv = $money;
- $pool_log->money = $add_money;
- $pool_log->total_price = $order->total_price;
- $pool_log->is_scan = $is_scan;
- $pool_log->pool_id = $pool->id;
- $pool_log->model_type = $model;
- $pool_log->desc = '订单号:' . $order->order_no . '追加';
- if (!$pool_log->save()) {
- //debug_log([__METHOD__, __LINE__, "---------------- 奖金池记录保存失败 -------------------" . json_encode($pool_log->errors)], "app_debug.log");
- return false;
- }
- }
- }
- //debug_log([__METHOD__, __LINE__, "---------------- 开始插入分红池结束 -------------------"], "app_debug.log");
- }
- public static function getTime($currency_id)
- {
- $data = Option::getDecode('currency_dividend_time',$currency_id,'currency');//使用币种id代替store_id
- $interval = $data['interval'] ?? 1;
- $timeunit = $data['timeunit'];
- if (!in_array($timeunit,['minute','hour','day','month'])) $timeunit = 'day';
- switch ($timeunit){
- case 'minute':
- $time = strtotime(date('Y-m-d H:i:00', time()));
- break;
- case 'hour':
- $time = strtotime(date('Y-m-d H:00:00', time()));
- break;
- case 'month':
- $time = strtotime(date('Y-m-1', time()));
- break;
- default:
- $time = strtotime(date('Y-m-d', time()));
- break;
- }
- return [
- 'start_time' => $time,
- 'end_time' => strtotime('+'.$interval.' '.$timeunit,$time)
- ];
- }
- }
|