UpdateCountController.class.php 3.79 KB
<?php
/**
 * 更新考试统计接口
 */

namespace Frontend\Controller\Temp;

use Common\Service\PaperService;
use Think\Exception;

class UpdateCountController extends AbstractController
{
    public $ep_id = null; // 试卷ID
    public $domain = null; // 企业标识

    /** @var PaperService 试卷信息表 */
    protected $paper_s;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->ep_id = I('ep_id', 0, 'intval');
        $this->domain = QY_DOMAIN;
        // 实例化试卷表
        $this->paper_s = new PaperService();

        return true;
    }

    public function Index()
    {
        $ep_id = $this->ep_id;
        $domain = $this->domain;
        
        // 非空判断
        if (empty($ep_id)) {

            return true;
        }

        if (empty($domain)) {

            return true;
        }

        // 获取试卷基本详情
        $data = $this->paper_s->get($ep_id);
        if (empty($data)) {

            return true;
        }
        
        // 实例化think下的model类
        $com_model = new \Think\Model();

        $this->paper_s->start_trans();
        try {
            // 如果是系统阅卷
            if ($data['marking_type'] == 1) {
                // 将状态为待批阅的答卷修改为已批阅
                $sql_one = 'UPDATE `oa_exam_answer_detail` SET `marking_status`=1 WHERE `marking_status`=0 AND ep_id=' . $ep_id . ' AND status<3 AND domain=\'' . $domain . '\'';

                $com_model->execute($sql_one);
            }

            // 将答卷详情表状态为已批阅的统计分数更新到答卷表
            $sql_two = 'UPDATE `oa_exam_answer` a INNER JOIN ( SELECT ea_id,sum(my_score) AS score FROM `oa_exam_answer_detail` WHERE `marking_status`=1 AND ep_id=' . $ep_id . ' GROUP BY ea_id )b SET a.my_score =b.score WHERE a.ea_id = b.ea_id AND status<3 AND domain=\'' . $domain . '\'';

            $com_model->execute($sql_two);

            // 将状态为已批阅且低于及格分数,答卷状态为已通过的答卷修改为未通过
            $sql_three = 'UPDATE `oa_exam_answer` b INNER JOIN ( SELECT ea_id,score FROM ( SELECT ea_id,sum(my_score) AS score FROM `oa_exam_answer_detail` WHERE `marking_status`=1 AND `ep_id`=' . $ep_id . ' GROUP BY ea_id ) as a WHERE a.score < ' . $data['pass_score'] . ' ) c SET b.my_is_pass=0 WHERE b.ea_id = c.ea_id AND status<3 AND domain=\'' . $domain . '\'';

            $com_model->execute($sql_three);

            $this->paper_s->commit();
        } catch (Exception $e) {
            // 事务回滚
            $this->paper_s->rollback();
        }

        return true;
    }

    /**
     * 重置统计状态
     *
     * @return bool
     */
    public function ResetStatus()
    {
        $ep_id = $this->ep_id;
        $domain = $this->domain;

        // 非空判断
        if (empty($ep_id)) {

            return true;
        }

        if (empty($domain)) {

            return true;
        }

        // 实例化think下的model类
        $com_model = new \Think\Model();

        $this->paper_s->start_trans();
        try {
            // 更新答卷详情表的计划任务字段状态
            $sql_one = 'UPDATE `oa_exam_answer_detail` SET `cron_status`=0 WHERE ep_id=' . $ep_id . ' AND cron_status=1 AND status<3 AND domain=\'' . $domain . '\'';

            $com_model->execute($sql_one);

            // 删除统计表数据
            $sql_two = 'DELETE FROM `oa_exam_statistics` WHERE ep_id = ' . $ep_id . ' and status < 3 and domain = \'' . $domain . '\'';

            $com_model->execute($sql_two);

            $this->paper_s->commit();
        } catch (Exception $e) {
            // 事务回滚
            $this->paper_s->rollback();
        }

        return true;
    }
}