StopController.class.php 2.71 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\CustomtaskCronService;
use Common\Service\CustomtaskService;
use Common\Service\UserTaskService;

class StopController extends \Apicp\Controller\AbstractController
{
    /**
     * Stop
     * @author tangxingguo
     * @desc 终止常规任务接口
     * @param Int customtask_id 常规任务ID
     * @param String stop_desc 终止原因
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'customtask_id' => 'require|integer',
            'stop_desc' => 'require',
        ];

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

        // 取数据
        $customtaskServ = new CustomtaskService();
        $customtaskInfo = $customtaskServ->get($customtaskId);
        if (empty($customtaskInfo)) {
            E('_ERR_CUSTOMTASK_NOT_FOUND');
        }
        if ($customtaskInfo['task_status'] != Constant::CUSTOMTASK_STATUS_PROCESS) {
            // 进行中的任务才能进行终止
            E('_ERR_DAILYTASK_STATUS_NOT_STOP');
        }

        // 终止常规任务
        $user = $this->_login->user;
        $data = [
            'task_status' => Constant::CUSTOMTASK_STATUS_STOP,
            'stop_desc' => $stopDesc,
            'stop_ea_id' => $user['eaId'],
            'stop_ea_name' => $user['eaRealname'],
            'stop_time' => MILLI_TIME,
        ];
        $customtaskServ->update($customtaskId, $data);
        if ($customtaskInfo['task_status'] == Constant::CUSTOMTASK_STATUS_PROCESS) {
            // 任务正在进行,消息通知未完成人员
            $customtaskInfo = $customtaskServ->get($customtaskId);
            $taskHelper = new TaskHelper();
            $taskHelper->preSendCustomtaskStopMsg($customtaskInfo);
        }

        // 将用户任务状态修改为已终止
        $userTaskServ = new UserTaskService();
        $userTaskServ->update_by_conds([
            'customtask_id' => $customtaskId,
            'task_status' => Constant::CUSTOMTASK_STATUS_PROCESS,
        ], ['task_status' => Constant::CUSTOMTASK_STATUS_STOP]);

        // 删除定时任务
        $cronServ = new CustomtaskCronService();
        $cronList = $cronServ->list_by_conds(['customtask_id' => $customtaskId]);
        if (!empty($cronList)) {
            foreach ($cronList as $v) {
                $cronServ->deleteCron_deprecated($v['cron_id']);
            }
        }
    }
}