public function actionIndex() { if (Date::isWorkingDay()) { $limit = (int) System::loadConfig('transaction_rule'); if ($limit) { $limit = $limit - 1; } else { $limit = 0; } if ($limit == 0) { $date = date('Y-m-d'); } else { $dates = Date::find()->where(['<', 'date', new Expression('curdate()')])->andWhere(['=', 'status', 0])->orderBy(['date' => SORT_DESC])->limit(2)->all(); $date = array_pop($dates); $date = $date->date; } $date .= ' 23:59:59'; $transactions = StackTransaction::find()->where(['=', 'status', 0])->andWhere(['<', 'created_at', $date])->all(); foreach ($transactions as $transaction) { if ($transaction->type == 0) { $this->dealBuyAction($transaction); } else { $this->dealSellAction($transaction); } } } else { return false; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = StackTransaction::find()->joinWith(['stack' => function ($query) { $query->from(['stack' => 'stack']); }])->joinWith(['member' => function ($query) { $query->from(['member' => 'member']); }])->orderBy(['created_at' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!Yii::$app->user->identity->isAdmin()) { $this->member_id = Yii::$app->user->identity->id; } if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } if ($this->created_at) { $date = explode(' - ', $this->created_at); if (count($date) == 2) { $query->andFilterWhere(['>=', $this::tableName() . '.created_at', $date[0] . ' 00:00:00']); $query->andFilterWhere(['<=', $this::tableName() . '.created_at', $date[1] . ' 23:59:59']); } } if ($this->updated_at) { $date = explode(' - ', $this->updated_at); if (count($date) == 2) { $query->andFilterWhere(['>=', $this::tableName() . '.updated_at', $date[0] . ' 00:00:00']); $query->andFilterWhere(['<=', $this::tableName() . '.updated_at', $date[1] . ' 23:59:59']); } } $query->andFilterWhere(['id' => $this->id, 'stack_id' => $this->stack_id, 'member_id' => $this->member_id, 'volume' => $this->volume, 'type' => $this->type, 'total_price' => $this->total_price, 'stack_transaction.status' => $this->status, 'total' => $this->total])->andFilterWhere(['like', 'stack.code', $this->stackcode])->andFilterWhere(['like', 'stack.name', $this->stackname])->andFilterWhere(['like', 'member.username', $this->membername])->orderBy(['created_at' => SORT_DESC]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getStackTransactions() { return $this->hasMany(StackTransaction::className(), ['stack_id' => 'id']); }
public function actionSell($id) { $stack = Stack::findOne($id); $model = new StackTransaction(); $memberStack = Yii::$app->user->identity->getMemberStack($stack->id); if ($model->load(Yii::$app->request->post())) { if (Date::isWorkingDay()) { if (Date::isWorkingTime()) { if ($model->account_type) { $data = Yii::$app->request->post(); $validate = true; if (!Yii::$app->user->identity->validatePassword2($data['StackTransaction']['password2'])) { $validate = false; $model->addError('password2', '第二密码不正确, 请确认后重新输入.'); } if (!$model->checkSellVolume($memberStack, $model->volume)) { $validate = false; } if ($validate) { $model->price = $stack->price; $model->member_id = Yii::$app->user->identity->id; $model->stack_id = $stack->id; $model->type = 1; $model->total_price = $stack->price * $model->volume; $memberStack->sell_volume -= $model->volume; $memberStack->lock_volume += $model->volume; $connection = Yii::$app->db; try { $transaction = $connection->beginTransaction(); if ($model->save() && $memberStack->save()) { $transaction->commit(); return $this->redirect(['transactions']); } else { Yii::error('Sell Stack Failed'); Yii::error(json_encode($model->getErrors())); Yii::error(json_encode($memberStack->getErrors())); $transaction->rollback(); } } catch (Exception $e) { } } } else { $model->total_price = $stack->price * $model->volume; } } else { Yii::$app->session->setFlash('danger', '非交易时间. 早上10:00 ~ 12:30. 下午2:00 ~ 4:00'); } } else { Yii::$app->session->setFlash('danger', '对不起,非交易日不能进行交易!'); } } return $this->render('sell', ['model' => $model, 'stack' => $stack, 'memberStack' => $memberStack]); }