Score.php 5.2 KB
<?php

namespace app\school\controller\project_common;

use app\common\controller\DoubleBase;
use app\school\model\project_common\ProjectReviewResult;
use app\school\model\project_common\ProjectReviewScore;
use app\school\model\setting\ProjectExperts;
use think\Db;
use think\Exception;

class Score extends DoubleBase
{
    protected $containerId = null;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->containerId = $_COOKIE['container_id'];
    }

    public function index()
    {
        $reviewScoreId = $this->request->param('review_score_id');
        if(!$reviewScoreId) {
            $this->error('未设置评分表,请先设置评分表');
        }
        $reviewScore = ProjectReviewScore::get($reviewScoreId);
        $scoreSetData = Db::name('project_review_set')->field('review_set_name, score_set_id')->where(['id' => $reviewScore->review_set_id])->find();
        $scoreTableId = $scoreSetData['score_set_id'];

        if (empty($scoreTableId)) {
            $errMsg = $scoreSetData['review_set_name'] . '未设置评分表,请先设置';
            $this->error($errMsg, '');
        }

        if ($this->request->isAjax()) {
            $res = [];

            $scoreItems = Db::name('project_score_set_fields')->where(['score_set_id' => $scoreTableId])->select();
            foreach ($scoreItems as &$val) {
                $val['item_details'] = json_decode($val['item_details'], true);
            }
            $res['score_items'] = $scoreItems;
            $res['score_detail'] = json_decode($reviewScore->score_detail, true);

            $this->success('', '', $res);
        }
        $this->assignconfig('review_score_id', $reviewScoreId);
        return $this->fetch();
    }

    /**
     * 打分接口
     */
    public function mark()
    {
        if ($this->request->isAjax()) {
            $reviewScoreId = $this->request->param('review_score_id');
            $totalScore = $this->request->param('total_score');
            $scoreDetail = $this->request->param('scores');
            $data = [
                'score_detail' => $scoreDetail,
                'score_total' => $totalScore
            ];

            Db::startTrans();
            try {
                $reviewScore = ProjectReviewScore::get($reviewScoreId);
                $reviewScore->allowField(true)->save($data, ['id' => $reviewScoreId]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
    }

    /**
     * 分数统计
     */
    public function statistic()
    {
        if ($this->request->isAjax()) {
            $ids = $this->request->param('ids');
            $reviewId = $this->request->param('review_id');
            $scoreType = $this->request->param('score_type');
            $reviewStage = $this->request->param('review_stage');

            $scoreWhere = ['review_stage' => $reviewStage, 'project_register_id' => ['in', $ids], 'review_id' => $reviewId];

            Db::startTrans();
            try {
                $reviewScoreModel = new ProjectReviewScore();
                // 需要统计分数的项目的打分记录
                $reviewScores = $reviewScoreModel
                    ->where($scoreWhere)
                    ->group('project_register_id')
                    ->column('GROUP_CONCAT(score_total) score_total', 'project_register_id');
                // 计算项目得分
                foreach ($reviewScores as &$v) {
                    if (is_string($v)) {
                        $v = $this->getProjectResult(explode(',', $v), $scoreType);
                    } else {
                        $v = 0;
                    }
                }

                $reviewResultModel = new ProjectReviewResult();
                // 查找需要统计分数的作品中已经打过分的记录
                $existResult = $reviewResultModel->where($scoreWhere)->column('id', 'project_register_id');

                // 构建需要修改/新增的统计分数数据
                $list = array_map(function($id) use ($existResult, $scoreType, $reviewStage, $reviewId, $reviewScores) {
                    $temp = [
                        'project_register_id' => $id,
                        'review_id' => $reviewId,
                        'review_stage' => $reviewStage,
                        'score_type' => $scoreType
                    ];
                    // 如果该项目已经统计过分数,更新记录,否则新增记录
                    if (isset($existResult[$id])) {
                        $temp['id'] = $existResult[$id];
                    }
                    if (isset($reviewScores[$id])) {
                        $temp['result'] = $reviewScores[$id];
                    } else {
                        $temp['result'] = 0;
                    }
                    return $temp;
                }, explode(',', $ids));
   
                $reviewResultModel->saveAll($list);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
    }


}