Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

李洋 / 消息系统

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • CMS-Message
  • app
  • Http
  • Controllers
  • MessageApiController.php
  • Joneq's avatar
    系统公告开发-基本页面完成 · 81946166
    Joneq committed 2 years ago
    81946166
MessageApiController.php 31 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
<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/12/7
 * Time: 13:04
 */

namespace App\Http\Controllers;

use App\Logic\SystemNoticeLogic;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use RedisDB;
use Config;
use \Exception;
use Excel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Session;


class MessageApiController extends Controller
{
    public function Entrance(Request $request, $id ){
        //统一入口
        $this->$id($request, $id);
    }

    private function Export($errcode=0,$errmsg='成功',$data=''){
        echo json_encode(['errcode'=>$errcode,'errmsg'=>$errmsg,'data'=>$data]);
        exit();
    }


    //获取系统更新信息列表
    private function systemUpdateList($request)
    {
        $data = $request->all();
        try{
            $returnData = (new SystemNoticeLogic())->getSystemUpdateList($data);
            echo json_encode(['code'=>0,'count'=>count($returnData),'data'=>$returnData,'msg'=>'获取成功']);
            exit();
        }catch(\Exception $e){
            echo json_encode(['code'=>400,'msg'=>$e->getMessage()]);
            exit();
        }
    }

    //获取系统宣讲列表
    private function systemPreachList($request)
    {
        $data = $request->all();
        try{
            $returnData = (new SystemNoticeLogic())->getSystemPreachList($data);
            echo json_encode(['code'=>0,'count'=>count($returnData),'data'=>$returnData,'msg'=>'获取成功']);
            exit();
        }catch(\Exception $e){
            echo json_encode(['code'=>400,'msg'=>$e->getMessage()]);
            exit();
        }
    }

    //获取系统公告列表
    private function systemBulletinList($request)
    {
        $data = $request->all();
        try{
            $returnData = (new SystemNoticeLogic())->getSystemBulletinList($data);
            echo json_encode(['code'=>0,'count'=>count($returnData),'data'=>$returnData,'msg'=>'获取成功']);
            exit();
        }catch(\Exception $e){
            echo json_encode(['code'=>400,'msg'=>$e->getMessage()]);
            exit();
        }
    }

    //保存系统更新信息
    private function saveSystemUpdate($request)
    {
        $data = $request->all();
        try{
            (new SystemNoticeLogic())->saveSystemUpdate($data);
            $this->Export(0, '操作成功');
        }catch(\Exception $e){
            $this->Export(400, $e->getMessage());
        }
    }

    //保存系统宣讲信息
    private function saveSystemPreach($request)
    {
        $data = $request->all();
        try{
            (new SystemNoticeLogic())->saveSystemPreach($data);
            $this->Export(0, '操作成功');
        }catch(\Exception $e){
            $this->Export(400, $e->getMessage());
        }
    }

    //保存系统公告信息
    private function saveSystemBulletin($request)
    {
        $data = $request->all();
        try{
            (new SystemNoticeLogic())->saveSystemBulletin($data);
            $this->Export(0, '操作成功');
        }catch(\Exception $e){
            $this->Export(400, $e->getMessage());
        }
    }

    //发送公告信息
    private function sendNotice($request)
    {
        $data = $request->all();
        try{
            (new SystemNoticeLogic())->sendNotice($data);
            $this->Export(0, '操作成功');
        }catch(\Exception $e){
            $this->Export(400, $e->getMessage());
        }
    }


    //删除消息模板
    public function deletetemplate($request){
        $data = $request->input();
        try{
            $tid = isset($data['id']) && $data['id'] ? intval($data['id']) : 0 ;
            $cnt = DB::connection('message');
            $bk = $cnt->table('lie_msg_tpl')->where("tpl_id",$tid)->update(['status'=>-1]);
            if($bk){
                $this->Export(0, '');
            }else{
                throw new \Exception('操作失败');
            }
        }catch(\Exception $e){
            $this->Export(1000, '操作失败');
        }

    }

    //新增/编辑 消息模板
    private function addTemplate($request)
    {
        $data = $request->input();
        $insert_tpl_id = $data['tpl_id'];

        //至少选择一个渠道
        if (empty($data['channels'])) {
            $this->Export(11001, '请勾选至少一个消息渠道');
        }
        if (empty($data['description'])) {
            $this->Export(11002, '请输入消息key');
        }
//        if (empty($data['tpl_key'])) {
//            $this->Export(11003, '请输入模板描述');
//        }
//        if (empty($data['cls_id'])){
//            $this->Export(11024, '请输入模板分类');
//        }
        if (empty($data['sub_cls_id'])){
            $this->Export(11025, '请输入模板二级分类');
        }

        $is_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->first();
        if(empty($insert_tpl_id))
        {
            if($is_dumplicated !== NULL)
            {
                $this->Export(11022, '已存在相同模板key,请更换key内容');
            }
        }else
        {
            $is_edit_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->whereNotIn('tpl_id',[$insert_tpl_id])->first();
            if($is_edit_dumplicated !== NULL)
            {
                $this->Export(11023, '已存在相同模板key,请更换key内容');
            }
        }


        //添加事务 保证 数据库多条修改时 原子操作
        DB::connection('message')->transaction(function() use($data) {
            //消息模板模型
            $tpl_model = [];
            $tpl_model['tpl_id'] = $data['tpl_id'];
            $tpl_model['channels'] = $data['channels'];
            $tpl_model['description'] = $data['description'];
            $tpl_model['ex_str'] = $data['tpl_key'];
            $tpl_model['source_type'] = $data['source_type'];
            $tpl_model['creater'] = $data['creater'];
            $tpl_model['obj_user'] = $data['obj_user'];
            $tpl_model['msg_type'] = $data['msg_type'];
            $tpl_model['cls_id'] = $data['sub_cls_id'];

            if (empty($tpl_model['tpl_id'])) {
                $tpl_model['create_time'] = time();
                $tpl_model['update_time'] = time();
                $insert_tpl_id = DB::connection('message')->table('lie_msg_tpl')->insertGetId($tpl_model);
//                var_dump($insert_tpl_id);die;
                if (!$insert_tpl_id) {
                    $this->Export(11020, '新增消息模板失败');
                }
            } else {
                $tpl_model['update_time'] = time();
                $result = DB::connection('message')->table('lie_msg_tpl')->where('tpl_id', $tpl_model['tpl_id'])->update($tpl_model);
                $insert_tpl_id = $tpl_model['tpl_id'];
                if (!$result) {
                    $this->Export(11021, '编辑消息模板失败');
                }
            }

            //每一个渠道模板 的验证与写数据库
            //选了站内信渠道 就要选站内信类型
            if (strpos($data['channels'], '1') !== false) {
                if (empty($data['msg_type'])) {
                    $this->Export(11003, '请选择站内信类型');
                }
                if (empty($data['inner_title'])) {
                    $this->Export(11004, '请填写站内信标题');
                }
                if (empty($data['inner_content'])) {
                    $this->Export(11005, '请填写站内信内容');
                }
                if (empty($data['op_type'])) {
                    $this->Export(11006, '请选择点击消息操作');
                }
                if (empty($data['url']) && $data['op_type'] == 2) {
                    $this->Export(11007, '请填写要打开的url');
                }
                $chn_tpl_model_inner = [];
                $chn_tpl_model_inner['channel_tpl_id'] = $data['inner_channel_tpl_id'];
                $chn_tpl_model_inner['channel_type'] = $data['inner_channel_type'];
                $chn_tpl_model_inner['title'] = $data['inner_title'];
                $chn_tpl_model_inner['content'] = $data['inner_content'];
                $chn_tpl_model_inner['op_type'] = $data['op_type'];
                $chn_tpl_model_inner['tpl_id'] = $insert_tpl_id;
                if ($data['op_type'] == 2) {
                    $chn_tpl_model_inner['url'] = $data['url'];
                }
                if (empty($chn_tpl_model_inner['channel_tpl_id'])) {
                    $chn_tpl_model_inner['create_time'] = time();
                    $chn_tpl_model_inner['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_inner);
                    if (!$result) {
                        $this->Export(11008, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_inner['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_inner['channel_tpl_id'])->update($chn_tpl_model_inner);
                    if (!$result) {
                        $this->Export(11009, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['inner_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['inner_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11009, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '2') !== false) {
                if (empty($data['sms_content'])) {
                    $this->Export(11010, '请填写短信内容');
                }
                $chn_tpl_model_sms = [];
                $chn_tpl_model_sms['channel_tpl_id'] = $data['sms_channel_tpl_id'];
                $chn_tpl_model_sms['channel_type'] = $data['sms_channel_type'];

                $chn_tpl_model_sms['content'] = $data['sms_content'];
                $chn_tpl_model_sms['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_sms['channel_tpl_id'])) {
                    $chn_tpl_model_sms['create_time'] = time();
                    $chn_tpl_model_sms['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_sms);
                    if (!$result) {
                        $this->Export(11011, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_sms['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_sms['channel_tpl_id'])->update($chn_tpl_model_sms);
                    if (!$result) {
                        $this->Export(11012, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['sms_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['sms_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11012, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '3') !== false) {
                if (empty($data['email_title'])) {
                    $this->Export(11013, '请填写邮件标题');
                }
                if (empty($data['email_content'])) {
                    $this->Export(11014, '请填写邮件内容');
                }
                $chn_tpl_model_email = [];
                $chn_tpl_model_email['channel_tpl_id'] = $data['email_channel_tpl_id'];
                $chn_tpl_model_email['channel_type'] = $data['email_channel_type'];
                $chn_tpl_model_email['title'] = $data['email_title'];
                $chn_tpl_model_email['content'] = $data['email_content'];
                $chn_tpl_model_email['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_email['channel_tpl_id'])) {
                    $chn_tpl_model_email['create_time'] = time();
                    $chn_tpl_model_email['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_email);
                    if (!$result) {
                        $this->Export(11015, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_email['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_email['channel_tpl_id'])->update($chn_tpl_model_email);
                    if (!$result) {
                        $this->Export(11016, '编辑消息模板失败', $result);
                    }
                }
            } else {
                if (!empty($data['email_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['email_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11016, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '4') !== false) {
                if (empty($data['wechat_notify_title'])) {
                    $this->Export(11017, '请填写微信模板编号');
                }
                $chn_tpl_model_wechatnotify = [];
                $chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id'];
                $chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type'];
                $chn_tpl_model_wechatnotify['title'] = $data['wechat_notify_title'];
                $chn_tpl_model_wechatnotify['tpl_id'] = $insert_tpl_id;
                $chn_tpl_model_wechatnotify['url'] = $data['wechat_notify_url'];
                $wechat_head = $data['wechat_notify_head'];
                $wechat_tail = $data['wechat_notify_tail'];
                $wechat_content = json_encode(array('wechat_tpl_head' => $wechat_head, 'wechat_tpl_tail' => $wechat_tail));
                $chn_tpl_model_wechatnotify['content'] = $wechat_content;
                if (empty($chn_tpl_model_wechatnotify['channel_tpl_id'])) {
                    $chn_tpl_model_wechatnotify['create_time'] = time();
                    $chn_tpl_model_wechatnotify['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_wechatnotify);
                    if (!$result) {
                        $this->Export(11018, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_wechatnotify['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_wechatnotify['channel_tpl_id'])->update($chn_tpl_model_wechatnotify);
                    if (!$result) {
                        $this->Export(11019, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['wechat_notify_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['wechat_notify_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11019, '编辑消息模板失败');
                    }
                }
            }
            if (strpos($data['channels'], '5') !== false) {
                if (empty($data['ding_content'])) {
                    $this->Export(11020, '请填写钉钉消息内容');
                }
                $chn_tpl_model_ding = [];
                $chn_tpl_model_ding['channel_tpl_id'] = $data['ding_channel_tpl_id'];
                $chn_tpl_model_ding['channel_type'] = $data['ding_channel_type'];

                $chn_tpl_model_ding['content'] = $data['ding_content'];
                $chn_tpl_model_ding['tpl_id'] = $insert_tpl_id;
                if (empty($chn_tpl_model_ding['channel_tpl_id'])) {
                    $chn_tpl_model_ding['create_time'] = time();
                    $chn_tpl_model_ding['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_ding);
                    if (!$result) {
                        $this->Export(11021, '新增消息模板失败');
                    }
                } else {
                    $chn_tpl_model_ding['update_time'] = time();
                    $result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_ding['channel_tpl_id'])->update($chn_tpl_model_ding);
                    if (!$result) {
                        $this->Export(11022, '编辑消息模板失败');
                    }
                }
            } else {
                if (!empty($data['ding_channel_tpl_id'])) {
                    $delete_result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $data['ding_channel_tpl_id'])->delete();
                    if (!$delete_result) {
                        $this->Export(11023, '编辑消息模板失败');
                    }
                }
            }
        });
        $this->Export(0, empty($tpl_model['tpl_id']) ? '新增成功' : '修改成功');
    }

    //编辑手动消息
    //发送手动消息
    private function sendManualMessage($request)
    {
        $data = $request->input();
        $is_dumplicated = DB::connection('message')->table('lie_msg_tpl')->where('description',$data['description'])->first();
        if($is_dumplicated !== NULL)
        {
            $this->Export(11024, '已存在相同模板key,请更换key内容');
        }

        $url = '';
        $current_domain = $_SERVER['HTTP_HOST'];
        if($current_domain === Config('msgconfig.domain_local'))
        {
            $url = Config('msgconfig.api_domain_local');
        }elseif ($current_domain === Config('msgconfig.domain_sz'))
        {
            $url = Config('msgconfig.api_domain_sz');
        }elseif ($current_domain === Config('msgconfig.domain_release'))
        {
            $url = Config('msgconfig.api_domain_release');
        }

        $params = [];
        $params['channels'] = $data['channels'];
        $params['msg_type'] = $data['msg_type'];
//        $params['creater'] = $data['creater'];
        $params['description'] = $data['description'];
        $params['ex_str'] = $data['true_description'];
        $params['title_1'] = $data['title_1'];
        $params['title_3'] = $data['title_3'];
        $params['content_1'] = $data['content_1'];
        $params['content_2'] = $data['content_2'];
        $params['content_3'] = $data['content_3'];
        $params['op_type'] = $data['op_type'];
        $params['url_1'] = $data['url_1'];
        $params['obj_user'] = $data['obj_user'];
        $params['title_4'] = $data['title_4'];
        $params['url_4'] = $data['url_4'];

        $wechat_head = $data['wechat_notify_head'];
        $wechat_tail = $data['wechat_notify_tail'];
        $wechat_content = json_encode(array('wechat_tpl_head'=>$wechat_head,'wechat_tpl_tail'=>$wechat_tail));
        $params['content_4'] = $wechat_content;
        $params['expect_send_time'] = $data['expect_send_time'];
        //csrf
        $check['k1'] = time();
        $check['k2'] = pwdhash($check['k1'],'fh6y5t4rr351d2c3bryi');
        $params['pf'] = 1;
        $params['k1'] = $check['k1'];
        $params['k2'] = $check['k2'];
        $response = json_decode(curl($url,$params,1),true);

        $this->Export($response['err_code'], $response['err_msg']);

    }

    /** 获取模板子分类
     * @param $request
     */
    private function getSubTplCls($request){
        $data = $request->input();
        $sub_tpl_info = DB::connection('message')->table('lie_msg_classify')->select('cls_id','name')
            ->where('parent_id',$data['sel_edit_cls'])->where('parent_id','!=',0)->get();
        $this->Export(0, 'success',objectToArray($sub_tpl_info));
    }

    //删除消息模板(软删)
    private function delTpl($request){
        $in_data = $request->input();
        $res = DB::connection('message')->table('lie_msg_tpl')->where('tpl_id',$in_data['tpl_id'])->update(['status'=>1]);
        if ($res && $res !== false){
            $this->Export(0, 'success');
        }
    }

    //新增/编辑分类
    private function editClassify($request){
        $in_data = $request->input('field');

        //验证数据合理性 并去除字符串中的空格
        $in_data = $this->verifyClassifyData($in_data);

        //数据库操作
        $db = DB::connection('message');
        if (1 == $in_data['edit_type']){
            $classify_model = $this->constructClassifyModel($db, [], $in_data);
            $this->insertClassify($db, $classify_model);
        }else{
            $this->updateClassify($db, $in_data);
        }
        $this->Export(0, '操作成功');
    }

    //获取父级iD路径
    private function getParentIdPath($db, $parent_id) {
        if ($parent_id==0) {
            return strval(1);
        }
        $parent_id_path = $db->table('lie_msg_classify')->select('id_path')->where('cls_id',$parent_id)->first();
        if (!$parent_id_path) {
            $this->Export(12001, '未查找到根分类!');
        }
        return $parent_id_path->id_path;
    }

    //添加分类
    private function insertClassify($db, $classify_model) {
        if ($classify_model['depth']==1) {
            $root_id = $db->table('lie_msg_classify')->insertGetId($classify_model);
            if (!$root_id) {
                $this->Export(12002, '创建根分类失败!');
            }
            $this->Export(0, '创建根分类成功');
        } else {
            //获取父级id路径
            $parent_id_path = $this->getParentIdPath($db, $classify_model['parent_id']);
            $db->beginTransaction();
            try {//添加一级/二级分类
                $new_cls_id = $db->table('lie_msg_classify')->insertGetId($classify_model);
                if (!$new_cls_id) {
                    $this->Export(12003, '创建分类失败!');
                }else {//补充id路径
                    $id_path = $parent_id_path.','.strval($new_cls_id);
                    $res = $db->table('lie_msg_classify')->where('cls_id',$new_cls_id)->update(['id_path'=>$id_path]);
                    if (!$res) {
                        $this->Export(12004, '更新分类id路径失败!');
                    }
                }
                $db->commit();
            } catch (Exception $e) {
                $db->rollback();
                $this->Export(12005, '新增分类失败');
            }
        }
    }

    //更新分类
    private function updateClassify($db, $in_data){
        if (!empty($in_data['sel_edit_sub_classify'])){
            $cls_id = $in_data['sel_edit_sub_classify'];
            $res = $db->table('lie_msg_classify')->where('cls_id',$cls_id)->update(['name'=>$in_data['edit_classify']]);
            if (!$res && $res !== 0){
                $this->Export(12006, '编辑子分类失败');
            }
        }else {
            $cls_id = $in_data['sel_edit_classify'];
            $db->beginTransaction();
            try {
                $res = $db->table('lie_msg_classify')->where('cls_id',$cls_id)->update(['name'=>$in_data['edit_classify']]);
                if (!$res && $res !== 0){
                    $this->Export(12007, '编辑子分类失败');
                }
                $res = $db->table('lie_msg_classify')->where('parent_id',$cls_id)->update(['parent_name'=>$in_data['edit_classify']]);
                if (!$res && $res !== 0){
                    $this->Export(12008, '编辑子分类失败');
                }
                $db->commit();
            } catch (Exception $e) {
                $db->rollback();
                $this->Export(12009, '编辑子分类失败');
            }
        }
    }

    //构建消息分类模型
    private function constructClassifyModel($db, $classifyModel, $in_data) {
        $depth = 0;//分类深度
        //检测表中是否有数据,没有则为根节点
        $res = $db->table('lie_msg_classify')->select('cls_id')->first();
        if (!$res) {
            $depth = 1;
        } else {
            if (empty($in_data['sel_add_classify'])) {
                $depth = 2;
            } else {
                $depth = 3;
            }
        }
        $classify_model['depth']    = $depth;
        $classify_model['status']   = 1;
        $classify_model['name'] = $in_data['add_classify'];
        if ($depth==1) {
            $classify_model['parent_id'] = 0;
            $classify_model['parent_name'] = '';
            $classify_model['id_path']  = $this->getParentIdPath($db, $classify_model['parent_id']);
        }elseif($depth==2) {
            $classify_model['parent_id'] = 1;
            $parent_name = $db->table('lie_msg_classify')->select('name')->where('cls_id',$classify_model['parent_id'])->first();
            $classify_model['parent_name'] = $parent_name->name;
        }elseif($depth==3) {
            $classify_model['parent_id'] = $in_data['sel_add_classify'];
            $parent_name = $db->table('lie_msg_classify')->select('name')->where('cls_id',$classify_model['parent_id'])->first();
            $classify_model['parent_name'] = $parent_name->name;
        }else {
            $this->Export(12010, '参数错误');
        }
        return $classify_model;
    }

    //验证修改分类数据的合理性
    private function verifyClassifyData($in_data){
        if (1 == $in_data['edit_type']){
            if (empty($in_data['add_classify'])){
                $this->Export(11104, '请填写要添加的分类名称');
            }
        }else{
            if (empty($in_data['sel_edit_classify']) && empty($in_data['sel_edit_sub_classify'])){
                $this->Export(11106, '请选择要编辑的分类或子分类');
            }
            if (empty($in_data['edit_classify'])){
                $this->Export(11107, '请填写要编辑的分类名称');
            }
        }

        //去字符串空格
        if (!empty($in_data)){
            foreach ($in_data as &$d){
                $d = trim($d);
            }
            unset($d);
        }
        return $in_data;
    }

    /**
     * 上传文件
     */
    public function updateMsgExcel(Request $request){
        $file = $request->file('file');
        $tp_id = $request->get('inner-choose-type');
        if(!$tp_id)  return  $this->Export(-1, "请选择对应消息模板");
        $info = DB::connection("message")->table("lie_msg_tpl")->where("tpl_id",intval($tp_id))->select("tpl_id","channels","description")->first();
        if(!$info) return  $this->Export(-1, "没找到对应消息模板");
        if(count(explode(",",$info->channels)) > 1) return  $this->Export(-1, "暂只支持一种渠道消息,请完善模板消息后再来");
        if(!in_array($info->channels,[1,2,3])) return  $this->Export(-1, "暂支持单独发送邮件或短信");
        if($file){
            try{
                $excel_file_path = $file->getRealPath();
                $res = [];
                $title = [];
                Excel::load($excel_file_path, function($reader) use( &$res,&$title ) {
                    $reader = $reader->getSheet(0);
                    $res = $reader->toArray();
                },'GBK');
                $title = $res[0];
                unset($res[0]);
                $title  = array_filter($title,function($val){
                    if($val) return true;
                });

                $exchange = Config("website.msg_exchange"); // 交换器,在我理解,如果两个队列使用一个交换器就代表着两个队列是同步的,这个队列里存在的消息,在另一个队列里也会存在
                $queue = Config("website.msg_queue"); // 队列名称
                $connection = new AMQPStreamConnection(Config("website.rabbitmq.host"),
                    Config("website.rabbitmq.port"), Config("website.rabbitmq.login"), Config("website.rabbitmq.password"), Config("website.rabbitmq.vhost")); // 创建连接
                $channel = $connection->channel();
                $channel->queue_declare($queue, false, true, false, false);
                $channel->exchange_declare($exchange, 'direct', false, true, false);
                $channel->queue_bind($queue, $exchange); // 队列和交换器绑定
                $channel->tx_select();//开启事务
                foreach($res as $key=>$val){
                    //$val[0] 为接收者
                    if(!$val[0]) continue;
                    $params = [];
                    $touser = $val[0];
                    unset($val[0]);
                    //循环参数
                    $_params  = array_filter($val,function($_v){
                        if($_v) return true;
                    });
                    if(count($_params) != count($title)-1) throw new \Exception(sprintf("第%s行参数不一致",$key+1));
                    foreach($_params as $kk=>$vv){
                        if(!$vv) break;
                        $params[array_get($title,$kk)] = strval(trim($vv));
                    }
                    if(!$touser) throw new \Exception('接收者不能为空');
                    if(count($params) <= 0) throw new \Exception('没找到对应参数');
                    $msg_arr = [];
                    $msg_arr['job'] = 'msg.batch.send';
                    $_data['is_oversea'] = false;
                    $_data['template_id'] = $info->tpl_id;
                    $_data['keyword'] = $info->description;
                    $_data['channel_type'] = $info->channels;
                    $_data['touser'] = strval(trim($touser));
                    $_data['data'] = $params;
                    $_data['url'] = '';
                    $_data['wechat_data'] = null;
                    $_data['is_ignore'] = 1;
                    $_data['ex_int'] = 0;
                    $_data['ex_str'] = "";
                    $_data['pf'] = "999999";
                    $_data['fromuser'] = "";
                    $_data['delay'] = 0;
                    $_data['rbmq'] = [
                        'expire_time'=>time()+3600*2
                    ];
                    $msg_arr['data'] = json_encode($_data);
                    $msg = new AMQPMessage(json_encode($msg_arr));
                    $channel->basic_publish($msg, $exchange);
                }
                $channel->tx_commit();//提交事务
                $channel->close();
                $connection->close();
//                exit;
                return  $this->Export(0, "上传成功");
            }catch(\Exception $e){
                $channel->tx_rollback();
                return  $this->Export(-1, $e->getMessage());
            }

        }
        return  $this->Export(-1, '请务必上传文件');
    }
}