TopicService.class.php 49.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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
<?php
/**
 * 考试-题目表
 * @author: houyingcai
 * @email:  594609175@qq.com
 * @date :  2017-05-19 17:45:33
 * @version $Id$
 */

namespace Common\Service;

use Common\Common\AttachOperation;
use Common\Model\AttrModel;
use Common\Model\BankModel;
use Common\Model\TopicAttrModel;
use Common\Model\TopicModel;
use Com\Validate;
use Think\Exception;

class TopicService extends AbstractService
{
    // 标题长度
    const TIT_MUN = 500;
    // 表头最小长度
    const TIT_LEN = 8;
    // 判断题字段长度
    const JU_LEN = 5;
    // 多选题限制个数
    const CHECKBOX_LEN = 2;
    // 模版选项文字长度
    const FONT_LEN = 120;
    // 模版数据记录数
    const TPL_LEN = 5;
    // 标题长度
    const TIT_LEN_LEN = 15;
    // 最大长度
    const HEAD_LEN = 16;

    // 初始值
    const LEN = 1;

    // 模板类型
    protected $_xls_type = [
        'radio' => '单选题',
        'checkbox' => '多选题',
        'jundgment' => '判断题'
    ];

    // 判断题答案
    protected $_choice_list = ['对', '错'];

    // 选项列表
    protected $_options_list = [
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J'
    ];

    // 错误列表
    protected $_errorList = [
        'et_type' => '题目类型不符合规范',
        'attr_list' => '属性不合法',
        'order_num' => '题目排序不符合规范',
        'score' => '题目分数不符合规范',
        'title' => '题目名称不符合规范',
        'answer' => '正确答案不符合规范',
        'answer_resolve' => '答案解析不符合规范',
        'A' => '选项A不符合规范',
        'B' => '选项B不符合规范',
        'C' => '选项C不符合规范',
        'D' => '选项D不符合规范',
        'E' => '选项E不符合规范',
        'F' => '选项F不符合规范',
        'G' => '选项G不符合规范',
        'H' => '选项H不符合规范',
        'I' => '选项I不符合规范',
        'J' => '选项J不符合规范'
    ];

    // 列表名称
    protected $__fieldName = [
        [
            'key' => 'et_type',
            'name' => '题目类型'
        ],
        [
            'key' => 'attr_list',
            'name' => '题目标签属性'
        ],
        [
            'key' => 'title',
            'name' => '题目名称'
        ],
        [
            'key' => 'score',
            'name' => '题目分数'
        ],
        [
            'key' => 'answer',
            'name' => '正确答案'
        ],
        [
            'key' => 'answer_resolve',
            'name' => '答案解析'
        ],
        [
            'key' => 'A',
            'name' => '选项A'
        ],
        [
            'key' => 'B',
            'name' => '选项B'
        ],
        [
            'key' => 'C',
            'name' => '选项C'
        ],
        [
            'key' => 'D',
            'name' => '选项D'
        ],
        [
            'key' => 'E',
            'name' => '选项E'
        ],
        [
            'key' => 'F',
            'name' => '选项F'
        ],
        [
            'key' => 'G',
            'name' => '选项G'
        ],
        [
            'key' => 'H',
            'name' => '选项H'
        ],
        [
            'key' => 'I',
            'name' => '选项I'
        ],
        [
            'key' => 'J',
            'name' => '选项J'
        ],
        [
            'key' => 'result',
            'name' => '导入结果'
        ]
    ];

    // 默认页数
    const DEFAULT_PAGE = 1;

    /** @var BankModel 初始化题目性表 */
    protected $_d_bank = null;
    /** @var AttrModel 初始化属性表 */
    protected $_d_attr = null;
    /** @var TagService 初始化标签表 */
    protected $_d_tag = null;
    /** @var TopicAttrModel 初始化题目属性表 */
    protected $_d_topic_attr = null;

    // 构造方法
    public function __construct()
    {
        $this->_d = new TopicModel();
        $this->_d_bank = new BankModel();
        $this->_d_attr = new AttrModel();
        $this->_d_topic_attr = new TopicAttrModel();
        $this->_d_tag = new TagService();

        parent::__construct();
    }

    /**
     * 添加题库题目(后台)
     *
     * @author 英才
     *
     * @param array $result 返回题目信息
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function add_topic(&$result, $reqData)
    {
        // 获取题目数据
        $topic = $this->fetch_topic($reqData);
        // 验证数据
        $this->validate_for_add($topic);

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

            // 题目信息入库
            $et_id = $this->_d->insert($topic);

            $condition = [];
            $condition['eb_id'] = $topic['eb_id'];

            if ($et_id) {

                // 题目标签属性入库
                if (!empty($topic['tag_list'])) {

                    $this->insert_topic_attr_data($topic['tag_list'], $topic['eb_id'], $et_id);
                }

                // 更新题库表中的题的个数
                $this->update_eb_topic_add($topic['et_type'], $condition);
            }

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

            $this->_d->rollback();
            E('_ERR_ADD_TOPIC_FAILED');
        }

        // 返回结果集
        $result['et_id'] = (int)$et_id;


        // 附件处理开始
        $attach_aerv = new AttachOperation();
        $at_ids = [];
        if (!empty($topic['title_pic'])) {
            // 题目图片
            $at_ids = explode(',', $topic['title_pic']);
        }

        $atids = [];
        if (!empty($topic['options'])) {
            $options = unserialize($topic['options']);
            // 获取题目选项中的图片附件ID集合
            if (!empty($options)) {
                $atids = array_filter(array_column($options, 'option_image_id'));
            }
        }

        $attach_ids = array_merge($at_ids, $atids);
        // 通知uc附件的使用状态
        $attach_aerv->insert_attach(
            APP_DIR,
            'topic',
            $et_id,
            ['attach_ids' => $attach_ids]
        );

        return true;
    }

    /**
     * 编辑题库题目(后台)
     *
     * @author 英才
     *
     * @param array $result 返回题目信息
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function update_topic(&$result, $reqData)
    {
        $et_id = intval($reqData['et_id']);
        // 题目ID不能为空
        if (empty($et_id)) {

            E('_EMPTY_ET_ID');
        }
        // 题目不存在
        if (!$old_topic = $this->_d->get($et_id)) {

            E('_ERR_TOPIC_NOT_FOUND');

        }
        // 获取提交的题目数据
        $topic = $this->fetch_topic($reqData);
        // 验证数据
        $this->validate_for_add($topic);

        // 验证题目类型是否被修改
        if ($old_topic['et_type'] != $topic['et_type']) {

            E('_ERR_TOPIC_TYPE_UPDATE');
        }

        // 标签列表
        $tag_list = $topic['tag_list'];

        unset($topic['tag_list']);

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

            // 更新题目信息
            $this->_d->update_by_conds(['et_id' => $et_id], $topic);
            // 删除原有的关联标签属性
            $this->_d_topic_attr->delete_by_conds(['et_id' => $et_id]);

            // 题目标签属性入库
            if (!empty($tag_list)) {
                // 重新插入关联标签属性
                $this->insert_topic_attr_data($tag_list, $topic['eb_id'], $et_id);
            }

            // 题目类型改变后,更新题库表中的题的个数(!!现在不允许修改题目类型了)
            if ($old_topic['et_type'] != $topic['et_type']) {

                $condition = [];
                $condition["eb_id"] = $topic['eb_id'];

                // 旧的题目类型数量-1
                $this->update_eb_topic_cutting($old_topic['et_type'], $condition);
                // 新的题目类型数量+1
                $this->update_eb_topic_add($topic['et_type'], $condition, 'edit');
            }

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

            $this->_d->rollback();
            E('_ERR_UPDATE_TOPIC_FAILED');
        }

        //返回结果集
        $result['et_id'] = (int)$et_id;

        // 附件处理开始[此处的所有附件只标识使用,不删除,因为不确定该试题被那些地方使用]
        $attach_aerv = new AttachOperation();

        // 获取题目标题图片集合
        $at_ids = explode(',', $topic['title_pic']);

        // 获取编辑后的选项图片集合
        $atids = [];
        $options = unserialize($topic['options']);
        if (!empty($options)) {
            $atids = array_filter(array_column($options, 'option_image_id'));
        }
        $attach_ids = array_merge($at_ids, $atids);
        // 通知uc附件的使用状态
        $attach_aerv->update_attach(
            APP_DIR,
            'topic',
            $et_id,
            ['attach_ids' => $attach_ids]
        );

        return true;
    }

    /**
     * 删除题库题目(后台)
     *
     * @author 英才
     *
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function delete_topic($reqData)
    {
        $et_ids = array_column($reqData['et_ids'], 'et_id');

        if (empty($et_ids) || !is_array($et_ids)) {

            E('_EMPTY_ET_ID');
        }

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

            // 获取需要删除的所有题目
            $topic_del_list = $this->_d->list_by_pks($et_ids);
            // 删除题目
            $this->_d->delete($et_ids);
            // 删除题目关联的属性
            $this->_d_topic_attr->delete_by_conds(['et_id' => $et_ids]);
            // 更新题库下的题目数量
            foreach ($topic_del_list as $value) {

                $condition['eb_id'] = $value['eb_id'];
                // 更新题库中题目所属类型的题目数量
                $this->update_eb_topic_cutting($value['et_type'], $condition);
                // 更新题库中题目的总数量
                $this->_d_bank->setDecNum('total_count', $condition, 1);
            }

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

            $this->_d->rollback();
            E('_ERR_DELETE_TOPIC_FAILED');
        }

        // 删除附件操作
        $attach_serv = new AttachOperation();
        $attach_serv->delete_attach(APP_DIR, 'topic', $et_ids);
        return true;
    }

    /**
     * 获取题库题目详情(后台)
     *
     * @author 英才
     *
     * @param array $result 返回题目信息
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function get_bank_topic(&$result, $reqData)
    {
        // 获取参数
        $et_id = rintval($reqData['et_id']);
        // 题目ID不能为空
        if (empty($et_id)) {

            E('_EMPTY_ET_ID');

        }
        // 获取题目详情
        $topic = $this->_d->get($et_id);
        if (empty($topic)) {

            E('_ERR_TOPIC_NOT_FOUND');
        }

        // 题目图片信息
        $pics_array = empty($topic['title_pic']) ? [] : explode(',', $topic['title_pic']);
        $pics_array_news = [];
        foreach ($pics_array as $v) {

            $pics_array_news[] = ['atId' => $v, 'atAttachment' => imgUrlReal($v)];
        }

        $topic['title_pic'] = $pics_array_news;

        // 题目选项反序列化
        $topic['options'] = empty($topic['options']) ? [] : unserialize($topic['options']);
        // 关键字反序列话
        $topic['answer_keyword'] = empty($topic['answer_keyword']) ? [] : unserialize($topic['answer_keyword']);
        // 获取题目属性列表
        $topic_attr_list = $this->_d_topic_attr->list_by_conds(['et_id' => $et_id]);
        // 题目属性ID列表
        $attr_ids = array_column($topic_attr_list, 'attr_id');

        // 获取所有标签属性列表
        $topic['tag_list'] = $this->_d_tag->get_all_tag_attr();

        // 重组标签属性列表
        foreach ($topic['tag_list'] as &$tag) {

            // 根据属性列表判断题目关联的标签属性选中状态
            foreach ($tag['attr_list'] as &$val) {
                if (in_array($val['attr_id'], $attr_ids)) {
                    $val['checked'] = 1;
                } else {
                    $val['checked'] = 0;
                }
            }
        }

        $topic['score'] = (int)$topic['score'];
        $result = $topic;

        return true;
    }

    /**
     * 获取题库题目列表(后台)
     *
     * @author 英才
     *
     * @param array $result 返回题目信息
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function get_bank_topic_list(&$result, $reqData)
    {
        $eb_id = rintval($reqData['eb_id']);
        $title = raddslashes($reqData['title']);

        $et_type = 0;
        if (isset($reqData['et_type']) && $reqData['et_type']) {

            $et_type = rintval($reqData['et_type']);
        }

        $attr_id = 0;
        if (isset($reqData['attr_id']) && $reqData['attr_id']) {

            $attr_id = rintval($reqData['attr_id']);
        }

        // 题库ID不能为空
        if (!$eb_id) {

            E('_EMPTY_EB_ID');
        }
        // 题库不存在
        if (!$this->_d_bank->get($eb_id)) {

            E('_ERR_BANK_NO_EXISTS');
        }

        // 默认值
        $page = !empty($reqData['page']) ? intval($reqData['page']) : self::DEFAULT_PAGE;
        $limit = !empty($reqData['limit']) ? intval($reqData['limit']) : self::DEFAULT_LIMIT_ADMIN;
        // 分页
        list($start, $limit) = page_limit($page, $limit);
        // 排序
        $order_option = ['order_num' => 'ASC', 'et_id' => 'DESC'];

        // 初始化数据
        $conds = [];
        $conds['eb_id'] = $eb_id;
        $conds['et_type'] = $et_type ? $et_type : '';
        $conds['title'] = !empty($title) ? $title : '';

        // 属性ID是否存在
        if ($attr_id) {

            $conds['attr_id'] = $attr_id;
        }

        // 获取题目总数
        $total = $this->_d->count_by_where($conds);

        $list = [];
        // 获取题目列表
        if ($total > 0) {

            $list = $this->_d->list_by_where($conds, [$start, $limit], $order_option);
        }

        // 获取题目关联的所有属性
        foreach ($list as &$val) {

            $topic_attr_list = $this->_d_topic_attr->list_by_conds(['et_id' => $val['et_id']], null, [],
                'attr_id');
            $attr_ids = array_column($topic_attr_list, 'attr_id');

            $attr_list = $this->_d_attr->list_by_pks($attr_ids);
            $val['attr_list'] = array_column($attr_list, 'attr_name');
        }

        $result['total'] = (int)$total;
        $result['page'] = (int)$page;
        $result['limit'] = (int)$limit;
        $result['list'] = $this->format_list($list);

        return true;
    }

    /**
     * 获取题库题目列表(RPC) 何岳龙
     *
     * @param array $result 返回题目信息
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function get_bank_topic_rpc_list(&$result, $reqData)
    {
        $eb_id = rintval($reqData['eb_id']);
        $title = raddslashes($reqData['title']);
        $type_list = $reqData['type_list'];
        $attr_id = rintval($reqData['attr_id']);

        // 默认值
        $page = !empty($reqData['page']) ? intval($reqData['page']) : self::DEFAULT_PAGE;
        $limit = !empty($reqData['limit']) ? intval($reqData['limit']) : self::DEFAULT_LIMIT_ADMIN;
        // 分页
        list($start, $limit) = page_limit($page, $limit);
        // 排序
        $order_option = ['order_num' => 'ASC'];

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

        // 如果题目ID存在
        if (!empty($eb_id)) {

            $conds['eb_id'] = $eb_id;
        }

        // 实例化
        $et_types = [];

        // 获取类型列表
        if (!empty($type_list)) {

            $et_types = array_unique(array_filter(array_column($type_list, 'type')));
        }

        // 如果类型不为空
        if (!empty($et_types)) {
            $conds['et_type'] = $et_types;
        }

        $conds['title'] = !empty($title) ? $title : '';

        // 属性ID是否存在
        if ($attr_id) {

            $conds['attr_id'] = $attr_id;
        }

        // 获取题目总数
        $total = $this->_d->count_by_where($conds);

        $list = [];
        // 获取题目列表
        if ($total > 0) {

            $list = $this->_d->list_by_where($conds, [$start, $limit], $order_option);
        }

        // 获取题目关联的所有属性
        foreach ($list as &$val) {

            $topic_attr_list = $this->_d_topic_attr->list_by_conds(['et_id' => $val['et_id']], null, [],
                'attr_id');
            $attr_ids = array_column($topic_attr_list, 'attr_id');

            $attr_list = $this->_d_attr->list_by_pks($attr_ids);
            $val['attr_list'] = array_column($attr_list, 'attr_name');
        }

        $result['total'] = (int)$total;
        $result['page'] = (int)$page;
        $result['limit'] = (int)$limit;
        $result['list'] = $this->format_list($list);

        return true;
    }

    /**
     * 验证新增题目数据
     * @author 英才
     *
     * @param array $topic 题目数据
     *
     * @return bool
     */
    protected function validate_for_add(&$topic)
    {
        // 验证规则
        $rules = [
            'eb_id' => 'require',
            'score' => 'require|number',
            'title' => 'require'
        ];

        // 错误提示
        $msgs = [
            'eb_id' => L('_EMPTY_EB_ID'),
            'score.number' => L('_ERR_SCORE_NUMBER'),
            'score.require' => L('_EMPTY_TOPIC_SCORE'),
            'title.require' => L('_EMPTY_ET_TITLE')
        ];

        // 开始验证
        $validate = new Validate($rules, $msgs);
        if (!$validate->check($topic)) {

            E($validate->getError());

            return false;
        }

        // 标题长度过长
        if (get_str_len($topic['title']) > 500) {

            E('_ERR_TITLE_LENGTH');
        }

        // 试题类型错误
        if (!in_array($topic['et_type'], [
            self::TOPIC_TYPE_SINGLE,
            self::TOPIC_TYPE_JUDGMENT,
            self::TOPIC_TYPE_QUESTION,
            self::TOPIC_TYPE_MULTIPLE,
            self::TOPIC_TYPE_VOICE
        ])) {

            E('_ERR_TOPIC_TYPE_WRONG');
        }

        // 不是语音题,答案不能为空
        if (empty($topic["answer"]) && $topic['et_type'] != self::TOPIC_TYPE_VOICE) {

            E('_EMPTY_ET_ANSWER');
        }

        // 题库是否存在
        if (!$this->_d_bank->get($topic['eb_id'])) {

            E('_ERR_BANK_NO_EXISTS');
        }

        // 题目图片
        if (!empty($topic['title_pic'])) {

            $atids = array_column($topic['title_pic'], 'atId');
            $topic['title_pic'] = implode(',', $atids);
        }

        // 选项 这是单选、多选题选项
        if (self::TOPIC_TYPE_MULTIPLE == $topic['et_type'] || self::TOPIC_TYPE_SINGLE == $topic['et_type']) {
            // 答案列表
            $answer_list = explode(',', $topic['answer']);

            // 如果是单选题
            if (self::TOPIC_TYPE_SINGLE == $topic['et_type']) {
                // 如果选项值大于或等于2
                if (count($answer_list) >= self::CHECKBOX_LEN) {

                    E('_ERR_ET_ANSWER');
                }
            }

            // 如果是多选
            if (self::TOPIC_TYPE_MULTIPLE == $topic['et_type']) {
                // 如果选项个数小于2
                if (count($answer_list) < self::CHECKBOX_LEN) {

                    E('_ERR_ET_ANSWER');
                }
            }

            // 选项不能为空
            if (empty($topic['options'])) {

                E('_EMPTY_TOPIC_OPTIONS');
            }

            // 初始化选项值
            $option_values = [];
            // 选项值列表
            foreach ($topic['options'] as $v) {
                // 选项值不能有空(如果值为0,则跳过)
                if (empty(trim($v['option_value'])) && $v['option_value'] !== '0') {

                    E('_EMPTY_OPTIONS_VALUE');
                }

                $option_values[] = $v['option_value'];
            }

            // 选项值不能有重复
            if (count($option_values) != count(array_unique($option_values))) {

                E('_ERR_OPTIONS_REPEAT');
            }

            // 选项个数最多20个
            if (count($topic['options']) > 20) {

                E('_ERR_OPTIONS_MAX');
            }

            $answer = array_unique(array_filter(explode(',', $topic['answer'])));
            // 多选题正确答案少于2个
            if ($topic["et_type"] == self::TOPIC_TYPE_MULTIPLE && count($answer) < 2) {

                E('_ERR_ANSWER_MIN_TWO');
            }

            // 重新排序答案选项
            sort($answer);
            $topic['answer'] = implode(',', $answer);
            // 序列化选项
            $topic['options'] = serialize($topic["options"]);
        }

        // 匹配关键字 问答题
        if (self::TOPIC_TYPE_QUESTION == $topic['et_type'] && self::KEYWORD_OPEN == $topic['match_type']) {
            // 判断关键字是否为空
            $keywords = array_column($topic['answer_keyword'], 'keyword');
            if (count($keywords) != count(array_filter($keywords))) {

                E('_EMPTY_TOPIC_KEYWORDS');
            }

            // 判断关键字是否有重复
            if (count($keywords) != count(array_unique($keywords))) {

                E('_ERR_TOPIC_KEYWORDS_REPEAD');
            }

            // 判断关键字覆盖率是否为100%
            if (array_sum(array_column($topic['answer_keyword'], 'percent')) != 100) {

                E('_ERR_KEYWORDS_COVERAGE');
            }

            $topic['answer_keyword'] = serialize($topic['answer_keyword']);
        } else {
            // 不是问答题,也没开启关键字匹配,清空关键字字段
            $topic['answer_keyword'] = serialize([]);
        }

        // 答案解析
        if (!empty($topic['answer_resolve'])) {
            if (get_str_len($topic['answer_resolve']) > 500) {

                E('_ERR_ANSWER_RESOLVE_LENGTH');
            }
        }

        // 正确答案
        if (!empty($topic['answer'])) {

            // 问答题正确答案超出字数限制
            if (self::TOPIC_TYPE_QUESTION == $topic['et_type'] && get_str_len($topic['answer']) > 500) {

                E('_ERR_ANSWER_LENGTH');
            }
        }

        // 正确答案覆盖率 【问答题】
        if (self::TOPIC_TYPE_QUESTION == $topic['et_type'] && !empty($topic['answer_coverage'])) {

            if ($topic['answer_coverage'] > 100) {

                E('_ERR_ANSWER_COVERAGE_LIMIT');
            }
        }

        return true;
    }

    /**
     * 获取题目数据
     * @author 英才
     *
     * @param array $topic 题目数据
     *
     * @return array|bool
     */
    protected function fetch_topic($topic)
    {
        return [
            'eb_id' => rintval($topic['eb_id']),
            'et_type' => rintval($topic['et_type']),
            'order_num' => rintval($topic['order_num']),
            'score' => rintval($topic['score']),
            'title' => raddslashes($topic['title']),
            'title_pic' => !empty($topic['title_pic']) ? $topic['title_pic'] : '',
            'options' => !empty($topic['options']) ? $topic['options'] : '',
            'answer_coverage' => !empty($topic['answer_coverage']) ? raddslashes($topic['answer_coverage']) : '',
            'match_type' => rintval($topic['match_type']),
            'answer_keyword' => !empty($topic['answer_keyword']) ? $topic['answer_keyword'] : '',
            'answer' => !empty($topic['answer']) ? raddslashes($topic['answer']) : '',
            'answer_resolve' => !empty($topic['answer_resolve']) ? raddslashes($topic['answer_resolve']) : '',
            'tag_list' => !empty($topic['tag_list']) ? $topic['tag_list'] : ''
        ];
    }

    /**
     * 标签属性入库
     *
     * @param array $tab_list 标签属性列表
     *
     * @author 英才
     *
     * @param int $eb_id 题库ID
     * @param int $et_id 题目ID
     *
     * @return bool
     */
    protected function insert_topic_attr_data($tab_list, $eb_id, $et_id)
    {
        foreach ($tab_list as $val) {

            foreach ($val['attr_list'] as $attr) {
                $attr_data[] = [
                    'etag_id' => $val['etag_id'],
                    'attr_id' => $attr['attr_id'],
                    'eb_id' => $eb_id,
                    'et_id' => $et_id
                ];
            }
        }

        // 如果题目关系属性数据存在
        if (!empty($attr_data)) {

            $this->_d_topic_attr->insert_all($attr_data);
        }

        return true;
    }

    /**
     * 更新题库中题目类别的个数(加)
     *
     * @author 英才
     *
     * @param  int $et_type 题目类型
     * @param  array $conds 更新条件
     * @param  string $type 操作方法
     *
     * @return bool
     */
    public function update_eb_topic_add($et_type, $conds, $type = 'add')
    {

        // 题库中这类题的个数 +1
        switch ($et_type) {

            case self::TOPIC_TYPE_SINGLE:
                // 单选题
                $this->_d_bank->setIncNum('single_count', $conds, 1);
                break;

            case self::TOPIC_TYPE_JUDGMENT:
                // 判断题
                $this->_d_bank->setIncNum('judgment_count', $conds, 1);
                break;

            case self::TOPIC_TYPE_QUESTION:
                // 问答题
                $this->_d_bank->setIncNum('question_count', $conds, 1);
                break;

            case self::TOPIC_TYPE_MULTIPLE:
                // 多选题
                $this->_d_bank->setIncNum('multiple_count', $conds, 1);
                break;

            case self::TOPIC_TYPE_VOICE:
                // 语音题
                $this->_d_bank->setIncNum('voice_count', $conds, 1);
                break;

            default:
                E('_ERR_TOPIC_TYPE_WRONG');
                break;
        }

        if ($type == 'add') {
            // 题库中的题的个数 +1
            $this->_d_bank->setIncNum('total_count', $conds, 1);
        }

        return true;
    }

    /**
     * 更新题库中题目类别的个数(减)
     *
     * @author 英才
     *
     * @param  int $et_type 题目类型
     * @param  array $conds 更新条件
     *
     * @return bool
     */
    public function update_eb_topic_cutting($et_type, $conds)
    {
        // 题库中这类题的个数 -1
        switch ($et_type) {
            case self::TOPIC_TYPE_SINGLE:
                // 单选题
                $this->_d_bank->setDecNum("single_count", $conds, 1);
                break;
            case self::TOPIC_TYPE_JUDGMENT:
                // 判断题
                $this->_d_bank->setDecNum("judgment_count", $conds, 1);
                break;
            case self::TOPIC_TYPE_QUESTION:
                // 问答题
                $this->_d_bank->setDecNum("question_count", $conds, 1);
                break;
            case self::TOPIC_TYPE_MULTIPLE:
                // 多选题
                $this->_d_bank->setDecNum("multiple_count", $conds, 1);
                break;
            case self::TOPIC_TYPE_VOICE:
                // 语音题
                $this->_d_bank->setDecNum("voice_count", $conds, 1);
                break;
            default:
                E('_ERR_TOPIC_TYPE_WRONG');

                break;
        }

        return true;
    }

    /**
     * 格式化题库列表数据
     *
     * @author 英才
     *
     * @param $data array 需要格式化的数据列表
     *
     * @return array
     */
    protected function format_list($data = [])
    {
        $list = [];
        if (!empty($data) && is_array($data)) {

            foreach ($data as $k => $v) {
                $list[$k]['et_id'] = rintval($v['et_id']);
                $list[$k]['title'] = $v['title'];
                $list[$k]['et_type'] = rintval($v['et_type']);
                $list[$k]['score'] = $v['score'];
                $list[$k]['order_num'] = rintval($v['order_num']);
                $list[$k]['use_num'] = rintval($v['use_num']);
                $list[$k]['attr_list'] = $v['attr_list'] ? $v['attr_list'] : [];
            }
        }

        return $list;
    }

    /**
     * 获取标题长度
     *
     * @author 岳龙
     *
     * @param array $list 列表数据
     *
     * @return int|string
     */

    private function countHead($list)
    {

        // 实例化
        $data = 0;

        // 循环数据
        for ($i = self::TIT_LEN_LEN; $i > 0; $i--) {

            // 判断非空
            if (!empty($list[$i]) || $list[$i] === '0') {

                // 赋值
                $data = $i + 1;

                break;
            }

        }

        return $data;

    }

    /**
     * 获取head数据列表以及整合过的xls中数据列表
     *
     * @author 岳龙
     *
     * @param array $list xls数据列表
     *
     * @return array
     */

    public function get_list($list = [])
    {

        // 初始化返回值
        $data = [];

        // 初始化
        $i = 0;

        // 存储标题长度
        $storage = [];

        // 循环数据
        foreach ($list as $key => $v) {

            // 截取前三条数据
            if ($i < self::TPL_LEN) {
                $i++;
                continue;
            }

            // 过滤空数据
            if (empty(trim($v[0])) && empty(trim($v[2]))) {

                break;
            }

            // 赋值
            $data[] = [
                'et_type' => $v[0] ? trim($v[0]) : '',
                'attr_list' => $v[1] ? $v[1] : '',
                'title' => $v[2] ? trim($v[2]) : '',
                'score' => (int)$v[3] ? trim($v[3]) : '',
                'answer' => $v[4] ? trim($v[4]) : '',
                'answer_resolve' => $v[5] ? trim($v[5]) : '',
                'A' => ($v[6] || $v[6] == 0) ? trim($v[6]) : '',
                'B' => ($v[7] || $v[7] == 0) ? trim($v[7]) : '',
                'C' => ($v[8] || $v[8] == 0) ? trim($v[8]) : '',
                'D' => ($v[9] || $v[9] == 0) ? trim($v[9]) : '',
                'E' => ($v[10] || $v[10] == 0) ? trim($v[10]) : '',
                'F' => ($v[11] || $v[11] == 0) ? trim($v[11]) : '',
                'G' => ($v[12] || $v[12] == 0) ? trim($v[12]) : '',
                'H' => ($v[13] || $v[13] == 0) ? trim($v[13]) : '',
                'I' => ($v[14] || $v[14] == 0) ? trim($v[14]) : '',
                'J' => ($v[15] || $v[15] == 0) ? trim($v[15]) : ''
            ];

            // 存储所有表格长度
            $storage[] = $this->countHead($v);
        }

        // 表格题目数组
        $title = [];

        // 初始化表头最小长度
        $min = self::TIT_LEN;

        // 获取本文件中的类型
        $types = array_column($data, 'et_type');

        // 含有多选题,则最小表头加一
        if (in_array($this->_xls_type['checkbox'], $types)) {
            $min = self::TIT_LEN + self::LEN;
        }

        // 设置最大表头长度
        if (max($storage) < $min) {

            $max_title = $min;
        } else {

            $max_title = max($storage);
        }

        // 重组表格标题
        for ($i = 0; $i < $max_title; $i++) {

            // 标题表格列表
            $title[$i] = $this->__fieldName[$i];

            // 如果是最大值
            if ($max_title - self::LEN == $i) {

                // 新增是否导入成功
                $title[$i + self::LEN] = $this->__fieldName[self::HEAD_LEN];

            }

        }

        return [
            $data,
            $title,
            count($data),
            count($title) - self::LEN
        ];
    }

    /**
     * 重新获取列表
     *
     * @author 岳龙
     *
     * @param array $data POST数据
     *
     * @return bool
     */
    public function get_data(&$data = [])
    {

        // 如果类型为判断题
        if (!empty($data['type']) && $data['type'] == $this->_xls_type['jundgment']) {

            // 初始化数据
            $i = 0;

            // 遍历数据
            foreach ($data as $key => $item) {

                // 如果为判断题
                if ($i > self::JU_LEN) {

                    $data[$key] = '';
                }

                $i++;
            }

        }

        return true;
    }

    /**
     * 判断列表条件
     *
     * @author 岳龙
     *
     * @param array $data POST 数据
     *
     * @return mixed
     */
    public function is_parameter($data)
    {
        // 实例化导出结果
        $result = [];

        // 是否为选择题
        $select = false;

        // 如果列表类型不符和要求
        if (!in_array($data['et_type'], $this->_xls_type)) {

            $result[] = [
                'code' => 'et_type',
                'msg' => $this->_errorList['et_type']
            ];
        }

        // 如果分数排序不符和要求
        if (!is_numeric($data['score']) || strstr($data['score'], ".") || intval($data['score']) < 1) {

            $result[] = [
                'code' => 'score',
                'msg' => $this->_errorList['score']
            ];
        }

        // 如果题目要求
        if (empty($data['title']) || get_str_len($data['title']) > self::TIT_MUN) {

            $result[] = [
                'code' => 'title',
                'msg' => $this->_errorList['title']
            ];
        }

        // 如果答案解析存在
        if (!empty($data['answer_resolve'])) {

            if (get_str_len($data['answer_resolve']) > self::TIT_MUN) {
                $result[] = [
                    'code' => 'answer_resolve',
                    'msg' => $this->_errorList['answer_resolve']
                ];
            }
        }

        // 答案数组
        $answer_data = [];

        // 选项数据A
        if (!empty($data['A']) || $data['A'] == 0) {

            array_push($answer_data, 'A');
        }

        // 选项数据B
        if (!empty($data['B']) || $data['B'] == 0) {

            array_push($answer_data, 'B');
        }

        // 选项数据C
        if (!empty($data['C']) || $data['C'] == 0) {

            array_push($answer_data, 'C');
        }

        // 选项数据D
        if (!empty($data['D']) || $data['D'] == 0) {

            array_push($answer_data, 'D');
        }

        // 选项数据E
        if (!empty($data['E']) || $data['E'] == 0) {

            array_push($answer_data, 'E');
        }

        // 选项数据F
        if (!empty($data['F']) || $data['F'] == 0) {

            array_push($answer_data, 'F');
        }

        // 选项数据G
        if (!empty($data['G']) || $data['G'] == 0) {

            array_push($answer_data, 'G');
        }

        // 选项数据H
        if (!empty($data['H']) || $data['H'] == 0) {

            array_push($answer_data, 'H');
        }

        // 选项数据I
        if (!empty($data['I']) || $data['I'] == 0) {

            array_push($answer_data, 'I');
        }

        // 选项数据J
        if (!empty($data['J']) || $data['J'] == 0) {

            array_push($answer_data, 'J');
        }

        // 如果类型为单选题
        if ($data['et_type'] == $this->_xls_type['radio']) {

            $select = true;
            $answer = str_split($data['answer']);
            // 兼容小写
            $data['answer'] = strtoupper($data['answer']);

            // 如果答案不在选项中
            if (!in_array($data['answer'], $this->_options_list) || !in_array($data['answer'],
                    $answer_data) || count($answer) >= self::CHECKBOX_LEN || empty($data['answer'])
            ) {

                $result[] = [
                    'code' => 'answer',
                    'msg' => $this->_errorList['answer']
                ];
            }

            // 如果选项A不存在
            if ($data['A'] == '') {
                $result[] = [
                    'code' => 'A',
                    'msg' => $this->_errorList['A']
                ];
            }

            // 如果选项B不存在
            if ($data['B'] == '') {

                $result[] = [
                    'code' => 'B',
                    'msg' => $this->_errorList['B']
                ];
            }
        }

        // 如果类型为多选题
        if ($data['et_type'] == $this->_xls_type['checkbox']) {

            $select = true;
            // 拆分为数组
            $answer = str_split($data['answer']);

            // 遍历答案
            foreach ($answer as $v) {
                // 兼容小写
                $v = strtoupper($v);
                // 如果答案不在选项中
                if (!in_array($v, $this->_options_list) || !in_array($v, $answer_data) || count($answer) < self::CHECKBOX_LEN || (empty($data[$v]) && $data[$v] != '0')
                ) {

                    $result[] = [
                        'code' => 'answer',
                        'msg' => $this->_errorList['answer']
                    ];

                    break;
                }
            }

            // 如果选项A不存在
            if ($data['A'] == '') {
                $result[] = [
                    'code' => 'A',
                    'msg' => $this->_errorList['A']
                ];
            }
            // 如果选项B不存在
            if ($data['B'] =='' ) {
                $result[] = [
                    'code' => 'B',
                    'msg' => $this->_errorList['B']
                ];
            }

            // 如果选项C不存在
            if ($data['C'] == '') {

                $result[] = [
                    'code' => 'C',
                    'msg' => $this->_errorList['C']
                ];
            }
        }

        // 如果是选择题
        if ($select) {
            // 获取选项错误原因
            $this->select($result, $data);
        }

        // 如果类型为判断题
        if ($data['et_type'] == $this->_xls_type['jundgment']) {
            // 如果答案不在选项中
            if (!in_array($data['answer'], $this->_choice_list)) {

                $result[] = [
                    'code' => 'answer',
                    'msg' => $this->_errorList['answer']
                ];
            }
        }

        return $result;
    }

    /**
     * 获取选项错误原因
     *
     * @author 岳龙
     *
     * @param array $result 错误结果集
     * @param array $data POST参数数组
     *
     * @return bool
     */
    private function select(&$result = [], $data = [])
    {

        // 遍历选项列表
        foreach ($this->_options_list as $key => $v) {

            // 如果选项存在
            if (!empty($data[$v])) {

                // 判断已存在对象上级是否存在
                if (empty($data[$this->_options_list[$key - self::LEN]]) && $key > self::LEN) {

                    // 遍历当前选项列表数据
                    for ($i = self::LEN + 1; $i < $key; $i++) {

                        // 如果选项列表对应的值不存在
                        if (empty($data[$this->_options_list[$i]]) && $data[$this->_options_list[$i]] != 0) {

                            $result[] = [
                                'code' => $this->_options_list[$i],
                                'msg' => $this->_errorList[$this->_options_list[$i]]
                            ];
                        }
                    }
                }

                // 如果选项中选项值超过指定个数抛出错误码
                if (get_str_len($data[$v]) > self::FONT_LEN) {

                    $result[] = [
                        'code' => $v,
                        'msg' => $this->_errorList[$v]
                    ];
                }
            }
        }

        return true;
    }

    /**
     * xls数据写入到数据库
     *
     * @author 岳龙
     *
     * @param array $params POST 数据
     *
     * @return bool
     */
    public function insert_xls_data($params = [])
    {

        try {
            $this->start_trans();

            // 实例化类型
            $et_type = 0;
            // 获取答案
            $answer = raddslashes($params['data']['answer']);

            // 如果类型为单选
            if ($params['data']['et_type'] == $this->_xls_type['radio']) {

                $et_type = self::TOPIC_TYPE_SINGLE;
            }

            // 如果类型为多选
            if ($params['data']['et_type'] == $this->_xls_type['checkbox']) {

                $et_type = self::TOPIC_TYPE_MULTIPLE;
                $answer = implode(',', str_split($answer));
            }

            // 如果类型为判断
            if ($params['data']['et_type'] == $this->_xls_type['jundgment']) {

                $et_type = self::TOPIC_TYPE_JUDGMENT;
            }

            // 初始化选项
            $options = [];

            // 循环遍历列表
            foreach ($this->_options_list as $v) {
                // 如果列表选项存在
                if ($params['data'][$v] != '') {

                    $options[] = [
                        'option_name' => $v,
                        'option_value' => $params['data'][$v],
                        'option_image_url' => '',
                        'option_image_id' => ''
                    ];
                }
            }
            // 获取题目列表
            $list = $this->_d->list_by_conds(['eb_id' => $params['eb_id']], null, [], 'order_num');

            // 初始化排序数
            $order_num = 1;

            // 获取有题目
            if (!empty($list)) {
                // 获取所有排序数
                $storage = array_column($list, 'order_num');
                // 获取最大排序数+1
                $order_num = max($storage) + 1;
            }

            // 组装数据
            $data = [
                'eb_id' => rintval($params['eb_id']),
                'et_type' => rintval($et_type),
                'order_num' => rintval($order_num),
                'score' => rintval($params['data']['score']),
                'title' => raddslashes($params['data']['title']),
                'options' => $params['data']['et_type'] != $this->_xls_type['jundgment'] ? serialize($options) : '',
                'answer' => !empty($answer) ? strtoupper($answer) : '',
                'answer_resolve' => !empty($params['data']['answer_resolve']) ? raddslashes($params['data']['answer_resolve']) : ''
            ];

            // 写入数据库
            $topic_id = $this->insert($data);

            // 如果已写入数据库且有对应属性值
            if (!empty($topic_id) && (!empty($params['data']['attr_list']))) {

                // 初始化属性数据
                $attr_data = [];
                // 置换和字符串
                $attr_lists = str_replace(';', ';', $params['data']['attr_list']);
                // 字符串转数组
                $attr_list = explode(';', $attr_lists);

                // 遍历数据
                foreach ($attr_list as &$v) {

                    $v = trim($v);
                }

                // 获取所有标签属性数据
                $list = $this->_d_attr->list_by_conds(['attr_name' => $attr_list]);

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

                    $attr_data[] = [
                        'etag_id' => $v['etag_id'],
                        'attr_id' => $v['attr_id'],
                        'eb_id' => $params['eb_id'],
                        'et_id' => $topic_id
                    ];
                }

                // 如果题目关系属性数据存在
                if (!empty($attr_data)) {

                    $this->_d_topic_attr->insert_all($attr_data);
                }
            }

            // 更新题目数
            $this->update_eb_topic_add($et_type, ['eb_id' => $params['eb_id']]);

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

            return false;
        } catch (\Exception $e) {

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

            return false;
        }

        return true;
    }

    /**
     * 根据条件,查询包含已删除的数据
     *
     * @author 岳龙
     *
     * @param array $conds 条件数组
     * @param int|array $page_option 分页参数
     * @param array $order_option 排序
     * @param string $fields 读取字段
     *
     * @return array|bool
     */
    public function list_topic_contain_del($conds, $page_option = [], $order_option = [], $fields = '*')
    {

        return $this->_d->list_topic_contain_del($conds, $page_option, $order_option, $fields);
    }

    /**
     *  根据条件,统计包含已删除的数据记录数
     *
     * @param array $conds 条件数组
     * @param string $fields 读取字段
     *
     * @return number
     */
    public function count_topic_contain_del($conds, $fields = '*')
    {

        return $this->_d->count_topic_contain_del($conds, $fields);
    }


    /**
     * 开始闯关
     * @author: houyingcai
     *
     * @param int $ec_id 课程ID
     * @param string $et_ids 题库
     *
     * @return  bool|array
     */
    public function start_break($ec_id = 0, $et_ids = '')
    {
        // 课程ID
        if (empty($ec_id)) {

            E('_EMPTY_BREAK_CID');
        }
        // 闯关题目Id
        if (empty($et_ids)) {

            E('_EMPTY_BREAK_TID');
        }
        // 闯关题目ID分隔
        $et_ids = explode(',', $et_ids);
        if (empty($et_ids)) {

            E('_EMPTY_BREAK_TID');
        }

        // 获取闯关试题列表
        $topic_list = $this->_d->list_by_pks($et_ids);

        // 没有题抛出错误
        if (empty($topic_list)) {

            E('_ERR_BANK_TOPIC_FAILED');
        }

        // 格式化返回数据
        $list = [];
        foreach ($topic_list as $val) {
            $options = [];
            // 题目选项
            if (!empty($val['options'])) {

                $et_option = unserialize($val['options']);
                $options = $this->format_option($et_option);
            }

            $list[] = [
                'et_id' => (int)$val['et_id'],
                'et_type' => (int)$val['et_type'],
                'title' => $val['title'],
                'title_pic' => $this->pic_url($val['title_pic']),
                'score' => $val['score'],
                'options' => $options
            ];
        }

        return $list;
    }

    /**
     * 格式化题目选项
     *
     * @author: houyingcai
     *
     * @param array $data 题目选项
     *
     * @return array
     */
    private function format_option($data = [])
    {
        $list = [];

        // 题目选项数据为空
        if (empty($data)) {

            return $list;
        }

        foreach ($data as $pic) {
            // 组装返回数据
            $list[] = [
                'option_name' => $pic['option_name'],
                'option_value' => $pic['option_value'],
                'option_image_id' => $pic['option_image_id'],
                'option_image_url' => $pic['option_image_id'] ? $this->format_cover($pic['option_image_id']) : '',
            ];
        }

        return $list;
    }


    /**
     * 图片数组
     * @author: houyingcai
     *
     * @param string $title_pics 题目图片
     *
     * @return array
     */
    private function pic_url($title_pics)
    {
        $result = [];
        if (empty($title_pics)) {

            return $result;
        }

        $data = explode(',', $title_pics);

        foreach ($data as $v) {
            if (!empty($v)) {

                $result[] = [
                    'atId' => $v,
                    'atAttachment' => imgUrlReal($v)
                ];
            }
        }

        return $result;
    }

    /**
     * 批量移动试题到指定题库(后台)
     *
     * @author pw
     *
     * @param array $etIds 所选题目ids
     * @param integer $ebId 目标题库id
     *
     * @return bool
     */
    public function move_topic($etIds, $ebId)
    {

        // 题库ID不能为空
        if (empty($ebId)) {
            E('_EMPTY_EB_ID');
        }

        // 题目ids为空
        if (!$etIds || empty($etIds) || !is_array($etIds)) {
            E('_EMPTY_ET_ID');
        }

        // 指定题库不存在
        if (!$nextBank = $this->_d_bank->get($ebId)) {

            E('_ERR_BANK_NO_EXISTS');
        }

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

            // 题目类型改变后,更新题库表中的题的个数
            $oldEbId = 0;
            $etIds = array_values($etIds);
            foreach ($etIds as $k => $etId) {
                $topicInfo = $this->get($etIds[$k]);
                // 旧题库该题目类型数量-1
                $this->update_eb_topic_cutting((int)$topicInfo['et_type'], ['eb_id' => (int)$topicInfo['eb_id']]);
                // 新题库该题目类型数量+1
                $this->update_eb_topic_add((int)$topicInfo['et_type'], ['eb_id' => (int)$ebId], 'edit');
                $oldEbId = (int)$topicInfo['eb_id'];
            }

            // 更新题库总数
            $topicCount = count($etIds);
            $this->_d_bank->setDecNum("total_count", ['eb_id' => (int)$oldEbId], $topicCount);
            $this->_d_bank->setDecNum("total_count", ['eb_id' => (int)$ebId], -$topicCount);

            // 批量更新题目所属题库
            $conditions['et_id IN (?)'] = $etIds;
            $data = ['eb_id' => $ebId];
            $this->_d->update_by_conds($conditions, $data);

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

            $this->_d->rollback();
            E('_ERR_UPDATE_TOPIC_FAILED');
        }

        return true;
    }
}