CommentService.class.php 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
<?php
/**
 * 培训评价表Service
 * @author: houyingcai
 * @email:     594609175@qq.com
 * @date :  2017-08-29 16:28:21
 * @version $Id$
 */

namespace Common\Service;

use Com\PythonExcel;
use Common\Common\ExportDownload;
use Common\Model\CommentModel;
use Common\Model\CommentOptionsModel;
use Common\Model\CommentScoreModel;
use Common\Model\CompositeScoreModel;
use Common\Model\EducationModel;

class CommentService extends AbstractService
{

    // 是否匿名:否
    const UN_ANONYMOUS = 0;
    // 是否匿名:是
    const ANONYMOUS = 1;

    /** @var CommentModel */
    protected $_d;
    /** @var CommentOptionsModel */
    protected $comment_options_d;
    /** @var CommentScoreModel */
    protected $comment_score_d;
    /** @var CompositeScoreModel */
    protected $composite_score_d;
    /** @var EducationModel */
    protected $education_d;

    public function __construct()
    {
        $this->_d = new CommentModel();
        $this->comment_options_d = new CommentOptionsModel();
        $this->comment_score_d = New CommentScoreModel();
        $this->composite_score_d = New CompositeScoreModel();
        $this->education_d = new EducationModel();

        parent::__construct();
    }

    /**
     * 新增评价
     * @author wanghuan
     *
     * @param array $data 评价数据
     *
     * @return \Think\mixed
     */
    public function add($data = [])
    {

        // 评价内容
        $c_content = $data['c_content'];

        // 默认不匿名
        $c_is_anonymous = self::UN_ANONYMOUS;
        // 是否匿名参数不为,为匿名
        if ($data['c_is_anonymous']) {

            $c_is_anonymous = $data['c_is_anonymous'];
        }

        $ed_option_cond['ed_id'] = $data['ed_id'];
        // 培训评价项
        $ed_options = $this->comment_options_d->list_by_conds($ed_option_cond);
        if (!empty($ed_options)) {

            $ed_options = array_combine_by_key($ed_options, 'option_id');
        }

        // 统计数据
        $composite = [];
        // 各评分项评分
        $options = $data['options'];
        // 循环计算各评价类型的平均分
        foreach ($options as $v) {
            // 培训评价项id
            $ed_option = $ed_options[$v['option_id']];
            $composite[$ed_option['option_type']]['score'] += $v['cs_score'];
            $composite[$ed_option['option_type']]['count'] += 1;
        }

        // 各评分类型key值
        $option1 = CommentOptionsService::OPTION_TYPE_PROCESS;
        $option2 = CommentOptionsService::OPTION_TYPE_CONTENT;
        $option3 = CommentOptionsService::OPTION_TYPE_TEACHER;

        // 计算各评分类型均分
        $c_avg_option1 = round(($composite[$option1]['score'] / $composite[$option1]['count']), 1);
        $c_avg_option2 = round(($composite[$option2]['score'] / $composite[$option2]['count']), 1);
        $c_avg_option3 = round(($composite[$option3]['score'] / $composite[$option3]['count']), 1);

        // 评价待新增数据
        $add_data = [
            'c_uid' => $data['uid'],
            'c_avg_option1' => $c_avg_option1,
            'c_avg_option2' => $c_avg_option2,
            'c_avg_option3' => $c_avg_option3,
            'c_content' => $c_content,
            'c_is_anonymous' => $c_is_anonymous,
            'ed_id' => $data['ed_id']
        ];

        // 新增返回
        return $this->_d->insert($add_data);
    }

    /**
     * 更新点赞量
     * @author wanghuan
     *
     * @param int $c_id 评论id
     * @param int $type 更新类型:0=加1,1=减1
     *
     * @return bool
     */
    public function update_likes($c_id = 0, $type = 0)
    {

        return $this->_d->update_likes($c_id, $type);
    }

    /**
     * 培训评价列表接口
     *
     * @author 蔡建华
     * @desc 培训评价列表接口
     *
     * @param $params array 搜索参数
     *
     * @return array
     */
    public function comment_search_list($params = [])
    {
        // 默认值
        $page = !empty($params['page']) ? intval($params['page']) : self::PAGE_DEFAULT;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::PAGE_LIMIT_DEFAULT;
        if (empty($params['ed_id'])) {

            E('_EMPTY_EDUCATION_ID');
        }
        // 分页
        list($start, $limit) = page_limit($page, $limit);
        $page_option = [$start, $limit];

        // 按照评价时间进行倒序排列
        $order_option = ['created' => 'desc'];
        $conds = [];

        // 部门,角色,岗位,用户名查询
        if (!empty($params['dpIDs']) || !empty($params['jobID']) || !empty($params['roleID']) || !empty($params['memUsername'])) {
            // 根据搜索条件获取用户ID集合(此处为各条件获取的用户uid的交集)
            $uids = $this->list_uids_by_dp_job_role($params['dpIDs'], $params['roleID'], $params['jobID'],
                $params['memUsername']);
            if (empty($uids)) {
                return [
                    'total' => 0,
                    'limit' => intval($limit),
                    'page' => intval($page),
                    'list' => [],
                ];
            } else {
                $conds['c_uid'] = $uids;
            }
        }

        $conds['ed_id'] = $params['ed_id'];

        // 列表总数
        $total = $this->_d->count_by_conds($conds);
        if ($total > 0) {

            $fields = 'c_avg_option1,c_avg_option2,c_avg_option3,c_uid,c_id,ed_id,c_content';
            $list = $this->_d->list_by_conds($conds, $page_option, $order_option, $fields);
        }

        if (!empty($list)) {
            $this->format_comment_list($list);
        }

        // 组装返回数
        return [
            'total' => intval($total),
            'limit' => intval($limit),
            'page' => intval($page),
            'list' => !empty($list) ? $list : [],
        ];
    }

    /**
     * 评论列表数据格式化
     *
     * @author 蔡建华
     * @desc 格式化
     *
     * @param $list array 评论列表数据
     */
    protected function format_comment_list(&$list)
    {
        $user_uid = array_column($list, 'c_uid');
        sort($user_uid);
        $user_list = $this->get_all_user_by_cache($user_uid);
        foreach ($list as &$val) {
            $user = $user_list[$val['c_uid']];
            $val['memUsername'] = $user['memUsername'];
            $dpName = array_column($user['dpName'], 'dpName');
            $val['dpName'] = implode(',', $dpName);
            $val['jobName'] = $user['memJob'];
            $val['roleName'] = $user['memRole'];
            $val['c_id'] = intval($val['c_id']);
            unset($val['c_uid'], $val['ed_id']);
        }
    }

    /**
     * 删除评价
     *
     * @author 蔡建华
     *
     * @param int $c_id 评价ID
     *
     * @return bool
     */
    public function delete_comment($c_id = 0)
    {
        // 获取评论详情
        $comment = $this->_d->get($c_id);

        $ed_id = $comment['ed_id'];
        // 获取评价项评分
        $comment_score = $this->comment_score_d->list_by_conds([
            'ed_id' => $ed_id,
            'c_id' => $c_id
        ]);

        // 获取评价项的ID集合
        $option_ids = array_column($comment_score, 'option_id');

        try {
            // 开始事务
            $this->start_trans();

            // 删除评价基础表
            $rel = $this->_d->delete_by_conds(['c_id' => $c_id]);

            if (!$rel) {
                // 失败回滚
                $this->rollback();
            }

            // 删除评价分值表
            $rel = $this->comment_score_d->delete_by_conds([
                'ed_id' => $ed_id,
                'c_id' => $c_id
            ]);

            if (!$rel) {
                // 失败回滚
                $this->rollback();
            }
            // 查询培训评价总数
            $total = $this->_d->count_by_conds(['ed_id' => $ed_id]);

            // 如果有记录更新综合评分表
            if ($total) {
                // 获取要删除的选项列表
                $score = $this->comment_score_d->list_by_conds([
                    'ed_id' => $ed_id,
                    'option_id' => $option_ids
                ]);

                $arr = [];
                foreach ($score as $key => $val) {
                    $arr[$val['option_id']] += $val['cs_score'];
                }
                // 重新计算平均分
                foreach ($arr as $key => $val) {
                    // 根据删除用户的评分选项更新对应的选项评分
                    $conds = [
                        'ed_id' => $ed_id,
                        'option_id' => $key

                    ];
                    $data = ['score' => round($val / $total, 1)];
                    // 更新评价统计表
                    $this->composite_score_d->update_by_conds($conds, $data);
                }

            } else {
                $conds = [
                    'ed_id' => $ed_id
                ];
                // 用户记录为0时,设置评分为0
                $this->composite_score_d->update_by_conds($conds, ['score' => 0]);
            }

            $this->commit();

        } catch (\Exception $e) {
            \Think\Log::record($e);
            $this->_set_error($e->getMessage(), $e->getCode());
            // 事务回滚
            $this->rollback();

            return false;
        }

        return $rel;
    }

    /**
     * 评价下载列表接口
     *
     * @author 蔡建华
     * @param $params array 请求参数
     * @param $user array 后台用户详情
     *
     * @return array|bool
     */
    public function comment_download_list($params = [], $user = [])
    {
        // 培训ID为空
        if (empty($params['ed_id'])) {

            return false;
        }

        // 按照评价时间进行倒序排列
        $order_option = ['c_id' => 'DESC'];

        $conds = [
            'ed_id' => $params['ed_id'],
        ];

        // 培训详情
        $education = $this->education_d->get($params['ed_id']);

        if (empty($education)) {

            return false;
        }

        // 查询根据培训ID查询评价列表
        $fields = 'c_id,c_uid,c_avg_option1,c_avg_option2,c_avg_option3,c_content,c_likes,c_is_anonymous';
        $list = $this->_d->list_by_conds($conds, null, $order_option, $fields);

        // 格式化评价数据
        $data = $this->format_down_list($list);

        // 生成下载Excel
        $this->_download($data, $education['ed_name'],$user);
        return $list;
    }

    /**
     * 评价下载数据格式化
     *
     * @author 蔡建华
     * @param $list array 评价数据
     *
     * @return array
     */
    protected function format_down_list($list)
    {
        // 查询所有用户
        $user_uid = array_column($list, 'c_uid');
        sort($user_uid);
        $user_list = $this->get_all_user_by_cache($user_uid);
        // 评价数据组装
        foreach ($list as &$val) {
            $val['memUsername'] = $user_list[$val['c_uid']]['memUsername'];
            $dpName = array_column($user_list[$val['c_uid']]['dpName'], 'dpName');
            $val['dpName'] = implode(',', $dpName);
            $val['jobName'] = $user_list[$val['c_uid']]['memJob'];
            $val['roleName'] = $user_list[$val['c_uid']]['memRole'];
        }
        return [
            'list' => $list
        ];
    }

    /**
     * 评价导出模板
     *
     * @author 蔡建华
     * @param array $list 列表数据
     * @param string $name 培训名称
     * @param array $user 后台用户详情
     * @return bool
     */
    private function _download($list = [], $name, $user = [])
    {
        // 初始化导出数据
        $file_name = $name . '-评价-' . rgmdate((string)NOW_TIME, 'Ymd');
        // Excel 表头字段
        $title = [
            '员工姓名',
            '组织',
            '岗位',
            '角色',
            '培训过程均分',
            '培训内容均分',
            '培训讲师均分',
            '评价内容'
        ];
        $rows = [];
        // 评价数据处理
        foreach ($list['list'] as $v) {
            $value = [
                $v['memUsername'],
                $v['dpName'],
                $v['jobName'],
                $v['roleName'],
                $v['c_avg_option1'],
                $v['c_avg_option2'],
                $v['c_avg_option3'],
                $v['c_content']
            ];
            $rows[] = $value;
        }

        // 生成Excel 下载
        $real_path = ExportDownload::get_down_dir($user['eaId'] . microtime(true)) . NOW_TIME . ".xls";
        $ret = PythonExcel::instance()->write($real_path, $title, $rows);
        if ($ret) {
            $conditon = [
                'title' => $file_name,
                'ea_id' => $user['eaId'],
                'username' => $user['eaRealname'],
                'type' => ExportDownload::EXCEL_TYPE,
                'url' => $real_path
            ];

            ExportDownload::insert_down_load($conditon);

        }

        return true;
    }

}