SendController.class.php 2.76 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 17/7/25
 * Time: 11:57
 */
namespace Apicp\Controller\Customtask;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\TaskHelper;
use Common\Service\CustomtaskContentService;
use Common\Service\CustomtaskCronService;
use Common\Service\CustomtaskService;

class SendController extends \Apicp\Controller\AbstractController
{
    /**
     * Send
     * @author tangxingguo
     * @desc 立刻发布常规任务接口
     * @param Int customtask_id 常规任务ID
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'customtask_id' => 'require|integer',
        ];

        // 验证数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $customtaskId = $validate->postData['customtask_id'];

        // 取数据
        $customtaskServ = new CustomtaskService();
        $customtaskInfo = $customtaskServ->get($customtaskId);
        if (empty($customtaskInfo)) {
            E('_ERR_CUSTOMTASK_NOT_FOUND');
        }

        // 任务必须为草稿和未开始开可以发布
        if (!in_array($customtaskInfo['task_status'], [Constant::CUSTOMTASK_STATUS_DRAFT, Constant::CUSTOMTASK_STATUS_UNSTART])) {
            E('_ERR_DAILYTASK_STATUS_NOT_SEND');
        }

        // 任务结束时间不能小于当前时间
        if ($customtaskInfo['end_time'] < MILLI_TIME) {
            E('_ERR_CUSTOMTASK_END_TIME_LT_TIME');
        }

        // 检查任务内容状态
        $taskHelper = &TaskHelper::instance();
        $ContentServ = new CustomtaskContentService();
        list($dels) = $ContentServ->contentStatus($customtaskId);
        if (!empty($dels)) {
            // 存在任务内容被删除
            $titles = implode(',', array_column($dels, 'title'));
            E($titles . '被删除');
        } else {
            // 立即发送
            if ($customtaskInfo['start_time'] <= MILLI_TIME) {
                $taskHelper->sendCustomtask($customtaskId);
            } else {

                // 修改状态为未开始
                if ($customtaskInfo['start_time'] > MILLI_TIME) {

                    $customtaskServ->update($customtaskId, ['task_status' => Constant::CUSTOMTASK_STATUS_UNSTART]);
                }

                // 定时发送的计划任务
                $cronServ = new CustomtaskCronService();
                $cronServ->sendCustomtask($customtaskInfo);
                // 定时结束的计划任务
                $cronServ->stopCustomtask($customtaskInfo);
            }

            // 更新参与人数
            list($userTotal) = $taskHelper->getCustomtaskUserData($customtaskId);
            $customtaskServ->update($customtaskId, ['user_total' => count($userTotal)]);
        }
    }
}