<?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.api.manage.dao.butlerService.SysInspectionRouteDao">
    <select id="list" parameterType="com.api.model.butlerService.SearchInspectionRoute" resultType="com.api.vo.butlerService.VoInspectionRoute">
        select id,code,name,status,space_time,create_date
        from sys_inspection_route
        <where>
            is_delete = 1
            <if test="code != null and code != ''">
                and code = #{code}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
        </where>
    </select>
    <insert id="insert" parameterType="com.api.model.butlerService.SysInspectionRoute">
        insert into sys_inspection_route(
            code,
            name,
            describes,
            status,
            space_time,
            create_id,
            create_date,
            is_delete
        )values (
            #{code},
            #{name},
            #{describes},
            #{status},
            #{spaceTime},
            #{createId},
            #{createDate},
            #{isDelete}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>
    <insert id="insertPointRoute" parameterType="com.api.model.butlerService.SysInspectionPointRoute">
        insert into sys_inspection_point_route(
            inspection_point_id,
            inspection_route_id
        )values (
            #{inspectionPointId},
            #{inspectionRouteId}
        )
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFBIInspectionRoute">
        select id,name,describes,space_time from sys_inspection_route where id = #{id} and is_delete = 1
    </select>

    <select id="findByIdRoutePoint" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFBIInspectionRoutePoint">
        select sip.id,sip.name from sys_inspection_point as sip,sys_inspection_point_route as sipr
        where sip.id = sipr.inspection_point_id and sip.is_delete = 1 and sipr.inspection_route_id = #{id}
    </select>

    <update id="update" parameterType="com.api.model.butlerService.SysInspectionRoute">
        update sys_inspection_route
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="describes != null and describes != ''">
                describes = #{describes},
            </if>
            <if test="spaceTime != null">
                space_time = #{spaceTime},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <delete id="deleteRoutePoint" parameterType="java.lang.Integer">
        delete from sys_inspection_point_route where inspection_route_id = #{id}
    </delete>

    <update id="falseDelete" parameterType="java.lang.Integer">
        update sys_inspection_route set is_delete = 0 where id = #{id}
    </update>

    <select id="findStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from sys_inspection_route where id = #{id} and is_delete = 1
    </select>

    <update id="updateStatusById" parameterType="com.api.model.butlerService.RouteIdAndStatus">
        update sys_inspection_route set status = #{status} where id = #{id}
    </update>

    <select id="findSpaceTimeById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select space_time from sys_inspection_route where id = #{inspectionRouteId}
    </select>

    <select id="findPlanStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">

        select sum(case sip.status when 1 then 1 else 0 end) as sum
        from sys_inspection_plan as sip,sys_inspection_route as sir,sys_user as su,sys_organization as so
        where sip.inspection_route_id = sir.id and sip.status=1  and sip.inspector = su.id
            and sip.organization_id = so.id
            and sip.is_delete = 1 and sir.id=#{id}
    </select>


</mapper>