<?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.system.mapper.SysSourceMapper">
    
    <resultMap type="SysSource" id="SysSourceResult">
        <result property="sourceId"    column="source_id"    />
        <result property="cityId"    column="city_id"    />
        <result property="city"    column="city"    />
        <result property="scenicId"    column="scenic_id"    />
        <result property="scenic"    column="scenic"    />
        <result property="part"    column="part"    />
        <result property="scale"    column="scale"    />
        <result property="coordinate"    column="coordinate"    />
        <result property="price"    column="price"    />
        <result property="sourceStatus"    column="source_status"    />
        <result property="workAddress"    column="work_address"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="bBought"    column="b_bought"    />
        <result property="orderId"    column="order_id"    />
        <result property="workId" column="work_id"/>
        <result property="cX" column="c_x"/>
        <result property="cY" column="c_y"/>
    </resultMap>

    <resultMap id="SourceListDtoResult" type="SourceListDto">
        <result property="sourceId"    column="source_id"    />
        <result property="city"    column="city"    />

        <result property="scenic"    column="scenic"    />
        <result property="part"    column="part"    />
        <result property="scale"    column="scale"    />

        <result property="price"    column="price"    />
        <result property="sourceStatus"    column="source_status"    />
        <result property="workAddress"    column="work_address"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="bBought"    column="b_bought"    />
        <result property="coordinate" column="coordinate" />
        <result property="cX" column="c_x"/>
        <result property="cY" column="c_y"/>

        <result property="workId" column="work_id"/>

    </resultMap>

    <resultMap id="HotSourceDtoResult" type="HotSourceDto">
        <result property="city"    column="city"    />
        <result property="scenic"    column="scenic"    />
        <result property="part"    column="part"    />
        <result property="sourceId" column="source_id"/>
        <result property="scale" column="scale"/>
        <result property="bBought" column="b_bought"/>
        <result property="coordinate" column="coordinate"/>
        <result property="cX" column="c_x"/>
        <result property="cY" column="c_y"/>

    </resultMap>

    <resultMap id="UserPublishWorkSourceListDtoResult" type="UserPublishWorkSourceListDto">
        <result property="sourceId"    column="source_id"    />
        <result property="city"    column="city"    />
        <result property="scenic"    column="scenic"    />
        <result property="part"    column="part"    />
        <result property="coordinate"    column="coordinate"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />

    </resultMap>

    <resultMap id="ShowSysSourceARListDtoResult" type="ShowSysSourceARListDto">
        <result property="sourceId"    column="source_id"    />
        <result property="cX"    column="c_x"    />
        <result property="cY"    column="c_y"    />
        <result property="sourceStatus"    column="source_status"    />
        <result property="part"    column="part"    />
        <result property="workAddress"    column="work_address"    />
        <result property="workName"    column="work_name"    />
        <result property="detail"    column="detail"    />
        <result property="workId" column="work_id"/>
    </resultMap>

    <resultMap id="SourceCoordinateDtoResult" type="SourceCoordinateDto">
        <result property="part"    column="part"    />
        <result property="cY"    column="c_y"    />
        <result property="cX"    column="c_x"    />
    </resultMap>


    <sql id="selectSysSourceVo">
        select source_id, city_id, city, scenic_id, scenic, part, scale, coordinate, price, source_status, work_address, start_time, end_time, b_bought, order_id, work_id, c_x, c_y from sys_source
    </sql>

    <select id="selectSysSourceList" parameterType="SysSource" resultMap="SysSourceResult">
        <include refid="selectSysSourceVo"/>
        <where>  
            <if test="cityId != null "> and city_id = #{cityId}</if>
            <if test="city != null  and city != ''"> and city = #{city}</if>
            <if test="scenicId != null "> and scenic_id = #{scenicId}</if>
            <if test="scenic != null  and scenic != ''"> and scenic = #{scenic}</if>
            <if test="part != null  and part != ''"> and part = #{part}</if>
            <if test="scale != null  and scale != ''"> and scale = #{scale}</if>
            <if test="coordinate != null  and coordinate != ''"> and coordinate = #{coordinate}</if>
            <if test="price != null  and price != ''"> and price = #{price}</if>
            <if test="sourceStatus != null  and sourceStatus != ''"> and source_status = #{sourceStatus}</if>
            <if test="workAddress != null  and workAddress != ''"> and work_address = #{workAddress}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="bBought != null "> and b_bought = #{bBought}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
             <if test="workId != null "> and work_id = #{workId}</if>
<!--              <if test="cX != null "> and c_x = #{cX}</if>-->
<!--               <if test="cY != null "> and c_y = #{cY}</if>-->
        </where>
    </select>
    
    <select id="selectSysSourceBySourceId" parameterType="Long" resultMap="SysSourceResult">
        <include refid="selectSysSourceVo"/>
        where source_id = #{sourceId}
    </select>
    <select id="getSysSourceList"  resultMap="SourceListDtoResult" parameterType="SysSource">
        select s.source_id, s.city, s.scenic, s.part, s.scale, s.price, s.source_status, s.work_address, o.start_time, o.end_time, s.b_bought, s.coordinate, s.c_x, s.c_y, s.work_id
        from sys_source s
        left join sys_order o
        on s.order_id = o.order_id
        <where>
            <if test="sourceId != null "> and s.source_id = #{sourceId}</if>
<!--&#45;&#45;             <if test="cityId != null "> and city_id = #{cityId}</if>-->
            <if test="city != null  and city != ''"> and city = #{city}</if>
<!--&#45;&#45;             <if test="scenicId != null "> and scenic_id = #{scenicId}</if>-->
            <if test="scenic != null  and scenic != ''"> and scenic = #{scenic}</if>
            <if test="part != null  and part != ''"> and part = #{part}</if>
<!--&#45;&#45;             <if test="scale != null  and scale != ''"> and scale = #{scale}</if>-->
<!--&#45;&#45;             <if test="coordinate != null  and coordinate != ''"> and coordinate = #{coordinate}</if>-->
<!--&#45;&#45;             <if test="price != null  and price != ''"> and price = #{price}</if>-->
            <if test="sourceStatus != null  and sourceStatus != ''"> and source_status = #{sourceStatus}</if>
<!--&#45;&#45;             <if test="workAddress != null  and workAddress != ''"> and work_address = #{workAddress}</if>-->
<!--&#45;&#45;             <if test="startTime != null "> and start_time = #{startTime}</if>-->
<!--&#45;&#45;             <if test="endTime != null "> and end_time = #{endTime}</if>-->
<!--&#45;&#45;             <if test="bBought != null "> and b_bought = #{bBought}</if>-->
<!--&#45;&#45;             <if test="orderId != null "> and order_id = #{orderId}</if>-->
<!--             <if test="workId != null "> and work_id = #{workId}</if>-->
              <if test="cX != -1 "> and c_x = #{cX}</if>
              <if test="cY != -1 "> and c_y = #{cY}</if>
        </where>


    </select>
    <select id="getHotSource" resultMap="HotSourceDtoResult">
        select city, scenic, part, source_id, scale, b_bought, coordinate, c_x, c_y
        from sys_source
        order by b_bought desc
        limit 6
    </select>
    <select id="getUserPublishWorkSourceList"  resultMap="UserPublishWorkSourceListDtoResult">

        select source_id, city, scenic, part, coordinate, start_time, end_time
        from sys_source
        where work_id = #{workId}


    </select>
    <select id="getSysSourceARList" resultMap="ShowSysSourceARListDtoResult">

        select s.source_id, s.c_x, s.c_y, s.source_status, s.part, s.work_address, w.work_name, w.detail
        from sys_source s
        left join user_work w
        on s.work_id = w.work_id

        <where>
        <if test="cityId != null "> and city_id = #{cityId}</if>
        <if test="city != null  and city != ''"> and s.city = #{city}</if>
        <if test="scenicId != null "> and scenic_id = #{scenicId}</if>
        <if test="scenic != null  and scenic != ''"> and s.scenic = #{scenic}</if>
        <if test="part != null  and part != ''"> and s.part = #{part}</if>
        <if test="scale != null  and scale != ''"> and scale = #{scale}</if>
        <if test="coordinate != null  and coordinate != ''"> and coordinate = #{coordinate}</if>
        <if test="price != null  and price != ''"> and price = #{price}</if>
        <if test="sourceStatus != null  and sourceStatus != ''"> and source_status = #{sourceStatus}</if>
        <if test="workAddress != null  and workAddress != ''"> and work_address = #{workAddress}</if>
        <if test="startTime != null "> and start_time = #{startTime}</if>
        <if test="endTime != null "> and end_time = #{endTime}</if>
        <if test="bBought != null "> and b_bought = #{bBought}</if>
        <if test="orderId != null "> and order_id = #{orderId}</if>
        <if test="workId != null "> and s.work_id = #{workId}</if>
        </where>
    </select>
    <select id="getARSource" resultMap="ShowSysSourceARListDtoResult">
        select s.source_id, s.c_x, s.c_y, s.source_status, s.part, s.work_address, w.work_name, w.detail, s.work_id
        from sys_source s
        left join user_work w
        on s.work_id = w.work_id

        <where>

            <if test="city != null  and city != ''"> and s.city = #{city}</if>

            <if test="scenic != null  and scenic != ''"> and s.scenic = #{scenic}</if>
            <if test="coordinates != null and !coordinates.isEmpty()">
                AND (s.c_x, s.c_y) IN
                <foreach
                        collection="coordinates"
                        item="point"
                        open="("
                        separator=","
                        close=")"
                >
                    <!-- 使用 _parameter 直接访问当前项 -->
                    (#{point.x}, #{point.y})
                </foreach>
            </if>

<!--            <if test="coordinate != null  and coordinate != ''"> and coordinate = #{coordinate}</if>-->
<!--            <if test="c != null  and part != ''"> and s.part = #{part}</if>-->

        </where>

    </select>
    <select id="getSourceCountByCity" resultType="java.lang.Long">
        select count(*) from sys_source where city = #{city}
    </select>
    <select id="getSourceCoordinateByOrderId" resultMap="SourceCoordinateDtoResult">
        select s.c_x, s.c_y, s.part
        from sys_order so
        left join order_info oi on so.order_info_id = oi.order_info_id
        left join sys_source s on oi.source_id = s.source_id
        where so.order_id = #{orderId}
    </select>
    <select id="getSysSourceByWorkId" resultMap="SysSourceResult">
        select * from sys_source where work_id = #{workId}
    </select>


    <insert id="insertSysSource" parameterType="SysSource" useGeneratedKeys="true" keyProperty="sourceId">
        insert into sys_source
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cityId != null">city_id,</if>
            <if test="city != null and city != ''">city,</if>
            <if test="scenicId != null">scenic_id,</if>
            <if test="scenic != null and scenic != ''">scenic,</if>
            <if test="part != null and part != ''">part,</if>
            <if test="scale != null and scale != ''">scale,</if>
            <if test="coordinate != null and coordinate != ''">coordinate,</if>
            <if test="price != null">price,</if>
            <if test="sourceStatus != null">source_status,</if>
            <if test="workAddress != null">work_address,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="bBought != null">b_bought,</if>
            <if test="orderId != null">order_id,</if>
            <if test="workId != null">work_id,</if>
            <if test="cX != null">c_x,</if>
            <if test="cY != null">c_y,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cityId != null">#{cityId},</if>
            <if test="city != null and city != ''">#{city},</if>
            <if test="scenicId != null">#{scenicId},</if>
            <if test="scenic != null and scenic != ''">#{scenic},</if>
            <if test="part != null and part != ''">#{part},</if>
            <if test="scale != null and scale != ''">#{scale},</if>
            <if test="coordinate != null and coordinate != ''">#{coordinate},</if>
            <if test="price != null">#{price},</if>
            <if test="sourceStatus != null">#{sourceStatus},</if>
            <if test="workAddress != null">#{workAddress},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="bBought != null">#{bBought},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="workId != null">#{workId},</if>
            <if test="cX != null">#{cX},</if>
            <if test="cY != null">#{cY},</if>
         </trim>
    </insert>

    <update id="updateSysSource" parameterType="SysSource">
        update sys_source
        <trim prefix="SET" suffixOverrides=",">
            <if test="cityId != null">city_id = #{cityId},</if>
            <if test="city != null and city != ''">city = #{city},</if>
            <if test="scenicId != null">scenic_id = #{scenicId},</if>
            <if test="scenic != null and scenic != ''">scenic = #{scenic},</if>
            <if test="part != null and part != ''">part = #{part},</if>
            <if test="scale != null and scale != ''">scale = #{scale},</if>
            <if test="coordinate != null and coordinate != ''">coordinate = #{coordinate},</if>
            <if test="price != null">price = #{price},</if>
            <if test="sourceStatus != null">source_status = #{sourceStatus},</if>
            <if test="workAddress != null">work_address = #{workAddress},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="bBought != null">b_bought = #{bBought},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="workId != null">work_id = #{workId},</if>
            <if test="cX != null">c_x = #{cX},</if>
            <if test="cY != null">c_y = #{cY},</if>
        </trim>
        where source_id = #{sourceId}
    </update>

    <update id="updateDefaultSourceWorkAddress" parameterType="String">
        update sys_source set work_address = #{workAddress} where city = #{city} and scenic = #{scenic} and part = #{part} and source_status = #{sourceStatus}
    </update>
    <update id="updateDefaultSourcePrice">
        update sys_source set price = #{price} where city = #{city} and scenic = #{scenic} and part = #{part} and source_status = "0"
    </update>
    <update id="updateSysSourceByCP" parameterType="SysSource">
        update sys_source set work_address = #{workAddress} where city = #{city} and scenic = #{scenic} and part = #{part} and source_status = "0"
    </update>

    <delete id="deleteSysSourceBySourceId" parameterType="Long">
        delete from sys_source where source_id = #{sourceId}
    </delete>

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