public function actionGetback() { $data = Yii::$app->request->post(); if (!(isset($data['phone']) && isset($data['grabcornid']))) { return array('flag' => 0, 'msg' => 'no enough arg!'); } //Users::findOne(['phone'=>$data['phone']]) $grabcorn = Grabcorns::findOne(['id' => $data['grabcornid']]); if (!$grabcorn) { return array('flag' => 0, 'msg' => 'activity not exist!'); } $user = Users::findOne(['phone' => $data['phone']]); if (!$user) { return array('flag' => 0, 'msg' => 'find user fail!'); } // if($grabcorn->winneruserid !=$user->id){ // return array ( // 'flag' => 0, // 'msg' => 'you are not the winner!' // ); // } // if($grabcorn->isgot !=0){ // return array ( // 'flag' => 0, // 'msg' => 'you has got the corn!' // ); // } $back = 0; $back = (new \yii\db\Query())->select('SUM(grabcornrecords.count) as back')->from('grabcornrecords')->where('grabcornrecords.userid = :userid and grabcornrecords.grabcornid = :id and grabcornrecords.isgotback=0', [':id' => $data['grabcornid'], ':userid' => $user->id])->one(); $back = $back['back']; $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); $updategrab = 0; try { $updategrab = $connection->createCommand('update grabcornrecords g1 set g1.isgotback=1 where g1.grabcornid = :id and g1.userid = :userid', [':id' => $data['grabcornid'], ':userid' => $user->id])->execute(); $updateuser = $connection->createCommand('update users u1 set u1.money = u1.money + :back where u1.id=:id', [':id' => $user->id, ':back' => intval($back * 0.9)])->execute(); if (!$updateuser) { throw new Exception("Value must be 1 or below"); } $trade = new Traderecords(); $trade->userid = $user['id']; $trade->type = 4; $trade->description = '自己人联盟提前保本'; $trade->cardid = 0; $trade->count = intval($back * 0.9); $trade->ishandled = 0; $trade->created_at = time(); if (!$trade->save()) { throw new Exception("Value must be 1 or below"); } // ... executing other SQL statements ... $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); //var_dump($e->getMessage()); //Yii::$app->log->logger-> return array('flag' => 0, 'msg' => 'get corn fail!'); } return array('flag' => 1, 'msg' => 'get corn success!'); }
public function actionRewardout() { $data = Yii::$app->request->post(); if (empty($data['count']) || empty($data['cardid']) || empty($data['phone'])) { return array('flag' => 0, 'msg' => 'no enough arg!'); } $user = Users::findOne(['phone' => $data['phone']]); if ($user['alliancerewards'] < 10) { return array('flag' => 0, 'msg' => 'reward not enough!'); } if ($user['status'] < 1) { return array('flag' => 0, 'msg' => 'have not real auth!'); } if ($user['status'] == 2) { return array('flag' => 0, 'msg' => 'have been get!'); } if (Traderecords::findOne(['userid' => $user['id'], 'type' => 0])) { return array('flag' => 0, 'msg' => 'have been get!'); } $model = new Traderecords(); $model->userid = $user['id']; $model->type = 0; $model->description = '自己人联盟联盟奖励提取'; $model->cardid = $data['cardid']; $model->count = $data['count']; $model->ishandled = 1; $model->created_at = time(); try { $result = $model->getDb()->transaction(function ($db) use($model, $user) { if (!$model->save()) { throw new Exception("save traderecord fail"); } $sub = $model['count'] * -1; $rows = Users::updateAllCounters(['alliancerewards' => $sub, 'status' => 1], 'id = ' . $user['id'] . ' and alliancerewards >= ' . $model['count']); if ($rows != 1) { throw new Exception("update user fail"); } }); } catch (\Exception $e) { return array('flag' => 0, 'error' => $e->getMessage(), 'msg' => 'reward out fail!'); } return array('flag' => 1, 'msg' => 'money out success!'); }