<?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.ruoyi.ehsRescueTeam.mapper.EhsRescueTeamMapper">

    <resultMap type="EhsRescueTeam" id="EhsRescueTeamResult">
        <result property="id" column="id"/>
        <result property="teamName" column="team_name"/>
        <result property="teamLevel" column="team_level"/>

        <result property="dutyer" column="dutyer"/>
        <result property="phone" column="phone"/>
        <result property="unit" column="unit"/>
        <result property="station" column="station"/>


        <result property="teamProperty" column="team_property"/>
        <!--<result property="rescueWorkerNum"    column="rescue_worker_num"    />-->
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="deptId" column="dept_id"/>
        <result property="createUserId" column="create_user_id"/>
        <result property="updateUserId" column="update_user_id"/>
    </resultMap>

    <sql id="selectEhsRescueTeamVo">
        select id, team_name, team_level, dutyer, phone, unit, station, team_property, create_by, create_time, update_by, update_time, remark, dept_id, create_user_id, update_user_id from ehs_rescue_team
    </sql>

    <select id="selectEhsRescueTeamList" parameterType="EhsRescueTeam" resultMap="EhsRescueTeamResult">
        <include refid="selectEhsRescueTeamVo"/>
        <where>
            <if test="teamName != null  and teamName != ''">and team_name like concat('%', #{teamName}, '%')</if>
            <if test="teamLevel != null  and teamLevel != ''">and team_level like concat('%', #{teamLevel}, '%')</if>
            <if test="teamProperty != null">and team_property = #{teamProperty}</if>
            <if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>

            <if test="dutyer != null and dutyer != ''">and dutyer like concat('%', #{dutyer}, '%')</if>
            <if test="phone != null and phone != ''">and phone = #{phone}</if>
            <if test="unit != null and unit != ''">and unit = #{unit}</if>
            <if test="station != null and station != ''">and station like concat('%', #{station}, '%')</if>
        </where>
    </select>

    <select id="selectEhsRescueTeamById" parameterType="Long" resultMap="EhsRescueTeamResult">
        <include refid="selectEhsRescueTeamVo"/>
        where id = #{id}
    </select>
    <select id="getWorkerNumByTeamId" resultType="java.lang.Long">
        SELECT IFNULL(
		  (SELECT COUNT(1) FROM
			  ehs_rescue_team t,
			  ehs_rescue_team_worker w
		  WHERE
			  t.id = w.rescue_team_id
			  AND t.id = #{id}
		  GROUP BY
			t.id),  0)
    </select>
    <select id="getTeamInfoByTeamId" resultType="com.ruoyi.ehsRescueTeam.vo.TeamResponseVo">
        SELECT id as rescueTeamId,team_name as rescueTeamName
        FROM ehs_rescue_team
        WHERE id = #{rescueTeamId}
    </select>


</mapper>