<?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="com.cool.store.mapper.StoreMessageMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.StoreMessageDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id"  property="id" />
    <result column="store_id"  property="storeId" />
    <result column="store_code"  property="storeCode" />
    <result column="store_name"  property="storeName" />
    <result column="message_template_id"  property="messageTemplateId" />
    <result column="read_status" jdbcType="BIT" property="readStatus" />
    <result column="read_time"  property="readTime" />
    <result column="process_status" jdbcType="TINYINT" property="processStatus" />
    <result column="process_time"  property="processTime" />
    <result column="actual_operator_id"  property="actualOperatorId" />
    <result column="actual_operator_name"  property="actualOperatorName" />
    <result column="revoke_status" jdbcType="BIT" property="revokeStatus" />
    <result column="revoke_time"  property="revokeTime" />
    <result column="revoke_operator_id"  property="revokeOperatorId" />
    <result column="revoke_operator_name"  property="revokeOperatorName" />
    <result column="create_time"  property="createTime" />
    <result column="update_time"  property="updateTime" />
    <result column="operator_list"  property="operatorList" />
  </resultMap>

  <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO zxjp_store_message (
    store_id,
    store_code,
    store_name,
    message_template_id,
    read_status,
    read_time,
    process_status,
    process_time,
    operator_list
    )
    VALUES
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.storeId},
      #{item.storeCode},
      #{item.storeName},
      #{item.messageTemplateId},
      #{item.readStatus},
      #{item.readTime},
      #{item.processStatus},
      #{item.processTime},
      #{item.operatorList}
      )
    </foreach>
  </insert>

  <select id="getMessageTemplateCountList" resultType="com.cool.store.dto.notice.MessageTemplateCountDTO">
    SELECT
    message_template_id AS templateId,
    COUNT(DISTINCT store_id) AS totalStoreCount,
    SUM(CASE WHEN process_status = 1 THEN 1 ELSE 0 END) AS processedStoreCount
    FROM
    zxjp_store_message
    <where>
      message_template_id IN
      <foreach item="item" collection="templateIds" index="index" separator="," close=")" open="(">
        #{item}
      </foreach>
    </where>
    GROUP BY
    message_template_id
  </select>

  <select id="getStoreMessageList" resultMap="BaseResultMap">
    SELECT
        id,
        store_id,
        store_code,
        store_name,
        message_template_id,
        read_status,
        read_time,
        process_status,
        process_time,
        actual_operator_id,
        actual_operator_name,
        revoke_status,
        revoke_time,
        revoke_operator_id,
        revoke_operator_name,
        create_time,
        update_time
    FROM zxjp_store_message
    <where>
        <if test="request.templateId != null">
            AND message_template_id = #{request.templateId}
        </if>
        <if test="request.readStatus != null">
            AND read_status = #{request.readStatus,jdbcType=BIT}
        </if>
        <if test="request.processStatus != null">
            AND process_status = #{request.processStatus,jdbcType=TINYINT}
        </if>
        <if test="request.keyword != null">
            AND ( store_code LIKE CONCAT('%',#{request.keyword},'%')
                OR store_name LIKE CONCAT('%',#{request.keyword},'%')
            )
        </if>
    </where>
    ORDER BY create_time DESC
</select>

    <update id="batchRevoke">
        UPDATE zxjp_store_message
        SET
        revoke_status = 1,
        revoke_time = NOW(),
        revoke_operator_id = #{userId},
        revoke_operator_name = #{userName},
        update_time = NOW()
        <where>
            message_template_id IN
            <foreach item="item" collection="ids" index="index" separator="," close=")" open="(">
                #{item}
            </foreach>
            AND process_status = 0
            AND revoke_status = 0
        </where>
    </update>

    <update id="revokeById">
    UPDATE zxjp_store_message
    SET
        revoke_status = 1,
        revoke_time = NOW(),
        revoke_operator_id = #{userId},
        revoke_operator_name = #{userName},
        update_time = NOW()
    WHERE
        id = #{id}
        AND process_status = 0
        AND revoke_status = 0
    </update>

    <select id="queryStoreModuleCount" resultType="java.lang.Integer">
        select count(1) from zxjp_store_message a
        left join zxjp_message_template b on a.message_template_id = b.id
        where a.store_id = #{storeId}
        and b.module_code = #{moduleCode}
        and a.operator_list like concat('%',#{userId},'%')
        limit 1
    </select>

    <select id="getStoreMessageListByCondition" resultType="com.cool.store.vo.notice.StoreMessageVO">
        select
        a.id,
        a.store_id  as storeId,
        a.store_code as storeCode,
        a.message_template_id as templateId,
        a.read_status as readStatus,
        a.process_status as processStatus,
        a.create_time as createTime,
        b.module_code as moduleCode,
        b.matter_type as matterType,
        b.message_title as messageTitle,
        b.message_image as messageImage,
        b.today_task as todayTask,
        b.jump_type as jumpType,
        b.jump_url as jumpUrl
        from zxjp_store_message a
        left join zxjp_message_template b on a.message_template_id = b.id
        <where>
            <if test="storeId != null">
                and a.store_id = #{storeId}
            </if>
            <if test="moduleCode != null">
                and b.module_code = #{moduleCode}
            </if>
            <if test="todayTask != null">
                and b.today_task = #{todayTask}
            </if>
            <if test="pendingFlag != null">
                and a.process_status = 0
                and a.process_status = 0
                and (b.remind_type = 1 or (b.remind_type = 2 and NOW() BETWEEN b.remind_start_time AND b.remind_end_time ))
            </if>
            <if test="userId != null">
                and a.operator_list like concat('%',#{userId},'%')
            </if>
            and revoke_status = 0
        </where>
        <if test="pendingFlag != null">
            order by b.today_task desc, a.create_time desc
        </if>
        <if test="pendingFlag == null">
            order by a.create_time desc
        </if>

    </select>

    <select id="getMessageDetail" resultType="com.cool.store.vo.notice.MessageDetailVO">
        select
        a.id,
        b.message_title,
        b.message_image,
        a.create_time,
        a.read_status as readStatus,
        a.process_status as processStatus,
        b.deadline as  deadline,
        b.publisher_user_id as publishUserId,
        b.message_content as messageContent,
        b.process_type as processType,
        b.handle_keyword as handleKeyword
        from
        zxjp_store_message a
        left join zxjp_message_template b on a.message_template_id = b.id
        where
        a.id = #{id}
    </select>

    <update id="batchUpdateHandle">
        UPDATE zxjp_store_message
        SET
        process_status = 1,
        process_time = NOW(),
        read_status = 1,
        read_time = NOW(),
        update_time = NOW()
        <where>
            <if test="id != null">
                AND message_template_id = #{id}
            </if>
            and store_code IN
            <foreach item="item" collection="storeCodeList" index="index" separator="," close=")" open="(">
                #{item}
            </foreach>
        </where>
    </update>



</mapper>