AchievementCrontabController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace addons\Achievement\console\controllers;
  3. use addons\Achievement\common\logic\AchievementLogic;
  4. use addons\Achievement\common\logic\AchievementOrderLogic;
  5. use addons\Achievement\common\models\AchievementGrantLog;
  6. use addons\Achievement\common\models\AchievementAwardLog;
  7. use common\enums\RedisKeyEnum;
  8. use common\enums\StatusEnum;
  9. use common\helpers\LockHelper;
  10. use common\models\mall\Mall;
  11. use common\models\order\Order;
  12. use yii\console\Controller;
  13. use yii\console\ExitCode;
  14. class AchievementCrontabController extends Controller
  15. {
  16. /**
  17. * 业绩进入待结算
  18. *
  19. */
  20. // 0 */1 * * * *
  21. public function actionAutoToBeSettledTimer()
  22. {
  23. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_ACHIEVEMENT_TO_BE_SETTLED);
  24. $identification = uniqid();
  25. try {
  26. \Yii::$app->db->close();
  27. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('业绩进入待结算,当前访问过多');
  28. // 获取当前系统所有可用的 mall_id
  29. $now = time();
  30. // $now ='1647219869';//调测使用
  31. $grant_list = AchievementGrantLog::find()->where(['is_wait' => 0, 'status' => StatusEnum::ENABLED])
  32. ->andWhere(['<=', 'wait_settlement_time', $now])
  33. ->all();
  34. if (!empty($grant_list)) {
  35. foreach ($grant_list as $grant) {
  36. $grant->is_wait = 1;
  37. $grant->save();
  38. // $res = AchievementLogic::toBeSettled($grant['id']);
  39. // if ($res === false) throw new \Exception(AchievementLogic::getStaticError());
  40. //更新所有不结算的数据 奖励金额为0
  41. AchievementAwardLog::updateSkipSettlement($grant['id']);
  42. //更新周期统计
  43. AchievementOrderLogic::updateGrant($grant);
  44. }
  45. }
  46. LockHelper::uLock($lock_key,$identification);
  47. return ExitCode::OK;
  48. } catch (\Exception $e) {
  49. LockHelper::uLock($lock_key,$identification);
  50. $errmsg = '业绩进入待结算: errmsg => ' . $e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine();
  51. echo $errmsg . PHP_EOL;
  52. \Yii::error($errmsg);
  53. \Yii::getLogger()->flush(true);
  54. return false;
  55. }
  56. }
  57. /**
  58. * 定时任务,结算业绩
  59. */
  60. public static function actionAutoGrantAchievement()
  61. {
  62. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_ACHIEVEMENT_GRANT);
  63. $identification = uniqid();
  64. try {
  65. \Yii::$app->db->close();
  66. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('结算业绩,当前访问过多');
  67. $now = time();
  68. // $now ='1647219869';//调测使用
  69. $grant_list = AchievementGrantLog::find()->where(['is_wait' => 1, 'is_settlement' => 0, 'status' => StatusEnum::ENABLED])
  70. ->andWhere(['<=', 'settlement_time', $now])->all();
  71. if (!empty($grant_list)) {
  72. foreach ($grant_list as $grant) {
  73. if ($grant['is_award_need_confirm']) {
  74. //开启业绩审核确认模式先不处理
  75. continue;
  76. }
  77. $res = AchievementLogic::grantAchievement($grant['id']);
  78. if ($res === false) throw new \Exception(AchievementLogic::getStaticError());
  79. }
  80. }
  81. LockHelper::uLock($lock_key,$identification);
  82. } catch (\Exception $e) {
  83. LockHelper::uLock($lock_key,$identification);
  84. $errmsg = '结算业绩: errmsg => ' . $e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine();
  85. echo $errmsg . PHP_EOL;
  86. \Yii::error($errmsg);
  87. \Yii::getLogger()->flush(true);
  88. return false;
  89. }
  90. }
  91. /**
  92. * 定时任务,整点添加规则
  93. */
  94. public static function actionAutoAddGrantLog()
  95. {
  96. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_ACHIEVEMENT_ADD_GRANT_LOG);
  97. $identification = uniqid();
  98. try {
  99. \Yii::$app->db->close();
  100. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('整点添加规则,当前访问过多');
  101. $malls = Mall::getEnableMallList();
  102. $mall_ids = array_column($malls, 'id');
  103. echo ' 获取到的 mall_id => ' . implode(', ', $mall_ids) . PHP_EOL;
  104. foreach ($mall_ids as $mall_id) {
  105. $res = AchievementLogic::addGrantLog($mall_id);
  106. if ($res === false) throw new \Exception(AchievementLogic::getStaticError());
  107. }
  108. LockHelper::uLock($lock_key,$identification);
  109. } catch (\Exception $e) {
  110. LockHelper::uLock($lock_key,$identification);
  111. $errmsg = '整点添加规则: errmsg => ' . $e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine();
  112. echo $errmsg . PHP_EOL;
  113. \Yii::error($errmsg);
  114. \Yii::getLogger()->flush(true);
  115. return false;
  116. }
  117. }
  118. public function actionTest()
  119. {
  120. // 13899 13900 13901
  121. $order = Order::findOne(13901);
  122. AchievementOrderLogic::orderStatistics($order);//统计业绩
  123. }
  124. }