<?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="cn.reghao.tnb.content.app.geo.db.mapper.CameraPhotoMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into geo_camera_photo
        (`type`,`album_id`,`object_id`,`channel_id`,`photo_url`,`manufacturer`,`model`,`software`,`shot_at`,`geo`)
        values 
        (#{type},#{albumId},#{objectId},#{channelId},#{photoUrl},#{manufacturer},#{model},#{software},#{shotAt},point(#{longitude},#{latitude}))
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert ignore into geo_camera_photo
        (`type`,`album_id`,`object_id`,`channel_id`,`photo_url`,`manufacturer`,`model`,`software`,`shot_at`,`geo`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.type},#{item.albumId},#{item.objectId},#{item.channelId},#{item.photoUrl},#{item.manufacturer},#{item.model},#{item.software},#{item.shotAt},point(#{item.longitude},#{item.latitude}))
        </foreach>
    </insert>

    <update id="updateSetAlbumId">
        update geo_camera_photo
        set album_id=#{albumId}
        where object_id=#{objectId}
    </update>

    <delete id="delete">
        delete from geo_camera_photo
        where id=#{id}
    </delete>

    <select id="findById" resultType="cn.reghao.tnb.content.app.geo.model.po.CameraPhoto">
        select *
        from geo_camera_photo
        where id=#{id}
    </select>
    <select id="findAll" resultType="cn.reghao.tnb.content.app.geo.model.po.CameraPhoto">
        select *
        from geo_camera_photo
    </select>

    <resultMap id="mapMarker" type="cn.reghao.tnb.content.app.geo.model.vo.MapMarker">
        <result property="id" column="id"/>
        <result property="title" column="cup"/>
        <association property="position" javaType="cn.reghao.tnb.content.app.geo.model.vo.MapPoint">
            <result property="lng" column="lng"/>
            <result property="lat" column="lat"/>
        </association>
    </resultMap>
    <select id="findByWestGeoGroup" resultMap="mapMarker">
        select id, manufacturer as cup,x(geo) as lng,y(geo) as lat
        from geo_camera_photo
        where `type`=#{itemType}
        group by geo
        order by shot_at desc
        limit #{pageSize}
    </select>
    <select id="findAllByReplyIdGroup" resultMap="mapMarker">
        select photo.id,photo.manufacturer as cup,photo.photo_url,x(photo.geo) as lng,y(photo.geo) as lat
        from geo_camera_photo photo
        inner join geo_mall_reply reply
        on photo.album_id=reply.reply_id
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <!-- 由西向东升序 -->
    <select id="findByWestChina" resultMap="mapMarker">
        select photo.id,reply.cup,photo.photo_url,x(photo.geo) as lng,y(photo.geo) as lat
        from geo_camera_photo photo
        inner join geo_mall_reply reply
        on photo.album_id=reply.reply_id
        and reply.item_type=#{itemType}
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <select id="findCommentMarkerInfo" resultType="cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo">
        select reply.item_id,reply.reply_id,reply.extra as sku,reply.reply_content,reply.append_content,
        photo.object_id as upload_id,photo.channel_id,photo.photo_url
        from geo_camera_photo photo
        inner join geo_mall_reply reply
        on photo.album_id=reply.reply_id and photo.id=#{id}
    </select>
    <select id="findMarkerInfo" resultType="cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo">
        select object_id as upload_id,channel_id,photo_url
        from geo_camera_photo
        where id=#{id}
    </select>
    <select id="findAlbumMarkerInfo" resultType="cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo">
        select object_id as upload_id,album_id as item_id,channel_id,photo_url
        from geo_camera_photo
        where id=#{id}
    </select>
</mapper>