TopicAttrService.class.php 12.4 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
<?php
/**
 * 考试-题目属性关联表
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-05-19 17:48:21
 */

namespace Common\Service;

use Common\Model\AttrModel;
use Common\Model\BankModel;
use Common\Model\TagModel;
use Common\Model\TopicAttrModel;
use Common\Model\TopicModel;

class TopicAttrService extends AbstractService
{

    // 构造方法
    public function __construct()
    {
        $this->_d = new TopicAttrModel();
        $this->_d_attr = new AttrModel();
        $this->_d_tag = new TagModel();
        $this->_d_bank = new BankModel();
        $this->_topic_attr = new TopicAttrModel();
        $this->_topic = new TopicModel();
        parent::__construct();
    }

    /**
     * 根据题库获取标签属性
     *
     * @author daijun
     * @param array $eb_ids
     *
     * @return array
     */
    public function get_bank_attr($eb_ids = [])
    {

        $topic_attr_list = $this->_d->list_by_conds(['eb_id' => $eb_ids], null, []);

        if (empty($topic_attr_list)) {
            return [];
        }
        // 获取标签ID集合
        $tag_ids = array_unique(array_column($topic_attr_list, 'etag_id'));

        // 查询标签信息
        $tag_list = $this->_d_tag->list_by_conds(['etag_id' => $tag_ids], null, ['etag_id' => 'ASC']);
        // 获取属性ID集合
        $attr_ids = array_unique(array_column($topic_attr_list, 'attr_id'));
        // 查询属性信息
        $attr_list = $this->_d_attr->list_by_conds(['attr_id' => $attr_ids], null, ['etag_id' => 'ASC']);

        $return_list = [];
        // 循环标签数据
        foreach ($tag_list as $k => $v) {

            $return_list[$k]['etag_id'] = intval($v['etag_id']);
            $return_list[$k]['etag_name'] = $v['tag_name'];

            $attr_data = [];
            // 循环属性数据
            foreach ($attr_list as $_k => $_v) {
                // 如果属性是该标签下的
                if ($_v['etag_id'] == $v['etag_id']) {
                    $attr = [];
                    $attr['attr_id'] = intval($_v['attr_id']);
                    $attr['attr_name'] = $_v['attr_name'];
                    $attr_data[] = $attr;
                    // 删除调该条记录
                    unset($attr_list[$_k]);
                }
            }

            $return_list[$k]['attr_data'] = $attr_data;
        }

        // 返回组装好的数据
        return $return_list;
    }

    /**
     * 【后台】根据题库和标签计算题库对应题目数量数据验证
     *
     * @author 何岳龙
     * @param array $params
     *
     * @return bool
     */
    public function bank_count_validation($params = [])
    {

        // 验证题库列表
        if (empty($params['bank_list']) || !is_array($params['bank_list'])) {
            E('_ERR_BANK_LIST');
        }

        // 获取题库IDS个数
        $eb_ids = count(array_unique(array_filter(array_column($params['bank_list'], 'eb_id'))));

        // 如果数据格式错误
        if (empty($eb_ids)) {
            E('_EMPTY_BANK_IDS_LIST');
        }

        $attr_ids = [];
        // 如果标签筛选方式存在
        if (!empty($params['tag_data']) && is_array($params['tag_data'])) {

            foreach ($params['tag_data'] as $v) {
                $attr_ids_arr = array_column($v['attr_data'], 'attr_id');
                $attr_ids = array_merge($attr_ids, $attr_ids_arr);
            }

            if (!empty($attr_ids)) {
                // 验证类型
                if (!is_numeric($params['search_type']) || !in_array($params['search_type'],
                        [self::SEARCH_ATTR_TYPE_ALL, self::SEARCH_ATTR_TYPE_NOT_ALL])
                ) {

                    E('_ERR_SEARCH_TYPE');
                }
            }
        }

        return true;
    }

    /**
     * 获取当前题库,标签,搜索类型 获取题目数
     * @param array $eb_ids 题库IDS
     * @param array $attr_ids 属性IDS
     * @param int $search_type
     * @return array
     */
    public function tag_count($eb_ids=[], $attr_ids=[], $search_type = 1)
    {
        // 获取根据题库和标签计算题库对应题目数量
        $bank_data = $this->get_bank_data($eb_ids, $attr_ids, $search_type);

        // 组装
        $bank_list = [
            'single_count' => 0, // 单选题数量
            'multiple_count' => 0,// 	多选题每题数量
            'judgment_count' => 0,// 判断题数量
            'question_count' => 0,// 问答题数量
            'voice_count' => 0,// 语音题数量
        ];

        // 遍历题库数据
        foreach ($bank_data as $val) {
            $bank_list['single_count'] += $val['single_count']; // 单选题数量
            $bank_list['multiple_count'] += $val['multiple_count']; // 	多选题每题数量
            $bank_list['judgment_count'] += $val['judgment_count']; // 判断题数量
            $bank_list['question_count'] += $val['question_count']; // 问答题数量
            $bank_list['voice_count'] += $val['voice_count']; // 语音题数量
        }

        return $bank_list;
    }

    /**
     * 【后台】根据题库和标签计算题库对应题目数量
     *
     * @author 何岳龙
     * @param array $eb_ids 题库集合
     * @param array $attr_ids 属性集合
     * @param Int $search_type 筛选方式
     *
     * @return array
     */
    public function get_bank_data($eb_ids = [], $attr_ids = [], $search_type = 0)
    {

        // 初始化数据
        $list = [];

        // 如果属性列表为空
        if (empty($attr_ids)) {

            // 获取全部题库的列表
            $list = $this->get_bank_all_count_list($eb_ids);
        }

        // 如果有属性列表
        if (!empty($attr_ids)) {

            // 全部满足
            if ($search_type == self::SEARCH_ATTR_TYPE_ALL) {

                $list = $this->search_type_all($eb_ids, $attr_ids);
            }

            // 满足一个
            if ($search_type == self::SEARCH_ATTR_TYPE_NOT_ALL) {

                $list = $this->search_type_not_all($eb_ids, $attr_ids);
            }

        }

        return $list;
    }

    /**
     * 【后台】获取全部题库下的所有题目统计数
     *
     * @author 岳龙
     * @param array $eb_ids 题库集合
     *
     * @return array
     */
    public function get_bank_all_count_list($eb_ids = [])
    {
        // 初始化
        $list = [];
        // 获取题库列表
        $data = $this->_d_bank->list_by_conds(['eb_id' => $eb_ids]);

        // 遍历数组
        foreach ($data as $key => $v) {

            $list[] = [
                'eb_id' => intval($v['eb_id']),
                'eb_name' => strval($v['eb_name']),
                'single_count' => intval($v['single_count']),
                'multiple_count' => intval($v['multiple_count']),
                'judgment_count' => intval($v['judgment_count']),
                'question_count' => intval($v['question_count']),
                'voice_count' => intval($v['voice_count'])
            ];
        }

        return $list;
    }

    /**
     * 【后台】满足全部
     *
     * @author 岳龙
     * @param array $eb_ids 题库ID集合
     * @param array $attr_ids 属性ID集合
     *
     * @return array
     */
    public function search_type_all($eb_ids = [], $attr_ids = [])
    {
        // 初始化
        $list = [];

        // 初始化题库下的题目IDS数组
        $eb_list = [];

        // 获取全部满足条件的
        $topic_attr_all_list = $this->_topic_attr->list_by_conds(['eb_id' => $eb_ids, 'attr_id' => $attr_ids]);

        // 遍历关联关系表
        foreach ($topic_attr_all_list as $item) {
            $eb_list[$item['et_id']][] = $item['attr_id'];
        }

        $topic_ids = [];
        // 组装符合条件的数据
        foreach ($eb_list as $key => $v) {
            if (array_intersect($attr_ids, $v) == $attr_ids) {
                $topic_ids[] = $key;
            }
        }

        // 组装数据
        $eb_list_arr = [];

        // 如果没有符合条件的数据
        if (!empty($topic_ids)) {

            // 查询符合条件的试题列表
            $topic_list = $this->_topic->list_by_conds(['et_id' => $topic_ids]);

            foreach ($topic_list as $key => $v) {

                $eb_list_arr[$v['eb_id']][] = $v['et_id'];
            }

        }

        // 获取题库列表
        $bank_list = $this->_d_bank->list_by_conds(['eb_id' => $eb_ids]);

        // 遍历题库列表
        foreach ($bank_list as $vo) {

            list(
                $single_count,
                $multiple_count,
                $judgment_count,
                $question_count,
                $voice_count) = $this->count_bank_num_list($eb_list_arr[$vo['eb_id']]);

            // 组装数据
            $list[] = [
                'eb_id' => intval($vo['eb_id']),
                'eb_name' => strval($vo['eb_name']),
                'single_count' => $single_count,
                'multiple_count' => $multiple_count,
                'judgment_count' => $judgment_count,
                'question_count' => $question_count,
                'voice_count' => $voice_count
            ];
        }

        return $list;
    }

    /**
     *【后台】获取题库ID对应题库五种题目数
     *
     * @param array $ids 题目IDS
     *
     * @return array
     */
    public function count_bank_num_list($ids = [])
    {
        // 初始化
        $list = [];

        // 去重复
        $ids = array_unique($ids);

        // 单选题数量
        $single_count = 0;

        // 多选题数量
        $multiple_count = 0;

        // 判断题数量
        $judgment_count = 0;

        // 问答题数量
        $question_count = 0;

        // 语音题数量
        $voice_count = 0;

        // 如果题目IDS不为空
        if (!empty($ids)) {

            // 获取列表
            $list = $this->_topic->list_by_conds(['et_id' => $ids]);

        }

        // 遍历数据
        foreach ($list as $key => $v) {

            // 试题类型:单选题
            if (self::TOPIC_TYPE_SINGLE == $v['et_type']) {
                $single_count++;
                continue;
            }

            // 试题类型:判断题
            if (self::TOPIC_TYPE_JUDGMENT == $v['et_type']) {
                $judgment_count++;
                continue;
            }

            // 试题类型:问答题
            if (self::TOPIC_TYPE_QUESTION == $v['et_type']) {
                $question_count++;
                continue;
            }

            // 试题类型:多选题
            if (self::TOPIC_TYPE_MULTIPLE == $v['et_type']) {
                $multiple_count++;
            }

            // 试题类型:语音题
            if (self::TOPIC_TYPE_VOICE == $v['et_type']) {
                $voice_count++;
                continue;
            }
        }

        return [$single_count, $multiple_count, $judgment_count, $question_count, $voice_count];
    }

    /**
     * 【后台】满足一个
     *
     * @author 岳龙
     * @param array $eb_ids 题库ID集合
     * @param array $attr_ids 属性ID集合
     *
     * @return array
     */
    public function search_type_not_all($eb_ids = [], $attr_ids = [])
    {
        // 初始化
        $list = [];
        // 查询关系表中列表
        $topic_attr_list = $this->_topic_attr->list_by_conds(['eb_id' => $eb_ids, 'attr_id' => $attr_ids]);

        // 初始化题库下的题目IDS数组
        $eb_list = [];
        // 组装数据
        foreach ($topic_attr_list as $key => $v) {

            $eb_list[$v['eb_id']][] = $v['et_id'];
        }

        // 获取题库列表
        $bank_list = $this->_d_bank->list_by_conds(['eb_id' => $eb_ids]);

        // 遍历题库列表
        foreach ($bank_list as $vo) {

            list(
                $single_count,
                $multiple_count,
                $judgment_count,
                $question_count,
                $voice_count) = $this->count_bank_num_list($eb_list[$vo['eb_id']]);

            // 组装数据
            $list[] = [
                'eb_id' => intval($vo['eb_id']),
                'eb_name' => strval($vo['eb_name']),
                'single_count' => $single_count,
                'multiple_count' => $multiple_count,
                'judgment_count' => $judgment_count,
                'question_count' => $question_count,
                'voice_count' => $voice_count
            ];
        }

        return $list;
    }
}