<?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.ycwl.basic.mapper.BrokerMapper">
    <insert id="add">
        insert into broker(scenic_id, `name`, phone, status, broker_enable, broker_rate, create_at, update_at) values (#{scenicId}, #{name}, #{phone}, 1, #{brokerEnable}, #{brokerRate}, now(), now())
    </insert>
    <update id="update">
        update broker set `name` = #{name}, phone = #{phone}, broker_enable = #{brokerEnable}, broker_rate = #{brokerRate}, update_at = now() where id = #{id}
    </update>
    <update id="updateStatus">
        update broker
        set status = (CASE
            status
                          WHEN 1 THEN
                              0
                          WHEN 0 THEN
                              1
                          ELSE 1
            END)
        where id = #{id}
    </update>
    <update id="updateBrokerEnable">
        update broker
        set broker_enable = (CASE
            broker_enable
                          WHEN 1 THEN
                              0
                          WHEN 0 THEN
                              1
                          ELSE 1
            END)
        where id = #{id}
    </update>
    <delete id="deleteById">
        delete from broker where id = #{id}
    </delete>
    <select id="list" resultType="com.ycwl.basic.model.pc.broker.resp.BrokerRespVO">
        select b.id, scenic_id, s.name as scenicName, b.`name`, b.phone, b.broker_enable, b.broker_rate, b.status,
               (select count(1) from statistics s where s.type = 20 and s.morph_id = b.id) as broker_scan_count,
               (select count(1) from broker_record r where r.broker_id = b.id) as broker_order_count,
               (select sum(order_price) from broker_record r where r.broker_id = b.id) as broker_order_amount,
               (select min(r.create_time) from broker_record r where r.broker_id = b.id) as first_broker_date,
               (select max(r.create_time) from broker_record r where r.broker_id = b.id) as last_broker_date,
               b.create_at, b.update_at
        from broker b left join scenic s on b.scenic_id = s.id
        <where>
            <if test="scenicId!= null">
                and b.scenic_id = #{scenicId}
            </if>
            <if test="name!= null and name!= ''">
                and b.`name` like concat('%', #{name}, '%')
            </if>
            <if test="phone!= null and phone!= ''">
                and b.`phone` like concat('%', #{phone}, '%')
            </if>
            <if test="status!= null">
                and b.`status` = #{status}
            </if>
            <if test="startTime!=null">
                and b.create_at >= #{startTime}
            </if>
            <if test="endTime!=null">
                and b.create_at &lt;= #{endTime}
            </if>
        </where>
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.broker.resp.BrokerRespVO">
        select b.id, scenic_id, s.name as scenicName, b.`name`, b.phone, b.broker_enable, b.broker_rate, b.status, b.create_at, b.update_at
        from broker b left join scenic s on b.scenic_id = s.id
        where b.id = #{id}
    </select>
</mapper>