AddController.class.php 3.31 KB
<?php
/**
 * 【手机端】09-提交评价接口
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-08-31
 */

namespace Api\Controller\Comment;

use Common\Service\CommentService;
use Common\Service\CommentScoreService;
use Common\Service\CompositeScoreService;
use Common\Service\RightUsersService;

class AddController extends \Api\Controller\AbstractController
{

    /** @var CommentService */
    protected $comment_s;
    /** @var CommentScoreService */
    protected $comment_score_s;
    /** @var CompositeScoreService */
    protected $composite_score_s;
    /** @var RightUsersService */
    protected $right_users_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化评论service
        $this->comment_s = new CommentService();
        // 实例化评论得分项service
        $this->comment_score_s = new CommentScoreService();
        // 实例化评价项综合评分service
        $this->composite_score_s = new CompositeScoreService();
        // 实例化员工名单service
        $this->right_users_s = new RightUsersService();

        return true;
    }

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

        // 接收post参数
        $params = I('post.');

        // 培训id
        $ed_id = $params['ed_id'];
        // 培训id为空
        if (!$ed_id) {

            E('_EMPTY_ED_ID');
        }

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

        // 适用范围查询条件
        $right_cond = [
            'ru_uid' => $this->uid,
            'ed_id' => $ed_id
        ];
        // 查询当前员工适用范围数据
        $user_right = $this->right_users_s->get_by_conds($right_cond);
        // 当前员工不在适用范围
        if (empty($user_right)) {

            E('_ERR_ED_NO_RIGHT');
        }

        // 各评分项评分
        $options = $params['options'];
        // 各评分项评分数据为空
        if (empty($options)) {

            E('_EMPTY_COMMENT_OPTIONS');
        }

        // 当前登录用户id
        $params['uid'] = $this->uid;
        $cond = [
            'ed_id' => $ed_id,
            'c_uid' => $this->uid
        ];
        $comment = $this->comment_s->get_by_conds($cond);
        if (!empty($comment)) {

            E('_ERR_COMMENT_EXIST');
        }

        try {
            $this->comment_s->start_trans();

            // 新增评价
            $c_id = $this->comment_s->add($params);

            // 各评分项待新增数据
            $score_data = [];
            foreach ($options as $key => $val) {

                $score_data[$key] = [
                    'cs_score' => $val['cs_score'],
                    'ed_id' => $ed_id,
                    'c_id' => $c_id,
                    'option_id' => intval($val['option_id'])
                ];
            }

            // 新增评价各项得分
            $this->comment_score_s->insert_all($score_data);

            // 新增或更新综合评分数据
            $this->composite_score_s->update_composite_score($ed_id);

            $this->comment_s->commit();
        } catch (\Exception $e) {

            $this->comment_s->rollback();
            E('_ERR_COMMENT_FAIL');

            return false;
        }

        return true;
    }
}