CommentService.class.php 42.1 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
<?php
/**
 * CommentService.class.php
 * 活动评论信息表
 *
 * @author: daijun
 * @copyright: vchangyi.com
 */

namespace Common\Service;

use Common\Common\AttachOperation;
use Common\Common\Helper;
use Common\Common\User;
use Common\Model\ActivityModel;
use Common\Model\CommentModel;
use Common\Model\LikeModel;
use Common\Model\PacketrecordModel;
use Common\Model\ReplyModel;
use Common\Model\RightModel;
use Think\Log;

class CommentService extends AbstractService
{

    // 回复第一页
    const REPLY_FIRST_PAGE = 1;
    // 分页从第二页开始
    const REPLY_SECOND_PAGE = 2;
    // 第一页回复每页条数
    const REPLY_LIMIT = 5;

    // 活动类型
    const ACTIVITY_TYPE = 1;
    // 回帖类型
    const COMMENT_TYPE = 2;
    // 回复类型
    const REPLY_TYPE = 3;

    // 默认获取点赞数
    const DEFAULT_LIKE_LIMIT = 12;

    // 分享(回帖/一级评论)审核通过
    const COMMENT_CHECK_PASS = 1;
    // 分享(回帖/一级评论)审核驳回
    const COMMENT_CHECK_REJECT = 2;

    // 自动审核:通过
    const AUTOCHECK_PASS = 200;
    // 自动审核:被驳回
    const AUTOCHECK_REFUSED = 404;
    // 自动审核:已核销
    const AUTOCHECK_ALREADY_COMPLETED = 405;
    // 自动审核文本:通过
    const AUTOCHECK_PASS_TEXT = '成功';
    // 自动审核回帖文本:通过
    const AUTOCHECK_WRITTEN_SUCCEED = '写入成功';
    // 自动审核文本:已核销
    const AUTOCHECK_ALREADY_COMPLETED_TEXT = '已核销';
    // 自动审核文本:被驳回
    const AUTOCHECK_REFUSED_TEXT = '无效数据';

    // 未被自动审核
    const IS_AUTOCHECKED_NO = 0;
    // 已被自动审核
    const IS_AUTOCHECKED_YES = 1;

    // 人工审核
    const CHECK_TYPE_USER = 1;
    // 系统自动审核
    const CHECK_TYPE_SYSTEM = 2;

    // 系统自动审核驳回理由
    const AUTOCHECK_REFUSE_REASON = '请在收银系统【报表】--【零售类】--“百日大战单据查询”报表中查询您的小票是否存在,不存在表示不符合活动规则。如存在,可能是您填写的店号或小票号有误。';
    // 系统自动审核核销理由
    const AUTOCHECK_ALREADY_COMPLETED_REASON = '重复晒单';

    /* @var  User 用户公用类 */
    protected $_user;

    /* @var  LikeModel 点赞数据model */
    protected $_d_like;

    /* @var  ActivityModel 活动数据model */
    protected $_d_activity;

    /* @var  RightModel 权限model */
    protected $_d_right;

    /* @var  ReplyModel 回复表model */
    protected $_d_reply;

    /* @var  PacketrecordModel 红包记录表 */
    protected $_d_packetrecord;

    // 构造方法
    public function __construct()
    {

        $this->_user = new User();

        $this->_d = new CommentModel();

        $this->_d_like = new LikeModel();

        $this->_d_activity = new ActivityModel();

        $this->_d_reply = new ReplyModel();

        $this->_d_right = new RightModel();

        $this->_d_packetrecord = new PacketrecordModel();

        parent::__construct();
    }

    /**
     * 查询回复评论信息
     *
     * @param $params array 查条件参数
     * @param $page_option array 分页参数
     * @param $uid String 用户UID
     *
     * @return array|bool
     */
    public function replay_list($params, $page_option, $uid)
    {

        $comment_id = intval($params['comment_id']);

        if (!$comment_id) {
            // 判断评论ID是否存在
            E('_ERR_AC_REPLAY_EMPTY');
        }

        // 查询评论信息
        $comment_info = $this->get($comment_id);

        if (empty($comment_info)) {
            // 评论信息不存在
            E('_ERR_DATA_NOT_EXIST');
        }

        // 查询发布评论的用户信息
        $user_info = $this->_user->getByUid($comment_info['uid']);

        $info = [
            'uid' => strval($comment_info['uid']),
            'username' => strval($user_info['memUsername']),
            'avatar' => strval($user_info['memFace']),
            'content' => $this->enter($comment_info['content']),
            'created' => $comment_info['created'],
            'likes' => intval($comment_info['likes']),
            'replys' => intval($comment_info['replys']),
        ];

        // 判断活动是否可以点赞
        $mylikecount = $this->_d_like->count_by_conds([
            'uid' => $uid,
            'cid' => $comment_id,
            'type' => self::COMMENT_TYPE,
        ]);

        $is_like = $mylikecount ? self::IS_LIKE_OK : self::IS_LIKE_NO;

        $info['is_like'] = $is_like;

        // 获取评论点赞列表
        $info['like_list'] = $this->like_list($comment_id);

        // 获取评论附件列表
        $info['img_list'] = $this->img_list($comment_id);

        //查询回复总条数
        $conds = ['parent_id' => $comment_id];

        $total = $this->count_by_conds($conds);

        $list = [];
        if ($total > 0) {

            $order_option = ['created' => 'ASC'];

            //查询回复列表
            $list = $this->list_by_conds($conds, $page_option, $order_option);

            if ($list) {
                // 回复列表数据格式化
                $list = $this->format_comment_list($list, $uid);
            }
        }

        $info['total'] = intval($total);
        $info['list'] = $list;

        return $info;
    }

    /**
     * 获取评论图片列表
     *
     * @param $comment_id int 评论ID
     *
     * @return array
     */
    protected function img_list($comment_id)
    {

        // 排序
        $order_option = ['created' => 'DESC'];

        $list = $this->_d_attach->list_by_conds(
            ['cid' => $comment_id],
            null,
            $order_option
        );

        $images = [];
        foreach ($list as $v) {
            $images[] = [
                'atId' => $v['at_id'],
                'imgUrl' => imgUrlReal($v['at_id']),
            ];
        }

        return $images;
    }

    /**
     * 获取评论点赞列表
     *
     * @param  $comment_id int 评论ID
     *
     * @return array
     */
    public function like_list($comment_id = 0)
    {

        // 排序
        $order_option = ['created' => 'DESC'];

        $user = [];
        $like_list = $this->_d_like->list_by_conds(
            ['obj_id' => $comment_id, 'type' => self::COMMENT_TYPE],
            null,
            $order_option
        );

        $user_arr = [];
        if (!empty($like_list)) {

            $uids = array_column($like_list, 'uid');
            // 查询用户集合
            $users = $this->_user->listAll(['memUids' => $uids]);

            // 获取被删除的用户信息
            $this->user_list($users, $uids);

            $user_list = array_combine_by_key($users, 'memUid');

            // 格式化点赞列表数据
            foreach ($like_list as $val) {
                $value = [];

                $value['created'] = $val['created'];
                $value['like_id'] = $val['like_id'];
                $value['uid'] = $val['uid'];
                $value['username'] = $user_list[$val['uid']]['memUsername'];
                $value['avatar'] = $user_list[$val['uid']]['memFace'];

                $user_arr[] = $value;
            }

        }

        return $user_arr;
    }

    /**
     * 格式化回帖列表数据(手机端)
     *
     * @param array $comm_list 回帖数据列表
     * @param string $uid 用户ID
     *
     * @return array
     */
    public function format_comment_list($comm_list = [], $uid = '')
    {

        // 获取用户ID集合
        $uids = array_column($comm_list, 'uid');

        // 查询用户列表(包含删除人员)
        $users = $this->_user->listUsersAll(['memUids' => $uids, 'memAll' => 1]);

        // 获取回帖ID集合
        $comment_ids = array_column($comm_list, 'comment_id');

        // 查询该用户点赞这些回帖的记录
        $like_list = $this->_d_like->list_by_conds(
            [
                'type' => self::LIKE_COMMENT_TYPE,
                'uid' => $uid,
                'obj_id' => $comment_ids
            ]
        );

        $like_ids = array_column($like_list, 'obj_id');

        // 查询这些回帖关联的红包记录数
        $record_list = $this->_d_packetrecord->list_by_conds(['cid' => $comment_ids,], null, [], 'rand_money,cid,uid');

        foreach ($comm_list as &$v) {

            // 转换数据返回格式
            $v['comment_id'] = intval($v['comment_id']);
            $v['likes'] = intval($v['likes']);
            $v['replys'] = intval($v['replys']);
            $v['content'] = strip_tags($this->enter($v['content']));
            // 自定义字段返回
            $v['ext_fields'] = !empty($v['ext_fields']) ? unserialize($v['ext_fields']) : [];

            // 组装用户数据
            $user_info = $users[$v['uid']];

            $v['check_time'] = $this->get_time($v['check_time']);
            $v['username'] = strval($user_info['memUsername']);
            $v['avatar'] = User::instance()->avatar($v['uid'], $user_info);

            // 组装是否已点赞标识
            if (in_array($v['comment_id'], $like_ids)) {
                $v['is_like'] = self::IS_LIKE_OK;
            } else {
                $v['is_like'] = self::IS_LIKE_NO;
            }

            // 获取回帖图片信息
            $v['images'] = $this->get_image_list($v['pic_ids']);
            unset($v['pic_ids']);

            $red_count = 0; // 帖子关联红包领取成功金额
            $red_num = 0; // 帖子关联红包数
            foreach ($record_list as $itm) {

                if ($itm['cid'] == $v['comment_id']) {
                    $red_num++;
                    if (self::PACKET_STATUS_SUCCESS == $itm['packet_status']) {
                        // 如果领取成功
                        $red_count += $itm['rand_money'];
                    }
                }
            }

            $v['red_count'] = sprintf("%.2f", $red_count / 100);

            // 组装是否可删除标识(是自己发布且没有关联红包记录时可删除)
            if ($v['uid'] == $uid && $red_num == 0) {
                $v['is_del'] = self::IS_DEL_OK;
            } else {
                $v['is_del'] = self::IS_DEL_NO;
            }
        }

        unset($v);

        return $comm_list;
    }

    /**
     * 格式化评论列表数据(后台)
     *
     * @param $comm_list array 评论列表数据
     * @param array $datalist
     *
     * @return array
     */
    public function format_comment_list_admin($comm_list, $datalist = [])
    {

        // 返回数组
        $return_list = [];

        // 将用户列表转换为以uid为key的二维数组
        $users = $this->user_uid_key_array($comm_list);

        // 实例化
        $user = User::instance();

        // 如果未开启审核
        if ($datalist['is_check_open'] == ActivityService::IS_CHECK_CLOSE) {

            // 组装返回数据
            $return_list = $this->passcheck($comm_list, $datalist, $users);
        }

        // 如果开启审核
        if ($datalist['is_check_open'] == ActivityService::IS_CHECK_OPEN) {

            // 按照状态去筛选
            switch ($datalist['check_status']) {

                // 待审核
                case ActivityService::CHECK_ING:

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

                        $return_list[$key]['ac_id'] = (int)$v['ac_id'];
                        $return_list[$key]['comment_id'] = (int)$comm_list[$key]['comment_id'];
                        $return_list[$key]['name'] = (string)$users[$comm_list[$key]['uid']]['memUsername'];
                        $return_list[$key]['avatar'] = $users[$comm_list[$key]['uid']]['memFace'];
                        $return_list[$key]['content'] = $this->enter($comm_list[$key]['content']);
                        $return_list[$key]['created'] = (string)$comm_list[$key]['created'];

                        // 获取评论图片信息
                        $images = [];

                        // 获取图片
                        if (!empty($return_list[$key]['pic_ids'])) {

                            $images = $this->get_image_list($return_list[$key]['pic_ids']);
                        }

                        $return_list[$key]['image_list'] = $images;
                    }
                    break;

                // 审核通过
                case ActivityService::CHECK_PASS:

                    // 组装返回数据
                    $return_list = $this->passcheck($comm_list, $datalist, $users);

                    break;

                // 审核被驳回
                case ActivityService::CHECK_FAIL:

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

                        $return_list[$key]['ac_id'] = (int)$comm_list[$key]['ac_id'];
                        $return_list[$key]['comment_id'] = (int)$comm_list[$key]['comment_id'];
                        $return_list[$key]['name'] = (string)$users[$comm_list[$key]['uid']]['memUsername'];
                        $return_list[$key]['avatar'] = $users[$comm_list[$key]['uid']]['memFace'];
                        $return_list[$key]['content'] = $this->enter($comm_list[$key]['content']);
                        $return_list[$key]['created'] = (string)$comm_list[$key]['created'];
                        $return_list[$key]['check_time'] = (string)$comm_list[$key]['check_time'];
                        $return_list[$key]['check_uname'] = (string)$comm_list[$key]['check_uname'];
                        $return_list[$key]['check_phone'] = (string)$comm_list[$key]['check_phone'];
                        $check_avatar = $user->avatar($comm_list[$key]['check_uid']);
                        $return_list[$key]['check_avatar'] = $check_avatar;

                        // 获取评论图片信息
                        $images = [];

                        // 获取图片
                        if (!empty($comm_list[$key]['pic_ids'])) {

                            $images = $this->get_image_list($comm_list[$key]['pic_ids']);
                        }

                        $return_list[$key]['image_list'] = $images;
                    }
                    break;
            }
        }

        return $return_list;
    }

    /**
     * 前端删除我的回帖
     *
     * @param int $comment_id 回帖id
     * @param string $uid 前端登录用户ID
     *
     * @return bool
     */
    public function del_comment($comment_id = 0, $uid = '')
    {

        // 查询回帖数据
        $data = $this->_d->get($comment_id);

        if (empty($data)) {
            // 回帖数据不存在
            E('_ERR_DATA_NOT_EXIST');
        }

        if ($uid != $data['uid']) {
            // 如果不是自己发布的,则不能删除
            E('_ERR_COMMENT_FORYOU');
        }

        // 查询活动详情
        $activity = $this->_d_activity->get_by_conds([
            'ac_id' => $data['ac_id'],
            'is_hide' => self::CLOSE_ACTIVITY_HIDE,
            'activity_status > ?' => ActivityService::ACTIVITY_DRAFT
        ]);

        // 活动不存在
        if (empty($activity)) {

            E('_ERR_DATA_NOT_EXIST');
        }

        // 获取这个帖子是否存在领取红包记录
        $record_total = $this->_d_packetrecord->count_by_conds(['cid' => $comment_id]);

        if (!empty($record_total)) {

            E('_ERR_ACTIVITY_HAVE_RED_NOT_DEL');
        }

        // 活动已终止不能删除
        if (ActivityService::ACTIVITY_STOP == $activity['activity_status']) {

            E('_ERR_ACTIVITY_STATUS_STOP_NOT_DEL');
        }

        // 活动已结束不能删除
        if (ActivityService::ACTIVITY_PUBLISH == $activity['activity_status'] && $activity['end_time'] <= MILLI_TIME) {

            E('_ERR_ACTIVITY_STATUS_END_NOT_DEL');
        }

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

            // 删除回帖
            $del_num = $this->_d->delete($comment_id);

            if ($del_num) {
                // 删除回帖点赞
                $this->_d_like->delete_by_conds(['obj_id' => $comment_id, 'type' => self::LIKE_COMMENT_TYPE]);

                // 查询该回帖下的所有回复
                $reply_list = $this->_d_reply->list_by_conds(['comment_id' => $comment_id]);

                // 获取该回帖回复的ID集合
                $reply_ids = array_column($reply_list, 'reply_id');

                if (!empty($reply_ids)) {
                    // 删除回复
                    $this->_d_reply->delete_by_conds(['reply_id' => $reply_ids]);

                    // 删除回复点赞
                    $this->_d_like->delete_by_conds([
                        'obj_id' => $reply_ids,
                        'type' => self::LIKE_REPLY_TYPE
                    ]);
                }

                if (self::CHECK_OK == $data['check_status']) {
                    // 如果删除的是审核通过的回帖,此处给该活动的回帖数减1
                    $this->_d_activity->update($data['ac_id'], ['comments = comments-?' => 1]);
                }
            }

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

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

        // 删除UC附件服务器文件
        if (!empty($data['pic_ids'])) {

            $attach_serv = new AttachOperation();
            $attach_serv->delete_attach(
                APP_DIR,
                'comment',
                [$comment_id]
            );
        }

        return true;
    }

    /**
     * 发布回帖
     *
     * @param array $data 请求参数
     * @param array $activity 活动详情信息
     * @param array $user 登录用户的信息
     *
     * @return mixed
     */
    public function publish_comment($data = [], $activity = [], $user = [])
    {

        // 活动id
        $ac_id = rintval($data['ac_id']);
        // 回帖内容
        $content = trim($data['content']);

        // 回帖内容至少2个字符,最多1000个字段
        if (!\Com\Validator::is_len_in_range($content, 2, 1000, 'utf-8')) {

            E('_ERR_AC_COMMENT_LENGTH');
        }

        // 图片列表(选填,最多9张图片)
        $pic_list = $data['images'];
        // 图片附件id集合
        $images = array_column($pic_list, 'atId');
        // 图片数量
        $pic_total = count($images);

        // 最多上传9张图片
        if ($pic_total > self::IMAGES_MAX_COUNT) {

            E('_ERR_AC_IMAGE_LENGTH');
        }

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

            // 组装插入数据
            $insert_data = [
                'ac_id' => $ac_id,
                'content' => $content,
                'pic_ids' => empty($images) ? '' : implode(',', $images),
                'uid' => $user['memUid'],
                'ext_fields' => !empty($data['ext_fields']) ? serialize($data['ext_fields']) : '',
                'is_autochecked' => self::IS_AUTOCHECKED_NO, // 未被自动审核
            ];

            // 开启审核
            if (self::OPEN_CHECK == $activity['is_check_open']) {
                // 待审核
                $insert_data['check_status'] = self::CHECK_ING;
            } else {
                // 审核通过
                $insert_data['check_status'] = self::CHECK_OK;
                // 审核时间
                $insert_data['check_time'] = MILLI_TIME;
            }

            // 新增回帖信息
            $comment_id = $this->_d->insert($insert_data);

            // 回帖成功 并且 未开启审核(审核通过)
            if ($comment_id && self::CLOSE_CHECK == $activity['is_check_open']) {

                $conditions = [
                    'check_status' => self::CHECK_OK,
                    'uid' => $user['memUid'],
                    'ac_id' => $ac_id
                ];
                // 查询该用户在此活动的已通过的参与记录(含已删除)
                $count = $this->_d->count_by_conds_contain_del($conditions);

                // 第一次回帖(只有一条记录[含已删除])
                if (Helper::COMMENT_COUNT_ONE == $count) {
                    // 更新活动表已参与人数与活动回帖数
                    $this->_d_activity->update($ac_id, ['join_num=join_num+(?)' => 1, 'comments=comments+(?)' => 1]);
                } else {
                    // 更新活动回帖数
                    $this->_d_activity->update($ac_id, ['comments=comments+(?)' => 1]);
                }
            }

            // 提交修改
            $this->_d->commit();
        } catch (\Think\Exception $e) {
            \Think\Log::record($e);

            $this->_d->rollback();
            unset($comment_id);

            return false;
        } catch (\Exception $e) {
            \Think\Log::record($e);
            $this->_d->rollback();
            unset($comment_id);

            return false;
        }

        // 回帖成功
        if ($comment_id) {
            // 开启审核
            if (self::OPEN_CHECK == $activity['is_check_open']) {
                // 消息参数
                $params = [
                    'comment_id' => $comment_id,
                    'ac_id' => $ac_id,
                    'subject' => $activity['subject'],
                    'created' => MILLI_TIME,
                    'content' => $content,
                    'uids' => $user['memUid'] // 发帖人uid
                ];
                // 给发帖人发消息【您已成功发帖,请耐心等待审核】
                $this->send_msg($params, self::MSG_CHECK_COMMENT_REMAIND_FOR_USER);

                // 审核人列表
                $check_user_list = unserialize($activity['check_user_list']);
                // 审核人id集合
                $check_mem_ids = array_column($check_user_list, 'memID');

                // 消息参数(审核人)
                $params['uids'] = $check_mem_ids;
                $params['username'] = $user['memUsername'];
                // 给审核人发送消息【您负责的活动有新的帖子需要审核】
                $this->send_msg($params, self::MSG_CHECK_COMMENT_REMAIND_FOR_ADMIN);
            } else { // 关闭审核
                // 初始化红包记录id
                $rid = 0;

                // 活动开启红包
                if (self::OPEN_RED == $activity['is_red_open']) {
                    // 组织分配红包请求参数
                    $red_params = [
                        'ac_id' => $ac_id,
                        'p_type' => self::RED_PACKET_RAND,
                        'uid' => $user['memUid'],
                        'comment_id' => $comment_id,
                        'username' => $user['memUsername']
                    ];
                    // 给该用户分配红包
                    $red_data = $this->get_red_record($red_params);
                    // 红包记录id
                    $rid = !empty($red_data['rid']) ? rintval($red_data['rid']) : $rid;
                }

                // 发红包,给发帖人推送消息
                if ($rid) {
                    // 消息参数
                    $params = [
                        'comment_id' => $comment_id,
                        'rid' => $red_data['rid'],
                        'ac_id' => $red_data['ac_id'],
                        'subject' => $activity['subject'],
                        'give_time' => $red_data['give_time'],
                        'uids' => $user['memUid'] // 接收消息人员集合
                    ];
                    // 给发帖人发消息【恭喜您,获得一个随机红包,请点击领取】
                    $this->send_msg($params, self::MSG_GET_RANDOM_REDPACKET);
                }

                // 开启发帖推送消息提醒
                if (Helper::SALE_ALL_LOOK == $activity['is_post_remind']) {
                    // 重新组装审核内容:回帖内容组装进数组
                    $activity['content'] = $content;
                    // 给有权限查看活动的人员推送消息 参数说明:(活动详情,回帖人名,回帖人uid)
                    $this->send_look_msg($activity, $user['memUsername'], $user['memUid']);
                }
            }
        }

        // 附件操作
        if (!empty($images)) {
            $attach_serv = new AttachOperation();
            $attach_serv->insert_attach(
                APP_DIR,
                'comment',
                $comment_id,
                ['attach_ids' => $images]
            );
        }

        return $comment_id;
    }

    /**
     * 给有权限查看的人员发送消息
     *
     * @param array $activity 活动详情+回帖内容
     * @param string $user_name 回帖用户名
     * @param string $uid 回帖用户UID
     */
    public function send_look_msg($activity = [], $user_name = '', $uid = '')
    {

        // 拼装需要发送消息的内容
        $send_params = [
            'ac_id' => $activity['ac_id'],
            'subject' => $activity['subject'],
            'begin_time' => $activity['begin_time'],
            'end_time' => $activity['end_time'],
            'content' => $activity['content'], // 回帖内容
            'username' => $user_name
        ];

        // 是全公司或者可围观
        if (ActivityService::IS_ALL == $activity['is_all'] || Helper::SALE_ALL_LOOK == $activity['is_all_look']) {
            // 获取有权限接收消息人员
            $send_params['uids'] = Helper::get_send_msg_user(ActivityService::IS_ALL, [], [], [], [], [$uid]);
        } else {
            // 获取权限范围内的人员
            $right = $this->_d_right->list_by_conds(['ac_id' => $activity['ac_id']]);

            // 用户id集合
            $uids = array_unique(array_filter(array_column($right, 'uid')));
            // 部门id集合
            $dp_ids = array_unique(array_filter(array_column($right, 'dp_id')));
            // 岗位id集合
            $job_ids = array_unique(array_filter(array_column($right, 'job_id')));
            // 角色id集合
            $role_ids = array_unique(array_filter(array_column($right, 'role_id')));

            // 获取有权限获取消息人员
            $send_params['uids'] = Helper::get_send_msg_user(0, $uids, $dp_ids, $job_ids, $role_ids, [$uid]);
        }

        // 给有权限查看的人员发消息
        $this->send_msg($send_params, self::MSG_ACTIVITY_LOOK_COMMENT);
    }

    /**
     * 格式化回帖详情【手机端】
     *
     * @param array $info 回帖详情数据
     * @param String $uid 用户uid
     *
     * @return array
     */
    public function format_detail_data($info = [], $uid = '')
    {

        // 获取发帖人信息
        $user = User::instance();
        $user_info = $user->getByUid($info['uid']);

        // 组装基本信息
        $result = [
            'comment_id' => intval($info['comment_id']),
            'uid' => $info['uid'],
            'username' => $user_info['memUsername'],
            'avatar' => $user->avatar($info['uid'], $user_info),
            'content' => $this->enter($info['content']),
            'check_time' => empty($info['check_time']) ? '' : $this->get_time($info['check_time']),
            'ext_fields' => !empty($info['ext_fields']) ? unserialize($info['ext_fields']) : []
        ];

        $img_list = [];
        if (!empty($info['pic_ids'])) {
            // 格式化图片数据
            $img_list = $this->get_image_list($info['pic_ids']);
        }
        $result['img_list'] = $img_list;

        // 点赞数
        $result['likes'] = intval($info['likes']);

        // 回复数
        $result['replys'] = intval($info['replys']);

        // 获取这个帖子已分配红包记录数
        $record_total = $this->_d_packetrecord->count_by_conds(['cid' => $info['comment_id']]);

        // 如果是自己发的帖子且没有领取过红包
        if ($info['uid'] == $uid && empty($record_total)) {
            // 可以删除
            $result['is_del'] = self::IS_DEL_OK;
        } else {
            // 不可删除
            $result['is_del'] = self::IS_DEL_NO;
        }

        // 查询当前用户是否点赞
        $mylikecount = $this->_d_like->count_by_conds(
            [
                'uid' => $uid,
                'obj_id' => $info['comment_id'],
                'type' => self::LIKE_COMMENT_TYPE,
            ]
        );

        // 是否已点赞
        $result['is_like'] = $mylikecount > 0 ? self::IS_LIKE_OK : self::IS_LIKE_NO;

        // 初始化当前帖子已领取成功的红包金额
        $red_count = 0;
        if ($record_total > 0) {

            // 查询这条分享用户领取红包成功的记录
            $record_list = $this->_d_packetrecord->list_by_conds(
                [
                    'packet_status' => self::PACKET_STATUS_SUCCESS,
                    'cid' => $info['comment_id']
                ], null, [], 'rand_money,cid,uid'
            );

            // 取出金额集合
            $rand_moneys = array_column($record_list, 'rand_money');
            // 求和
            $red_count = array_sum($rand_moneys);
        }

        // 格式化红包总金额
        $result['red_count'] = sprintf("%.2f", $red_count / 100);

        if ($result['likes'] > 0) {

            $like_conds = [
                'obj_id' => $info['comment_id'],
                'type' => self::LIKE_COMMENT_TYPE
            ];

            // 查询点赞列表(最新的10条)
            $like_list = $this->_d_like->list_by_conds($like_conds, [0, 10], ['like_id' => 'DESC'],
                'like_id,uid,created');

            // 获取点赞用户id集合
            $uids = array_column($like_list, 'uid');

            // 获取用户信息(包含删除人员)
            $user_list = $user->listUsersAll(['memUids' => $uids, 'memAll' => 1]);

            $like_info = [];
            foreach ($like_list as $_v) {

                $user_info = $user_list[$_v['uid']];

                // 组装点赞列表数据
                $like_info[] = [
                    'like_id' => intval($_v['like_id']),
                    'username' => $user->avatar($_v['uid'], $user_info),
                    'avatar' => $user_info['memFace'],
                    'created' => $_v['created'],
                ];
            }

            // 给点赞列表赋值
            $result['like_list'] = $like_info;
        } else {
            $result['like_list'] = [];
        }

        return $result;
    }

    /**
     * 格式化回帖审核详情【手机端】
     *
     * @param array $info 回帖详情数据
     * @param String $uid 用户uid
     * @param array $activity_info 活动详情
     *
     * @return array
     */
    public function format_check_detail_data($info = [], $uid = '', $activity_info = [])
    {

        // 获取用户信息
        $user = User::instance();
        $user_info = $user->getByUid($info['uid']);

        // 组装基本信息
        $result = [
            'comment_id' => rintval($info['comment_id']),
            'uid' => $info['uid'],
            'username' => $user_info['memUsername'],
            'avatar' => $user->avatar($info['uid'], $user_info),
            'content' => $this->enter($info['content']),
            'created' => $this->get_time($info['created']),
            'check_status' => rintval($info['check_status']),
            'reason' => $this->enter($info['reason']),
            'check_uname' => strval($info['check_uname']),
            'check_time' => empty($info['check_time']) ? '' : $this->get_time($info['check_time']),
            'ext_fields' => !empty($info['ext_fields']) ? unserialize($info['ext_fields']) : []
        ];

        // 格式化图片数据
        $img_list = [];
        if (!empty($info['pic_ids'])) {
            // 格式化图片
            $img_list = $this->get_image_list($info['pic_ids']);
        }

        $result['img_list'] = $img_list;
        // 判断当前用户是否是发帖人
        if ($info['uid'] == $uid) {
            // 可以删除
            $result['is_del'] = self::IS_DEL_OK;
        } else {
            // 不可删除
            $result['is_del'] = self::IS_DEL_NO;
        }

        // 初始化审核权限
        $result['is_auth'] = Helper::SALE_NOT_CHECK_AUTH;
        // 如果开启审核
        if ($activity_info['is_check_open'] == ActivityService::IS_CHECK_OPEN) {
            // 审核人员列表
            $check_user_list = unserialize($activity_info['check_user_list']);
            $mem_uids = array_column($check_user_list, 'memID');
            // 如果当前用户在审核人列表中
            if (in_array($uid, $mem_uids)) {
                $result['is_auth'] = Helper::SALE_CHECK_AUTH;
            }
        }

        return $result;
    }

    /**
     * 管理后台活动已参与列表
     *
     * @param $params
     *
     * @return mixed
     */
    public function list_join($params)
    {

        // 默认值
        $page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;

        // 分页
        list($start, $limit) = page_limit($page, $limit);

        // 按照发布时间排序
        $order_option = ['created' => 'DESC'];

        // 查询条件:已审核
        $conds = [
            'check_status' => ActivityService::CHECK_PASS,
            'ac_id' => $params['ac_id']
        ];

        $join_list = $this->_d->list_join($conds, [$start, $limit], $order_option);
        $uids = array_column($join_list, 'uid');

        $user_s = new User();
        $user_list = $user_s->listByUid($uids);

        foreach ($join_list as $join_k => $join_v) {
            // 获取用户信息
            $user_info = $user_list[$join_v['uid']];
            // 获取被删除的用户信息
            if (empty($user_info)) {

                $user_info = $user_s->getByUid($join_v['uid']);
            }

            $join_list[$join_k]['memId'] = $join_v['uid'];
            $join_list[$join_k]['memUsername'] = $user_info['memUsername'];
            $join_list[$join_k]['dpName'] = $user_info['dpName'][0]['dpName'];
            $join_list[$join_k]['roleName'] = $user_info['memRole'];
            $join_list[$join_k]['jobName'] = $user_info['memJob'];
            $join_list[$join_k]['telephone'] = $user_info['memMobile'];
            $join_list[$join_k]['last_join_time'] = $join_v['created'];
        }

        return $join_list;
    }

    /**
     * 获取活动已参与人数
     *
     * @param array $params 过滤条件
     *
     * @return array
     */
    public function count_join($params)
    {

        $conds = [
            'check_status' => ActivityService::CHECK_PASS,
            'ac_id' => $params['ac_id']
        ];

        return $this->_d->count_join($conds);
    }

    /**
     * 获取活动未参与列表
     *
     * @param array $params 过滤条件
     * @param array $unjoin_uids 未参与的uid集合数组
     *
     * @return array
     */
    public function list_unjoin($params, $unjoin_uids)
    {

        // 数组分页
        $page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
        // 分页
        $uids = $this->get_page_data($unjoin_uids, $page, $limit);

        // 查询用户信息
        $user_s = new User();
        $unjoin_user_list = $user_s->listByUid($uids);

        $unjoin_list = [];
        foreach ($unjoin_user_list as $user_v) {

            $unjoin_list[] = [
                'memId' => $user_v['memUid'],
                'memUsername' => $user_v['memUsername'],
                'dpName' => $user_v['dpName'][0]['dpName'],
                'roleName' => $user_v['memRole'],
                'jobName' => $user_v['memJob'],
                'telephone' => $user_v['memMobile'],
            ];
        }

        return $unjoin_list;
    }

    /**
     * 根据条件计算数量【能查询包含删除的数据】
     *
     * @param $conds
     * @param string $fields
     *
     * @return number
     */
    public function count_by_conds_contain_del($conds, $fields = '*')
    {

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

    /**
     * 审核通过与未开启审核共用方法
     *
     * @param $comm_list 待处理的数组
     * @param $datalist 条件数组
     * @param $users 用户与UID数组
     *
     * @return array
     */
    public function passcheck($comm_list, $datalist, $users)
    {

        // 返回数组
        $return_list = [];

        // 初始化图片数组
        $images = [];

        // 初始化用户回复数组
        $reply_list = [];

        // 初始化红包数组
        $red_list = [];

        $user = User::instance();

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

            $return_list[$key]['ac_id'] = (int)$comm_list[$key]['ac_id'];
            $return_list[$key]['comment_id'] = (int)$comm_list[$key]['comment_id'];
            $return_list[$key]['replys'] = (int)$comm_list[$key]['replys'];
            $return_list[$key]['name'] = (string)$users[$comm_list[$key]['uid']]['memUsername'];
            $return_list[$key]['avatar'] = $users[$comm_list[$key]['uid']]['memFace'];
            $return_list[$key]['content'] = $this->enter($comm_list[$key]['content']);
            $return_list[$key]['created'] = (string)$comm_list[$key]['created'];
            $return_list[$key]['check_time'] = (string)$comm_list[$key]['check_time'];
            $return_list[$key]['check_uname'] = (string)$comm_list[$key]['check_uname'];
            $return_list[$key]['check_phone'] = (string)$comm_list[$key]['check_phone'];
            $check_avatar = $user->avatar($comm_list[$key]['check_uid']);
            $return_list[$key]['check_avatar'] = isset($check_avatar) ? $check_avatar : "";
            $return_list[$key]['likes_num'] = (int)$comm_list[$key]['likes'];
            $return_list[$key]['reply_total'] = (int)$comm_list[$key]['replys'];
            $return_list[$key]['reply_limit'] = self::REPLY_LIMIT;
            $return_list[$key]['reply_page'] = self::REPLY_FIRST_PAGE;

            // 获取图片
            if (!empty($v['pic_ids'])) {

                $images = $this->get_image_list($v['pic_ids']);
            }

            $return_list[$key]['image_list'] = $images;

            // 获取第一页一级评论的回复列表
            if ($comm_list[$key]['replys'] > 0) {

                $comm_list[$key]['reply_list'] = $this->_d_reply->list_by_conds(
                    [
                        'ac_id' => $comm_list[$key]['ac_id'],
                        'comment_id' => $comm_list[$key]['comment_id'],
                    ],
                    [
                        self::REPLY_FIRST_PAGE,
                        self::REPLY_LIMIT,
                    ],
                    [
                        'created' => 'DESC',
                    ],
                    'reply_id,comment_id,uid,content,created,content,likes,pic_ids'
                );
            }

            // 获取回复用户信息及图片信息
            if ($comm_list[$key]['reply_list']) {

                foreach ($comm_list[$key]['reply_list'] as $replykey => $reply) {

                    $reply_list[$replykey]['likes_num'] = (int)$comm_list[$key]['reply_list'][$replykey]['likes'];
                    $replyUser = $user->getByUid($comm_list[$key]['reply_list'][$replykey]['uid']);
                    $reply_list[$replykey]['name'] = (string)$replyUser['memUsername'];
                    $reply_list[$replykey]['avatar'] = $replyUser['memFace'];
                    $reply_list[$replykey]['content'] = $this->enter($comm_list[$key]['reply_list'][$replykey]['content']);
                    $reply_list[$replykey]['time'] = (string)($comm_list[$key]['reply_list'][$replykey]['created']);

                    // 获取评论图片信息
                    $reply_images = [];

                    // 获取图片
                    if (!empty($comm_list[$key]['reply_list'][$replykey]['pic_ids'])) {

                        $reply_images = $this->get_image_list($comm_list[$key]['reply_list'][$replykey]['pic_ids']);
                    }
                    $reply_list[$replykey]['image_list'] = $reply_images;
                }
            }

            $return_list[$key]['reply_list'] = $reply_list;

            if ($datalist['red_status'] == ActivityService::RED_OPENED) {

                $total = $this->_d_packetrecord->count_by_conds(['cid' => $comm_list[$key]['comment_id']]);
                if ($total > 0) {

                    $red = $this->_d_packetrecord->list_by_conds(['cid' => $comm_list[$key]['comment_id']]);

                    foreach ($red as $redkey => $value) {

                        $red_list[$redkey]['red_id'] = intval($red[$redkey]['pid']);
                        $red_list[$redkey]['red_type'] = intval($red[$redkey]['p_type']);
                        $red_list[$redkey]['packet_status'] = intval($red[$redkey]['packet_status']);
                        $red_list[$redkey]['red_money'] = number_format(($red[$redkey]['rand_money'] / 100), 2, ".",
                            "");
                        $red_list[$redkey]['send_uname'] = raddslashes($red[$redkey]['send_uname']);
                        $red_list[$redkey]['give_time'] = raddslashes($red[$redkey]['give_time']);
                    }
                }
            }
            $return_list[$key]['red_list'] = $red_list;
        }

        return $return_list;
    }

    /**
     * 将用户列表转换为以uid为key的二维数组
     *
     * @param $comm_list 待处理数组
     *
     * @return array 处理后数组
     */
    public function user_uid_key_array($comm_list)
    {

        // 获取用户ID集合
        $uids = array_column($comm_list, 'uid');

        // 查询用户列表
        $user_list = $this->_user->listAll(['memUids' => $uids]);

        // 获取被删除的用户信息
        $this->user_list($user_list, $uids);

        // 将用户列表转换为以uid为key的二维数组
        $users = array_combine_by_key($user_list, 'memUid');

        return $users;
    }
}