<?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.patrol.business.dao.mapper.LatentDangerMapper">

    <insert id="save" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo"
            useGeneratedKeys="true">
        insert into p_latent_danger
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessKey != null">
                business_key,
            </if>
            <if test="orgCode != null">
                org_code,
            </if>
            <if test="dangerName != null">
                danger_name,
            </if>
            <if test="currentFlowRecordId != null">
                current_flow_record_id,
            </if>
            <if test="instanceId != null">
                instance_id,
            </if>
            <if test="dangerLevel != null">
                danger_level,
            </if>
            <if test="dangerPosition != null">
                danger_position,
            </if>
            <if test="dangerType != null">
                danger_type,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="overtimeState != null">
                overtime_state,
            </if>
            <if test="reformType != null">
                reform_type,
            </if>
            <if test="reformLimitDate != null">
                reform_limit_date,
            </if>
            <if test="reformJson != null">
                reform_json,
            </if>
            <if test="dangerState != null">
                danger_state,
            </if>
            <if test="discovererUserId != null">
                discoverer_user_id,
            </if>
            <if test="discovererDepartmentId != null">
                discoverer_department_id,
            </if>
            <if test="photoUrls != null">
                photo_urls,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="createDate != null">
                create_date,
            </if>
            <if test="updateDate != null">
                update_date,
            </if>
            <if test="problemDescription != null">
                problem_description,
            </if>
            <if test="checkInputId != null">
                check_input_id,
            </if>
            <if test="structureId != null">
                structure_id,
            </if>
            <if test="structureName != null">
                structure_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="businessKey != null">
                #{businessKey},
            </if>
            <if test="orgCode != null">
                #{orgCode},
            </if>
            <if test="dangerName != null">
                #{dangerName},
            </if>
            <if test="currentFlowRecordId != null">
                #{currentFlowRecordId},
            </if>
            <if test="instanceId != null">
                #{instanceId},
            </if>
            <if test="dangerLevel != null">
                #{dangerLevel},
            </if>
            <if test="dangerPosition != null">
                #{dangerPosition},
            </if>
            <if test="dangerType != null">
                #{dangerType},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <if test="overtimeState != null">
                #{overtimeState},
            </if>
            <if test="reformType != null">
                #{reformType},
            </if>
            <if test="reformLimitDate != null">
                #{reformLimitDate},
            </if>
            <if test="reformJson != null">
                #{reformJson},
            </if>
            <if test="dangerState != null">
                #{dangerState},
            </if>
            <if test="discovererUserId != null">
                #{discovererUserId},
            </if>
            <if test="discovererDepartmentId != null">
                #{discovererDepartmentId},
            </if>
            <if test="photoUrls != null">
                #{photoUrls},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
            <if test="createDate != null">
                #{createDate},
            </if>
            <if test="updateDate != null">
                #{updateDate},
            </if>
            <if test="problemDescription != null">
                #{problemDescription},
            </if>
            <if test="checkInputId != null">
                #{checkInputId},
            </if>
            <if test="structureId != null">
                #{structureId},
            </if>
            <if test="structureName != null">
                #{structureName},
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        update p_latent_danger
        <set>
            <if test="businessKey != null">
                business_key = #{businessKey},
            </if>
            <if test="orgCode != null">
                org_code = #{orgCode},
            </if>
            <if test="dangerName != null">
                danger_name = #{dangerName},
            </if>
            <if test="currentFlowRecordId != null">
                current_flow_record_id = #{currentFlowRecordId},
            </if>
            <if test="instanceId != null">
                instance_id = #{instanceId},
            </if>
            <if test="dangerLevel != null">
                danger_level = #{dangerLevel},
            </if>
            <if test="dangerPosition != null">
                danger_position = #{dangerPosition},
            </if>
            <if test="dangerType != null">
                danger_type = #{dangerType},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="overtimeState != null">
                overtime_state = #{overtimeState},
            </if>
            <if test="currentFlowRecordId != null">
                current_flow_record_id = #{currentFlowRecordId},
            </if>
            <if test="reformType != null">
                reform_type = #{reformType},
            </if>
            <if test="reformLimitDate != null">
                reform_limit_date = #{reformLimitDate},
            </if>
            <if test="reformJson != null">
                reform_json = #{reformJson},
            </if>
            <if test="dangerState != null">
                danger_state = #{dangerState},
            </if>
            <if test="discovererUserId != null">
                discoverer_user_id = #{discovererUserId},
            </if>
            <if test="discovererDepartmentId != null">
                discoverer_department_id = #{discovererDepartmentId},
            </if>
            <if test="photoUrls != null">
                photo_urls = #{photoUrls},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate},
            </if>
            <if test="delayLimitDate != null">
                delay_limit_date = #{delayLimitDate},
            </if>
            <if test="reasonAnalysis != null">
                reason_analysis = #{reasonAnalysis},
            </if>
            <if test="inferOtherThings != null">
                infer_other_things = #{inferOtherThings},
            </if>
            <if test="problemDescription != null">
                problem_description = #{problemDescription},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="deleteByIds">
        update p_latent_danger set deleted = 1 where id in
        <foreach collection="list" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getById" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        select * from p_latent_danger where deleted = 0 and id = #{id}
    </select>

    <select id="getByInstanceId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        select * from p_latent_danger where deleted = 0 and instance_id = #{instanceId} and danger_state = 3 limit 1
    </select>

    <select id="listByMap" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        select
          a.*
        from
          p_latent_danger as a
        left join
          p_latent_danger_flow_record as b on a.current_flow_record_id = b.id
        <where>
<!--            case-->
<!--            when a.danger_type = 1 and find_in_set(b.action_flag, 'B_3,B_4') then-->
<!--            1=1-->
<!--            <if test="userId != null and userId != ''">-->
<!--                and find_in_set( #{userId}, b.flow_task_user_ids )-->
<!--            </if>-->
<!--            else-->
<!--            1=1-->
<!--            <if test="userId != null and userId != ''">-->
<!--                and (find_in_set(#{userId}, b.flow_task_user_ids) or find_in_set(b.action_flag, #{permissions}))-->
<!--            </if>-->
<!--            end-->
<!--            and a.deleted = 0-->
<!--            and b.deleted = 0-->
            <if test="dangerLevel != null and dangerLevel != -1">
                and a.danger_level=#{dangerLevel}
            </if>
            <if test="dangerState != null and dangerState != ''">
                and a.danger_state=#{dangerState}
            </if>
            <if test="states != null">
                and a.danger_state in
                <foreach collection="states" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dangerName != null and dangerName != ''">
                and a.danger_name like concat('%',#{dangerName},'%')
            </if>
            <if test="org_code != null and org_code != ''">
                and a.org_code = #{org_code}
            </if>
<!--            <if test="discoverer_department_id != null and discoverer_department_id != ''">-->
<!--                and (a.discoverer_department_id = #{discoverer_department_id} or ISNULL(a.discoverer_department_id))-->
<!--            </if>-->
        </where>
        order by a.create_date desc
        <if test="limit != null and offset != null">
            limit #{offset},#{limit}
        </if>
    </select>

    <select id="countByMap" resultType="java.lang.Long">
        select
            count(1)
        from
            p_latent_danger as a
        left join
            p_latent_danger_flow_record as b on a.current_flow_record_id = b.id
        <where>
            case
            when a.danger_type = 1 and find_in_set(b.action_flag, 'B_3,B_4') then
            1=1
            <if test="userId != null and userId != ''">
            and find_in_set( #{userId}, b.flow_task_user_ids )
            </if>
            else
            1=1
            <if test="userId != null and userId != ''">
            and (find_in_set(#{userId}, b.flow_task_user_ids) or find_in_set(b.action_flag, #{permissions}))
            </if>
            end
            and a.deleted = 0
            and b.deleted = 0
            <if test="dangerLevel != null and dangerLevel != -1">
                and a.danger_level=#{dangerLevel}
            </if>
            <if test="dangerState != null and dangerState != ''">
                and a.danger_state=#{dangerState}
            </if>
            <if test="states != null">
                and a.danger_state in
                <foreach collection="states" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dangerName != null and dangerName != ''">
                and a.danger_name like concat('%',#{dangerName},'%')
            </if>
            <if test="org_code != null and org_code != ''">
                and a.org_code = #{org_code}
            </if>
            <if test="discoverer_department_id != null and discoverer_department_id != ''">
                and (a.discoverer_department_id = #{discoverer_department_id} or ISNULL(a.discoverer_department_id))
            </if>
        </where>
    </select>

    <select id="listOfOvertime" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        select * from p_latent_danger where now()>reform_limit_date and overtime_state = 0 and deleted = 0 and danger_state in (2,3,4)
    </select>

    <select id="countNotFinishByFlowId" resultType="java.lang.Integer">
        select
            count(1)
        from
            p_latent_danger
        where
            deleted = 0
        and
            danger_state in (1,2,3,4)
        and
            id in
            (
                select
                    latent_danger_id
                from
                    p_latent_danger_patrol
                where
                    risk_factor_flow_id = #{value}
                and
                    deleted = 0
            )
    </select>

    <select id="listNotFinishByPointId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        select
            *
        from
            p_latent_danger
        where
            deleted = 0
        and
            danger_state in (1,2,3,4)
        and
            id in
            (
                select
                    latent_danger_id
                from
                    p_latent_danger_patrol
                where
                    point_id = #{pointId}
                and
                    deleted = 0
            )
         and id !=#{dangerId}
    </select>

    <select id="countByFlowUserIds" resultType="java.lang.Long">
        select
            count(1)
        from
            p_latent_danger as a
        left join
            p_latent_danger_flow_record as b on a.current_flow_record_id = b.id
        where
            find_in_set(#{userId},b.flow_task_user_ids)
        and
            a.deleted = 0
        and
            a.danger_state in (1,2,4)
    </select>

    <select id="countNotFinishByTypeAndDeptId" resultType="java.lang.Long">
		SELECT
			count(1)
		FROM
			p_latent_danger a,
		    p_latent_danger_patrol b,
		    p_point c,
		    spc_risk_source d
		WHERE
		    a.danger_level = #{type}
            <if test="departmentId != null and departmentId != -1">
                and d.belong_department_id = #{departmentId}
            </if>
            and a.danger_type IN (2, 3)
            AND (
                a.danger_state IN (2, 3, 4)
                OR a.danger_state = 5
                AND DATEDIFF(a.update_date, CURDATE()) = 0
            )
            and b.latent_danger_id = a.id
            and b.point_id = c.id
            and c.original_id = d.id
    </select>

    <select id="getNotFinishIdsByTypeAndDeptId" resultType="java.lang.String">
        SELECT
            GROUP_CONCAT(DISTINCT a.id ) extraIds
        FROM
            p_latent_danger a,
            p_latent_danger_patrol b,
            p_point c,
            spc_risk_source d
        WHERE
            a.danger_level = #{type} -- (1,2)
            <if test="departmentId != null and departmentId != -1">
                and d.belong_department_id = #{departmentId}
            </if>
            AND a.danger_type IN ( 2, 3 ) -- 2：巡检隐患；3：有码无计划隐患

            AND (
            a.danger_state IN ( 2, 3, 4 ) -- 2：待治理；3：安措计划中；4：待验证；5：治理完毕

            OR a.danger_state = 5
            AND DATEDIFF( a.update_date, CURDATE( ) ) = 0
            )
            AND b.latent_danger_id = a.id
            AND b.point_id = c.id
            AND c.original_id = d.id;
    </select>

    <select id="listNeedNoticeDanger"
            resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerNoticeBo">
        SELECT
            ld.id dangerId,
            ld.org_code orgCode,
            ld.danger_state dangerState,
            ld.danger_name dangerName,
            ld.instance_id instanceId,
            ld.reform_limit_date reformLimitDate
        FROM
            p_latent_danger ld
        WHERE
            TIMESTAMPDIFF( MINUTE, now(), ld.reform_limit_date ) = TIMESTAMPDIFF( MINUTE, (SELECT DATE_ADD(CURDATE(),
                                                                                                           INTERVAL 8 HOUR)), ld.reform_limit_date ) -- 截止日期到当天早上8:00:00分钟数
            AND ld.overtime_state = 0
            AND ld.deleted = 0
            AND ld.danger_state = 2;
    </select>

    <select id="getReViewInfo" resultType="java.util.HashMap">
        SELECT
        f.id,
        f.danger_id dangerId,
        f.action_flag actionFlag,
        f.flow_task_name flowTaskName,
        CASE
        WHEN f.excute_state = 2 THEN
        '通过'
        WHEN f.excute_state = 3 THEN
        '驳回'
        END executeState,
        f.excute_user_id executeUserId,
        f.update_date reViewDate,
        ld.reform_limit_date reformLimitDate,
        f.flow_json flowJson,
        f.remark,
        ld.infer_other_things inferOtherThings,
        (select ff.flow_task_user_ids from p_latent_danger_flow_record ff where ff.danger_id = f.danger_id and
            ff.action_flag = 'B_3' limit 1 ) handleUserIds,
        CASE
            WHEN ld.reform_type	= 1 THEN
                '常规整改'
            WHEN ld.reform_type	= 2 THEN
                '安措计划'
            WHEN ld.reform_type	= 3 THEN
                '延期治理'
            END reformTypeName,
        ld.reform_type reformType,
        ld.delay_limit_date delayLimitDate
        FROM
        p_latent_danger_flow_record f
        LEFT JOIN p_latent_danger ld ON ld.id = f.danger_id
        WHERE
        f.action_flag != 'B_1'
        AND NOT ISNULL( f.update_date )
        AND f.excute_state != 1
        AND f.danger_id = #{dangerId}
    </select>
    <select id="dangerListByMap" resultType="com.yeejoin.amos.patrol.core.common.response.DangerListResponse">
        SELECT
        *
        FROM
        (
        SELECT
        pld.id,
        pld.danger_name dangerName,
        pld.danger_position dangerPosition,
        pld.danger_level dangerLevel,
        pld.reform_type reformType,
        pld.reform_limit_date deadlineDate,
        pld.danger_state dangerState,
        pld.overtime_state overtimeState,
        pld.problem_description problemDescription,
        pld.reason_analysis reasonAnalysis,
        pld.infer_other_things inferOtherThings,
        pld.remark,
        pp.plan_type planType,
        pp.`name` planName,
        pr.`name` routeName,
        pld.danger_type dangerType,
        pc.check_time checkTimeDate,
        pii.`name` checkContent,
        pc.user_id checkUser,
        pc.error checkResult,
        pc.is_ok checkStatus,
        pld.id dangerId,
        pld.reform_json measures,
        pld.instance_id processInstanceId,
        pld.discoverer_user_id discoverUser,
        p.original_id riskSourceId,
        pld.structure_id structureId,
        pld.structure_name structureName
        FROM
        p_latent_danger pld
        LEFT JOIN p_latent_danger_patrol ldp ON pld.id = ldp.latent_danger_id
        LEFT JOIN p_point p ON p.id = ldp.point_id
        LEFT JOIN p_point_classify ppc ON ppc.id = ldp.point_classify_id
        LEFT JOIN p_input_item pii ON pii.id = ldp.item_id
        LEFT JOIN p_check pc ON pc.id = ldp.check_id
        LEFT JOIN p_plan pp ON pp.id = pc.plan_id
        LEFT JOIN p_route pr ON pr.id = pc.route_id
        <where>
            <if test="dangerIds != null">
                pld.id IN
                <foreach collection="dangerIds" item="id" index="index" open="(" separator="," close=")" >
                    #{id}
                </foreach>
            </if>
        </where>
        ) temp
        WHERE
        1 = 1
        <if test="type != null and type !=''">
            AND type = #{type}
        </if>
        <if test="eqCode != null and eqCode !=''">
            AND eqCode LIKE concat('%',#{eqCode},'%')
        </if>
        <if test="dangerName != null and dangerName !=''">
            AND dangerName LIKE concat('%',#{dangerName},'%')
        </if>
        <if test="planType != null and planType !=''">
            AND planType = #{planType}
        </if>
        <if test="structureId != null and structureId.size > 0">
            AND structureId in
            <foreach collection="structureId" open="(" close=")" separator="," item="sid">
                #{sid}
            </foreach>
        </if>
        <if test="routeName != null and routeName !=''">
            AND routeName LIKE concat('%',#{routeName},'%')
        </if>
        <if test="planName != null and planName !=''">
            AND planName LIKE concat('%',#{planName},'%')
        </if>
        <if test="beginCheckTime != null and beginCheckTime !=''">
            AND checkTimeDate <![CDATA[>=]]> #{beginCheckTime}
        </if>
        <if test="endCheckTime != null and endCheckTime !=''">
            AND checkTimeDate <![CDATA[<=]]> #{endCheckTime}
        </if>
        <if test="checkDep != null and checkDep !=''">
            AND checkDep = #{checkDep}
        </if>
        <if test="checkUser != null and checkUser !=''">
            AND checkUser = #{checkUser}
        </if>
        <if test="checkResult != null and checkResult !=''">
            AND checkResult LIKE concat('%',#{checkResult},'%')
        </if>
        <if test="beginDeadline != null and beginDeadline !=''">
            AND deadlineDate <![CDATA[>=]]> #{beginDeadline}
        </if>
        <if test="endDeadline != null and endDeadline !=''">
            AND deadlineDate <![CDATA[<=]]> #{endDeadline}
        </if>
        <if test="governDep != null and governDep !=''">
            AND governDep = #{governDep}
        </if>
        <if test="governUser != null and governUser !=''">
            AND governUser = #{governUser}
        </if>
        <if test="dangerLevel != null and dangerLevel !=''">
            AND dangerLevel = #{dangerLevel}
        </if>
        <if test="dangerType != null and dangerType !=''">
            AND dangerType = #{dangerType}
        </if>
        <if test="reformType != null and reformType !=''">
            AND reformType = #{reformType}
        </if>
        <if test="overtimeState != null and overtimeState !=''">
            AND overtimeState = #{overtimeState}
        </if>
        <if test="dangerState != null and dangerState !=''">
            AND dangerState = #{dangerState}
        </if>
        <if test="verifyDep != null and verifyDep !=''">
            AND verifyDep = #{verifyDep}
        </if>
        <if test="verifyUser != null and verifyUser !=''">
            AND verifyUser = #{verifyUser}
        </if>
        <if test="riskSourceId != null and riskSourceId !=''">
            AND riskSourceId = #{riskSourceId}
        </if>
        ORDER BY temp.id desc
        <if test="offset != null and pageSize != null">
            limit #{offset},#{pageSize}
        </if>
    </select>
    <select id="countDangerListByMap" resultType="java.lang.Long">
        SELECT
        COUNT(1)
        FROM
        (
        SELECT
        pld.danger_name dangerName,
        pld.danger_position dangerPosition,
        pld.structure_name structureName,
        pld.structure_id structureId,
        pld.danger_level dangerLevel,
        pld.reform_type reformType,
        pld.reform_limit_date deadlineDate,
        pld.danger_state dangerState,
        pld.overtime_state overtimeState,
        pp.plan_type planType,
        pp.`name` planName,
        pr.`name` routeName,
        pld.danger_type dangerType,
        pc.check_time checkTimeDate,
        pii.`name` checkContent,
        pc.user_id checkUser,
        pc.error checkResult,
        pc.is_ok checkStatus,
        pld.id dangerId,
        dangerFlow.governDep,
        dangerFlow.governUser,
        dangerFlow.governDate,
        dangerFlow.verifyDep,
        dangerFlow.verifyUser,
        dangerFlow.verifyResult,
        pld.reform_json measures
        FROM
        p_latent_danger pld
        LEFT JOIN p_latent_danger_patrol ldp ON pld.id = ldp.latent_danger_id
        LEFT JOIN p_point_classify ppc ON ppc.id = ldp.point_classify_id
        LEFT JOIN p_input_item pii ON pii.id = ldp.item_id
        LEFT JOIN p_check pc ON pc.id = ldp.check_id
        LEFT JOIN p_plan pp ON pp.id = pc.plan_id
        LEFT JOIN p_route pr ON pr.id = pc.route_id
        LEFT JOIN (
        SELECT
        GROUP_CONCAT(
        CASE
        WHEN action_flag = 'B_3' THEN
        excute_department_id
        ELSE
        NULL
        END
        ) governDep,
        GROUP_CONCAT(
        CASE
        WHEN action_flag = 'B_3' THEN
        excute_user_id
        ELSE
        NULL
        END
        ) governUser,
        GROUP_CONCAT(
        CASE
        WHEN action_flag = 'B_3' THEN
        update_date
        ELSE
        NULL
        END
        ) governDate,
        GROUP_CONCAT(
        CASE
        WHEN action_flag = 'B_4' THEN
        excute_department_id
        ELSE
        NULL
        END
        ) verifyDep,
        GROUP_CONCAT(
        CASE
        WHEN action_flag = 'B_4' THEN
        excute_user_id
        ELSE
        NULL
        END
        ) verifyUser,
        GROUP_CONCAT(
        CASE
        WHEN action_flag = 'B_4' THEN
        pldfr.excute_result
        ELSE
        NULL
        END
        ) verifyResult,
        pldfr.danger_id dangerId
        FROM
        (
        SELECT
        *
        FROM
        p_latent_danger_flow_record
        WHERE
        id IN (
        SELECT
        max(id)
        FROM
        p_latent_danger_flow_record
        WHERE
        deleted = 0
        AND action_flag IN ('B_3', 'B_4')
        GROUP BY
        action_flag,
        danger_id
        )
        ) pldfr
        GROUP BY
        pldfr.danger_id
        ) dangerFlow ON dangerFlow.dangerId = pld.id
        <where>
            <if test="dangerIds != null">
                pld.id IN
                <foreach collection="dangerIds" item="id" index="index" open="(" separator="," close=")" >
                    #{id}
                </foreach>
            </if>
        </where>
        ) temp
        WHERE
        1 = 1
        <if test="type != null and type !=''">
            AND type = #{type}
        </if>
        <if test="eqCode != null and eqCode !=''">
            AND eqCode LIKE concat('%',#{eqCode},'%')
        </if>
        <if test="dangerName != null and dangerName !=''">
            AND dangerName LIKE concat('%',#{dangerName},'%')
        </if>
        <if test="planType != null and planType !=''">
            AND planType = #{planType}
        </if>
        <if test="structureId != null and structureId.size > 0">
            AND structureId in
            <foreach collection="structureId" open="(" close=")" separator="," item="sid">
                #{sid}
            </foreach>
        </if>
        <if test="routeName != null and routeName !=''">
            AND routeName LIKE concat('%',#{routeName},'%')
        </if>
        <if test="planName != null and planName !=''">
            AND planName LIKE concat('%',#{planName},'%')
        </if>
        <if test="beginCheckTime != null and beginCheckTime !=''">
            AND checkTimeDate <![CDATA[>=]]> #{beginCheckTime}
        </if>
        <if test="endCheckTime != null and endCheckTime !=''">
            AND checkTimeDate <![CDATA[<=]]> #{endCheckTime}
        </if>
        <if test="checkDep != null and checkDep !=''">
            AND checkDep = #{checkDep}
        </if>
        <if test="checkUser != null and checkUser !=''">
            AND checkUser = #{checkUser}
        </if>
        <if test="checkResult != null and checkResult !=''">
            AND checkResult LIKE concat('%',#{checkResult},'%')
        </if>
        <if test="beginDeadline != null and beginDeadline !=''">
            AND deadlineDate <![CDATA[>=]]> #{beginDeadline}
        </if>
        <if test="endDeadline != null and endDeadline !=''">
            AND deadlineDate <![CDATA[<=]]> #{endDeadline}
        </if>
        <if test="governDep != null and governDep !=''">
            AND governDep = #{governDep}
        </if>
        <if test="governUser != null and governUser !=''">
            AND governUser = #{governUser}
        </if>
        <if test="dangerLevel != null and dangerLevel !=''">
            AND dangerLevel = #{dangerLevel}
        </if>
        <if test="dangerType != null and dangerType !=''">
            AND dangerType = #{dangerType}
        </if>
        <if test="reformType != null and reformType !=''">
            AND reformType = #{reformType}
        </if>
        <if test="overtimeState != null and overtimeState !=''">
            AND overtimeState = #{overtimeState}
        </if>
        <if test="dangerState != null and dangerState !=''">
            AND dangerState = #{dangerState}
        </if>
        <if test="verifyDep != null and verifyDep !=''">
            AND verifyDep = #{verifyDep}
        </if>
        <if test="verifyUser != null and verifyUser !=''">
            AND verifyUser = #{verifyUser}
        </if>
        <if test="riskSourceId != null and riskSourceId !=''">
            AND riskSourceId = #{riskSourceId}
        </if>
    </select>

    <select id="getByBathBusinessKeys" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        SELECT
         *
        FROM
            p_latent_danger pld
        WHERE
            pld.business_key IN
        <foreach collection = "businessKeys" item = "businessKey" index="index" open = "(" close = ")" separator = "," >
            #{businessKey}
        </foreach>
        <if test="latentDangerListParam.dangerLevel != null and latentDangerListParam.dangerLevel != -1">
            and pld.danger_level=#{latentDangerListParam.dangerLevel}
        </if>
        <if test="latentDangerListParam.dangerState != null and latentDangerListParam.dangerState != ''">
            and pld.danger_state=#{latentDangerListParam.dangerState}
        </if>
        <if test="latentDangerListParam.userId != null and latentDangerListParam.userId != ''">
            and pld.discoverer_user_id=#{latentDangerListParam.userId}
        </if>
        <if test="latentDangerListParam.dangerName != null and latentDangerListParam.dangerName !=''">
            AND pld.danger_name LIKE concat('%',#{latentDangerListParam.dangerName},'%')
        </if>
        ORDER BY create_date DESC
        <if test="latentDangerListParam.pageSize != null and latentDangerListParam.offset != null">
            limit #{latentDangerListParam.offset}, #{latentDangerListParam.pageSize}
        </if>
    </select>

    <select id="countByBathBusinessKeys" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        p_latent_danger pld
        WHERE
        pld.business_key IN
        <foreach collection = "businessKeys" item = "businessKey" index="index" open = "(" close = ")" separator = "," >
            #{businessKey}
        </foreach>
        <if test="latentDangerListParam.dangerLevel != null and latentDangerListParam.dangerLevel != -1">
            and pld.danger_level=#{latentDangerListParam.dangerLevel}
        </if>
        <if test="latentDangerListParam.dangerState != null and latentDangerListParam.dangerState != ''">
            and pld.danger_state=#{latentDangerListParam.dangerState}
        </if>
        <if test="latentDangerListParam.userId != null and latentDangerListParam.userId != ''">
            and pld.discoverer_user_id=#{latentDangerListParam.userId}
        </if>
        <if test="latentDangerListParam.dangerName != null and latentDangerListParam.dangerName !=''">
            AND pld.danger_name LIKE concat('%',#{latentDangerListParam.dangerName},'%')
        </if>
    </select>

    <select id="countByStatus" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        p_latent_danger pld
        WHERE
        pld.deleted = 0
        AND
        pld.danger_state in (1,2,4,7,8,9)
        AND
        pld.business_key IN
        <foreach collection = "businessKeys" item = "businessKey" index="index" open = "(" close = ")" separator = "," >
            #{businessKey}
        </foreach>

    </select>
    <update id="updateCheckInputDangerState">
        update
            p_check_input
            set danger_handle_state = #{code}
            where id = #{id};
    </update>

    <select id="getbyBusinessKey" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerBo">
        SELECT
        *
        FROM
        p_latent_danger pld
        WHERE
        pld.business_key = #{businessKey}
    </select>
</mapper>