<?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.ScenicMapper">
    <insert id="add">
        insert into scenic(id, `name`, introduction, phone, cover_url, longitude, latitude, radius, province, city, area, address, price, kf_code_url, kf_phone, logo_url, source_video_price, source_image_price)
        values (#{id}, #{name}, #{introduction}, #{phone}, #{coverUrl},#{longitude}, #{latitude}, #{radius}, #{province}, #{city}, #{area}, #{address}, #{price}, #{kfCodeUrl}, #{kfPhone}, #{logoUrl}, #{sourceVideoPrice}, #{sourceImagePrice})
    </insert>
    <insert id="addConfig">
        insert into scenic_config(id, scenic_id, create_time)
        values (#{id}, #{scenicId}, now())
    </insert>
    <update id="update">
        update
        scenic
        <set>
            <if test="name!=null and name!=''">
                `name`=#{name},
            </if>
            <if test="phone!=null and phone!=''">
                `phone`=#{phone},
            </if>
            <if test="introduction!=null and introduction!=''">
                introduction=#{introduction},
            </if>
            <if test="coverUrl!=null and coverUrl!=''">
                cover_url=#{coverUrl},
            </if>
            <if test="longitude!=null">
                longitude=#{longitude},
            </if>
            <if test="latitude!=null">
                latitude=#{latitude},
            </if>
            <if test="radius!=null">
                radius=#{radius},
            </if>
            <if test="province!=null and province!=''">
                province=#{province},
            </if>
            <if test="city!=null and city!=''">
                city=#{city},
            </if>
            <if test="area!=null and area!=''">
                area=#{area},
            </if>
            <if test="address!=null and address!=''">
                address=#{address},
            </if>
            <if test="kfCodeUrl!=null">
                kf_code_url=#{kfCodeUrl},
            </if>
            <if test="kfPhone!=null">
                kf_phone=#{kfPhone},
            </if>
            <if test="logoUrl!=null">
                logo_url=#{logoUrl},
            </if>
            <if test="price!=null">
                price=#{price},
            </if>
            <if test="sourceVideoPrice!=null">
                source_video_price=#{sourceVideoPrice},
            </if>
            <if test="sourceImagePrice!=null">
                source_image_price=#{sourceImagePrice},
            </if>
        </set>
            where id = #{id}
    </update>
    <update id="updateStatus">
        update
        scenic
        set status = (CASE
            status
            WHEN 1 THEN
                0
            WHEN 0 THEN
                1
        END)
            where id = #{id}
    </update>
    <update id="updateConfigById">
        update scenic_config
        <set>
            <if test="startTime!=null">
                start_time=#{startTime},
            </if>
            <if test="endTime!=null">
                end_time=#{endTime},
            </if>
            <if test="isDefault!=null">
                is_default=#{isDefault},
            </if>
            all_free=#{allFree},
            book_routine=#{bookRoutine},
            tour_time=#{tourTime},
            sample_store_day=#{sampleStoreDay},
            video_store_day=#{videoStoreDay},
            template_new_video_type=#{templateNewVideoType},
            anti_screen_record_type=#{antiScreenRecordType},
            disable_source_video=#{disableSourceVideo},
            disable_source_image=#{disableSourceImage},
            video_source_store_day=#{videoSourceStoreDay},
            image_source_store_day=#{imageSourceStoreDay},
            user_source_expire_day=#{userSourceExpireDay},
            face_score_threshold=#{faceScoreThreshold},
            force_finish_time=#{forceFinishTime},
            face_detect_helper_threshold=#{faceDetectHelperThreshold},
            store_type=#{storeType},
            store_config_json=#{storeConfigJson},
            broker_direct_rate=#{brokerDirectRate},
            watermark_type=#{watermarkType},
            watermark_scenic_text=#{watermarkScenicText},
            watermark_dt_format=#{watermarkDtFormat}
        </set>
        where id = #{id}
    </update>
    <delete id="deleteById">
        delete from scenic where id = #{id}
    </delete>
    <delete id="deleteConfigByScenicId">
        delete from scenic_config where scenic_id = #{scenicId}
    </delete>
    <select id="list" resultMap="scenic">
        select s.id, `name`, `phone`, introduction,logo_url,cover_url, longitude, latitude, radius, province, city, area, address, `status`, s.create_time, update_time, kf_code_url, kf_phone,
               s.price, s.source_video_price, s.source_image_price,
        (select scenic_account.account from scenic_account where scenic_account.scenic_id = s.id and scenic_account.is_super = 1 limit 1) as account,
        s.price
            from scenic s
        <where>
            <if test="name!=null and name!=''">
                and locate(#{name},`name`) > 0
            </if>
            <if test="province!=null and province!=''">
                and locate(#{province},`province`) > 0
            </if>
            <if test="city!=null and city!=''">
                and locate(#{city},`city`) > 0
            </if>
            <if test="area!=null and area!=''">
                and locate(#{area},`area`) > 0
            </if>
            <if test="status!=null">
                and `status` = #{status}
            </if>
            <if test="startTime!=null">
                and s.create_time &gt;= #{startTime}
            </if>
            <if test="endTime!=null">
                and s.create_time &lt;= #{endTime}
            </if>
        </where>
    </select>
    <select id="getById" resultMap="scenic">
        select s.id, `name`, `phone`, introduction, logo_url,cover_url, longitude, latitude, radius, province, city, area, address, `status`, s.create_time, update_time,
            s.price, s.source_video_price, s.source_image_price, kf_code_url, kf_phone
        from scenic s
        where s.id = #{id}
    </select>
    <select id="appList" resultType="com.ycwl.basic.model.mobile.scenic.ScenicAppVO">
        select s.id, `name`, `phone`, introduction,cover_url, longitude, latitude, radius, province, city, area, address
        from scenic s
        where
                `status` = 1
            <if test="name!=null and name!=''">
                and locate(#{name},`name`) > 0
            </if>
            <if test="province!=null and province!=''">
                and locate(#{province},`province`) > 0
            </if>
            <if test="city!=null and city!=''">
                and locate(#{city},`city`) > 0
            </if>
            <if test="area!=null and area!=''">
                and locate(#{area},`area`) > 0
            </if>
            <if test="startTime!=null">
                and s.create_time &gt;= #{startTime}
            </if>
            <if test="endTime!=null">
                and s.create_time &lt;= #{endTime}
            </if>
    </select>
    <select id="getAppById" resultType="com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO">
        select s.id, `name`, `phone`, introduction, logo_url,cover_url, longitude, latitude, radius, province, city, area, address,  kf_code_url, kf_phone
        from scenic s
        where `status` = 1 and s.id = #{id}
    </select>
    <select id="scenicListByLnLa" resultType="com.ycwl.basic.model.mobile.scenic.ScenicAppVO">
        select s.id,
               `name`,
               `phone`,
               introduction,
               cover_url,
               longitude,
               latitude,
               radius,
               province,
               city,
               area,
               address,
               (SELECT COUNT(1) FROM device WHERE scenic_id = s.id AND status = 1) as deviceNum,
               ifnull(
                       cast(
                               ST_Distance_Sphere(
                                       Point(longitude, latitude), Point(#{params.longitude}, #{params.latitude})
                               ) AS
                           DECIMAL(10, 2)
                       ), 0
               )                                                                   AS distance
        from scenic s
        where `status` = 1
        ORDER BY distance ASC
    </select>
    <select id="getConfig" resultType="com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity">
        select *
        from scenic_config
        where scenic_id = #{scenicId}
    </select>
    <select id="get" resultType="com.ycwl.basic.model.pc.scenic.entity.ScenicEntity">
        select *
        from scenic
        where id = #{id}
    </select>

    <resultMap id="scenic" type="com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="account" column="account"/>
        <result property="introduction" column="introduction"/>
        <result property="logoUrl" column="logo_url"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="radius" column="radius"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="area" column="area"/>
        <result property="address" column="address"/>
        <result property="status" column="status"/>
        <result property="price" column="price"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
</mapper>