<?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.base.biz.mapper.SceneTargetPointMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.base.biz.model.entity.SceneTargetPoint">
        <id column="id" property="id" />
        <result column="target_id" property="targetId" />
        <result column="point_code" property="pointCode" />
        <result column="latitude" property="latitude" />
        <result column="longitude" property="longitude" />
        <result column="sequence" property="sequence" />
        <result column="created_date" property="createdDate" />
        <result column="created_by" property="createdBy" />
        <result column="last_modified_by" property="lastModifiedBy" />
        <result column="last_modified_date" property="lastModifiedDate" />
        <result column="version" property="version" />
        <result column="status" property="status" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        target_id, point_code, latitude, longitude, sequence, created_date, created_by, last_modified_by, last_modified_date, version, status, id,source
    </sql>

    <insert id="batch" parameterType="com.base.biz.model.entity.SceneTargetPoint">
        insert into scene_target_point (<include refid="Base_Column_List"/>)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.targetId},
            #{item.pointCode},
            #{item.latitude},
            #{item.longitude},
            #{item.sequence},
            #{item.createdDate},
            #{item.createdBy},
            #{item.lastModifiedBy},
            #{item.lastModifiedDate},
            #{item.version},
            #{item.status},
            #{item.id},
            #{item.source}
            )
        </foreach>
    </insert>

    <!-- 通用查询映射目标点集合 -->
    <resultMap id="TargetPointDTOResp" type="com.base.biz.model.dto.response.TargetPointDTO">
        <result column="targetId" property="targetId" />
        <result column="targetCode" property="targetCode" />
        <result column="targetLevel" property="targetLevel" />
        <result column="targetName" property="targetName" />
        <result column="countryName" property="countryName" />
        <result column="type" property="type" />
        <result column="priority" property="priority" />
        <result column="pointDirection" property="pointDirection" />
        <result column="pointCount" property="pointCount" />
        <collection property="pointDTOList" ofType="com.base.biz.model.PointDTO">
            <result column="pointId" property="pointId"/>
            <result column="sequence" property="sequence"/>
            <result column="lat" property="lat"/>
            <result column="lng" property="lng"/>
        </collection>
    </resultMap>

    <select id="getTargetPointDTO" parameterType="com.base.biz.model.dto.request.QueryTargetListDTO" resultMap="TargetPointDTOResp">
        select t.id targetId,t.target_code targetCode,t.target_level targetLevel,t.target_name targetName,t.type,t.country_name countryName,
               t.priority,t.point_direction pointDirection,t.point_count pointCount,p.id pointId,p.sequence,p.latitude lat,p.longitude lng
        from scene_basis_target t left join scene_target_point p on t.id = p.target_id
        <where>
            t.type is not null
            <if test="req.targetName!=null and req.targetName!=''">
            and t.target_name like concat('%', #{req.targetName}, '%')
            </if>
        </where>
    </select>


    <select id="getTargetByVo" parameterType="com.base.biz.model.dto.request.QueryTargetListDTO" resultType="com.base.biz.model.dto.response.TargetPointDTO">
        SELECT
            t.id targetId,
            t.target_code targetCode,
            t.target_level targetLevel,
            t.target_name targetName,
            t.type,
            t.country_name countryName,
            t.priority,
            t.point_direction pointDirection,
            t.point_count pointCount
        FROM
            scene_basis_target t
        <where>
        1=1
            <if test="req.targetName!=null and req.targetName!=''">
                and t.target_name like concat('%', #{req.targetName}, '%')
            </if>
            <if test="req.type!=null">
                and t.type = #{req.type}
            </if>
        </where>
        ORDER BY
            CAST(SUBSTRING_INDEX(t.target_name, '测试点位', -1) AS UNSIGNED)
    </select>
</mapper>


