Commit 44be4f92 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/airdrop' into 'master'

Feature/airdrop See merge request !323
parents 67e4fe8c 109fa616
......@@ -2,14 +2,13 @@
namespace api\controllers;
use common\models\psources\AirDropRulePool;
use Yii;
use api\base\BaseController;
use common\models\psources\AirDrop;
use common\service\chain33\Chain33Service;
use common\models\psources\AirDropRulePool;
use common\models\psources\CoinAirDropTrade;
use common\models\psources\AirDropApplyRecord;
use yii\db\Exception;
class AirDropController extends BaseController
{
......@@ -25,7 +24,6 @@ class AirDropController extends BaseController
if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post();
$identifier = $data['identifier'] ?? null;
if (false == $identifier) {
$this->code = -1;
......@@ -38,45 +36,16 @@ class AirDropController extends BaseController
$this->msg = 'Validation failed.';
goto doEnd;
}
$time = $exist->rule->duration - 1;
$create_time = date('Y-m-d');
$finish_time = date('Y-m-d', strtotime("+$time day"));
$transaction = Yii::$app->db->beginTransaction();
try {
$model = new AirDrop();
$model->setScenario(AirDrop::SCENARIOS_CREATE);
$model->load($data, '');
if (!$model->validate()) {
$this->code = -1;
$this->msg = $model->errors;
goto doEnd;
}
$model->save();
$apply_id = $model->id;
$expiry_date = $this->getDatesFromRange($create_time, $finish_time);
$apply_record_model = new AirDropApplyRecord();
foreach ($expiry_date as $val) {
$apply_record_model->setIsNewRecord(true);
$apply_record_model->apply_id = $apply_id;
$apply_record_model->reach = AirDropApplyRecord::REACH_NO;
$apply_record_model->amount = $exist->rule->amount;
$apply_record_model->token = $exist->rule->token;
$apply_record_model->draw_status = AirDropApplyRecord::STATUS_UNDRAW;
$apply_record_model->create_time = $val;
$apply_record_model->save() && $apply_record_model->id = 0;;
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
$model = new AirDrop();
$model->setScenario(AirDrop::SCENARIOS_CREATE);
$model->load($data, '');
if (!$model->validate()) {
$this->code = -1;
$this->msg = $e->getMessage();
$this->msg = $model->errors;
goto doEnd;
}
if (empty($this->msg)) {
$this->msg = 'Validation failed.';
}
$model->save();
$redis = Yii::$app->redis_app;
$redis->hmset('airdrop:' . $data['identifier'], 'reach', 0, 'draw_success', 0, 'income', 0, 'un_draw', 0);
......@@ -146,7 +115,7 @@ class AirDropController extends BaseController
}
$this->data = AirDropApplyRecord::find()
->where(['apply_id' => $model['id'], 'reach' => AirDropApplyRecord::REACH_YES])
->where(['apply_id' => $model['id'], 'reach' => AirDropApplyRecord::REACH_YES, 'draw_status' => AirDropApplyRecord::STATUS_DRAW_SUEEESS])
->sum('amount');
if (empty($this->data)) {
$this->data = 0;
......@@ -238,7 +207,6 @@ class AirDropController extends BaseController
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
}
public function actionGameTrade()
{
$coins_address = Yii::$app->request->post('coins_address', '');
......@@ -358,27 +326,4 @@ class AirDropController extends BaseController
}
return ['code' => 1, 'msg' => 'ok'];
}
function getDatesFromRange($start, $end, $format = 'Y-m-d')
{
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new \DateInterval('P1D');
$realEnd = new \DateTime($end);
$realEnd->add($interval);
$period = new \DatePeriod(new \DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach ($period as $date) {
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
}
\ No newline at end of file
......@@ -16,7 +16,7 @@ class Tools
{
$chars = '1234567890abcdefghijklmnopqrstuvwxyz';
switch($type) {
switch ($type) {
case 'number':
$startIndex = 0;
$endIndex = 9;
......@@ -32,7 +32,7 @@ class Tools
}
$randomNumber = '';
for($i = 0; $i<$count; $i++) {
for ($i = 0; $i < $count; $i++) {
$randomNumber .= substr($chars, rand($startIndex, $endIndex), 1);
}
return $randomNumber;
......@@ -50,15 +50,15 @@ class Tools
return $filename;
switch ($type) {
case 'case':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'video':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'banner':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'weixin':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
case 'public':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
return $filename ? Url::to('@resDomain/' . $type . '/' . $filename) : '';
default:
return '';
}
......@@ -78,10 +78,10 @@ class Tools
public static function isAvailableDate($start, $end)
{
$current = date('Y-m-d');
$start = $start?date('Y-m-d', strtotime($start)):$current;
$end = $end?date('Y-m-d', strtotime($end)):$current;
$start = $start ? date('Y-m-d', strtotime($start)) : $current;
$end = $end ? date('Y-m-d', strtotime($end)) : $current;
if($start <= $current && $end >= $current) {
if ($start <= $current && $end >= $current) {
return true;
} else {
return false;
......@@ -106,9 +106,9 @@ class Tools
*/
public static function getPost(array $values, $key = null)
{
if(Yii::$app->request->isPost) {
if (Yii::$app->request->isPost) {
$data = Yii::$app->request->post();
if($key !== null) {
if ($key !== null) {
$data[$key] = ArrayHelper::merge($data[$key], $values);
} else {
$data = ArrayHelper::merge($data, $values);
......@@ -127,27 +127,21 @@ class Tools
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
{
if ($length < 1.0)
{
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++) {
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
if ($length < 1.0) {
break;
}
$result .= substr($string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
} else {
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen)
{
if ($i < $strlen) {
$result .= $etc;
}
return $result;
......@@ -162,4 +156,27 @@ class Tools
array_multisort($keysValue, $sort, $array);
return $array;
}
public static function getDatesFromRange($start, $end, $format = 'Y-m-d')
{
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new \DateInterval('P1D');
$realEnd = new \DateTime($end);
$realEnd->add($interval);
$period = new \DatePeriod(new \DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach ($period as $date) {
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
}
\ No newline at end of file
......@@ -7,6 +7,9 @@ use yii\db\Expression;
class AirDrop extends CommonActiveRecord
{
const REACH_INIT_NO = 0; //未达标
const REACH_INIT_YES = 1; //已达标
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
......@@ -25,8 +28,8 @@ class AirDrop extends CommonActiveRecord
{
return [
[['identifier', 'wallet_address', 'miner_address'], 'required'],
[['reach_times'], 'integer'],
[['create_time', 'finish_time'], 'safe'],
[['reach_times', 'reach_init'], 'integer'],
[['reach_times', 'reach_init', 'create_time', 'finish_time'], 'safe'],
[['identifier'], 'string', 'length' => [5, 50]],
[['wallet_address', 'miner_address'], 'string', 'length' => [10, 50]],
['identifier', 'unique', 'message' => '树莓派编号已存在'],
......@@ -37,7 +40,7 @@ class AirDrop extends CommonActiveRecord
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['identifier', 'miner_address', 'reach_times', 'create_time'],
self:: SCENARIOS_CREATE => ['identifier', 'miner_address', 'reach_init', 'reach_times', 'create_time'],
self:: SCENARIOS_UPDATE => ['wallet_address'],
];
return array_merge(parent:: scenarios(), $scenarios);
......@@ -49,6 +52,7 @@ class AirDrop extends CommonActiveRecord
'identifier' => '树莓派编号',
'wallet_address' => '矿主地址',
'miner_address' => '矿工地址',
'reach_init' => '达标初始化',
'reach_times' => '达标次数',
'create_time' => '开始时间',
'finish_time' => '结束时间'
......
......@@ -2,6 +2,8 @@
namespace console\controllers;
use common\components\Tools;
use common\models\psources\AirDropRulePool;
use Yii;
use yii\console\Controller;
use common\models\psources\AirDrop;
......@@ -9,6 +11,7 @@ use common\service\chain33\Chain33Service;
use common\models\psources\AirDropApplyRecord;
use common\models\psources\CoinAirDropTransfer;
use common\models\psources\AirDropApplyTransferRecord;
use yii\db\Exception;
class AirDropController extends Controller
{
......@@ -91,6 +94,57 @@ class AirDropController extends Controller
}
/**
* 达标初始化
* 通过矿主地址查询是否有余额,有则生成记录
* @return
*/
public function actionReachInit()
{
$applies = AirDrop::find()->where(['reach_init' => AirDrop::REACH_INIT_NO])->all();
if (empty($applies)) return 0;
$service = new Chain33Service();
$execer = 'ticket';
foreach ($applies as $apply) {
$exist = AirDropRulePool::find()->where(['identifier' => $apply->identifier])->one();
if (false == $exist || false == $apply->wallet_address) continue;
$result = $service->getBalance($apply->wallet_address, $execer);
if (0 != $result['code']) continue;
$balance = $result['result'][0]['balance'] ?? 0;
if ($balance == 0) continue;
$transaction = Yii::$app->db->beginTransaction();
try {
$apply->reach_init = AirDrop::REACH_INIT_YES;
$apply->save();
$time = $exist->rule->duration - 1;
$create_time = date('Y-m-d');
$finish_time = date('Y-m-d', strtotime("+$time day"));
$expiry_date = Tools::getDatesFromRange($create_time, $finish_time);
$apply_record_model = new AirDropApplyRecord();
foreach ($expiry_date as $key => $val) {
$apply_record_model->setIsNewRecord(true);
$apply_record_model->apply_id = $apply->id;
if (0 == $key) {
$apply_record_model->reach = AirDropApplyRecord::REACH_YES;
} else {
$apply_record_model->reach = AirDropApplyRecord::REACH_NO;
}
$apply_record_model->amount = $exist->rule->amount;
$apply_record_model->token = $exist->rule->token;
$apply_record_model->draw_status = AirDropApplyRecord::STATUS_UNDRAW;
$apply_record_model->create_time = $val;
$apply_record_model->save() && $apply_record_model->id = 0;;
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
$msg = $e->getMessage();
echo date('Y-m-d H:i:s') . $msg . PHP_EOL;
}
}
}
/**
* 查询地址余额
* 通过矿主地址查询是否有冻结资金,有BTY代表达标
* @return
......@@ -113,8 +167,8 @@ class AirDropController extends Controller
\Co::sleep(0.5);
$result = $service->getBalance($val->apply->wallet_address, $execer);
if (0 == $result['code']) {
$frozen = $result['result'][0]['frozen'] ?? 0;
if ($frozen > 0) {
$balance = $result['result'][0]['balance'] ?? 0;
if ($balance > 0) {
$val->reach = AirDropApplyRecord::REACH_YES;
$val->save();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment