SignController.class.php 2.97 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 2018-5-18
 * Time: 15:06:45
 */

namespace Api\Controller\Course;

use Common\Common\Constant;
use Common\Service\CourseArticleService;
use Common\Service\PlanDoService;
use Common\Service\RightUsersService;
use Common\Service\SignService;

class SignController extends \Api\Controller\AbstractController
{
    /**
     * Sign
     * @author zhonglei
     * @desc 线下课程签到接口
     * @param Int article_id:true 课程ID
     * @return Array
     */
    public function Index_post()
    {
        $article_id = I('post.article_id', 0, 'intval');

        if (empty($article_id)) {
            E('_ERR_ARTICLE_ID_INVALID');
        }

        $articleServ = new CourseArticleService();
        $article = $articleServ->get($article_id);

        // 未找到课程
        if (empty($article)) {
            E('_ERR_ARTICLE_DATA_NOT_FOUND');
        }

        // 课程未启用签到
        if ($article['is_sign'] != Constant::COURSE_IS_SIGN_IS_TRUE) {
            E('_ERR_ARTICLE_SIGN_DISABLED');
        }

        // 课程已授课结束
        if (MILLI_TIME > $article['end_time']) {
            E('_ERR_ARTICLE_TIMEEND');
        }

        $rightUsersServ = new RightUsersService();
        $uid = $this->_login->user['memUid'];
        $conds = ['ru_uid' => $uid, 'ed_id' => $article['ed_id']];
        $right_users = $rightUsersServ->get_by_conds($conds);

        // 没有权限学习课程
        if (empty($right_users)) {
            E('_ERR_ARTICLE_PERMISSION_DEND');
        }

        // 未报名
        if ($right_users['ru_sign_status'] != RightUsersService::SIGN_STATUS_SIGNED) {
            E('_ERR_USER_NOT_SIGNED');
        }

        $signServ = new SignService();
        $conds = [
            'sign_uid' => $uid,
            'ed_id' => $article['ed_id'],
            'plan_id' => $article['plan_id'],
            'article_id' => $article_id,
        ];

        // 已经签到
        if (!empty($signServ->get_by_conds($conds))) {
            E('_ERR_ARTICLE_ALREADY_SIGN');
        }

        $data = array_merge($conds, ['sign_on_time' => MILLI_TIME]);

        try {
            // 开启事务
            $signServ->start_trans();

            $signServ->insert($data);

            $plan_do_serv = new PlanDoService();
            // plan_do表待加数据
            $plan_do_data = [
                'pd_uid' => $this->uid,
                'plan_id' => $article['plan_id'],
                'ed_id' => $article['ed_id']
            ];
            // plan_do表中添加数据,标识已完成
            $plan_do_serv->insert($plan_do_data);

            // 提交事务
            $signServ->commit();

        } catch (\Think\Exception $e) {
            // 事务回滚
            $signServ->rollback();
            E('_ERR_SIGN_FAIL');

            return false;
        }

        $this->_result = [
            'start_time' => $article['start_time'],
            'end_time' => $article['end_time'],
        ];
    }
}