<?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.system.mapper.CpsMeetingUsrMapper">

    <resultMap type="com.ruoyi.system.domain.CpsMeetingUsr" id="CpsMeetingUsrResult">
        <result property="id" column="id"/>
        <result property="usrName" column="usr_name"/>
        <result property="tel" column="tel"/>
        <result property="openId" column="open_id"/>
        <result property="enterpriseId" column="enterprise_id"/>
        <result property="tripId" column="trip_id"/>
        <result property="sex" column="sex"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <sql id="selectCpsMeetingUsrVo">
        SELECT usr.id,
               usr_name,
               usr.tel,
               open_id,
               usr.enterprise_id,
               usr.trip_id,
               trip.trip_name,
               usr.sex,
               avatar_url,
               enterprise.enterprise_name,
               usr.update_time,
               usr.create_time,
               usr.create_by,
               usr.update_by
        FROM cps_meeting_usr usr
                 LEFT JOIN cps_meeting_enterprise enterprise ON usr.enterprise_id = enterprise.id
                 LEFT JOIN cps_meeting_enterprise_trip_info trip ON usr.trip_id = trip.id
    </sql>

    <select id="selectCpsMeetingUsrList" parameterType="com.ruoyi.system.domain.CpsMeetingUsr"
            resultType="com.ruoyi.system.domain.CpsMeetingUsr">
        <include refid="selectCpsMeetingUsrVo"/>
        <where>
            <if test="usrName != null  and usrName != ''">and usr_name like concat('%', #{usrName}, '%')</if>
            <if test="sex != null  and sex != ''">and usr.sex = #{sex}</if>
            <if test="tel != null  and tel != ''">and usr.tel = #{tel}</if>
            <if test="openId != null  and openId != ''">and open_id = #{openId}</if>
            <if test="enterpriseId != null ">and usr.enterprise_id = #{enterpriseId}</if>
            <if test="tripId != null ">and usr.trip_id = #{tripId}</if>
        </where>
    </select>

    <select id="selectCpsMeetingUsrById" parameterType="Long" resultMap="CpsMeetingUsrResult">
        <include refid="selectCpsMeetingUsrVo"/>
        where usr.id = #{id}
    </select>

    <select id="selectCpsMeetingUsrByOpenId" parameterType="String" resultMap="CpsMeetingUsrResult">
        <include refid="selectCpsMeetingUsrVo"/>
        where usr.open_id = #{openId}
        limit 1
    </select>

    <insert id="insertCpsMeetingUsr" parameterType="com.ruoyi.system.domain.CpsMeetingUsr" useGeneratedKeys="true"
            keyProperty="id">
        insert into cps_meeting_usr
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="usrName != null">usr_name,</if>
            <if test="tel != null">tel,</if>
            <if test="openId != null">open_id,</if>
            <if test="enterpriseId != null">enterprise_id,</if>
            <if test="tripId != null">trip_id,</if>
            <if test="avatarUrl != null">avatar_url,</if>
            <if test="sex != null">sex,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="usrName != null">#{usrName},</if>
            <if test="tel != null">#{tel},</if>
            <if test="openId != null">#{openId},</if>
            <if test="enterpriseId != null">#{enterpriseId},</if>
            <if test="tripId != null">#{tripId},</if>
            <if test="avatarUrl != null">#{avatarUrl},</if>
            <if test="sex != null">#{sex},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateCpsMeetingUsr" parameterType="com.ruoyi.system.domain.CpsMeetingUsr">
        update cps_meeting_usr
        <trim prefix="SET" suffixOverrides=",">
            <if test="usrName != null">usr_name = #{usrName},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
            <if test="tripId != null">trip_id = #{tripId},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCpsMeetingUsrById" parameterType="Long">
        delete
        from cps_meeting_usr
        where id = #{id}
    </delete>

    <delete id="deleteCpsMeetingUsrByIds" parameterType="String">
        delete from cps_meeting_usr where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="exitUser" resultType="java.lang.Integer">
        -- 判断是否存在openid的数据
        select count(1)
        from cps_meeting_usr
        where tel = #{tel}
    </select>

    <select id="qryUsrByTel" resultType="com.ruoyi.system.domain.CpsMeetingUsr">
        <include refid="selectCpsMeetingUsrVo"/>
        where usr.tel = #{tel}
        limit 1
    </select>

    <update id="updateCpsMeetingUsrByTel" parameterType="com.ruoyi.system.domain.CpsMeetingUsr">
        update cps_meeting_usr
        <trim prefix="SET" suffixOverrides=",">
            <if test="usrName != null">usr_name = #{usrName},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
            <if test="tripId != null">trip_id = #{tripId},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where tel = #{tel}
    </update>
    <select id="qryUsrCollectContent" resultType="com.ruoyi.system.domain.CpsContentInfo">
        SELECT content.content_type,
        content.title,
        content.thumbnail,
        rel.update_time,
        content.id
        FROM cps_content_info content
        INNER JOIN cps_usr_content_rel rel ON content.id = rel.content_id
        WHERE rel.open_id = #{openId}
        <if test="keyword !=null">
            AND content.title LIKE CONCAT('%', #{keyword}, '%')
        </if>
        ORDER BY update_time DESC
    </select>
</mapper>
