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

    <insert id="save" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo"
            useGeneratedKeys="true">
        insert into p_latent_danger_flow_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dangerId != null">
                danger_id,
            </if>
            <if test="actionFlag != null">
                action_flag,
            </if>
            <if test="flowTaskName != null">
                flow_task_name,
            </if>
            <if test="flowTaskUserIds != null">
                flow_task_user_ids,
            </if>
            <if test="flowTaskId != null">
                flow_task_id,
            </if>
            <if test="excuteState != null">
                excute_state,
            </if>
            <if test="excuteUserId != null">
                excute_user_id,
            </if>
            <if test="excuteDepartmentId != null">
                excute_department_id,
            </if>
            <if test="excuteResult != null">
                excute_result,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="flowJson != null">
                flow_json,
            </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="dangerId != null">
                #{dangerId},
            </if>
            <if test="actionFlag != null">
                #{actionFlag},
            </if>
            <if test="flowTaskName != null">
                #{flowTaskName},
            </if>
            <if test="flowTaskUserIds != null">
                #{flowTaskUserIds},
            </if>
            <if test="flowTaskId != null">
                #{flowTaskId},
            </if>
            <if test="excuteState != null">
                #{excuteState},
            </if>
            <if test="excuteUserId != null">
                #{excuteUserId},
            </if>
            <if test="excuteDepartmentId != null">
                #{excuteDepartmentId},
            </if>
            <if test="excuteResult != null">
                #{excuteResult},
            </if>
            <if test="remark != null">
                #{remark},
            </if>
            <if test="flowJson != null">
                #{flowJson},
            </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.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        update p_latent_danger_flow_record
        <set>
            <if test="dangerId != null">
                danger_id = #{dangerId},
            </if>
            <if test="actionFlag != null">
                action_flag = #{actionFlag},
            </if>
            <if test="flowTaskName != null">
                flow_task_name = #{flowTaskName},
            </if>
            <if test="flowTaskUserIds != null">
                flow_task_user_ids = #{flowTaskUserIds},
            </if>
            <if test="flowTaskId != null">
                flow_task_id = #{flowTaskId},
            </if>
            <if test="excuteState != null">
                excute_state = #{excuteState},
            </if>
            <if test="excuteUserId != null">
                excute_user_id = #{excuteUserId},
            </if>
            <if test="excuteDepartmentId != null">
                excute_department_id = #{excuteDepartmentId},
            </if>
            <if test="excuteResult != null">
                excute_result = #{excuteResult},
            </if>
            <if test="remark != null">
                remark = #{remark},
            </if>
            <if test="flowJson != null">
                flow_json = #{flowJson},
            </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>

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

    <select id="getNewestRecordByDangerId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select * from p_latent_danger_flow_record where deleted = 0 and danger_id = #{dangerId} order by create_date desc limit 1
    </select>

    <select id="listPassByDangerId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select
            *
        from
            p_latent_danger_flow_record
        where
            id in
            (
                select
                    max(id)
                from
                    p_latent_danger_flow_record
                where
                    deleted = 0
                and
                    danger_id = #{dangerId}
                and
                    excute_state = 2
                group by action_flag
            )
    </select>

    <select id="listByDangerId" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select
            *
        from
            p_latent_danger_flow_record
        where
            deleted = 0
        and
            danger_id = #{dangerId}
    </select>

    <select id="getByDangerIdAndActionFlag" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select
            *
        from
            p_latent_danger_flow_record
        where
            danger_id = #{dangerId}
        and
            action_flag = #{actionFlag}
        order by create_date desc
        limit 1
    </select>

    <select id="getByDangerIdAndCreate" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select
            *
        from
            p_latent_danger_flow_record
        where
            danger_id = #{dangerId}
        order by create_date desc
        limit 1
    </select>

    <select id="listNewestRecordByDangerIds" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select
            a.*,b.overtime_state as dangerOvertimeState,b.danger_state as dangerState
        from
            p_latent_danger_flow_record as a
        left join
            p_latent_danger as b on a.danger_id = b.id
        where
            a.id in
            (
                select
                    max(id)
                from
                    p_latent_danger_flow_record
                where
                    deleted = 0
				and
					danger_id in
                    <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                and
                    excute_state in (2,3)
                group by danger_id
            )
    </select>

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

    <select id="listExecuteLog" resultType="com.yeejoin.amos.patrol.business.vo.DangerTimeAxisVo">
        SELECT
            pld.id AS dangerId,
            pldfr.excute_user_id AS executeUserId,
            pldfr.excute_result AS executeResult,
            pld.create_date AS createDate,
            pld.update_date AS updateDate,
            pld.danger_name AS dangerName,
            pld.danger_state AS dangerState,
            pld.structure_id AS structureId,
            pld.reform_limit_date AS reformLimitDate,
            pld.reform_type AS reformType,
            pld.delay_limit_date AS delayLimitDate,
            wws.full_name AS structureName
        FROM
            p_latent_danger pld
            LEFT JOIN p_latent_danger_flow_record pldfr ON pldfr.id = pld.current_flow_record_id
            LEFT JOIN wl_warehouse_structure wws ON wws.id = pld.structure_id
<!--        WHERE-->
<!--            <if test="beginTime != null and beginTime != ''">-->
<!--                pld.reform_limit_date <![CDATA[>=]]>  #{beginTime}-->
<!--            </if>-->
        ORDER BY
            pld.update_date DESC
    </select>
    <select id="getByIdOderByDate"
            resultType="com.yeejoin.amos.patrol.business.entity.mybatis.extend.LatentDangerFlowRecordBo">
        select * from p_latent_danger_flow_record where deleted = 0 and danger_id = #{dangerId} ORDER BY create_date DESC LIMIT 1
    </select>
</mapper>