AccountController::actionCreate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace app\modules\v1\controllers;
4
5
use app\core\models\Account;
6
use app\core\services\AccountService;
7
use app\core\traits\ServiceTrait;
8
use app\core\types\AccountStatus;
9
use app\core\types\AccountType;
10
use Exception;
11
use Yii;
12
use yii\web\NotFoundHttpException;
13
use yiier\helpers\Setup;
14
15
/**
16
 * Account controller for the `v1` module
17
 */
18
class AccountController extends ActiveController
19
{
20
    use ServiceTrait;
21
22
    public $modelClass = Account::class;
23
    public $noAuthActions = [];
24
    public $defaultOrder = ['sort' => SORT_ASC, 'id' => SORT_DESC];
25
    public $partialMatchAttributes = ['name'];
26
    public $stringToIntAttributes = ['type' => AccountType::class, 'status' => AccountStatus::class];
27
28
    public function actions()
29
    {
30
        $actions = parent::actions();
31
        // 注销系统自带的实现方法
32
        unset($actions['update'], $actions['create']);
33
        return $actions;
34
    }
35
36
    /**
37
     * @return Account
38
     * @throws Exception
39
     */
40
    public function actionCreate()
41
    {
42
        $params = Yii::$app->request->bodyParams;
43
        $model = new Account();
44
        $model->user_id = 0;
45
        if (data_get($params, 'type') == AccountType::CREDIT_CARD) {
46
            $model->setScenario(AccountType::CREDIT_CARD);
47
        }
48
        /** @var Account $model */
49
        $model = $this->validate($model, $params);
50
51
        return $this->accountService->createUpdate($model);
52
    }
53
54
    /**
55
     * @param int $id
56
     * @return Account
57
     * @throws NotFoundHttpException
58
     * @throws Exception
59
     */
60
    public function actionUpdate(int $id)
61
    {
62
        $params = Yii::$app->request->bodyParams;
63
        if (!$model = AccountService::findCurrentOne($id)) {
64
            throw new NotFoundHttpException();
65
        }
66
67
        if (data_get($params, 'type') == AccountType::CREDIT_CARD) {
68
            $model->setScenario(AccountType::CREDIT_CARD);
69
        }
70
        /** @var Account $model */
71
        $model = $this->validate($model, $params);
72
73
        return $this->accountService->createUpdate($model);
74
    }
75
76
77
    /**
78
     * @return array
79
     * @throws Exception
80
     */
81
    public function actionTypes()
82
    {
83
        $items = [];
84
        $texts = AccountType::texts();
85
        foreach (AccountType::names() as $key => $name) {
86
            $items[] = ['type' => $name, 'name' => data_get($texts, $key)];
87
        }
88
        return $items;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function actionOverview()
95
    {
96
        $balanceCentSum = Account::find()
97
            ->where(['user_id' => Yii::$app->user->id, 'exclude_from_stats' => false])
98
            ->sum('balance_cent');
99
        $items['net_asset'] = $balanceCentSum ? Setup::toYuan($balanceCentSum) : 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = array(); before regardless.
Loading history...
100
101
        $balanceCentSum = Account::find()
102
            ->where(['user_id' => Yii::$app->user->id, 'exclude_from_stats' => false])
103
            ->andWhere(['>', 'balance_cent', 0])
104
            ->sum('balance_cent');
105
        $items['total_assets'] = $balanceCentSum ? Setup::toYuan($balanceCentSum) : 0;
106
107
        $balanceCentSum = Account::find()
108
            ->where(['user_id' => Yii::$app->user->id, 'exclude_from_stats' => false])
109
            ->andWhere(['<', 'balance_cent', 0])
110
            ->sum('balance_cent');
111
        $items['liabilities'] = $balanceCentSum ? Setup::toYuan($balanceCentSum) : 0;
112
113
        $items['count'] = Account::find()
114
            ->where(['user_id' => Yii::$app->user->id, 'exclude_from_stats' => false])
115
            ->count('id');
116
117
        return $items;
118
    }
119
}
120