<?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.xunmei.core.resumption.mapper.AppRulePointMapper">
    <select id="selectDataListByItemId" resultType="com.xunmei.core.resumption.dto.appRulePoint.AppRulePointDetailDto">
        select p.*,a.name as areaName
        from core_resumption_rule_point p
                 inner join sys_area a on p.area_id = a.id
        where item_id = #{id}
          and p.deleted = 0
    </select>
<!--    <select id="selectDataListByIdList" resultType="com.xunmei.core.resumption.dto.appRulePoint.AppRulePointInfoDto"-->
<!--            parameterType="java.util.List">-->
<!--        select-->
<!--        ri.id as itemId,-->
<!--        ri.item_name as itemName,-->
<!--        ri.item_num as itemNum,-->
<!--        ri.item_desc as itemDesc,-->
<!--        p.id as id,-->
<!--        p.point_num as pointNum,-->
<!--        p.point_name as pointName,-->
<!--        p.safe_type as safeType,-->
<!--        d.name as safeTypeText,-->
<!--        p.area_id as areaId,-->
<!--        p.business_type as businessType,-->
<!--        a.name as areaName,-->
<!--        ap.plan_name as planName,-->
<!--        ap.id as planId,-->
<!--        1 as pointSource-->

<!--        from core_resumption_rule_point p-->
<!--        inner join t_app_rule_item ri on ri.id=p.item_id-->
<!--        inner join sys_area a on p.area_id = a.id-->
<!--        inner join sys_dict_detail d on d.code = p.safe_type and d.dict_code = 'check_type'-->
<!--        inner join core_resumption_plan_to_item ti on ti.item_id=p.id-->
<!--        inner join core_resumption_plan ap on ti.plan_id =ap.id-->
<!--        where p.deleted = 0-->
<!--        <if test="pointIds != null and pointIds.size > 0">-->
<!--            and p.id in-->
<!--            <foreach collection="pointIds" item="pointId" open="(" separator="," close=")">-->
<!--                #{pointId}-->
<!--            </foreach>-->
<!--        </if>-->
<!--        <if test="planIdList != null and planIdList.size > 0">-->
<!--            and ap.id in-->
<!--            <foreach collection="planIdList" item="planId" open="(" separator="," close=")">-->
<!--                #{planId}-->
<!--            </foreach>-->
<!--        </if>-->
<!--    </select>-->
    <select id="findPointByPlan" parameterType="Long" resultType="com.xunmei.core.resumption.vo.AppRulePointTaskVo">
        SELECT a.*, b.point_scan
        from core_resumption_rule_point a
                 LEFT JOIN core_resumption_plan_to_item b ON a.item_id = b.item_id
                 LEFT JOIN core_resumption_plan c ON b.plan_id = c.id
        WHERE c.id = #{planid}
    </select>
    <select id="selectPointListByIdList" resultType="com.xunmei.core.resumption.dto.appRulePoint.AppRulePointInfoDto"
            parameterType="java.util.List">
        select
        ri.id as itemId,
        ri.item_name as itemName,
        ri.item_num as itemNum,
        ri.item_desc as itemDesc,
        p.id as id,
        p.point_num as pointNum,
        p.point_name as pointName,
        p.safe_type as safeType,
        d.name as safeTypeText,
        p.area_id as areaId,
        p.business_type as businessType,
        a.name as areaName,
        ap.rule_name as planName,
        ap.id as planId,
        2 as pointSource
        from core_resumption_rule_point p
        inner join t_app_rule_item ri on ri.id=p.item_id
        inner join t_area a on p.area_id = a.id
        inner join sys_dict_detail d on d.code = p.safe_type and d.dict_code = 'check_type'
        inner join t_app_rule ap on p.rule_id =ap.id
        where p.deleted = 0
        <if test="pointIds != null and pointIds.size > 0">
            and p.id in
            <foreach collection="pointIds" item="pointId" open="(" separator="," close=")">
                #{pointId}
            </foreach>
        </if>
        <if test="normIdList != null and normIdList.size > 0">
            and ap.id in
            <foreach collection="normIdList" item="planId" open="(" separator="," close=")">
                #{planId}
            </foreach>
        </if>
    </select>


<!--    <select id="findPointByPlan" parameterType="Long" resultType="com.xunmei.core.resumption.vo.resumption.AppRulePointTaskVo">-->
<!--        SELECT a.*, b.point_scan-->
<!--        from core_resumption_rule_point a-->
<!--                 LEFT JOIN core_resumption_plan_to_item b ON a.id = b.item_id-->
<!--                 LEFT JOIN core_resumption_plan c ON b.plan_id = c.id-->
<!--        WHERE c.id = #{planid}-->
<!--    </select>-->

    <select id="findAreaListPlan" parameterType="Long" resultType="java.util.Map">
        SELECT a.area_id as areaid, d.name as areaname
        from core_resumption_rule_point a
                 LEFT JOIN core_resumption_plan_to_item b ON a.id = b.item_id
                 LEFT JOIN core_resumption_plan c ON b.plan_id = c.id
                 LEFT JOIN t_area d ON a.area_id = d.id
        WHERE c.id = #{planid}
    </select>


    <select id="findByName" parameterType="java.util.HashMap" resultType="java.util.Map">
        SELECT id, area_id
        FROM core_resumption_rule_point a
        WHERE a.name = #{name}
          and rule_id = #{ruleId}
    </select>

    <select id="checkIsUsed" resultType="java.lang.Integer">
        SELECT 1
        FROM core_resumption_data a
        WHERE a.item_id = #{pointId}
        limit 1
    </select>


    <select id="selectPointIds" resultType="java.lang.Long">
        select id
        from core_resumption_rule_point
        where item_id = #{id}
          and deleted = 0
    </select>

    <update id="updateDeleted">
        update core_resumption_rule_point
        set deleted=#{deleted}
        where id = #{id}
    </update>

    <update id="updatePlanItem">
        update core_resumption_plan_to_item
        set item_id=#{newId}
        where item_id = #{id}
    </update>

<!--    <select id="checkPointNumRepeat" resultType="java.lang.Integer">-->
<!--        select count(*)-->
<!--        from core_resumption_rule_point-->
<!--        where -->
<!--        and item_id = #{itemId}-->
<!--        and deleted = 0-->
<!--        <if test="pointId != null">-->
<!--            and id != #{pointId}-->
<!--        </if>-->
<!--    </select>-->

    <select id="checkPointNameRepeat" resultType="java.lang.Integer">
        select count(*)
        from core_resumption_rule_point
        where name = #{pointName}
        and item_id = #{itemId}
        and deleted = 0
        <if test="pointId != null">
            and id != #{pointId}
        </if>
    </select>

<!--    <select id="findPointBySafetyPlan" parameterType="java.util.HashMap" resultType="com.isp.safecheck.vo.PointVo">-->
<!--        SELECT a.id as pointId,a.point_name as pointName ,a.point_num as pointNum,a.item_id as itemId,a.area_id as areaId,f.name as areaName,a.time_type as timeType,-->
<!--        b.plan_id as planId,e.item_name as itemName,b.point_scan as pointScan,d.DATA_NAME as dataName,d1.DATA_ITEM_NAME as dataDetailNameA,d2.DATA_ITEM_NAME as dataDetailNameB-->
<!--        from core_resumption_rule_point a-->
<!--                 LEFT JOIN t_app_safecheck_plan_to_item b ON a.id = b.item_id-->
<!--                 LEFT JOIN t_app_safecheck_plan c ON b.plan_id = c.id-->
<!--                 LEFT JOIN t_app_rule_item e ON a.item_id = e.id-->
<!--                 left join t_area f on a.area_id = f.id-->
<!--                 left join sec_book_dt_inst d on a.data_type = d.id-->
<!--                 left join sec_book_dti_inst d1 on d.id = d1.SEC_BOOK_DT_INST_ID and d1.DATA_ITEM_NUMB='001'-->
<!--                 left join sec_book_dti_inst d2 on d.id = d2.SEC_BOOK_DT_INST_ID and d2.DATA_ITEM_NUMB='002'-->
<!--        WHERE c.id = #{planId} and a.deleted=0-->
<!--    </select>-->

<!--    <select id="findPointBySafetyPlanb" parameterType="java.util.HashMap" resultType="com.isp.safecheck.vo.PointVo">-->
<!--        SELECT  b.id as pointId,b.point_name as pointName ,b.point_num as pointNum,b.item_id as itemId,b.area_id as areaId,b.time_type as timeType,f.name as areaName,-->
<!--        a.plan_id as planId,e.item_name as itemName,a.id as pointDataId,a.res_value as resValue,a.res_time as resTime,a.res_value as resValue,d.DATA_NAME as dataName,-->
<!--       d1.DATA_ITEM_NAME as dataDetailNameA,d2.DATA_ITEM_NAME as dataDetailNameB-->
<!--       FROM t_app_safety_task_data a LEFT JOIN core_resumption_rule_point b ON a.point_id  = b.id-->
<!--       LEFT JOIN t_area f ON a.area_id = f.id-->
<!--       LEFT JOIN t_app_rule_item e ON a.item_id = e.id-->
<!--       left join sec_book_dt_inst d on b.data_type = d.id-->
<!--       left join sec_book_dti_inst d1 on d.id = d1.SEC_BOOK_DT_INST_ID and d1.DATA_ITEM_NUMB='001'-->
<!--       left join sec_book_dti_inst d2 on d.id = d2.SEC_BOOK_DT_INST_ID and d2.DATA_ITEM_NUMB='002'-->
<!--      WHERE a.task_id =#{taskId}-->

<!--    </select>-->

    <select id="findPointByRulePlan" resultType="com.xunmei.core.resumption.vo.ResumptionPointVo">
         SELECT a.id as pointid,a.point_name as pointname ,a.point_num as pointnum,a.item_id as itemid,a.area_id as areaid,f.name as areaname,a.time_type as timeType,
        b.plan_id as planId,e.item_name as itemname,b.point_scan as pointScan,b.required,a.business_type as businessType
        from core_resumption_rule_point a
                 LEFT JOIN core_resumption_plan_to_item b ON a.id = b.item_id
                 LEFT JOIN core_resumption_plan c ON b.plan_id = c.id
                 LEFT JOIN t_app_rule_item e ON a.item_id = e.id
                 left join t_area f on a.area_id = f.id
        WHERE c.id = #{planId} and a.deleted=0
    </select>
    <select id="checkIsJoinResumptionPlan" resultType="java.lang.Integer">
        SELECT DISTINCT 1
        FROM sys_plan_to_item
        WHERE item_id in
        <foreach collection="pointIds" item="pointId" index="index" open="(" separator="," close=")">
            #{pointId}
        </foreach>
        limit 1
    </select>
    <select id="pointSelectionPage" resultType="com.xunmei.core.resumption.vo.appRuleItem.AppRulePointSelectVo">
        SELECT
        p.id,
        p.rule_id AS ruleId,
        r.`name` AS ruleName,
        p.item_id AS itemId,
        i.`name` AS itemName,
        p.`name` AS pointName,
        a.`name` AS areaName
        FROM
        core_resumption_rule_point p
        INNER JOIN core_resumption_rule_item i ON p.item_id = i.id
        INNER JOIN core_resumption_rule r ON p.rule_id = r.id
        LEFT JOIN sys_area a ON p.area_id = a.id
        WHERE
        p.deleted = 0
        AND p.rule_id =#{query.ruleId}
        <if test="query.key!=null and query.key!=''">
            AND (
            p.`NAME` LIKE concat('%',#{query.key},'%')
            OR i.`NAME` LIKE concat('%',#{query.key},'%'))
        </if>
    </select>
<!--    <select id="checkIsJoinSafetyCheckTaskPlan" resultType="java.lang.Integer">-->
<!--        SELECT DISTINCT 1-->
<!--        FROM t_app_safecheck_plan_to_item-->
<!--        WHERE item_id in-->
<!--        <foreach collection="pointIds" item="pointId" index="index" open="(" separator="," close=")">-->
<!--            #{pointId}-->
<!--        </foreach>-->
<!--        limit 1-->
<!--    </select>-->
</mapper>
