WarningMsgMapper.xml 36.8 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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.mindworkplate.mapper.warning.WarningMsgMapper" >
  <resultMap id="BaseResultMap" type="cn.com.glsx.dj.mindworkplate.model.warning.WarningMsg" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <id column="rule_id" property="ruleId" jdbcType="INTEGER" />
    <result column="monitor_type" property="monitorType" jdbcType="BIT" />
    <result column="business_type" property="businessType" jdbcType="BIT" />
    <result column="data_statistics_type" property="dataStatisticsType" jdbcType="BIT" />
    <result column="priority_level" property="priorityLevel" jdbcType="TINYINT" />
    <result column="stage" property="stage" jdbcType="INTEGER" />
    <result column="staff_id" property="staffId" jdbcType="VARCHAR" />
    <result column="staff_name" property="staffName" jdbcType="VARCHAR" />
    <result column="staff_phone" property="staffPhone" jdbcType="VARCHAR" />
    <result column="merchant_id4s" property="merchantId4s" jdbcType="INTEGER" />
    <result column="merchant_name4s" property="merchantName4s" jdbcType="VARCHAR" />
    <result column="rule_type" property="ruleType" jdbcType="VARCHAR" />
    <result column="rule" property="rule" jdbcType="VARCHAR" />
    <result column="star_date" property="starDate" jdbcType="BIT" />
    <result column="end_date" property="endDate" jdbcType="BIT" />
    <result column="status" property="status" jdbcType="BIT" />
    <result column="send_status" property="sendStatus" jdbcType="BIT" />
    <result column="follower_id" property="followerId" jdbcType="VARCHAR" />
    <result column="follower_phone" property="followerPhone" jdbcType="VARCHAR" />
    <result column="follower_name" property="followerName" jdbcType="VARCHAR" />
    <result column="next_follow_time" property="nextFollowTime" jdbcType="TIMESTAMP" />
    <result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
    <result column="created_by" property="createdBy" jdbcType="VARCHAR" />
    <result column="updated_time" property="updatedTime" jdbcType="TIMESTAMP" />
    <result column="updated_by" property="updatedBy" jdbcType="VARCHAR" />
    <result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
    <result column="rule_Detail" property="ruleDetail" jdbcType="VARCHAR" />
    <result column="salesperson" property="salesperson" jdbcType="VARCHAR" />
    <result column="supervisor" property="supervisor" jdbcType="VARCHAR" />
    <result column="send_batch" property="sendBatch" jdbcType="VARCHAR" />
    <result column="supervisor_completion_flag" property="supervisorCompletionFlag" jdbcType="VARCHAR" />
    <result column="msg_detail" property="msgDetail" jdbcType="VARCHAR" />
  </resultMap>
  <resultMap id="resResultMap" type="cn.com.glsx.dj.mindworkplate.response.warning.WarningMsgRes" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <id column="rule_id" property="ruleId" jdbcType="INTEGER" />
    <result column="monitor_type" property="monitorType" jdbcType="BIT" />
    <result column="business_type" property="businessType" jdbcType="BIT" />
    <result column="data_statistics_type" property="dataStatisticsType" jdbcType="BIT" />
    <result column="priority_level" property="priorityLevel" jdbcType="TINYINT" />
    <result column="stage" property="stage" jdbcType="INTEGER" />
    <result column="staff_id" property="staffId" jdbcType="VARCHAR" />
    <result column="staff_name" property="staffName" jdbcType="VARCHAR" />
    <result column="staff_phone" property="staffPhone" jdbcType="VARCHAR" />
    <result column="merchant_id4s" property="merchantId4s" jdbcType="INTEGER" />
    <result column="merchant_name4s" property="merchantName4s" jdbcType="VARCHAR" />
    <result column="rule_type" property="ruleType" jdbcType="VARCHAR" />
    <result column="rule" property="rule" jdbcType="VARCHAR" />
    <result column="star_date" property="starDate" jdbcType="BIT" />
    <result column="end_date" property="endDate" jdbcType="BIT" />
    <result column="status" property="status" jdbcType="BIT" />
    <result column="send_status" property="sendStatus" jdbcType="BIT" />
    <result column="follower_id" property="followerId" jdbcType="VARCHAR" />
    <result column="follower_name" property="followerName" jdbcType="VARCHAR" />
    <result column="next_follow_time" property="nextFollowTime" jdbcType="TIMESTAMP" />
    <result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
    <result column="created_by" property="createdBy" jdbcType="VARCHAR" />
    <result column="updated_time" property="updatedTime" jdbcType="TIMESTAMP" />
    <result column="updated_by" property="updatedBy" jdbcType="VARCHAR" />
    <result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
    <result column="salesperson" property="salesperson" jdbcType="VARCHAR" />
    <result column="supervisor" property="supervisor" jdbcType="VARCHAR" />
    <result column="supervisor_completion_flag" property="supervisorCompletionFlag" jdbcType="INTEGER" />
    <result column="msg_detail" property="msgDetail" jdbcType="VARCHAR" />
  </resultMap>
  <resultMap id="staffWaringMsgMap" extends="BaseResultMap" type="cn.com.glsx.dj.mindworkplate.dto.EarlyWarningMsgDTO">
    <id column="yq_merchant_id" property="yqMerchantId" jdbcType="INTEGER" />
    <id column="yq_merchant_name" property="yqMerchantName" jdbcType="VARCHAR" />
    <id column="scene" property="scene" jdbcType="INTEGER" />
  </resultMap>
  <resultMap id="managerBusinessTypeNumMap" type="cn.com.glsx.dj.mindworkplate.dto.BusinessOrMonitorTypeNumDTO">
    <id column="business_type" property="businessType" jdbcType="TINYINT" />
    <id column="monitor_type" property="monitorType" jdbcType="TINYINT" />
    <id column="msgNum" property="msgNum" jdbcType="VARCHAR" />
  </resultMap>
  <resultMap id="managerWarningMsgDetailMap" type="cn.com.glsx.dj.mindworkplate.dto.ManagerWarningMsgDetailDTO">
    <id column="staff_id" property="staffId" jdbcType="VARCHAR" />
    <id column="staff_name" property="staffName" jdbcType="VARCHAR" />
    <id column="staff_phone" property="staffPhone" jdbcType="VARCHAR" />
    <id column="merchant_name4s" property="merchantName4s" jdbcType="VARCHAR" />
    <id column="merchant_id4s" property="merchantId4s" jdbcType="VARCHAR" />
    <collection property="msgDetailList" javaType="java.util.ArrayList" ofType="cn.com.glsx.dj.mindworkplate.dto.ManagerGroupWarningMsgDetailDTO">
      <id column="rule_type" property="ruleType" jdbcType="VARCHAR" />
      <id column="priority_level" property="priorityLevel" jdbcType="BIGINT" />
      <id column="stage" property="stage" jdbcType="INTEGER" />
      <id column="rule" property="rule" jdbcType="VARCHAR" />
      <id column="rule_id" property="ruleId" jdbcType="INTEGER" />
      <id column="star_date" property="starDate" jdbcType="TIMESTAMP" />
      <id column="end_date" property="endDate" jdbcType="TIMESTAMP" />
      <id column="id" property="id" jdbcType="INTEGER" />
      <id column="follower_id" property="followerId" jdbcType="VARCHAR" />
      <id column="created_time" property="createdTime" jdbcType="VARCHAR" />
      <result column="msg_detail" property="msgDetail" jdbcType="VARCHAR"/>
    </collection>
  </resultMap>
  <resultMap id="staffWarningMsgMap" type="cn.com.glsx.dj.mindworkplate.dto.StaffWarningMsgNumDTO">
    <result column="data_statistics_type" property="dataStatisticsType" jdbcType="BIGINT" />
    <result column="msgNum" property="msgNum" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, rule_id, monitor_type, business_type, data_statistics_type, priority_level, stage, staff_id,
    staff_name, staff_phone, merchant_id4s, merchant_name4s, rule_type, rule, star_date, end_date, send_status,
    status, follower_id, follower_phone, follower_name, next_follow_time, created_time, created_by, updated_time,
    updated_by, deleted_flag,send_batch,msg_detail,supervisor_completion_flag
  </sql>
  <sql id="MsgFilterCondition">
    <if test="filterCriteria != null and filterCriteria == 1">
      <if test="roleType == 1 or roleType == 2">
        and (ewpr.source_type = #{roleType,jdbcType=TINYINT} and ewpr.staff_id = #{staffId,jdbcType=VARCHAR})
      </if>
      <if test="roleType != 1 and roleType != 2 and storePersonRoleFlag">
        and (ewpr.source_type = 3 and ewpr.staff_id = #{staffId,jdbcType=VARCHAR})
      </if>
    </if>
    <if test="filterCriteria != null and filterCriteria == 2">
      and ewm.follower_id = #{staffId,jdbcType=VARCHAR}
    </if>
    <if test="filterCriteria != null and filterCriteria == 3">
        AND ewfr.follower_id = #{staffId,jdbcType=VARCHAR}
    </if>
    <if test="filterCriteria != null and filterCriteria == 0">
      <if test="roleType == 1 or roleType == 2">
        and ((ewpr.source_type = #{roleType,jdbcType=TINYINT} and ewpr.staff_id = #{staffId,jdbcType=VARCHAR})
        or ewm.follower_id = #{staffId,jdbcType=VARCHAR})
      </if>
    </if>
    <if test="merchantId4sList != null and !merchantId4sList.isEmpty()">
      and ewm.merchant_id4s in
      <foreach collection="merchantId4sList" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
  </sql>
  <update id="updateSendStatus">
    update early_warning_msg
    set send_status=1,
    updated_time=now(),
    send_batch=#{batch}
    where 1=1
    <if test="collect != null and collect.size() > 0">
      and id in
      <foreach collection="collect" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </update>

  <select id="staffWarningMsgListWithPage"
          resultType="cn.com.glsx.dj.mindworkplate.response.warning.WarningMsgListWithPageRes">
    select t.staff_id as staffId,t.staff_name as staffName,t.staff_phone as staffPhone,t.merchant_id4s as merchantId,t.merchant_name4s as merchantName,t.business_type as bussinessType from
    ( SELECT
    ewm.*,
    MAX(CASE WHEN ewm.status = 2 THEN ewm.created_time END) AS max_created_time_2,
    MAX(ewm.created_time) AS max_created_time
    FROM early_warning_msg ewm
    WHERE ewm.monitor_type = 1
    AND ewm.deleted_flag = 'N'
    AND ewm.status IN (1, 2)
    <if test="staffName != null and staffName != ''">
      and ewm.staff_name like concat('%',#{staffName},'%')
    </if>
    <if test="staffPhone != null and staffPhone != ''">
      and ewm.staff_phone like concat('%',#{staffPhone},'%')
    </if>
    <if test="merchantName != null and merchantName != ''">
      and ewm.merchant_name4s like concat('%',#{merchantName},'%')
    </if>
    <if test="bussinessType != null">
      and ewm.business_type=#{bussinessType}
    </if>

    <if test="permissMerchantNoList != null and permissMerchantNoList.size>0">
      and (ewm.business_type, ewm.merchant_id4s) IN
      <foreach item="pair" collection="permissMerchantNoList" separator="," open="(" close=")">
        (#{pair.bussinessType}, #{pair.merchantNo})
      </foreach>
    </if>

    <if test="msgIds != null">
      and ewm.id in
      <foreach collection="msgIds" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    GROUP BY ewm.staff_id, ewm.merchant_id4s, ewm.business_type
    )t
    GROUP BY t.staff_id, t.merchant_id4s, t.business_type
    ORDER BY
    CASE
    WHEN t.max_created_time_2 IS NOT NULL THEN 1
    ELSE 2
    END,
    t.max_created_time DESC

  </select>
  <select id="findStaffWarningMsgByCondition"
          resultMap="BaseResultMap">
    select t1.*,group_concat(distinct t2.staff_name) as supervisor,group_concat(distinct t3.staff_name) as salesperson from early_warning_msg t1
    left join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=1)
    left join early_warning_person_relations t3 on (t1.id=t3.msg_id AND t3.source_type=2)
    where t1.monitor_type=1 and t1.deleted_flag='N' and status in (1,2)
    <if test="collect != null and collect.size()>0">
    and (t1.staff_id, t1.merchant_id4s, t1.business_type) IN
    <foreach item="pair" collection="collect" separator="," open="(" close=")">
      (#{pair.staffId}, #{pair.merchantId},#{pair.bussinessType})
    </foreach>
      group by t1.id
  </if>
  </select>
  <select id="warningMsgDetailListWithPage"
          resultMap="resResultMap">
    SELECT t1.* FROM early_warning_msg t1
    left join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=1)
    left join early_warning_person_relations t3 on (t1.id=t3.msg_id AND t3.source_type=2)
    left join early_warning_person_relations t4 on (t1.id=t4.msg_id AND t4.source_type=3)
    where t1.deleted_flag='N'
    <if test="condition.staffName != null and condition.staffName!= ''">
      and t1.staff_name like concat('%',#{condition.staffName},'%')
    </if>
    <if test="condition.msgIds != null">
      and t1.id in
      <foreach collection="condition.msgIds" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    <if test="condition.statusList!= null and condition.statusList.size()>0">
      and t1.status in
      <foreach item="item" collection="condition.statusList" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
    <if test="condition.permissMerchantNoList != null and condition.permissMerchantNoList.size>0">
      and (t1.business_type, t1.merchant_id4s) IN
      <foreach item="pair" collection="condition.permissMerchantNoList" separator="," open="(" close=")">
        (#{pair.bussinessType}, #{pair.merchantNo})
      </foreach>
    </if>
    <if test="condition.staffPhone != null and condition.staffPhone!= ''">
      and t1.staff_phone like concat('%',#{condition.staffPhone},'%')
    </if>
    <if test="condition.merchantName != null and condition.merchantName!= ''">
      and t1.merchant_name4s like concat('%',#{condition.merchantName},'%')
    </if>
    <if test="condition != null and condition.monitorType != null">
      and t1.monitor_Type=#{condition.monitorType}
    </if>
    <if test="condition.ruleType != null and condition.ruleType!= ''">
      and t1.rule_type=#{condition.ruleType}
    </if>
    <if test="condition != null and condition.bussinessType != null">
      and t1.business_type=#{condition.bussinessType}
    </if>
    <if test="condition.status != null">
      and t1.status=#{condition.status}
    </if>
    <if test="condition.supervisorCompletionFlag != null">
      and t1.supervisor_completion_flag=#{condition.supervisorCompletionFlag}
    </if>
    <if test="condition.dataStatisticsType != null and condition.dataStatisticsType!= ''">
      and t1.data_statistics_type=#{condition.dataStatisticsType}
    </if>
    <if test="condition != null and condition.priorityLevel != null">
      and t1.priority_level=#{condition.priorityLevel}
    </if>
    <if test="condition.warningItems != null and condition.warningItems!= ''">
      and t1.rule=#{condition.warningItems}
    </if>
    <if test="condition.warningStartDate != null ">
      and  #{condition.warningStartDate} >= t1.star_date
    </if>
    <if test="condition.warningEndDate != null">
      and t1.end_date >= #{condition.warningEndDate}
    </if>
    <if test="condition.staffId != null and condition.staffId != ''">
      and t1.staff_id=#{condition.staffId}
    </if>
    <if test="condition.merchantNo != null">
      and t1.merchant_id4s=#{condition.merchantNo}
    </if>
    <if test="condition != null and condition.stage != null">
      and t1.stage=#{condition.stage}
    </if>
    <if test="condition.supervisorId != null and condition.supervisorId!= ''">
      and t2.staff_id=#{condition.supervisorId}
    </if>
    <if test="condition.salespersonId != null and condition.salespersonId!= ''">
      and t3.staff_id=#{condition.salespersonId}
    </if>
    <if test="condition.storePersonId != null and condition.storePersonId!= ''">
      and t4.staff_id=#{condition.storePersonId}
    </if>
    <if test="condition.startDate != null">
      and DATE_FORMAT(t1.created_time, '%Y-%m-%d') &gt;= #{condition.startDate}
    </if>
    <if test="condition.endDate != null">
      and DATE_FORMAT(t1.created_time, '%Y-%m-%d') &lt;= #{condition.endDate}
    </if>


    group by t1.id
    ORDER BY
    CASE
    WHEN t1.status = 2 THEN 1
    WHEN t1.status = 1 THEN 2
    WHEN t1.status = 0 THEN 3
    WHEN t1.status = 3 THEN 4
    END,t1.priority_level,t1.stage desc,
    t1.created_time DESC
  </select>
  <select id="merchantWarningMsgListWithPage"
          resultType="cn.com.glsx.dj.mindworkplate.response.warning.WarningMsgListWithPageRes">
    select t.merchant_id4s as merchantId,t.merchant_name4s as merchantName,t.business_type as bussinessType from
    (SELECT
    ewm.*,
    MAX(CASE WHEN ewm.status = 2 THEN ewm.created_time END) AS max_created_time_2,
    MAX(ewm.created_time) AS max_created_time
    FROM early_warning_msg ewm
    WHERE ewm.monitor_type = 2
    AND ewm.deleted_flag = 'N'
    AND ewm.status IN (1, 2)
    <if test="merchantName != null and merchantName != ''">
      and ewm.merchant_name4s like concat('%',#{merchantName},'%')
    </if>
    <if test="bussinessType != null">
      and ewm.business_type=#{bussinessType}
    </if>
    <if test="permissMerchantNoList != null and permissMerchantNoList.size>0">
      and (ewm.business_type, ewm.merchant_id4s) IN
      <foreach item="pair" collection="permissMerchantNoList" separator="," open="(" close=")">
        (#{pair.bussinessType}, #{pair.merchantNo})
      </foreach>
    </if>
    <if test="msgIds != null">
      and ewm.id in
      <foreach collection="msgIds" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    GROUP BY ewm.merchant_id4s, ewm.business_type
    )t
    group by t.merchant_id4s,t.business_type
    ORDER BY
    CASE
    WHEN t.max_created_time_2 IS NOT NULL THEN 1
    ELSE 2
    END,
    t.max_created_time DESC
  </select>
  <select id="findMerchantWarningMsgByCondition"
          resultMap="BaseResultMap">
    select t1.*,group_concat(distinct t2.staff_name) as supervisor,group_concat(distinct t3.staff_name) as salesperson from early_warning_msg t1
    left join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=1)
    left join early_warning_person_relations t3 on (t1.id=t3.msg_id AND t3.source_type=2)
    where t1.monitor_type=2 and t1.deleted_flag='N' and t1.status in (1,2)
    <if test="collect != null and collect.size()>0">
    and (t1.merchant_id4s, t1.business_type) IN
    <foreach item="pair" collection="collect" separator="," open="(" close=")">
      (#{pair.merchantId},#{pair.bussinessType})
    </foreach>
    </if>
    group by t1.id
  </select>
  <select id="findResStaffWarningMsg"
          resultMap="BaseResultMap">
    select * from early_warning_msg
    where monitor_type=1 and deleted_flag='N' and send_status in (0,2)
  </select>
  <select id="findSalesSend"
    resultMap="BaseResultMap">
    select * from
    (select t1.*,t2.staff_phone as salesperson from early_warning_msg t1
    left join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=2)
    where t2.id is not null and t1.status in (1,2)
    <if test="uuid != null">
      and t2.staff_id=#{uuid}
    </if>
    union all
    select t1.*,t1.follower_phone as salesperson from early_warning_msg t1
    where 1=1 and t1.status in (1,2)
    <if test="uuid != null">
      and t1.follower_id=#{uuid}
    </if>
    <if test="uuid == null">
      and t1.follower_id in (select distinct staff_id from early_warning_person_relations where source_type=2)
    </if>
    )t
    group by t.id
  </select>

  <select id="findSupSend"
    resultMap="BaseResultMap">
    select
      t1.*,
      t2.staff_phone as supervisor
    from early_warning_msg t1
    inner join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=1)
    where t1.priority_level = 1 and t1.status in (1,2) and (t1.supervisor_completion_flag = 0 or t1.supervisor_completion_flag is null) and t1.deleted_flag='N'
    <if test="merchantName4s!=null">
      and t1.merchant_name4s like concat('%',#{merchantName4s},'%')
    </if>
    <if test="uuid!=null">
      and t2.staff_id=#{uuid}
    </if>
  </select>

  <select id="findSupSendByCondition"
    resultMap="BaseResultMap">
    select
      t1.*,
      t2.staff_phone as supervisor
    from early_warning_msg t1
    inner join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=1)
    where t1.priority_level = 1 and t1.status in (1,2) and (t1.supervisor_completion_flag = 0 or t1.supervisor_completion_flag is null) and t1.deleted_flag='N'
    <if test="condition.merchantName4s != null">
      and t1.merchant_name4s like concat('%',#{condition.merchantName4s},'%')
    </if>
    <if test="condition.supStaffUuid != null">
      and t2.staff_id=#{condition.supStaffUuid}
    </if>
    <if test="condition.merchantId4sList != null and condition.merchantId4sList.size()>0">
      and t1.merchant_id4s IN
      <foreach item="merchantId" collection="condition.merchantId4sList" separator="," open="(" close=")">
        #{merchantId}
      </foreach>
    </if>
    <if test="condition.msgDTOList != null and condition.msgDTOList.size()>0">
      and (t1.business_type, t1.merchant_id4s) in
      <foreach item="msgDTO" collection="condition.msgDTOList" separator="," open="(" close=")">
        (#{msgDTO.businessType}, #{msgDTO.merchantId4s})
      </foreach>
    </if>
  </select>

  <select id="opeStaffSend"
    resultMap="BaseResultMap">
    select * from early_warning_msg
     where deleted_flag='N' and status in (1,2)
    <if test="uuid != null">
      and follower_id=#{uuid}
    </if>
  </select>

  <!-- 查询驻店员工预警信息 -->
    <select id="listStaffWaringMsgWithPage" parameterType="cn.com.glsx.dj.mindworkplate.dto.EarlyWarningMsgDTO" resultMap="staffWaringMsgMap">
      select ewm.rule_type,ewm.rule, ewm.star_date,ewm.end_date, ewm.merchant_id4s, ewm.merchant_name4s, ewm.business_type,ewm.data_statistics_type,
             ewm.priority_level,ewm.staff_id,ewm.staff_name,ewm.staff_phone,mcf.yq_merchant_id,mcf.yq_merchant_name,mcf.scene
      from early_warning_msg ewm
        left join dj_car_life_merchant_contract_flow mcf on ewm.merchant_id4s = mcf.flow_merchant_id and mcf.deleted_flag = 'N'
         and mcf.scene = (case ewm.business_type when 2 then 1 when 3 then 0 when 4 then 0 end)
      where ewm.deleted_flag = 'N'
        and ewm.monitor_type = 1
      <if test="sendBatch != null and sendBatch != ''">
        and ewm.send_batch = #{sendBatch ,jdbcType=VARCHAR}
      </if>
      <if test="dataStatisticsType != null and dataStatisticsType != ''">
        and ewm.data_statistics_type = #{dataStatisticsType, jdbcType=TINYINT}
      </if>
      order by ewm.id desc
    </select>

  <!-- 统计不同业务场景预警消息数目 -->
  <select id="countManagerWarningMsgByBusinessType" parameterType="cn.com.glsx.dj.mindworkplate.dto.ManagerWarningMsgDTO" resultMap="managerBusinessTypeNumMap">
    select t.business_type, count(distinct (t.id)) AS msgNum
    from (select ewm.*  from early_warning_msg ewm
           left join early_warning_person_relations ewpr on ewm.id = ewpr.msg_id and ewpr.deleted_flag = 'N'
    where ewm.deleted_flag = 'N'
      and ewm.status in (1, 2)
      <if test="roleType == 1 or roleType == 2">
        and ((ewpr.source_type = #{roleType,jdbcType=TINYINT} and ewpr.staff_id = #{staffId,jdbcType=VARCHAR})
                 or ewm.follower_id = #{staffId,jdbcType=VARCHAR})
      </if>
    <if test="merchantId4sList != null and !merchantId4sList.isEmpty()">
      and ewm.merchant_id4s in
      <foreach collection="merchantId4sList" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    group by ewm.rule_id, ewm.merchant_id4s, ewm.staff_id
    ) t
    group by t.business_type
  </select>

  <!-- 统计固定场景下员工和门店预警数目 -->
  <select id="countManagerMonitorTypeNum"  parameterType="cn.com.glsx.dj.mindworkplate.dto.ManagerWarningMsgDTO" resultMap="managerBusinessTypeNumMap">
    select t.monitor_type, count(distinct (t.id)) AS msgNum
    from (select ewm.* from early_warning_msg ewm
           left join early_warning_person_relations ewpr on ewm.id = ewpr.msg_id and ewpr.deleted_flag = 'N'
    where ewm.deleted_flag = 'N'
      and ewm.status in (1, 2)
      and ewm.business_type = #{businessType,jdbcType=TINYINT}
      <if test="roleType == 1 or roleType == 2">
        and ((ewpr.source_type = #{roleType,jdbcType=TINYINT} and ewpr.staff_id = #{staffId,jdbcType=VARCHAR})
        or ewm.follower_id = #{staffId,jdbcType=VARCHAR})
      </if>
    <if test="merchantId4sList != null and !merchantId4sList.isEmpty()">
      and ewm.merchant_id4s in
      <foreach collection="merchantId4sList" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    group by ewm.rule_id, ewm.merchant_id4s, ewm.staff_id
    ) t
    group by t.monitor_type
  </select>

  <!-- 小程序端运营人员分页查询员工信息 -->
  <select id="appletStaffWarningMsgListWithPage" parameterType="cn.com.glsx.dj.mindworkplate.dto.ManagerWarningMsgDTO" resultMap="managerWarningMsgDetailMap">
    SELECT sum(msgNum) num,temp.staff_id,temp.merchant_id4s from(
      select CASE WHEN ewm.follower_id = #{staffId,jdbcType=VARCHAR} THEN 1 ELSE 0 END AS msgNum,ewm.staff_id,ewm.merchant_id4s
      FROM early_warning_msg ewm
      LEFT JOIN early_warning_person_relations ewpr ON ewm.id = ewpr.msg_id AND ewpr.deleted_flag = 'N'
      LEFT JOIN early_warning_follow_record ewfr on ewm.id = ewfr.msg_id and ewfr.deleted_flag = 'N'
        WHERE ewm.deleted_flag = 'N'
        AND ewm.status IN (1, 2)
        AND ewm.business_type = #{businessType,jdbcType=TINYINT}
        AND ewm.monitor_type = #{monitorType,jdbcType=TINYINT}
        <include refid="MsgFilterCondition"/>
        <if test="keyWord != null and keyWord != ''">
        and (ewm.staff_id, ewm.merchant_id4s) in
            (
             select ewm2.staff_id, ewm2.merchant_id4s from early_warning_person_relations ewpr2
                  left join early_warning_msg ewm2 on ewpr2.msg_id=ewm2.id
                where  ewm2.deleted_flag = 'N'
                  and ewm2.status IN (1, 2)
                  and ewm2.business_type = #{businessType,jdbcType=TINYINT}
                  and ewm2.monitor_type = #{monitorType,jdbcType=TINYINT}
                  and (ewm2.staff_name like concat ('%', #{keyWord,jdbcType=VARCHAR},'%')
                  or ewm2.staff_phone like concat ('%', #{keyWord,jdbcType=VARCHAR},'%')
                  or ewm2.merchant_name4s like concat ('%', #{keyWord,jdbcType=VARCHAR},'%')
                  or ewpr2.staff_name like concat ('%', #{keyWord,jdbcType=VARCHAR},'%'))
                  and ewpr2.deleted_flag = 'N'
                  GROUP BY ewm2.staff_id, ewm2.merchant_id4s
            )
        </if>
        ) temp
        GROUP BY staff_id, merchant_id4s  order by num desc
  </select>

  <!-- 小程序端管理人员分页查询门店信息 -->
  <select id="listAppletWarningMsgDetail" parameterType="cn.com.glsx.dj.mindworkplate.dto.ManagerMsgDetailDTO" resultMap="managerWarningMsgDetailMap">
    select * from (
        select distinct
            <if test="shopMerchantStaffId != null and shopMerchantStaffId != ''">
              ewm.staff_id,ewm.staff_name,ewm.staff_phone,
            </if>
            ewm.merchant_id4s,ewm.merchant_name4s,
            ewm.rule_type,ewm.priority_level,ewm.stage,ewm.rule_id,ewm.rule,ewm.star_date,ewm.end_date,ewm.id,ewm.follower_id,ewm.created_time, ewm.msg_detail
        from early_warning_msg ewm
          left join early_warning_person_relations ewpr on ewm.id = ewpr.msg_id and ewpr.deleted_flag = 'N'
          LEFT JOIN early_warning_follow_record ewfr on ewm.id = ewfr.msg_id and ewfr.deleted_flag = 'N'
        where ewm.deleted_flag = 'N'
          and ewm.status in (1, 2)
          and ewm.business_type = #{businessType,jdbcType=TINYINT}
          and ewm.monitor_type = #{monitorType,jdbcType=TINYINT}
          <include refid="MsgFilterCondition"/>
          <if test="shopMerchantStaffId != null and shopMerchantStaffId != ''">
            and ewm.staff_id = #{shopMerchantStaffId,jdbcType=VARCHAR}
          </if>
          <if test="merchantId4s != null and merchantId4s != ''">
            and ewm.merchant_id4s = #{merchantId4s,jdbcType=VARCHAR}
          </if>
          ) temp
        order by  CASE WHEN temp.follower_id = #{staffId,jdbcType=VARCHAR}
            THEN 1 ELSE 0 END desc, temp.stage desc,temp.id desc
  </select>

  <!-- 运营人员查询门店信息详情 -->
  <select id="appletMerchantWarningMsgListWithPage" parameterType="cn.com.glsx.dj.mindworkplate.dto.ManagerWarningMsgDTO" resultMap="managerWarningMsgDetailMap">
    SELECT sum(msgNum) num,temp.merchant_id4s from(
      select CASE WHEN ewm.follower_id = #{staffId,jdbcType=VARCHAR} THEN 1 ELSE 0 END AS msgNum,ewm.merchant_id4s
        FROM early_warning_msg ewm
            LEFT JOIN early_warning_person_relations ewpr ON ewm.id = ewpr.msg_id AND ewpr.deleted_flag = 'N'
            LEFT JOIN early_warning_follow_record ewfr on ewm.id = ewfr.msg_id and ewfr.deleted_flag = 'N'
        WHERE ewm.deleted_flag = 'N'
        AND ewm.status IN (1, 2)
        AND ewm.business_type = #{businessType,jdbcType=TINYINT}
        AND ewm.monitor_type = #{monitorType,jdbcType=TINYINT}
          <include refid="MsgFilterCondition"/>
          <if test="keyWord != null and keyWord != ''">
            and (ewm.merchant_id4s) in
            (
            select ewm2.merchant_id4s from early_warning_person_relations ewpr2
            left join early_warning_msg ewm2 on ewpr2.msg_id=ewm2.id
            where ewm2.deleted_flag = 'N'
            and ewm2.status IN (1, 2)
            and ewm2.business_type = #{businessType,jdbcType=TINYINT}
            and ewm2.monitor_type = #{monitorType,jdbcType=TINYINT}
            and (ewm2.staff_name like concat ('%', #{keyWord,jdbcType=VARCHAR},'%')
            or ewm2.staff_phone like concat ('%', #{keyWord,jdbcType=VARCHAR},'%')
            or ewm2.merchant_name4s like concat ('%', #{keyWord,jdbcType=VARCHAR},'%')
            or ewpr2.staff_name like concat ('%', #{keyWord,jdbcType=VARCHAR},'%'))
            and ewpr2.deleted_flag = 'N'
            GROUP BY ewm2.merchant_id4s
            )
          </if>
        ) temp
        GROUP BY merchant_id4s order by num desc
  </select>

  <!-- 修改预警消息状态 -->
  <update id="updateMsgStatusById" parameterType="cn.com.glsx.dj.mindworkplate.dto.DealWithWarningMsgDTO">
    update early_warning_msg wm
    set wm.status = #{status},
        <if test="status != null and status == 3">
          wm.follower_id = null,
          wm.follower_phone = null,
          wm.follower_name = null,
          wm.next_follow_time = null,
        </if>
        wm.updated_by= #{followerId,jdbcType=VARCHAR},
        wm.updated_time = NOW()
    where wm.id = #{msgId,jdbcType=INTEGER}
      and wm.deleted_flag = 'N'
  </update>
  <update id="updateFollowStatusById">
    update early_warning_msg wm
    set wm.follower_id = null,
        wm.follower_phone = null,
        wm.follower_name = null,
        wm.next_follow_time=null
    where wm.id = #{msgId,jdbcType=INTEGER}
  </update>
  <update id="updateFollowTimeStatusById">
    update early_warning_msg wm
    set wm.next_follow_time=null
    where wm.id = #{msgId,jdbcType=INTEGER}
  </update>

  <!-- 统计员工预警消息数量 -->
  <select id="countStaffWarningMsg" parameterType="cn.com.glsx.dj.mindworkplate.dto.StaffWarningMsgNumDTO" resultMap="staffWarningMsgMap">
    select wm.data_statistics_type, count(wm.id) as msgNum
        from early_warning_msg wm
    where wm.deleted_flag = 'N'
        and wm.monitor_type = 1
    <if test="sendBatch != null and sendBatch != ''">
      and wm.send_batch = #{sendBatch ,jdbcType=VARCHAR}
    </if>
        group by wm.data_statistics_type
  </select>
  <select id="findMsgByOwner" resultType="java.lang.Integer">

    SELECT distinct msg_id FROM early_warning_person_relations where source_type=3 and staff_id=#{loginUuid}
  </select>
  <select id="findMsgByWaitProcess" resultType="java.lang.Integer">
    SELECT id FROM early_warning_msg where follower_id=#{loginUuid}
  </select>
  <select id="findMsgByFollowed" resultType="java.lang.Integer">
    SELECT distinct msg_id from early_warning_follow_record WHERE follower_id=#{loginUuid}
  </select>
  <select id="warningMsgStaffList"
          resultType="cn.com.glsx.dj.mindworkplate.response.warning.WarningMsgStaffRes">
    SELECT t1.staff_id as staffId,t1.staff_name as staffName FROM early_warning_msg t1
    left join early_warning_person_relations t2 on (t1.id=t2.msg_id AND t2.source_type=1)
    left join early_warning_person_relations t3 on (t1.id=t3.msg_id AND t3.source_type=2)
    left join early_warning_person_relations t4 on (t1.id=t4.msg_id AND t4.source_type=3)
    where t1.deleted_flag='N'
    <if test="condition.staffName != null and condition.staffName!= ''">
      and t1.staff_name like concat('%',#{condition.staffName},'%')
    </if>
    <if test="condition.msgIds != null">
      and t1.id in
      <foreach collection="condition.msgIds" item="item" separator="," open="(" close=")">
        #{item}
      </foreach>
    </if>
    <if test="condition.statusList!= null and condition.statusList.size()>0">
      and t1.status in
      <foreach item="item" collection="condition.statusList" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
    <if test="condition.staffPhone != null and condition.staffPhone!= ''">
      and t1.staff_phone like concat('%',#{condition.staffPhone},'%')
    </if>
    <if test="condition.merchantName != null and condition.merchantName!= ''">
      and t1.merchant_name4s like concat('%',#{condition.merchantName},'%')
    </if>
    <if test="condition != null and condition.monitorType != null">
      and t1.monitor_Type=#{condition.monitorType}
    </if>
    <if test="condition.ruleType != null and condition.ruleType!= ''">
      and t1.rule_type=#{condition.ruleType}
    </if>
    <if test="condition != null and condition.bussinessType != null">
      and t1.business_type=#{condition.bussinessType}
    </if>
    <if test="condition.status != null">
      and t1.status=#{condition.status}
    </if>
    <if test="condition.dataStatisticsType != null and condition.dataStatisticsType!= ''">
      and t1.data_statistics_type=#{condition.dataStatisticsType}
    </if>
    <if test="condition != null and condition.priorityLevel != null">
      and t1.priority_level=#{condition.priorityLevel}
    </if>
    <if test="condition.warningItems != null and condition.warningItems!= ''">
      and t1.rule=#{condition.warningItems}
    </if>
    <if test="condition.warningStartDate != null ">
      and  #{condition.warningStartDate} >= t1.star_date
    </if>
    <if test="condition.warningEndDate != null">
      and t1.end_date >= #{condition.warningEndDate}
    </if>
    <if test="condition.staffId != null and condition.staffId != ''">
      and t1.staff_id=#{condition.staffId}
    </if>
    <if test="condition.merchantNo != null">
      and t1.merchant_id4s=#{condition.merchantNo}
    </if>
    <if test="condition != null and condition.stage != null">
      and t1.stage=#{condition.stage}
    </if>
    <if test="condition.supervisorId != null and condition.supervisorId!= ''">
      and t2.staff_id=#{condition.supervisorId}
    </if>
    <if test="condition.salespersonId != null and condition.salespersonId!= ''">
      and t3.staff_id=#{condition.salespersonId}
    </if>
    <if test="condition.storePersonId != null and condition.storePersonId!= ''">
      and t4.staff_id=#{condition.storePersonId}
    </if>
    <if test="condition.startDate != null">
      and DATE_FORMAT(t1.created_time, '%Y-%m-%d') &gt;= #{condition.startDate}
    </if>
    <if test="condition.endDate != null">
      and DATE_FORMAT(t1.created_time, '%Y-%m-%d') &lt;= #{condition.endDate}
    </if>
    group by t1.staff_id
  </select>
  <select id="findWarnMerchantByMonth" resultType="java.lang.Integer">
    select distinct merchant_id4s
    from early_warning_msg
    where deleted_flag='N' and DATE_FORMAT(created_time, '%Y-%m') = #{month}
    and rule_id in
    <foreach item="item" collection="rules" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>
  <select id="findWarnMsgListByMonth"
          resultType="cn.com.glsx.dj.mindworkplate.model.warning.WarningMsgResponse">
    select t1.rule_id as ruleId,t1.merchant_id4s as merchantId4s,t1.business_type as bussinessType,t1.rule as rule,t1.created_time as createTime,t1.cycle_indicator as cycleIndicator,t1.fallback_indicator as fallbackIndicator
    from early_warning_msg t1
    where t1.deleted_flag='N' and DATE_FORMAT(t1.created_time, '%Y-%m') = #{warnParam.month}
    and t1.merchant_id4s=#{warnParam.merchantNo}
    and t1.rule_id in
    <foreach item="item" collection="rules" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>
  <select id="findWarnMsgById" resultType="java.lang.Integer">
    select id from early_warning_msg 
    where deleted_flag='N' and  status IN (1, 2) and rule_id=#{warningMsg.ruleId}
    <if test="warningMsg.staffId!=null">
      and staff_id=#{warningMsg.staffId}
    </if>
    <if test="warningMsg.staffId==null">
      and merchant_id4s=#{warningMsg.merchantId4s}
    </if>
  </select>
</mapper>