<?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.ss.complaint.mapper.SmComplaintMapper">

    <resultMap type="SmComplaintVo" id="SmComplaintResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="content"    column="content"    />
        <result property="userName"    column="user_name"    />
        <result property="type"    column="type"    />
        <result property="picture"    column="picture"    />
        <result property="contact"    column="contact"    />
    </resultMap>

    <sql id="selectSmComplaintVo">
        select
            sc.id,
            sc.user_id,
            sc.create_time,
            sc.content,
            sc.type,
            sc.picture,
            sc.contact,
            su.user_name user_name
        from <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        sm_complaint sc
            left join sm_user su on su.user_id = sc.user_id
    </sql>

    <sql id="searchCondition">
        <if test="userId != null "> and sc.user_id = #{userId}</if>
        <if test="content != null  and content != ''"> and sc.content = #{content}</if>
        <if test="userName != null "> and su.user_name like concat('%', #{userName}, '%')</if>
        <if test="type != null "> and sc.type = #{type}</if>
    </sql>

    <select id="selectSmComplaintList" parameterType="SmComplaintQuery" resultMap="SmComplaintResult">
        <include refid="selectSmComplaintVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by sc.create_time desc
    </select>

    <select id="selectSmComplaintById" parameterType="Long" resultMap="SmComplaintResult">
        <include refid="selectSmComplaintVo"/>
        where sc.id = #{id}
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(sc.id)
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertSmComplaint" parameterType="SmComplaint" useGeneratedKeys="true" keyProperty="id">
        insert into sm_complaint
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="content != null">content,</if>
            <if test="type != null">`type`,</if>
            <if test="picture != null">picture,</if>
            <if test="contact != null">contact,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="content != null">#{content},</if>
            <if test="type != null">#{type},</if>
            <if test="picture != null">#{picture},</if>
            <if test="contact != null">#{contact},</if>
         </trim>
    </insert>

    <update id="updateSmComplaint" parameterType="SmComplaint">
        update sm_complaint
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="content != null">content = #{content},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="picture != null">picture = #{picture},</if>
            <if test="contact != null">contact = #{contact},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSmComplaintById" parameterType="Long">
        delete from sm_complaint where id = #{id}
    </delete>

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