ScoreController.class.php 2.04 KB
<?php
/**
 * 【手机端】13-评价综合评分接口
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-08-31
 */

namespace Api\Controller\Comment;

use Common\Service\CommentService;
use Common\Service\CommentOptionsService;

class ScoreController extends \Api\Controller\AbstractController
{
    // 未评价
    const UN_COMMENT = 0;
    // 已评价
    const COMMENT = 1;

    /**
     * 评价列表
     */
    public function Index_post()
    {

        // 培训id
        $ed_id = I('post.ed_id', 0, 'intval');
        // 培训id为空
        if (!$ed_id) {

            E('_EMPTY_ED_ID');
        }

        // 培训详情
        $this->get_education($ed_id);

        // 实例化评论service
        $comment_s = new CommentService();
        // 评价查询条件
        $cond['ed_id'] = $ed_id;
        // 获取记录总数
        $total = $comment_s->count_by_conds($cond);

        // 当前登录用户是否已评价,默认未评价
        $comment_status = self::UN_COMMENT;
        // 查询条件
        $user_comment_cond = [
            'c_uid' => $this->uid,
            'ed_id' => $ed_id
        ];
        // 用户评价数据
        $user_comment = $comment_s->get_by_conds($user_comment_cond);
        // 用户评价数据不为空
        if (!empty($user_comment)) {
            // 已评
            $comment_status = self::COMMENT;
        }

        // 实例化评价项service
        $comment_options_s = new CommentOptionsService();
        // 获取综合评分数据
        $composite_score = $comment_options_s->get_composite_score($ed_id);
        $options = [];
        if (!empty($composite_score['options_score'])) {

            $options = $composite_score['options_score'];
        }

        // 返回结果
        $this->_result = [
            'total_score' => intval($composite_score['total_score']),
            'score' => round($composite_score['score'], 1),
            'comment_status' => $comment_status,
            'comment_num' => intval($total),
            'options' => $options
        ];
    }
}