<?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.customerService.mapper.CustomerServiceMapper">

    <resultMap type="CustomerServiceVO" id="CustomerServiceResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="name"    column="name"    />
        <result property="contact"    column="contact"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="isEnabled"    column="is_enabled"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCustomerServiceVo">
        select
            bcs.id,
            bcs.area_id,
            bcs.name,
            bcs.contact,
            bcs.start_time,
            bcs.end_time,
            bcs.is_enabled,
            bcs.create_time,
            bcs.store_id,
            bcs.deleted,
            su.agent_id,
            ba.name as area_name
        from <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        bst_customer_service bcs
            left join sys_user su on bcs.store_id = su.user_id
            left join bst_area ba on bcs.area_id = ba.id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bcs.id = #{query.id}</if>
        <if test="query.areaId != null "> and bcs.area_id = #{query.areaId}</if>
        <if test="query.storeId != null "> and bcs.store_id = #{query.storeId}</if>
        <if test="query.name != null  and query.name != ''"> and bcs.name like concat('%', #{query.name}, '%')</if>
        <if test="query.contact != null  and query.contact != ''"> and bcs.contact like concat('%',#{query.contact},'%')</if>
        <if test="query.startTime != null "> and bcs.start_time = #{query.startTime}</if>
        <if test="query.endTime != null "> and bcs.end_time = #{query.endTime}</if>
        <if test="query.isEnabled != null "> and bcs.is_enabled = #{query.isEnabled}</if>
        <if test="query.deleted != null "> and bcs.deleted = #{query.deleted}</if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
            .userSetAlias("bcs.store_id,su.agent_id")
            .areaAlias("bcs.area_id", query.areaPermissions)
            .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectCustomerServiceList" parameterType="CustomerServiceQuery" resultMap="CustomerServiceResult">
        <include refid="selectCustomerServiceVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCustomerServiceById" parameterType="Long" resultMap="CustomerServiceResult">
        <include refid="selectCustomerServiceVo"/>
        where bcs.id = #{id}
    </select>

    <insert id="insertCustomerService" parameterType="CustomerService" useGeneratedKeys="true" keyProperty="id">
        insert into bst_customer_service
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="name != null">name,</if>
            <if test="contact != null and contact != ''">contact,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isEnabled != null">is_enabled,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="name != null">#{name},</if>
            <if test="contact != null and contact != ''">#{contact},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="isEnabled != null">#{isEnabled},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="CustomerService" useGeneratedKeys="true" keyProperty="id">
        insert into bst_customer_service
        <trim prefix="(" suffix=")" suffixOverrides=",">
            area_id,
            name,
            contact,
            start_time,
            end_time,
            is_enabled,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.areaId != null ">#{i.areaId},</if>
                <if test="i.areaId == null ">default,</if>
                <if test="i.name != null ">#{i.name},</if>
                <if test="i.name == null ">default,</if>
                <if test="i.contact != null  and i.contact != ''">#{i.contact},</if>
                <if test="i.contact == null  or i.contact == ''">default,</if>
                <if test="i.startTime != null ">#{i.startTime},</if>
                <if test="i.startTime == null ">default,</if>
                <if test="i.endTime != null ">#{i.endTime},</if>
                <if test="i.endTime == null ">default,</if>
                <if test="i.isEnabled != null ">#{i.isEnabled},</if>
                <if test="i.isEnabled == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_customer_service
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="area_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.areaId != null ">
                        WHEN #{item.id} THEN #{item.areaId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `area_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="name = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.name != null ">
                        WHEN #{item.id} THEN #{item.name}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="contact = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.contact != null  and item.contact != ''">
                        WHEN #{item.id} THEN #{item.contact}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `contact`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="start_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.startTime != null ">
                        WHEN #{item.id} THEN #{item.startTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `start_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="end_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.endTime != null ">
                        WHEN #{item.id} THEN #{item.endTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `end_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="is_enabled = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.isEnabled != null ">
                        WHEN #{item.id} THEN #{item.isEnabled}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `is_enabled`
                    </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>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateCustomerService" parameterType="CustomerService">
        update bst_customer_service
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <!--logicDel-->
    <update id="logicDel">
        update bst_customer_service
        set deleted = true
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and deleted = false
    </update>

    <sql id="updateColumns">
        <if test="data.areaId != null">area_id = #{data.areaId},</if>
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.name != null">name = #{data.name},</if>
        <if test="data.contact != null and data.contact != ''">contact = #{data.contact},</if>
        <if test="data.startTime != null">start_time = #{data.startTime},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
        <if test="data.isEnabled != null">is_enabled = #{data.isEnabled},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteCustomerServiceById" parameterType="Long">
        delete from bst_customer_service where id = #{id}
    </delete>

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

    <!-- selectIdByQuery --> 

    <select id="selectIdByQuery">
        select bcs.id 
        from <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>
    
</mapper>
