| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\constants\OptionSetting;
- use app\plugins\scanCodePay\models\Order as ScanOrder;
- use app\models\PluginPoolDetail;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%UnitFounder_pool}}".
- *
- * @property integer $id
- * @property integer $config_id
- * @property integer $store_id
- * @property integer $start_time
- * @property integer $end_time
- * @property float $money
- * @property float $rl_rate
- * @property float $mx_rate
- * @property integer $is_send
- * @property integer $is_scan
- * @property integer $send_time
- * @property integer $created_at
- * @property integer $updated_at
- */
- class PluginPool extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%plugin_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', 'config_id', 'end_time', 'store_id', 'is_scan'], 'integer'],
- [['money', 'rl_rate', 'mx_rate'], 'number'],
- [['created_at', 'updated_at'], 'safe']
- ];
- }
- /**
- * 增加合伙人分红池
- * @param $order
- * @param $money
- * @param string $desc
- * @return false|void
- */
- public static function poolPush($config_id, $order, $money, $desc = '', $pay_price = 0, $rl_rate = 0, $mx_rate = 0, $model = 0, $add_money = 0)
- {
- $is_scan = $order instanceof ScanOrder ? 1 : 0;
- if ($is_scan) {
- $store_id = $order->store_id;
- $bd_admin = Admin::findOne(['store_id' => $order->store_id, 'type' => 'bd_agent', 'is_delete' => 0, 'is_enable' => 1]);
- if (empty($bd_admin)) {
- $admin_id = Salesman::getBdId($order->store_id);
- if ($admin_id) {
- $bd_admin = Admin::findOne(['id' => $admin_id, 'type' => 'bd_agent', 'is_delete' => 0, 'is_enable' => 1]);
- $store_id = $bd_admin->store_id;
- }
- }
- }else{
- $config = PluginPoolConfig::findOne($config_id);
- $store_id = $config->store_id;
- }
- $query = self::find()->where(['and',
- [
- '<', 'start_time', time()
- ],
- [
- '>=', 'end_time', time()
- ],
- [
- '=', 'config_id', $config_id
- ],
- [
- '=', 'is_send', 0
- ],
- [
- '=', 'store_id', $store_id
- ]
- ]);
- $bd_setting = [];
- if ($bd_admin) {
- $bd_setting = Option::getDecode(OptionSetting::SHARE_STRING_CODE_SALE_SETTING_BD, $bd_admin->id, OptionSetting::SHARE_GROUP_NAME);
- }
- $store_setting = PluginPoolConfig::poolSetting($store_id);
- $store_award = $store_setting['pool_award_type'] ?? 1;
- $bd_award = $bd_setting['pool_award_type'] ?? 1;
- if ($store_award != $bd_award) {
- $query->andWhere(['=', 'is_scan', $is_scan]);
- }
- $pool = $query->orderBy(['id' => SORT_DESC])->one();
- // 如果没有奖金池,重新创建奖金池
- if (!$pool) {
- $pool = new self();
- [$start_time, $end_time] = self::getTime($config_id);
- $pool->start_time = $start_time;
- $pool->end_time = $end_time;
- $pool->is_send = 0;
- $pool->send_time = 0;
- $pool->is_scan = $is_scan;
- $pool->store_id = $store_id;
- $pool->mx_rate = $mx_rate;
- $pool->rl_rate = $rl_rate;
- $pool->config_id = $config_id;
- }
- $before_money = $pool->money ? $pool->money : 0;
- $pool->money += $money;
- if (!$pool->save()) {
- throw new \ErrorException("---------------- 合伙人奖金池更新失败 -------------------" . $pool->getErrorSummary(false)[0]);
- }
- $pool_log = new PluginPoolDetail();
- $pool_log->order_id = $order->id;
- $pool_log->config_id = $config_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 = $add_money;
- $pool_log->rl_rate = $rl_rate;
- $pool_log->mx_rate = $mx_rate;
- $pool_log->money = $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 = $desc ?: '订单号:' . $order->order_no . '追加';
- if (!$pool_log->save()) {
- throw new \ErrorException("---------------- 奖金池记录保存失败 -------------------" . $pool_log->getErrorSummary(false)[0]);
- }
- if ($is_scan) {
- switch ($model) {
- case 0:
- ScanOrder::updateAll(['small_model_pv_bonus' => $money], ['id' => $order['id']]);
- break;
- case 1:
- ScanOrder::updateAll(['big_model_pv_bonus' => $money], ['id' => $order['id']]);
- break;
- default:
- break;
- }
- }
- }
- private static function getTime($config_id)
- {
- $config = PluginPoolConfig::findOne($config_id);
- if (empty($config) || empty($config->time_value) || empty($config->time_type)) {
- $start_time = strtotime(date('Y-m-d'));
- //debug_log([__METHOD__, __LINE__, "---------------- -------------------start_time:" . $start_time . "======"], "app_debug.log");
- return [$start_time, strtotime('+1 day', $start_time)];
- }
- // 计算 $start_time
- switch ($config->time_type) {
- case 'minute':
- $start_time = strtotime(date('Y-m-d H:i') . ':00');
- break;
- case 'hour':
- $start_time = strtotime(date('Y-m-d H') . ':00:00');
- break;
- case 'week':
- $start_time = strtotime(date('Y-m-d'), strtotime('monday this week'));
- break;
- case 'month':
- $start_time = strtotime(date('Y-m'));
- break;
- default:
- $start_time = strtotime(date('Y-m-d'));
- break;
- }
- // 计算 $end_time
- $end_time = strtotime('+' . $config->time_value . ' ' . $config->time_type, $start_time) - 1;
- return [$start_time, $end_time];
- }
- }
|