<?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.LatentDangerPatrolMapper">

    <insert id="save" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.supervision.business.entity.mybatis.extend.LatentDangerPatrolBo"
            useGeneratedKeys="true">
        insert into p_latent_danger_patrol
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="latentDangerId != null">
                latent_danger_id,
            </if>
            <if test="pointClassifyId != null">
                point_classify_id,
            </if>
<!--            <if test="riskFactorFlowId != null">-->
<!--                risk_factor_flow_id,-->
<!--            </if>-->
            <if test="checkId != null">
                check_id,
            </if>
            <if test="itemId != null">
                item_id,
            </if>
            <if test="pointId != null">
                point_id,
            </if>
            <if test="routeId != null">
                route_id,
            </if>
            <if test="routePointItemId != null">
                route_point_item_id,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="createDate != null">
                create_date,
            </if>
            <if test="updateDate != null">
                update_date,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="latentDangerId != null">
                #{latentDangerId},
            </if>
            <if test="pointClassifyId != null">
                #{pointClassifyId},
            </if>
<!--            <if test="riskFactorFlowId != null">-->
<!--                #{riskFactorFlowId},-->
<!--            </if>-->
            <if test="checkId != null">
                #{checkId},
            </if>
            <if test="itemId != null">
                #{itemId},
            </if>
            <if test="pointId != null">
                #{pointId},
            </if>
            <if test="routeId != null">
                #{routeId},
            </if>
            <if test="routePointItemId != null">
                #{routePointItemId},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
            <if test="createDate != null">
                #{createDate},
            </if>
            <if test="updateDate != null">
                #{updateDate},
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.yeejoin.amos.supervision.business.entity.mybatis.extend.LatentDangerPatrolBo">
        update p_latent_danger_patrol
        <set>
            <if test="latentDangerId != null">
                latent_danger_id = #{latentDangerId},
            </if>
            <if test="pointClassifyId != null">
                point_classify_id = #{pointClassifyId},
            </if>
            <if test="checkId != null">
                check_id = #{checkId},
            </if>
            <if test="riskFactorFlowId != null">
                risk_factor_flow_id = #{riskFactorFlowId},
            </if>
            <if test="itemId != null">
                item_id = #{itemId},
            </if>
            <if test="pointId != null">
                point_id = #{pointId},
            </if>
            <if test="routeId != null">
                route_id = #{routeId},
            </if>
            <if test="routePointItemId != null">
                route_point_item_id = #{routePointItemId},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate},
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteByLatentDangerIds">
        update p_latent_danger_patrol set deleted = 1 where latent_danger_id in
        <foreach collection="list" index="index" item="latentDangerId" open="(" separator="," close=")">
            #{latentDangerId}
        </foreach>
    </delete>

    <select id="getByDangerId" resultType="com.yeejoin.amos.supervision.business.entity.mybatis.extend.LatentDangerPatrolBo">
       select
            a.*,
            b.name as itemName,
            b.original_id as itemOriginalId,
            b.basis_json as itemBasis,
            b.remark as itemRemark,
            c.name as pointName,
            c.point_no as pointNo,
            c.level as pointLevel,
            c.charge_dept_id as pointDepartMentId,
            c.original_id as pointOriginalId,
            d.name as routeName,
            e.check_time as checkTime,
            e.user_id as checkUserId,
            e.dep_id as checkDepartmentId,
            f.name as planName,
            f.plan_type as planType,
            f.execute_rate as executeRate,
            g.original_id as classifyOriginalId,
            g.name as classifyName
        from
            p_latent_danger_patrol as a
        left join
            p_input_item as b on a.item_id = b.id
        left join
            p_point as c on a.point_id = c.id
        left join
            p_route as d on a.route_id = d.id
        left join
            p_check as e on a.check_id = e.id
        left join
            p_plan as f on e.plan_id = f.id
        left join
            p_point_classify as g on a.point_classify_id = g.id
        where
            a.deleted = 0
        and
            a.latent_danger_id = #{dangerId}
    </select>

    <select id="listByMap" resultType="com.yeejoin.amos.supervision.business.entity.mybatis.extend.LatentDangerPatrolBo">
        select
        a.*
        from
        p_latent_danger_patrol as a
        left join
        p_latent_danger as b
        <where>
            <if test="pointId != null">
                and a.point_id = #{pointId}
            </if>
            <if test="itemId != null">
                and a.item_id = #{itemId}
            </if>
            <if test="pointClassifyId != null">
                and a.point_classify_id = #{pointClassifyId}
            </if>
            <if test="dangerType != null">
                and b.danger_type = #{dangerType}
            </if>
        </where>
    </select>

    <select id="listByPointId" resultType="com.yeejoin.amos.supervision.business.entity.mybatis.extend.LatentDangerPatrolBo">
        select
            *
        from
            p_latent_danger_patrol
        where
            point_id = #{pointId}
    </select>

    <select id="queryByPointClassifyIds"
            resultType="com.yeejoin.amos.supervision.business.entity.mybatis.extend.LatentDangerPatrolBo">
        select * from p_latent_danger_patrol where point_classify_id in
        <foreach collection="list" index="index" item="pointClassifyId" open="(" separator="," close=")">
            #{pointClassifyId}
        </foreach>
    </select>
</mapper>