services->setting->addons->get($mallId, HappinessEnum::ADDONS_NAME, 'basic'); if ($config['compute_period'] == 4) { continue; } // 奖金池 list( 'continue' => $continue, 'start_day' => $start_day, 'end_day' => $end_day, 'compute_period' => $compute_period ) = self::getExecutePeriod($config, 'compute_period', $mallId); if ($continue) continue; $date_limit = $start_day . '-' . $end_day; $job = HappinessWeightPoolJob::findOne(['mall_id' => $mallId, 'compute_period' => $compute_period, 'date_limit' => $date_limit, 'status' => StatusEnum::ENABLED]); if ($job) continue; // 生成执行记录 $job = new HappinessWeightPoolJob(); $job->mall_id = $mallId; $job->date_limit = $date_limit; $job->compute_period = $compute_period; $job->total_amount = 0; $job->period_total_amount = 0; $last_period = HappinessWeightPoolJob::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED]) ->select('period')->orderBy('id DESC')->scalar(); $period = $last_period ? $last_period + 1 : 1; $job->period = $period; if (!$job->save()) throw new Exception($job->getErrorMessage()); $service = new WeightService(); $service->send($mallId, $config, $start_day, $end_day, $job->id); } return true; } } public static function executeNoAuto($mallId) { // 获取基础配置 if (!MallAddons::isInstall($mallId, HappinessEnum::ADDONS_NAME)) { throw new Exception('插件未安装'); } $config = Yii::$app->services->setting->addons->get($mallId, HappinessEnum::ADDONS_NAME, 'basic'); if ($config['compute_period'] != 4) { throw new Exception('只能手动执行'); } // 奖金池 list( 'continue' => $continue, 'start_day' => $start_day, 'end_day' => $end_day, 'compute_period' => $compute_period ) = self::getExecutePeriod($config, 'compute_period', $mallId); $date_limit = $start_day . '-' . $end_day; $job = HappinessWeightPoolJob::findOne(['mall_id' => $mallId, 'compute_period' => $compute_period, 'date_limit' => $date_limit, 'status' => StatusEnum::ENABLED]); if ($job) throw new Exception('记录已经存在'); // 生成执行记录 $job = new HappinessWeightPoolJob(); $job->mall_id = $mallId; $job->date_limit = $date_limit; $job->compute_period = $compute_period; $job->total_amount = 0; $job->period_total_amount = 0; $last_period = HappinessWeightPoolJob::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED]) ->select('period')->orderBy('id DESC')->scalar(); $period = $last_period ? $last_period + 1 : 1; $job->period = $period; if (!$job->save()) throw new Exception($job->getErrorMessage()); $service = new WeightService(); return $service->send($mallId, $config, $start_day, $end_day, $job->id); } public static function getExecutePeriod($config, $key, $mall_id) { $continue = false; $start_day = 0; $end_day = 0; // 获取结算周期,0=天,1=周,2=月 $compute_period = $config[$key]; switch ($config[$key]) { case 1: $start_day = date("Ymd", strtotime("-1 day")); // $start_day = date("Ymd"); $end_day = $start_day; break; case 2: if (date('w') != 1) $continue = true; // 非周一直接退出 $start_day = date("Ymd", strtotime("last Monday")); $end_day = date('Ymd', strtotime('-1 sunday', time()));; break; case 3: if (date('d') != 1) $continue = true; // 非当月1号直接退出 (测试时注释,否则需要改服务器时间) $start_day = date("Ym01", strtotime("-1 month")); $end_day = date('Ymd', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400); break; case 4: //自定义结算 $has = HappinessWeightPoolJob::find()->where(['mall_id' => $mall_id])->orderBy('id desc')->one();//最后结算的 if (!$has) { $has = HappinessWeightPoolOrderLog::find()->where(['mall_id' => $mall_id])->orderBy('id asc') ->one();//最先的订单 } $start_day = null; if (!$has) { $start_day = date("YmdHis", time() - 86400); } else { $start_day = date("YmdHis", $has->created_at); } $end_day = date("YmdHis", time()); break; } return compact('continue', 'start_day', 'end_day', 'compute_period'); } protected function getLogPath($type) { return Yii::getAlias('@runtime') . "/logs/Happiness/" . $type . '/' . date('Y_m_d') . '.txt'; } }