<?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.message.frequency.goeasymessagenew.mapper.GoeasyMessageNewMapper">

    <resultMap type="com.ruoyi.message.frequency.goeasymessagenew.entity.GoeasyMessageNew" id="GoeasyMessageNewResult">
        <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="userId"    column="user_id"    />
        <result property="otherUserId"    column="other_user_id"    />
        <result property="payload"    column="payload"    />
        <result property="goeasyTime"    column="goeasy_time"    />
        <result property="type"    column="type"    />
        </resultMap>

    
    
    <sql id="selectGoeasyMessageNewVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.user_id ,
            t.other_user_id ,
            t.payload ,
            t.goeasy_time ,
            t.type 
        from t_goeasy_message_new AS t
                
    </sql>

    <select id="selectGoeasyMessageNewList" parameterType="com.ruoyi.message.frequency.goeasymessagenew.entity.GoeasyMessageNew" resultMap="GoeasyMessageNewResult">
        <include refid="selectGoeasyMessageNewVo"/>
        <where>AND t.del_flag != 2
            AND (
            t.user_id in (select id from t_customer UNION ALL select id from t_store)
            )
            <if test="payload != null  and payload != ''">and t.payload = #{payload}</if>
            <if test="goeasyTime != null ">and t.goeasy_time = #{goeasyTime}</if>
            <if test="type != null  and type != ''">and t.type = #{type}</if>
            <if test="delFlag != null and delFlag != ''">and t.del_flag = #{delFlag}</if>

            <if test="userInfo != null ">
                <if test="userInfo.nickname != null and userInfo.nickname  != ''">
                    AND (
                    (
                        t.user_id IN (
                        SELECT
                        id
                        FROM
                        t_customer
                        WHERE
                        nickname LIKE CONCAT('%',#{userInfo.nickname},'%')
                        )
                        OR t.user_id IN (
                        SELECT
                        id
                        FROM
                        t_store
                        WHERE
                        nickname LIKE CONCAT('%',#{userInfo.nickname},'%')
                        )
                    ) or
                    (
                        t.other_user_id IN (
                        SELECT
                        id
                        FROM
                        t_customer
                        WHERE
                        nickname LIKE CONCAT('%',#{userInfo.nickname},'%')
                        )
                        OR t.other_user_id IN (
                        SELECT
                        id
                        FROM
                        t_store
                        WHERE
                        nickname LIKE CONCAT('%',#{userInfo.nickname},'%')
                        )
                    )
                    )
                </if>
                <if test="userInfo.mobile != null and userInfo.mobile  != ''">
                    AND (
                    (
                    t.user_id IN (
                    SELECT
                    id
                    FROM
                    t_customer
                    WHERE
                    mobile LIKE CONCAT('%',#{userInfo.mobile},'%')
                    )
                    OR t.user_id IN (
                    SELECT
                    id
                    FROM
                    t_store
                    WHERE
                    mobile LIKE CONCAT('%',#{userInfo.mobile},'%')
                    )
                    ) or
                    (
                    t.other_user_id IN (
                    SELECT
                    id
                    FROM
                    t_customer
                    WHERE
                    mobile LIKE CONCAT('%',#{userInfo.mobile},'%')
                    )
                    OR t.other_user_id IN (
                    SELECT
                    id
                    FROM
                    t_store
                    WHERE
                    mobile LIKE CONCAT('%',#{userInfo.mobile},'%')
                    )
                    )
                    )
                </if>
            </if>
        </where>
        order by t.goeasy_time desc
    </select>

    <select id="selectGoeasyMessageNewById" parameterType="Long" resultMap="GoeasyMessageNewResult">
        <include refid="selectGoeasyMessageNewVo"/>
        where t.id = #{id}
    </select>
    <select id="getMessage" resultType="com.ruoyi.message.frequency.goeasymessagenew.entity.GoeasyMessageNew">
        SELECT *
        FROM t_goeasy_message_new
        WHERE (
                    user_id = #{userId}
                AND other_user_id = #{otherUserId}
            )
           OR (
                    user_id = #{otherUserId}
                AND other_user_id = #{userId}
            )
    </select>

    <insert id="insertGoeasyMessageNew" parameterType="com.ruoyi.message.frequency.goeasymessagenew.entity.GoeasyMessageNew">
        insert into t_goeasy_message_new
        <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="userId != null">user_id,</if>
            <if test="otherUserId != null">other_user_id,</if>
            <if test="payload != null">payload,</if>
            <if test="goeasyTime != null">goeasy_time,</if>
            <if test="type != null">type,</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="userId != null">#{userId},</if>
            <if test="otherUserId != null">#{otherUserId},</if>
            <if test="payload != null">#{payload},</if>
            <if test="goeasyTime != null">#{goeasyTime},</if>
            <if test="type != null">#{type},</if>
        </trim>
    </insert>

    <update id="updateGoeasyMessageNew" parameterType="com.ruoyi.message.frequency.goeasymessagenew.entity.GoeasyMessageNew">
        update t_goeasy_message_new
        <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="userId != null">user_id = #{userId},</if>
            <if test="otherUserId != null">other_user_id = #{otherUserId},</if>
            <if test="payload != null">payload = #{payload},</if>
            <if test="goeasyTime != null">goeasy_time = #{goeasyTime},</if>
            <if test="type != null">type = #{type},</if>
        </trim>
        where id = #{id}
    </update>
                                                                                                                        </mapper>