<?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.elephant.framework.galaxy.aroad.module.emergency.mapper.EmergencyTeamMapper">

    <resultMap id="emergencyTeamMap" type="com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeam">
        <id property="rowId" column="row_id"/>
        <result property="emergencyType" column="emergency_type"/>
        <result property="teamId" column="team_id"/>
        <result property="teamName" column="team_name"/>
        <result property="organizeId" column="organize_id"/>
        <result property="organizeName" column="organize_name"/>
        <result property="parentOrganizeName" column="parent_organize_name"/>
        <result property="parentOrganizeId" column="parent_organize_id"/>
        <result property="teamAdress" column="team_adress"/>
        <result property="equipmentAdress" column="equipment_adress"/>
        <result property="departmentManager" column="department_manager"/>
        <result property="managerTelephone" column="manager_telephone"/>
        <result property="contactPeople" column="contact_people"/>
        <result property="contactTelephone" column="contact_telephone"/>
        <result property="peopleAmount" column="people_amount"/>
        <result property="carAmount" column="car_amount"/>
        <result property="equipmentAmount" column="equipment_amount"/>
        <result property="bz" column="bz"/>
    </resultMap>

    <!-- userVo结果集 -->
    <resultMap id="emergencyMaterialAndEquipmentVoMap"
               type="com.elephant.framework.galaxy.aroad.module.emergency.vo.EmergencyMaterialAndEquipmentVo">
        <id column="t_row_id" property="rowId"/>
        <result column="t_emergency_type" property="emergencyType"/>
        <result column="t_team_id" property="teamId"/>
        <result column="t_team_name" property="teamName"/>
        <result column="t_organize_id" property="organizeId"/>
        <result column="t_organize_name" property="organizeName"/>
        <result column="t_parent_organize_name" property="parentOrganizeName"/>
        <result column="t_parent_organize_id" property="parentOrganizeId"/>
        <result column="t_longitude" property="longitude"/>
        <result column="t_latitude" property="latitude"/>
        <result column="t_team_adress" property="teamAdress"/>
        <result column="t_equipment_adress" property="equipmentAdress"/>
        <result column="t_department_manager" property="departmentManager"/>
        <result column="t_manager_telephone" property="managerTelephone"/>
        <result column="t_contact_people" property="contactPeople"/>
        <result column="t_contact_telephone" property="contactTelephone"/>
        <result column="t_deptName" property="deptName"/>
        <result column="t_people_amount" property="peopleAmount"/>
        <result column="t_car_amount" property="carAmount"/>
        <result column="t_equipment_amount" property="equipmentAmount"/>
        <result column="t_bz" property="bz"/>

        <collection property="materialHouses"
                    ofType="com.elephant.framework.galaxy.aroad.module.emergency.entity.MaterialHouse">
            <id column="h_row_id" property="rowId"/>
            <result column="h_house_name" property="houseName"/>
            <result column="h_address" property="address"/>
            <result column="h_longitude" property="longitude"/>
            <result column="h_latitude" property="latitude"/>
            <result column="h_manager_phone" property="managerPhone"/>
            <result column="h_material_type" property="materialType"/>
            <result column="h_material_name" property="materialName"/>
            <result column="h_manager_phone" property="managerPhone"/>
            <result column="h_unit" property="unit"/>
            <result column="h_amount" property="amount"/>
            <result column="h_manager" property="manager"/>
            <result column="h_telephone" property="telephone"/>
            <result column="h_bz" property="bz"/>
        </collection>

        <collection property="emergencyMaterials"
                    ofType="com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyMaterial">
            <id column="m_row_id" property="rowId"/>
            <result column="m_emergency_type" property="emergencyType"/>
            <result column="m_team_id" property="teamId"/>
            <result column="m_team_name" property="teamName"/>
            <result column="m_parent_organize_id" property="parentOrganizeId"/>
            <result column="m_parent_organize_name" property="parentOrganizeName"/>
            <result column="m_equipment_type" property="equipmentType"/>
            <result column="m_material_name" property="materialName"/>
            <result column="m_manager_phone" property="managerPhone"/>
            <result column="m_unit" property="unit"/>
            <result column="m_amount" property="amount"/>
            <result column="m_manager" property="manager"/>
            <result column="m_telephone" property="telephone"/>
            <result column="m_bz" property="bz"/>
        </collection>

        <collection property="emergencyEquipments"
                    ofType="com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyEquipment">
            <id column="e_row_id" property="rowId"/>
            <result column="e_emergency_type" property="emergencyType"/>
            <result column="e_team_id" property="teamId"/>
            <result column="e_team_name" property="teamName"/>
            <result column="e_parent_organize_id" property="parentOrganizeId"/>
            <result column="e_parent_organize_name" property="parentOrganizeName"/>
            <result column="e_equipment_type" property="equipmentType"/>
            <result column="e_equipment_name" property="equipmentName"/>
            <result column="e_unit" property="unit"/>
            <result column="e_amount" property="amount"/>
            <result column="e_manager" property="manager"/>
            <result column="e_telephone" property="telephone"/>
            <result column="e_bz" property="bz"/>
        </collection>

        <collection property="emergencyCars"
                    ofType="com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyCar">
            <id property="rowId" column="row_id"/>
            <result property="emergencyType" column="c_emergency_type"/>
            <result property="teamId" column="c_team_id"/>
            <result property="teamName" column="c_team_name"/>
            <result property="parentOrganizeId" column="c_parent_organize_id"/>
            <result property="parentOrganizeName" column="c_parent_organize_name"/>
            <result property="equipmentType" column="c_equipment_type"/>
            <result property="carName" column="c_car_name"/>
            <result property="unit" column="c_unit"/>
            <result property="carNumber" column="c_car_number"/>
            <result property="driver" column="c_driver"/>
            <result property="telephone" column="c_telephone"/>
            <result property="bz" column="c_bz"/>
        </collection>
    </resultMap>

    <resultMap id="teamCoordinateDtoMap"
               type="com.elephant.framework.galaxy.aroad.module.emergency.dto.TeamCoordinateDto">
        <id column="row_id" property="rowId"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
    </resultMap>

    <sql id="emergencyTeamFields">
        t.row_id as t_row_id,
        t.emergency_type as t_emergency_type,
        t.team_id as t_team_id,
        t.team_name as t_team_name,
        t.organize_id as t_organize_id,
        t.organize_name as t_organize_name,
        t.longitude as t_longitude,
        t.latitude as t_latitude,
        t.parent_organize_name as t_parent_organize_name,
        t.parent_organize_id as t_parent_organize_id,
        t.team_adress as t_team_adress,
        t.equipment_adress as t_equipment_adress,
        t.department_manager as t_department_manager,
        t.manager_telephone as t_manager_telephone,
        t.contact_people as t_contact_people,
        t.contact_telephone as t_contact_telephone,
        t.people_amount as t_people_amount,
        t.car_amount as t_car_amount,
        t.equipment_amount as t_equipment_amount,
        t.bz as t_bz,
    </sql>

    <sql id="emergencyMaterialFields">
        m.row_id as m_row_id,
        m.emergency_type as m_emergency_type,
        m.team_id as m_team_id,
        m.team_name as m_team_name,
        m.parent_organize_id as m_parent_organize_id,
        m.parent_organize_name as m_parent_organize_name,
        m.equipment_type as m_equipment_type,
        m.material_name as m_material_name,
        m.unit as m_unit,
        m.amount as m_amount,
        m.manager as m_manager,
        m.telephone as m_telephone,
        m.bz as m_bz,
    </sql>

    <sql id="materialHouseFields">
        h.row_id as h_row_id ,
        h.house_name as h_house_name,
        h.address as h_address,
        h.longitude as h_longitude,
        h.latitude as h_latitude,
        h.manager_phone as h_manager_phone,
        h.material_type as h_material_type,
        h.material_name as h_material_name,
        h.unit as h_unit,
        h.amount as h_amount,
        h.manager as h_manager,
        h.telephone as h_telephone,
        h.bz as h_bz,
    </sql>

    <sql id="emergencyEquipmentFields">
        e.row_id as e_row_id,
        e.emergency_type as e_emergency_type,
        e.team_id as e_team_id,
        e.team_name as e_team_name,
        e.parent_organize_id as e_parent_organize_id,
        e.parent_organize_name as e_parent_organize_name,
        e.equipment_type as e_equipment_type,
        e.equipment_name as e_equipment_name,
        e.unit as e_unit,
        e.number as e_number,
        e.manager as e_manager,
        e.telephone as e_telephone,
        e.bz as e_bz,
    </sql>

    <sql id="emergencyCarFields">
        c.row_id as c_row_id,
        c.emergency_type as c_emergency_type,
        c.team_id as c_team_id,
        c.team_name as c_team_name,
        c.parent_organize_id as c_parent_organize_id,
        c.parent_organize_name as c_parent_organize_name,
        c.equipment_type as c_equipment_type,
        c.car_name as c_car_name,
        c.unit as c_unit,
        c.car_number as c_car_number,
        c.driver as c_driver,
        c.telephone as c_telephone,
        c.bz as c_bz
    </sql>

    <select id="getEmergencyMaterialAndEquipmentVo" resultMap="emergencyMaterialAndEquipmentVoMap">
        SELECT
        <include refid="emergencyTeamFields"/>
        <include refid="materialHouseFields"/>
        <include refid="emergencyMaterialFields"/>
        <include refid="emergencyEquipmentFields"/>
        <include refid="emergencyCarFields"/>
        FROM emergency_team t
        LEFT JOIN material_house h on t.row_id = h.team_id
        LEFT JOIN emergency_material m on t.row_id = m.team_id
        LEFT JOIN emergency_equipment e on t.row_id = e.team_id
        LEFT JOIN emergency_car c on t.row_id = c.team_id
        WHERE t.row_id = #{id}
    </select>

    <select id="getCoordinates" resultMap="teamCoordinateDtoMap">
        select row_id, longitude, latitude
        from emergency_team;
    </select>

    <select id="getGoalTeamVo" resultMap="emergencyMaterialAndEquipmentVoMap">
        SELECT
        <include refid="emergencyTeamFields"/>
        <include refid="materialHouseFields"/>
        <include refid="emergencyMaterialFields"/>
        <include refid="emergencyEquipmentFields"/>
        FROM emergency_team t
        LEFT JOIN material_house h on t.row_id = h.team_id
        LEFT JOIN emergency_material m on t.row_id = m.team_id
        LEFT JOIN emergency_equipment e on t.row_id = e.team_id
        WHERE t.row_id
        in
        <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <delete id="removeById">
        DELETE a,b,c,d,e
        FROM emergency_team a
                 LEFT JOIN emergency_car b ON a.team_id = b.team_id
                 LEFT JOIN emergency_material c ON a.team_id = c.team_id
                 LEFT JOIN emergency_equipment d ON a.team_id = d.team_id
                 LEFT JOIN emergency_team_equipment e ON a.team_id = e.team_id
        WHERE a.team_id = #{teamId}
    </delete>

    <insert id="save" parameterType="com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeam">
        insert into emergency_team (row_id, emergency_type, team_id, team_name, organize_id, organize_name,
                                    parent_organize_id, parent_organize_name, team_adress, longitude, latitude,
                                    equipment_adress, department_manager, manager_telephone, contact_people,
                                    contact_telephone,
                                    people_amount, car_amount, equipment_amount, bz)
        values (#{rowId}, #{emergencyType}, #{teamId}, #{teamName}, #{organizeId}, #{organizeName},
                #{parentOrganizeId}, #{parentOrganizeName}, #{teamAdress}, #{longitude}, #{latitude},
                #{equipmentAdress}, #{departmentManager}, #{managerTelephone}, #{contactPeople}, #{contactTelephone},
                #{peopleAmount},
                #{carAmount}, #{equipmentAmount}, #{bz});
    </insert>

    <update id="updateWithId"
            parameterType="com.elephant.framework.galaxy.aroad.module.emergency.entity.EmergencyTeam">
        update emergency_team
        set emergency_type=#{emergencyType},
            team_name=#{teamName},
            organize_id=#{organizeId},
            organize_name=#{organizeName},
            parent_organize_id=#{parentOrganizeId},
            parent_organize_name=#{parentOrganizeName},
            team_adress=#{teamAdress},
            longitude=#{longitude},
            latitude=#{latitude},
            equipment_adress=#{equipmentAdress},
            department_manager=#{departmentManager},
            manager_telephone=#{managerTelephone},
            contact_people=#{contactPeople},
            contact_telephone=#{contactTelephone},
            people_amount=#{peopleAmount},
            car_amount=#{carAmount},
            equipment_amount=#{equipmentAmount},
            bz=#{bz}
        where team_id = #{teamId};
    </update>
</mapper>
