<?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
        (`album_type`,`album_id`,`object_id`,`channel_code`,`photo_url`,`manufacturer`,`model`,`software`,`shot_at`,`geo`,`geo_valid`,`area_code`)
        values 
        (#{albumType},#{albumId},#{objectId},#{channelCode},#{photoUrl},#{manufacturer},#{model},#{software},#{shotAt},point(#{longitude},#{latitude}),#{geoValid},#{areaCode})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert ignore into geo_camera_photo
        (`album_type`,`album_id`,`object_id`,`channel_code`,`photo_url`,`manufacturer`,`model`,`software`,`shot_at`,`geo`,`geo_valid`,`area_code`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.albumType},#{item.albumId},#{item.objectId},#{item.channelCode},#{item.photoUrl},
            #{item.manufacturer},#{item.model},#{item.software},#{item.shotAt},point(#{item.longitude},#{item.latitude}),#{item.geoValid},#{item.areaCode})
        </foreach>
    </insert>

    <update id="updateSetAreaCode">
        update geo_camera_photo
        set area_code=#{areaCode}
        where object_id=#{objectId}
    </update>

    <resultMap id="mapMarker" type="cn.reghao.tnb.content.app.geo.model.vo.MapMarker">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="distance" column="distance"/>
        <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="findAlbumIdGroup" resultMap="mapMarker">
        select photo.id,photo.manufacturer as title,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 photo.geo_valid=1
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <select id="findByImagePost" resultMap="mapMarker">
        select photo.id,photo.manufacturer as title,photo.photo_url,x(photo.geo) as lng,y(photo.geo) as lat
        from geo_camera_photo photo
        where photo.album_type='ImagePost' and photo.geo_valid=1
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <select id="findByMallReply" resultMap="mapMarker">
        select photo.id,photo.manufacturer as title,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 photo.geo_valid=1 and photo.album_type='MallReply'
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <select id="findByMallReplyCup" resultMap="mapMarker">
        select photo.id,reply.cup as title,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 photo.geo_valid=1 and photo.album_type='MallReply'
        and reply.item_type=4
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <select id="findByMallReplyCupArea" resultMap="mapMarker">
        select photo.id,reply.cup as title,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 photo.geo_valid=1 and photo.album_type='MallReply' and reply.item_type=4
        where photo.area_code like concat(#{areaCode},'%')
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>
    <select id="findByMallReplyItem" resultMap="mapMarker">
        select photo.id,photo.manufacturer as title,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 photo.geo_valid=1 and photo.album_type='MallReply'
        and reply.item_type=#{itemType}
        group by photo.album_id
        order by photo.id desc
        limit #{pageSize}
    </select>

    <select id="findImagePostInfo" resultType="cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo">
        select object_id as upload_id,album_id as item_id,channel_code,photo_url
        from geo_camera_photo
        where id=#{id} and geo is not null
    </select>
    <select id="findMallReplyInfo" resultType="cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo">
        select reply.item_id,reply.reply_id,reply.extra as sku,reply.reply_content,
        photo.object_id as upload_id,photo.channel_code,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} and photo.geo is not null
    </select>
    <select id="findMarkerInfo" resultType="cn.reghao.tnb.content.app.geo.model.vo.MarkerInfo">
        select object_id as upload_id,channel_code,photo_url
        from geo_camera_photo
        where id=#{id} and geo is not null
    </select>

    <select id="findAll" resultType="cn.reghao.tnb.content.app.geo.model.po.CameraPhoto">
        select `id`,`album_type`,`album_id`,`object_id`,`channel_code`, `photo_url`,
        `manufacturer`,`model`,`software`,`shot_at`,x(geo) as longitude,y(geo) as latitude,`geo_valid`
        from geo_camera_photo
        where geo_valid is true and area_code=0
    </select>
    <select id="findById" resultType="cn.reghao.tnb.content.app.geo.model.po.CameraPhoto">
        select *
        from geo_camera_photo
        where id=#{id}
    </select>
    <select id="findByDistance" resultMap="mapMarker">
        select photo.id,reply.cup as title,photo.photo_url,x(photo.geo) as lng,y(photo.geo) as lat,
            round(st_distance_sphere(point(#{mapPoint.lng}, #{mapPoint.lat}),point(x(geo), y(geo))), 2) as distance
        from geo_camera_photo photo
        inner join geo_mall_reply reply
        on photo.album_id=reply.reply_id and photo.geo_valid=1 and photo.album_type='MallReply' and reply.item_type=4
        group by photo.album_id
        HAVING distance > 0 and distance &lt; #{distance}
        order by distance
        limit 1000
    </select>
    <select id="findAreaCountByGroup" resultType="cn.reghao.tnb.content.app.geo.model.vo.AreaCount">
        select area_code as areaCode, group_concat(album_id) as albumIds
        from geo_camera_photo
        where album_type='MallReply' and area_code like concat(#{areaCode},'%')
        group by area_code
    </select>
</mapper>