140 lines
5.4 KiB
PHP
Executable File
140 lines
5.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\repository;
|
|
|
|
use app\model\Task;
|
|
use app\model\Task as TaskModel;
|
|
use app\model\TaskAccount;
|
|
use app\service\Repository;
|
|
use app\traits\task\TaskAccountTrait;
|
|
use app\traits\task\TaskCompletionRecordTrait;
|
|
use think\Model;
|
|
|
|
/**
|
|
* 任务
|
|
*
|
|
* @package app\repository
|
|
* @method self getInstance(Model $model = null) static
|
|
*/
|
|
class TaskRepository extends Repository
|
|
{
|
|
|
|
use TaskCompletionRecordTrait;
|
|
use TaskAccountTrait;
|
|
|
|
|
|
//当前进行中的任务
|
|
public function getDoingTaskCount($taskId = 0)
|
|
{
|
|
return $this->model->when($taskId != 0, function ($q) use ($taskId) {
|
|
$q->where("task_id", $taskId);
|
|
})->where("status", Task::COMMON_ON)->count();
|
|
}
|
|
|
|
|
|
/**
|
|
* 过滤用户的任务
|
|
* */
|
|
public function filterUserTask($item, $account)
|
|
{
|
|
switch ($item->type) {
|
|
//分享
|
|
case TaskModel::TYPE_SHARE :
|
|
$accountTask = TaskRepository::getInstance()->userTask($item["id"], $account["id"]);
|
|
//没有提交 或者 已提交的任务状态
|
|
$item->account_status = empty($accountTask) ? TaskAccount::$status_notsubmit : $accountTask["status"];
|
|
$completed_quantity = TaskRepository::getInstance()->getShareTaskSuccess($item, $account);
|
|
if ($item->account_status == TaskAccount::$status_success) {
|
|
$item->completed_quantity = ($completed_quantity > $item["cycle"]) ? $item["cycle"] : $completed_quantity;
|
|
} else {
|
|
$item->completed_quantity = $completed_quantity;
|
|
}
|
|
break;
|
|
//线上签到
|
|
case TaskModel::TYPE_SIGN_IN :
|
|
$accountTask = TaskRepository::getInstance()->userTask($item["id"], $account["id"]);
|
|
//没有提交 或者 已提交的任务状态
|
|
$item->account_status = empty($accountTask) ? TaskAccount::$status_notsubmit : $accountTask["status"];
|
|
$completed_quantity = TaskRepository::getInstance()->getSignInTaskSuccess($item, $account);
|
|
if ($item->account_status == TaskAccount::$status_success) {
|
|
$item->completed_quantity = ($completed_quantity > $item["cycle"]) ? $item["cycle"] : $completed_quantity;
|
|
} else {
|
|
$item->completed_quantity = $completed_quantity;
|
|
}
|
|
break;
|
|
//线下签到
|
|
case TaskModel::TYPE_OFFLINE_SIGN_IN :
|
|
$accountTask = TaskRepository::getInstance()->userTask($item["id"], $account["id"]);
|
|
//没有提交 或者 已提交的任务状态
|
|
$item->account_status = empty($accountTask) ? TaskAccount::$status_notsubmit : $accountTask["status"];
|
|
$completed_quantity = TaskRepository::getInstance()->getOfflineSignInTaskSuccess($item, $account);
|
|
if ($item->account_status == TaskAccount::$status_success) {
|
|
$item->completed_quantity = ($completed_quantity > $item["cycle"]) ? $item["cycle"] : $completed_quantity;
|
|
} else {
|
|
$item->completed_quantity = $completed_quantity;
|
|
}
|
|
break;
|
|
//截图分享
|
|
case TaskModel::TYPE_SHARE_SCREENSHOTS :
|
|
$item = TaskRepository::getInstance()->getShareScreenshotsTaskSuccess($item, $account);
|
|
break;
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
|
|
|
|
/**
|
|
* 检查完成条件
|
|
* */
|
|
public function checkCompletionConditions($task, $accout)
|
|
{
|
|
switch ($task->type) {
|
|
//分享
|
|
case TaskModel::TYPE_SHARE :
|
|
return TaskRepository::getInstance()->checkShareTaskCompletionConditions($task, $accout);
|
|
break;
|
|
//线上签到
|
|
case TaskModel::TYPE_SIGN_IN :
|
|
return TaskRepository::getInstance()->checkSignInTaskCompletionConditions($task, $accout);
|
|
break;
|
|
//线下签到
|
|
case TaskModel::TYPE_OFFLINE_SIGN_IN :
|
|
return TaskRepository::getInstance()->checkOfflineSignInTaskCompletionConditions($task, $accout);
|
|
break;
|
|
//截图分享
|
|
case TaskModel::TYPE_SHARE_SCREENSHOTS :
|
|
if (empty(input("image"))) {
|
|
return false;
|
|
}
|
|
return TaskRepository::getInstance()->checkShareScreenshotsTaskCompletionConditions($task, $accout);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 完成一个任务
|
|
* */
|
|
public function completeTask($task, $accout)
|
|
{
|
|
switch ($task->type) {
|
|
//分享
|
|
case TaskModel::TYPE_SHARE :
|
|
return TaskRepository::getInstance()->completeShareTask($task, $accout);
|
|
break;
|
|
//线上签到
|
|
case TaskModel::TYPE_SIGN_IN :
|
|
return TaskRepository::getInstance()->completeSignInTask($task, $accout);
|
|
break;
|
|
//线下签到
|
|
case TaskModel::TYPE_OFFLINE_SIGN_IN :
|
|
return TaskRepository::getInstance()->completeOfflineSignInTask($task, $accout);
|
|
break;
|
|
//截图分享
|
|
case TaskModel::TYPE_SHARE_SCREENSHOTS :
|
|
return TaskRepository::getInstance()->completeShareScreenshotsTask($task, $accout);
|
|
break;
|
|
}
|
|
}
|
|
|
|
} |