<?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.clean.mapper.CleanMapper">

    <resultMap type="CleanVO" id="ECleanResult" autoMapping="true" />

    <sql id="selectECleanVo">
        select
            c.clean_id,
            c.user_id,
            c.user_name,
            c.user_phone,
            c.room_id,
            c.room_name,
            c.status,
            c.remark,
            c.start_time,
            c.end_time,
            c.actual_end_time,
            c.create_time,
            c.merchant_id,
            c.store_id,
            c.store_name,
            c.order_no,
            m.merchant_id,
            m.real_name merchantRealName,
            m.user_name merchantName
        from ss_clean c
        left join ss_user m on m.user_id = c.merchant_id
    </sql>

    <sql id="searchCondition">
        <if test="query.userId != null "> and c.user_id = #{query.userId}</if>
        <if test="query.userName != null  and query.userName != ''"> and c.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.userPhone != null  and query.userPhone != ''"> and c.user_phone = #{query.userPhone}</if>
        <if test="query.roomId != null "> and c.room_id = #{query.roomId}</if>
        <if test="query.roomName != null  and query.roomName != ''"> and c.room_name like concat('%', #{query.roomName}, '%')</if>
        <if test="query.status != null  and query.status != ''"> and c.status = #{query.status}</if>
        <if test="query.startTime != null "> and c.start_time = #{query.startTime}</if>
        <if test="query.endTime != null "> and c.end_time = #{query.endTime}</if>
        <if test="query.merchantId != null "> and c.merchant_id = #{query.merchantId}</if>
        <if test="query.storeId != null "> and c.store_id = #{query.storeId}</if>
        <if test="query.storeName != null  and query.storeName != ''"> and c.store_name like concat('%', #{query.storeName}, '%')</if>
        <if test="query.orderNo != null  and query.orderNo != ''"> and c.order_no = #{query.orderNo}</if>
        <if test="query.keywords != null and query.keywords != ''">
            AND (c.store_name LIKE CONCAT('%', #{keywords}, '%') OR c.room_name LIKE CONCAT('%', #{keywords}, '%'))
        </if>
    </sql>

    <select id="selectECleanList" parameterType="CleanQuery" resultMap="ECleanResult">
        <include refid="selectECleanVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by c.start_time desc
    </select>

    <select id="selectECleanByCleanId" parameterType="Long" resultMap="ECleanResult">
        <include refid="selectECleanVo"/>
        where c.clean_id = #{cleanId}
    </select>

    <insert id="insertEClean" parameterType="Clean">
        insert into ss_clean
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cleanId != null">clean_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="userPhone != null">user_phone,</if>
            <if test="roomId != null">room_id,</if>
            <if test="roomName != null">room_name,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="actualEndTime != null">actual_end_time,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="storeName != null">store_name,</if>
            <if test="orderNo != null">order_no,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cleanId != null">#{cleanId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="userPhone != null">#{userPhone},</if>
            <if test="roomId != null">#{roomId},</if>
            <if test="roomName != null">#{roomName},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="actualEndTime != null">#{actualEndTime},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="storeName != null">#{storeName},</if>
            <if test="orderNo != null">#{orderNo},</if>
         </trim>
    </insert>

    <update id="updateEClean" parameterType="Clean">
        update ss_clean
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where clean_id = #{data.cleanId}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.userName != null">user_name = #{data.userName},</if>
        <if test="data.userPhone != null">user_phone = #{data.userPhone},</if>
        <if test="data.roomId != null">room_id = #{data.roomId},</if>
        <if test="data.roomName != null">room_name = #{data.roomName},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.startTime != null">start_time = #{data.startTime},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
        <if test="data.actualEndTime != null">actual_end_time = #{data.actualEndTime},</if>
        <if test="data.merchantId != null">merchant_id = #{data.merchantId},</if>
    </sql>

    <delete id="deleteECleanByCleanId" parameterType="Long">
        delete from ss_clean where clean_id = #{cleanId}
    </delete>

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