138 lines
3.7 KiB
PHP
138 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace common\modules\dailytask\vo;
|
|
|
|
use common\modules\dailytask\DailyTaskUtil;
|
|
use yii\behaviors\TimestampBehavior;
|
|
use yii\db\ActiveRecord;
|
|
|
|
/**
|
|
*
|
|
* CREATE TABLE `daily_task` (
|
|
* `id` int(11) NOT NULL AUTO_INCREMENT,
|
|
* `user_id` int(11) NOT NULL,
|
|
* `config_id` int(11) NOT NULL,
|
|
* `name` varchar(500) DEFAULT NULL,
|
|
* `type` int(11) NOT NULL COMMENT '目标类型',
|
|
* `item_id` VARCHAR(45) NOT NULL COMMENT '目标物品ID',
|
|
* `item_num` int(11) NOT NULL COMMENT '目标物品数量',
|
|
* `max_item_num` int(11) NOT NULL COMMENT '完成任务需要达到的数量',
|
|
* `expire_time` int(11) NOT NULL COMMENT '任务过期时间',
|
|
* `is_get_gift` int(11) NOT NULL COMMENT '是否领取奖励',
|
|
* `created_at` int(11) DEFAULT NULL,
|
|
* `updated_at` int(11) DEFAULT NULL,
|
|
* PRIMARY KEY (`id`),
|
|
* KEY `user_id` (`user_id`),
|
|
* KEY `type` (`type`),
|
|
* KEY `item_id` (`item_id`),
|
|
* KEY `expire_time` (`expire_time`)
|
|
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='日常任务'
|
|
*/
|
|
|
|
/**
|
|
* 日常任务
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int $config_id
|
|
* @property string $name
|
|
* @property int $type //目标类型
|
|
* @property string $item_id //目标物品ID
|
|
* @property int $item_num //目标物品数量
|
|
* @property int $max_item_num //完成任务需要达到的数量
|
|
* @property int $expire_time//任务过期时间
|
|
* @property int $is_get_gift//是否领取奖励
|
|
* @property int $created_at
|
|
* @property int $updated_at
|
|
*/
|
|
class DailyTask extends ActiveRecord
|
|
{
|
|
|
|
public function behaviors()
|
|
{
|
|
return [
|
|
TimestampBehavior::className(),
|
|
];
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
['id', 'default', 'value' => 0],
|
|
|
|
['user_id', 'required'],
|
|
['user_id', 'exist', 'targetClass' => '\common\models\User', 'targetAttribute' => 'id'],
|
|
|
|
['config_id', 'default', 'value' => ''],
|
|
|
|
['name', 'default', 'value' => ''],
|
|
|
|
['type', 'default', 'value' => DailyTaskUtil::TYPE_SIGN],
|
|
['type', 'in', 'range' => DailyTaskUtil::TYPE_LIST],
|
|
|
|
['item_id', 'default', 'value' => ''],
|
|
|
|
['item_num', 'default', 'value' => 0],
|
|
['item_num', 'number', 'max' => $this->max_item_num],
|
|
|
|
['max_item_num', 'default', 'value' => 0],
|
|
|
|
['expire_time', 'default', 'value' => 0],
|
|
|
|
['is_get_gift', 'default', 'value' => DailyTaskUtil::GIFT_NOT_GET],
|
|
['is_get_gift', 'in', 'range' => DailyTaskUtil::GIFT_GET_LIST],
|
|
|
|
['created_at', 'default', 'value' => 0],
|
|
['updated_at', 'default', 'value' => 0],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $userID
|
|
* @param $type
|
|
* @param $itemID
|
|
* @return static[]
|
|
*/
|
|
public static function findAllByUserIDAndParam($userID, $type, $itemID)
|
|
{
|
|
$where['user_id'] = $userID;
|
|
$where['type'] = $type;
|
|
|
|
if (!empty($itemID)) $where['item_id'] = $itemID;
|
|
|
|
return self::findAll($where);
|
|
}
|
|
|
|
/**
|
|
* @param $userID
|
|
* @return static[]
|
|
*/
|
|
public static function findAllByUserID($userID)
|
|
{
|
|
return self::findAll(['user_id' => $userID]);
|
|
}
|
|
|
|
/**
|
|
* 清理过期任务
|
|
* @param $userID
|
|
*/
|
|
public static function cleanExpireByUserID($userID, $time)
|
|
{
|
|
self::deleteAll('user_id=:user_id and expire_time<=:expire_time',
|
|
[':user_id' => $userID, ':expire_time' => $time]);
|
|
}
|
|
|
|
/**
|
|
* 重置任务
|
|
* @param $userID
|
|
*/
|
|
public static function resetByUserID($userID)
|
|
{
|
|
self::updateAll(['item_num' => 0], ['user_id' => $userID]);
|
|
}
|
|
|
|
public static function findByID($id)
|
|
{
|
|
return self::findOne(['id' => $id]);
|
|
}
|
|
}
|