Skip to content

Commit

Permalink
连续n天做一件事,比如登陆,送额外积分 feat: yiier#2
Browse files Browse the repository at this point in the history
  • Loading branch information
noname007 committed Jul 14, 2017
1 parent ff326a9 commit 388befc
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 6 deletions.
34 changes: 31 additions & 3 deletions MeritBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use yii\base\Behavior;
use yii\db\Exception;
use yii\di\Instance;
use yii\helpers\VarDumper;
use yii\web\Controller;
use yiier\merit\models\Continuous;
use yiier\merit\models\LevelCalcInterface;
use yiier\merit\models\Merit;
use yiier\merit\models\MeritLog;
Expand Down Expand Up @@ -107,8 +109,35 @@ public function update(MeritTemplate $meritTemplate)
{
$meritLog = new MeritLog();
$user = \Yii::$app->user->identity;

$transaction = \Yii::$app->db->beginTransaction();

if($meritTemplate->events_type == MeritTemplate::EVENTS_TYPE_CONTINUOUS)
{
$user_id = $user->getId();

$m = Continuous::find()
->where(['user_id' => $user_id])
->one();

if(!$m)
{
$m = new Continuous;
}

$count = $m->count;

if($count == $m->calc_count() || $m->count != $meritTemplate->continuous_count)
{
$transaction->commit();
return ;
}

if(!$m->save())
{
throw new Exception(VarDumper::dumpAsString($m->getFirstErrors()));
}
}

try {
/** @var Merit $userMerit */
$userMerit = Merit::findOne(['user_id' => $user->getId(), 'type' => $meritTemplate->type]);
Expand Down Expand Up @@ -173,11 +202,10 @@ public function update(MeritTemplate $meritTemplate)
}

$transaction->commit();
} catch (Exception $e) {
} catch (\Exception $e) {
Yii::error($e->getMessage(), __METHOD__);
$transaction->rollBack();
}

}

}
3 changes: 2 additions & 1 deletion migrations/m170710_030015_add_level.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function safeUp()
public function safeDown()
{
echo "m170710_030015_add_level cannot be reverted.\n";

$this->dropColumn('{{%merit}}','level');
$this->dropColumn('{{%merit}}','pos_accu_merit');
return false;
}

Expand Down
71 changes: 71 additions & 0 deletions migrations/m170713_091926_add_continus_events.php
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;
}
*/
}
79 changes: 79 additions & 0 deletions models/Continuous.php
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;
}
}

24 changes: 23 additions & 1 deletion models/MeritTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
* @property integer events_type
* @property integer continuous_count
*/
class MeritTemplate extends \yii\db\ActiveRecord
{
Expand Down Expand Up @@ -54,6 +56,16 @@ class MeritTemplate extends \yii\db\ActiveRecord
*/
const ACTIVE_TYPE_ADD = 2;

/**
* @var int 普通的事件,比如每天登陆送积分,此为默认类型
*/
const EVENTS_TYPE_NORMAL = 0;

/**
* @var int 连续登陆事件类型
*/
const EVENTS_TYPE_CONTINUOUS = 1;

/**
* 自动更新created_at和updated_at时间
* @return array
Expand All @@ -79,7 +91,7 @@ public static function tableName()
public function rules()
{
return [
[['type', 'method', 'event', 'action_type', 'rule_key', 'rule_value', 'increment', 'status', 'created_at', 'updated_at'], 'integer'],
[['type', 'method', 'event', 'action_type', 'rule_key', 'rule_value', 'increment', 'status', 'created_at', 'updated_at', 'events_type', 'continuous_count'], 'integer'],
[['title', 'unique_id'], 'required'],
[['title', 'unique_id'], 'string', 'max' => 255]
];
Expand All @@ -104,6 +116,8 @@ public function attributeLabels()
'status' => Yii::t('app', '状态'),
'created_at' => Yii::t('app', '创建时间'),
'updated_at' => Yii::t('app', '更新时间'),
'events_type' => Yii::t('app', '事件类型'),
'continuous_count' => Yii::t('app', '持续次数')
];
}

Expand Down Expand Up @@ -146,4 +160,12 @@ public static function getStatuses()
self::STATUS_DELETE => '停用',
];
}

public static function getEventsType()
{
return [
self::EVENTS_TYPE_NORMAL => '做一次,就有一次',
self::EVENTS_TYPE_CONTINUOUS => '连续做,送额外奖励',
];
}
}
4 changes: 3 additions & 1 deletion models/MeritTemplateSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MeritTemplateSearch extends MeritTemplate
public function rules()
{
return [
[['id', 'type', 'method', 'event', 'action_type', 'rule_key', 'rule_value', 'increment', 'status', 'created_at', 'updated_at'], 'integer'],
[['id', 'type', 'method', 'event', 'action_type', 'rule_key', 'rule_value', 'increment', 'status', 'created_at', 'updated_at', 'events_type', 'continuous_count'], 'integer'],
[['title', 'unique_id'], 'safe'],
];
}
Expand Down Expand Up @@ -66,6 +66,8 @@ public function search($params)
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'events_type' => $this->events_type,
'continuous_count' => $this->continuous_count,
]);

$query->andFilterWhere(['like', 'title', $this->title])
Expand Down
4 changes: 4 additions & 0 deletions views/merit-template/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

<?= $form->field($model, 'status')->dropDownList(MeritTemplate::getStatuses()) ?>

<?= $form->field($model, 'events_type')->dropDownList(MeritTemplate::getEventsType()) ?>

<?= $form->field($model, 'continuous_count')->textInput() ?>

<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
Expand Down
7 changes: 7 additions & 0 deletions views/merit-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
return MeritTemplate::getStatuses()[$data->status];
}
],
[
'attribute' => 'events_type',
'value' => function ($data) {
return MeritTemplate::getEventsType()[$data->events_type];
}
],
'continuous_count',
'created_at:datetime',
'updated_at:datetime',

Expand Down
7 changes: 7 additions & 0 deletions views/merit-template/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
'attribute' => 'status',
'value' => MeritTemplate::getStatuses()[$model->status]
],
[
'attribute' => 'events_type',
'value' => function ($data) {
return MeritTemplate::getEventsType()[$data->events_type];
}
],
'continuous_count',
'created_at:datetime',
'updated_at:datetime',
],
Expand Down

0 comments on commit 388befc

Please sign in to comment.