<?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.MessageTemplateMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.MessageTemplateDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="message_code" jdbcType="VARCHAR" property="messageCode" />
    <result column="module_code" jdbcType="TINYINT" property="moduleCode" />
    <result column="matter_type" jdbcType="TINYINT" property="matterType" />
    <result column="message_title" jdbcType="VARCHAR" property="messageTitle" />
    <result column="system_source" jdbcType="VARCHAR" property="systemSource" />
    <result column="process_type" jdbcType="TINYINT" property="processType" />
    <result column="remind_type" jdbcType="TINYINT" property="remindType" />
    <result column="remind_start_time" jdbcType="TIMESTAMP" property="remindStartTime" />
    <result column="remind_end_time" jdbcType="TIMESTAMP" property="remindEndTime" />
    <result column="deadline" jdbcType="TIMESTAMP" property="deadline" />
    <result column="today_task" jdbcType="BIT" property="todayTask" />
    <result column="message_image" jdbcType="VARCHAR" property="messageImage" />
    <result column="publish_status" jdbcType="BIT" property="publishStatus" />
    <result column="publish_time" jdbcType="TIMESTAMP" property="publishTime" />
    <result column="publisher_user_id" jdbcType="BIGINT" property="publisherUserId" />
    <result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="updater_user_id" jdbcType="BIGINT" property="updaterUserId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="deleted" jdbcType="BIT" property="deleted" />
    <result column="message_content" jdbcType="LONGVARCHAR" property="messageContent" />
    <result column="handle_person_info" jdbcType="LONGVARCHAR" property="handlePersonInfo" />
    <result column="store_info" jdbcType="LONGVARCHAR" property="storeInfo" />
  </resultMap>


  <select id="selectByIds"  resultMap="BaseResultMap">
    SELECT
    *
    FROM zxjp_message_template
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id,jdbcType=BIGINT}
    </foreach>
    AND deleted = 0
    ORDER BY create_time DESC
  </select>

  <update id="updateDeleteStatusBatch" >
    UPDATE zxjp_message_template
    SET
    deleted = 1,
    update_time = NOW(), updater_user_id = #{userId,jdbcType=VARCHAR}
    WHERE id IN
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id,jdbcType=BIGINT}
    </foreach>
  </update>


  <select id="getMessageTemplateList" resultType="com.cool.store.dto.notice.NoticeDTO" >
    SELECT
        id as id,
        message_code as messageCode,
        module_code as moduleCode,
        matter_type as matterType,
        message_title as messageTitle,
        system_source as systemSource,
        process_type as processType,
        remind_type as remindType,
        remind_start_time as remindStartTime,
        remind_end_time as remindEndTime,
        deadline as  deadline,
        today_task as todayTask,
        message_image as messageImage,
        publish_status as publishStatus,
        publish_time as publishTime,
        publisher_user_id as publishUserId,
        create_user_id as createUserId,
        create_time as createTime,
        handle_person_info as handlePersonInfo,
        jump_type as jumpType,
        jump_url as jumpUrl,
        store_info as storeInfo
    FROM zxjp_message_template
    WHERE deleted = 0
    <if test="request.moduleCode != null">
        AND module_code = #{request.moduleCode,jdbcType=TINYINT}
    </if>
    <if test="request.matterType != null">
        AND matter_type = #{request.matterType,jdbcType=TINYINT}
    </if>
    <if test="request.messageTitle != null and request.messageTitle != ''">
        AND message_title LIKE CONCAT('%', #{request.messageTitle,jdbcType=VARCHAR}, '%')
    </if>
    <if test="request.todayTask != null">
        AND today_task = #{request.todayTask,jdbcType=BIT}
    </if>
    <if test="request.systemSource != null and request.systemSource != ''">
        AND system_source = #{request.systemSource,jdbcType=VARCHAR}
    </if>
    <if test="request.publishStatus != null">
        AND publish_status = #{request.publishStatus,jdbcType=BIT}
    </if>
    <if test="request.publishUserId != null">
      AND publisher_user_id = #{request.publishUserId,jdbcType=BIT}
    </if>
    ORDER BY create_time DESC
  </select>

    <update id="batchUpdateStoreInfoAndUserInfo">
        UPDATE zxjp_message_template
        SET
        store_info = #{storeInfo,jdbcType=LONGVARCHAR},
        handle_person_info = #{userInfo,jdbcType=LONGVARCHAR},
        update_time = NOW(),
        publish_status = 1,
        publish_time = NOW(),
        publisher_user_id = #{userId}
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id,jdbcType=BIGINT}
        </foreach>
    </update>

    <update id="batchUpdatePublishStatus">
        UPDATE zxjp_message_template
        SET
        publish_status = 2
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id,jdbcType=BIGINT}
        </foreach>
    </update>

    <select id="getByKeyword" resultMap="BaseResultMap">
        SELECT
        *
        FROM zxjp_message_template
        WHERE deleted = 0
        <if test="keyword != null and keyword != ''">
            AND handle_keyword = #{keyword,jdbcType=VARCHAR}
        </if>
    </select>

</mapper>