forked from yiier/yii2-merit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
use yii\db\Migration; | ||
|
||
class m170713_091926_add_continus_events extends Migration | ||
{ | ||
/** | ||
* 创建表选项 | ||
* @var string | ||
*/ | ||
public $tableOptions = null; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if ($this->db->driverName === 'mysql') { //Mysql 表选项 | ||
$this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; | ||
} | ||
} | ||
|
||
public function safeUp() | ||
{ | ||
$this->addColumn('{{%merit_template}}', 'events_type', | ||
$this->integer() | ||
->notNull() | ||
->defaultValue(0) | ||
->comment("0: 普通的 1:连续登陆有额外的奖励") | ||
); | ||
|
||
$this->addColumn('{{%merit_template}}', 'continuous_count', | ||
$this->integer() | ||
->notNull() | ||
->defaultValue(0) | ||
->comment("获得额外积分,需要连续做的次数") | ||
); | ||
|
||
$this->createTable('{{%continuous}}', [ | ||
'user_id' => $this->string(100)->notNull()->defaultValue('')->comment('用户ID'), | ||
'count' => $this->integer()->defaultValue(0)->notNull(), | ||
'next_start' => $this->integer()->defaultValue(0)->notNull(), | ||
'next_end' => $this->integer()->defaultValue(0)->notNull(), | ||
], $this->tableOptions); | ||
$this->createIndex('user_id','{{%continuous}}', 'user_id'); | ||
|
||
} | ||
|
||
public function safeDown() | ||
{ | ||
echo "m170713_091926_add_continus_events cannot be reverted.\n"; | ||
$this->dropColumn('{{%merit_template}}', 'continuous_count'); | ||
$this->dropColumn('{{%merit_template}}', 'events_type'); | ||
$this->dropTable('{{%continuous}}'); | ||
return true; | ||
} | ||
|
||
/* | ||
// Use up()/down() to run migration code without a transaction. | ||
public function up() | ||
{ | ||
} | ||
public function down() | ||
{ | ||
echo "m170713_091926_add_continus_events cannot be reverted.\n"; | ||
return false; | ||
} | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: soul11201 <[email protected]> | ||
* Date: 2017/7/12 | ||
* Time: 16:25 | ||
*/ | ||
|
||
namespace yiier\merit\models; | ||
|
||
/** | ||
* This is the model class for table "continuous". | ||
* | ||
* @property string $user_id | ||
* @property integer $count | ||
* @property integer $next_start | ||
* @property integer $next_end | ||
*/ | ||
class Continuous extends \yii\db\ActiveRecord | ||
{ | ||
public $range = 24 * 3600; | ||
public function __construct(array $config = []) | ||
{ | ||
parent::__construct($config); | ||
$this->count = 0; | ||
$this->next_start = 0; | ||
$this->next_end = 0; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function tableName() | ||
{ | ||
return 'continuous'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
[['count', 'next_start', 'next_end'], 'integer'], | ||
[['user_id'], 'string', 'max' => 100], | ||
]; | ||
} | ||
|
||
public function calc_count() | ||
{ | ||
$time = time(); | ||
|
||
//已累计 | ||
if($this->next_start > $time) | ||
{ | ||
return $this->count; | ||
} | ||
|
||
// 连续登陆中间断开重新计数 | ||
if($this->next_end < $time) | ||
{ | ||
$this->count = 0; | ||
} | ||
|
||
//重新计数初始化 | ||
if($this->count == 0) | ||
{ | ||
$this->next_start = strtotime(date('Y-m-d')." 00:00:00"); //今日凌晨 | ||
$this->next_end = $this->next_start + $this->range; | ||
} | ||
|
||
$this->next_start += $this->range; | ||
$this->next_end += $this->range; | ||
++ $this->count; | ||
|
||
return $this->count; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters