<?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.ruoyi.frequency.appealdetail.mapper.AppealDetailMapper">
    
    <resultMap type="com.ruoyi.frequency.appealdetail.entity.AppealDetail" id="AppealDetailResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="appealId"    column="appeal_id"    />
        <result property="sendUserId"    column="send_user_id"    />
        <result property="sendUserType"    column="send_user_type"    />
        <result property="message"    column="message"    />
        <result property="images"    column="images"    />
        <result property="messageType"    column="message_type"    />
    </resultMap>

    <sql id="selectAppealDetailVo">
        SELECT
            t.id,
            t.create_by,
            t.create_time,
            t.update_by,
            t.update_time,
            t.del_flag,
            t.appeal_id,
            t.send_user_id,
            t.message,
            t.send_user_type,
            t.images,
            t.message_type
        FROM
            t_appeal_detail AS t
                LEFT JOIN t_store s on t.send_user_id = s.id and t.send_user_type = 2
                LEFT JOIN t_customer c on t.send_user_id = c.id and t.send_user_type = 1
                LEFT JOIN t_sale sa on t.send_user_id = sa.id and t.send_user_type = 3
                LEFT JOIN t_enterprise e on t.send_user_id = e.id and t.send_user_type = 4
                LEFT JOIN t_store es on e.id = es.bind_enterprise_id and t.send_user_type = 4
    </sql>

    <select id="selectAppealDetailList" parameterType="com.ruoyi.frequency.appealdetail.entity.AppealDetail" resultMap="AppealDetailResult">
        <include refid="selectAppealDetailVo"/>
        <where>  and t.del_flag != 2
            <if test="createTime != null "> and t.create_time = #{createTime}</if>
            <if test="appealId != null "> and t.appeal_id = #{appealId}</if>
            <if test="sendUserId != null "> and t.send_user_id = #{sendUserId}</if>
            <if test="message != null  and message != ''"> and t.message = #{message}</if>
            <if test="userInfo != null and userInfo.nickname != null  and userInfo.nickname != '' ">
                and (
                s.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                c.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                sa.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                e.name like concat('%', #{userInfo.nickname}, '%')
                )

            </if>
            <if test="userInfo != null and  userInfo.mobile != null and  userInfo.mobile != '' ">
                and (
                s.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                c.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                sa.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                es.mobile like concat('%', #{userInfo.mobile}, '%')
                )
            </if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
        </where>
        order by t.create_time desc
    </select>
    
    <select id="selectAppealDetailById" parameterType="Long" resultMap="AppealDetailResult">
        <include refid="selectAppealDetailVo"/>
        where t.id = #{id}
    </select>
        
    <insert id="insertAppealDetail" parameterType="com.ruoyi.frequency.appealdetail.entity.AppealDetail">
        insert into t_appeal_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="appealId != null">appeal_id,</if>
            <if test="sendUserId != null">send_user_id,</if>
            <if test="message != null">message,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="appealId != null">#{appealId},</if>
            <if test="sendUserId != null">#{sendUserId},</if>
            <if test="message != null">#{message},</if>
         </trim>
    </insert>

    <update id="updateAppealDetail" parameterType="com.ruoyi.frequency.appealdetail.entity.AppealDetail">
        update t_appeal_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="appealId != null">appeal_id = #{appealId},</if>
            <if test="sendUserId != null">send_user_id = #{sendUserId},</if>
            <if test="message != null">message = #{message},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppealDetailById" parameterType="Long">
        delete from t_appeal_detail where id = #{id}
    </delete>

    <delete id="deleteAppealDetailByIds" parameterType="String">
        delete from t_appeal_detail where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="appealMessagePage" resultType="com.ruoyi.controller.resp.AppealMessagePageResp">
        SELECT
            t.id,
            t.create_time,
            t.del_flag,
            t.appeal_id,
            t.send_user_id,
            t.send_user_type,
            t.message,
            t.images,
            t.message_type
        FROM
            t_appeal_detail AS t
        WHERE
            t.del_flag = 1
          AND t.appeal_id = #{req.appealId}
        <if test="req.beginTime != null "> AND t.create_time >= #{req.beginTime}</if>
        <if test="req.endTime != null "> AND t.create_time &lt;= #{req.endTime}</if>
        <if test="req.lasttimestamp != null">and t.create_time &lt;= #{req.lasttimestamp}</if>
        ORDER BY
            t.create_time desc
    </select>
</mapper>