| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace addons\Achievement\console\controllers;
- use addons\Achievement\common\logic\AchievementLogic;
- use addons\Achievement\common\logic\AchievementOrderLogic;
- use addons\Achievement\common\models\AchievementGrantLog;
- use addons\Achievement\common\models\AchievementAwardLog;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\LockHelper;
- use common\models\mall\Mall;
- use common\models\order\Order;
- use yii\console\Controller;
- use yii\console\ExitCode;
- class AchievementCrontabController extends Controller
- {
- /**
- * 业绩进入待结算
- *
- */
- // 0 */1 * * * *
- public function actionAutoToBeSettledTimer()
- {
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_ACHIEVEMENT_TO_BE_SETTLED);
- $identification = uniqid();
- try {
- \Yii::$app->db->close();
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('业绩进入待结算,当前访问过多');
- // 获取当前系统所有可用的 mall_id
- $now = time();
- // $now ='1647219869';//调测使用
- $grant_list = AchievementGrantLog::find()->where(['is_wait' => 0, 'status' => StatusEnum::ENABLED])
- ->andWhere(['<=', 'wait_settlement_time', $now])
- ->all();
- if (!empty($grant_list)) {
- foreach ($grant_list as $grant) {
- $grant->is_wait = 1;
- $grant->save();
- // $res = AchievementLogic::toBeSettled($grant['id']);
- // if ($res === false) throw new \Exception(AchievementLogic::getStaticError());
- //更新所有不结算的数据 奖励金额为0
- AchievementAwardLog::updateSkipSettlement($grant['id']);
- //更新周期统计
- AchievementOrderLogic::updateGrant($grant);
- }
- }
- LockHelper::uLock($lock_key,$identification);
- return ExitCode::OK;
- } catch (\Exception $e) {
- LockHelper::uLock($lock_key,$identification);
- $errmsg = '业绩进入待结算: errmsg => ' . $e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine();
- echo $errmsg . PHP_EOL;
- \Yii::error($errmsg);
- \Yii::getLogger()->flush(true);
- return false;
- }
- }
- /**
- * 定时任务,结算业绩
- */
- public static function actionAutoGrantAchievement()
- {
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_ACHIEVEMENT_GRANT);
- $identification = uniqid();
- try {
- \Yii::$app->db->close();
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('结算业绩,当前访问过多');
- $now = time();
- // $now ='1647219869';//调测使用
- $grant_list = AchievementGrantLog::find()->where(['is_wait' => 1, 'is_settlement' => 0, 'status' => StatusEnum::ENABLED])
- ->andWhere(['<=', 'settlement_time', $now])->all();
- if (!empty($grant_list)) {
- foreach ($grant_list as $grant) {
- if ($grant['is_award_need_confirm']) {
- //开启业绩审核确认模式先不处理
- continue;
- }
- $res = AchievementLogic::grantAchievement($grant['id']);
- if ($res === false) throw new \Exception(AchievementLogic::getStaticError());
- }
- }
- LockHelper::uLock($lock_key,$identification);
- } catch (\Exception $e) {
- LockHelper::uLock($lock_key,$identification);
- $errmsg = '结算业绩: errmsg => ' . $e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine();
- echo $errmsg . PHP_EOL;
- \Yii::error($errmsg);
- \Yii::getLogger()->flush(true);
- return false;
- }
- }
- /**
- * 定时任务,整点添加规则
- */
- public static function actionAutoAddGrantLog()
- {
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_ACHIEVEMENT_ADD_GRANT_LOG);
- $identification = uniqid();
- try {
- \Yii::$app->db->close();
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('整点添加规则,当前访问过多');
- $malls = Mall::getEnableMallList();
- $mall_ids = array_column($malls, 'id');
- echo ' 获取到的 mall_id => ' . implode(', ', $mall_ids) . PHP_EOL;
- foreach ($mall_ids as $mall_id) {
- $res = AchievementLogic::addGrantLog($mall_id);
- if ($res === false) throw new \Exception(AchievementLogic::getStaticError());
- }
- LockHelper::uLock($lock_key,$identification);
- } catch (\Exception $e) {
- LockHelper::uLock($lock_key,$identification);
- $errmsg = '整点添加规则: errmsg => ' . $e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine();
- echo $errmsg . PHP_EOL;
- \Yii::error($errmsg);
- \Yii::getLogger()->flush(true);
- return false;
- }
- }
- public function actionTest()
- {
- // 13899 13900 13901
- $order = Order::findOne(13901);
- AchievementOrderLogic::orderStatistics($order);//统计业绩
- }
- }
|