<?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.bst.complaint.mapper.ComplaintMapper">

    <resultMap type="ComplaintVO" id="ComplaintResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="content"    column="content"    />
        <result property="type"    column="type"    />
        <result property="picture"    column="picture"    />
        <result property="contact"    column="contact"    />
    </resultMap>

    <sql id="selectComplaintVo">
        select
            bc.id,
            bc.user_id,
            bc.create_time,
            bc.content,
            bc.type,
            bc.picture,
            bc.contact,
            su.user_name
        from bst_complaint bc
        left join sys_user su on bc.user_id = su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and id = #{query.id}</if>
        <if test="query.userId != null "> and user_id = #{query.userId}</if>
        <if test="query.content != null  and query.content != ''"> and content = #{query.content}</if>
        <if test="query.type != null  and query.type != ''"> and type = #{query.type}</if>
        <if test="query.picture != null  and query.picture != ''"> and picture = #{query.picture}</if>
        <if test="query.contact != null  and query.contact != ''"> and contact like concat('%', #{query.contact}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and su.userName like concat('%', #{query.userName}, '%')</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectComplaintList" parameterType="ComplaintQuery" resultMap="ComplaintResult">
        <include refid="selectComplaintVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectComplaintById" parameterType="Long" resultMap="ComplaintResult">
        <include refid="selectComplaintVo"/>
        where id = #{id}
    </select>

    <insert id="insertComplaint" parameterType="Complaint" useGeneratedKeys="true" keyProperty="id">
        insert into bst_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>

    <insert id="batchInsert" parameterType="Complaint" useGeneratedKeys="true" keyProperty="id">
        insert into bst_complaint
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            create_time,
            content,
            type,
            picture,
            contact,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.content != null ">#{i.content},</if>
                <if test="i.content == null ">default,</if>
                <if test="i.type != null ">#{i.type},</if>
                <if test="i.type == null ">default,</if>
                <if test="i.picture != null ">#{i.picture},</if>
                <if test="i.picture == null ">default,</if>
                <if test="i.contact != null ">#{i.contact},</if>
                <if test="i.contact == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_complaint
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="user_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="content = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.content != null ">
                        WHEN #{item.id} THEN #{item.content}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `content`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="type = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.type != null ">
                        WHEN #{item.id} THEN #{item.type}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `type`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="picture = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.picture != null ">
                        WHEN #{item.id} THEN #{item.picture}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `picture`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="contact = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.contact != null ">
                        WHEN #{item.id} THEN #{item.contact}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `contact`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateComplaint" parameterType="Complaint">
        update bst_complaint
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.content != null">content = #{data.content},</if>
        <if test="data.type != null">type = #{data.type},</if>
        <if test="data.picture != null">picture = #{data.picture},</if>
        <if test="data.contact != null">contact = #{data.contact},</if>
    </sql>

    <delete id="deleteComplaintById" parameterType="Long">
        delete from bst_complaint where id = #{id}
    </delete>

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