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]; } }