<?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.yeejoin.amos.supervision.business.dao.mapper.RoutePointItemMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.yeejoin.amos.supervision.dao.entity.RoutePointItem" id="routePointItemMap">
        <result property="routePointId" column="route_point_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="pointClassifyId" column="point_classify_id"/>
        <result property="pointInputItemId" column="point_input_item_id"/>
        <result property="creatorId" column="creator_id"/>
        <result property="createDate" column="create_date"/>
        <result property="pointClassifyId" column="point_classify_id"/>
        <result property="basisJson" column="basis_json"/>
    </resultMap>

    <update id="updateRoutePointItem" parameterType="com.yeejoin.amos.supervision.dao.entity.RoutePointItem">
        UPDATE p_route_point_item
        <trim prefix="set" suffixOverrides=",">
            <if test="routePointId != null">route_point_id=#{routePointId},</if>
            <if test="orderNo != null ">order_no=#{orderNo},</if>
            <if test="pointInputItemId != null">point_input_item_id=#{pointInputItemId},</if>
            <if test="creatorId != null">creator_id=#{creatorId},</if>
            <if test="createDate != null">create_date=#{createDate},</if>
            <if test="pointClassifyId != null">point_classify_id=#{pointClassifyId},</if>
            <if test="basisJson != null ">basis_json=#{basisJson},</if>
        </trim>
        WHERE id=#{id}
    </update>

    <delete id="delRoutePointItemByRouteId">
        DELETE
        FROM
          p_route_point_item
        WHERE
          route_point_id IN (
            SELECT
              id
            FROM
              p_route_point prp
            WHERE
              route_id = #{routeId}
          )
    </delete>
    <!--统计 -->
    <select id="queryPageCount" resultType="long">
        SELECT
            COUNT(1)
        FROM
           (SELECT
                COUNT(1)
            FROM
            p_route_point_item pi
            LEFT JOIN p_input_item i ON i.id = pi.input_item_id
            <where>
                <if test="name!=null"> and i.name like concat(concat("%",#{name}),"%")</if>
                <if test="planId!=null"> and pi.plan_id = #{planId} </if>
                <!--    <if test="orgCode!=null"> and i.org_Code = #{orgCode}</if>-->
            </where>
            GROUP BY i.id
           ) a
    </select>
    <!--查询 -->
    <select id="queryPage" resultType="com.yeejoin.amos.supervision.business.vo.RoutePointItemVo">
        SELECT
        pi.id,
        i.id AS itemId,
        i.`name`,
        i.item_type,
        i.input_classify,
        i.check_type,
        i.item_classify,
        i.item_type_classify,i.item_level
        FROM
        p_route_point_item pi
        LEFT JOIN p_input_item i ON i.id = pi.input_item_id
        <where>
            <if test="name!=null"> and i.name like concat(concat("%",#{name}),"%")</if>
            <if test="planId!=null"> and pi.plan_id = #{planId} </if>
            <!--    <if test="orgCode!=null"> and i.org_Code = #{orgCode}</if>-->
        </where>
        GROUP BY i.id
        <choose>
            <when test="pageSize==-1"></when>
            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
    </select>

    <select id="getPointItemCount" resultType="int">
    SELECT
        COUNT(prpi.input_item_id)
    FROM
        p_route_point_item prpi
        LEFT JOIN p_route_point prp ON prp.id = prpi.route_point_id
    WHERE
        prp.route_id = #{routeId} AND prp.point_id = #{pointId}
    </select>
</mapper>