1 | <?php |
||
2 | |||
3 | namespace app\commands; |
||
4 | |||
5 | use app\core\exceptions\ThirdPartyServiceErrorException; |
||
6 | use app\core\models\AuthClient; |
||
7 | use app\core\models\Recurrence; |
||
8 | use app\core\models\User; |
||
9 | use app\core\services\RecurrenceService; |
||
10 | use app\core\traits\ServiceTrait; |
||
11 | use app\core\types\AnalysisDateType; |
||
12 | use app\core\types\AuthClientType; |
||
13 | use app\core\types\RecurrenceStatus; |
||
14 | use Yii; |
||
15 | use yii\base\InvalidConfigException; |
||
16 | use yii\console\Controller; |
||
17 | use yii\db\Exception; |
||
18 | use yii\web\NotFoundHttpException; |
||
19 | |||
20 | class CrontabController extends Controller |
||
21 | { |
||
22 | use ServiceTrait; |
||
23 | |||
24 | /** |
||
25 | * @throws InvalidConfigException |
||
26 | * @throws Exception |
||
27 | * @throws NotFoundHttpException|ThirdPartyServiceErrorException |
||
28 | */ |
||
29 | public function actionRecurrence() |
||
30 | { |
||
31 | $items = Recurrence::find() |
||
32 | ->where(['status' => RecurrenceStatus::ACTIVE]) |
||
33 | ->andWhere(['execution_date' => Yii::$app->formatter->asDatetime('now', 'php:Y-m-d')]) |
||
34 | ->asArray() |
||
35 | ->all(); |
||
36 | $transaction = Yii::$app->db->beginTransaction(); |
||
37 | try { |
||
38 | foreach ($items as $item) { |
||
39 | \Yii::$app->user->setIdentity(User::findOne($item['user_id'])); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
40 | if ($t = $this->transactionService->copy($item['transaction_id'], $item['user_id'])) { |
||
41 | $keyboard = $this->telegramService->getRecordMarkup($t); |
||
42 | $text = $this->telegramService->getMessageTextByTransaction($t, '定时记账成功'); |
||
43 | $this->telegramService->sendMessage($text, $keyboard); |
||
44 | $this->stdout("定时记账成功,transaction_id:{$t->id}\n"); |
||
45 | } |
||
46 | } |
||
47 | RecurrenceService::updateAllExecutionDate(); |
||
48 | $transaction->commit(); |
||
49 | } catch (\Exception $e) { |
||
50 | $this->stdout("定时记账失败:{$e->getMessage()}\n"); |
||
51 | $transaction->rollBack(); |
||
52 | throw $e; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $type |
||
58 | * @throws Exception |
||
59 | */ |
||
60 | public function actionReport(string $type = AnalysisDateType::YESTERDAY) |
||
61 | { |
||
62 | $items = AuthClient::find() |
||
63 | ->where(['type' => AuthClientType::TELEGRAM]) |
||
64 | ->asArray() |
||
65 | ->all(); |
||
66 | $transaction = Yii::$app->db->beginTransaction(); |
||
67 | try { |
||
68 | foreach ($items as $item) { |
||
69 | $this->telegramService->sendReport($item['user_id'], $type); |
||
70 | $this->stdout("定时发送报告成功,user_id:{$item['user_id']}\n"); |
||
71 | } |
||
72 | $transaction->commit(); |
||
73 | } catch (\Exception $e) { |
||
74 | $this->stdout("定时发送报告失败:{$e->getMessage()}\n"); |
||
75 | $transaction->rollBack(); |
||
76 | throw $e; |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 |