<?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.xinelu.manage.mapper.nursestationdepartment.NurseStationDepartmentMapper">

    <resultMap type="NurseStationDepartment" id="NurseStationDepartmentResult">
        <result property="id" column="id"/>
        <result property="nurseStationId" column="nurse_station_id"/>
        <result property="departmentCode" column="department_code"/>
        <result property="departmentName" column="department_name"/>
        <result property="departmentPerson" column="department_person"/>
        <result property="phone" column="phone"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectNurseStationDepartmentVo">
        select id,
               nurse_station_id,
               department_code,
               department_name,
               department_person,
               phone,
               create_by,
               create_time,
               update_by,
               update_time
        from nurse_station_department
    </sql>

    <select id="selectNurseStationDepartmentListtest" parameterType="NurseStationDepartment"
            resultMap="NurseStationDepartmentResult">
        <include refid="selectNurseStationDepartmentVo"/>
        <where>
            <if test="nurseStationId != null ">
                and nurse_station_id = #{nurseStationId}
            </if>
            <if test="departmentCode != null  and departmentCode != ''">
                and department_code = #{departmentCode}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="departmentPerson != null  and departmentPerson != ''">
                and department_person = #{departmentPerson}
            </if>
            <if test="phone != null  and phone != ''">
                and phone = #{phone}
            </if>
        </where>
    </select>

    <select id="selectNurseStationDepartmentByIdTest" parameterType="Long"
            resultMap="NurseStationDepartmentResult">
        <include refid="selectNurseStationDepartmentVo"/>
        where id = #{id}
    </select>

    <insert id="insertNurseStationDepartment" parameterType="NurseStationDepartment" useGeneratedKeys="true"
            keyProperty="id">
        insert into nurse_station_department
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="nurseStationId != null">nurse_station_id,
            </if>
            <if test="departmentCode != null">department_code,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="departmentPerson != null">department_person,
            </if>
            <if test="phone != null">phone,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="nurseStationId != null">#{nurseStationId},
            </if>
            <if test="departmentCode != null">#{departmentCode},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="departmentPerson != null">#{departmentPerson},
            </if>
            <if test="phone != null">#{phone},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateNurseStationDepartment" parameterType="NurseStationDepartment">
        update nurse_station_department
        <trim prefix="SET" suffixOverrides=",">
            <if test="nurseStationId != null">nurse_station_id =
                #{nurseStationId},
            </if>
            <if test="departmentCode != null">department_code =
                #{departmentCode},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="departmentPerson != null">department_person =
                #{departmentPerson},
            </if>
            <if test="phone != null">phone =
                #{phone},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteNurseStationDepartmentById" parameterType="Long">
        delete
        from nurse_station_department
        where id = #{id}
    </delete>

    <delete id="deleteNurseStationDepartmentByIds" parameterType="String">
        delete from nurse_station_department where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectNurseStationDepartmentList"
            resultType="com.xinelu.manage.vo.nursestationdepartment.NurseStationDepartmentVO">
        select
        nsd.id,
        nsd.nurse_station_id,
        nsd.department_code,
        nsd.department_name,
        nsd.department_person,
        nsd.phone,
        nsd.create_by,
        nsd.create_time,
        nsd.update_by,
        nsd.update_time,
        ns.nurse_station_name,
        ns.nurse_station_code
        from nurse_station_department nsd
        INNER JOIN nurse_station ns ON ns.id = nsd.nurse_station_id
        <where>
            <if test="nurseStationId != null ">
                and nsd.nurse_station_id = #{nurseStationId}
            </if>
            <if test="departmentCode != null  and departmentCode != ''">
                and nsd.department_code = #{departmentCode}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and nsd.department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="departmentPerson != null  and departmentPerson != ''">
                and nsd.department_person = #{departmentPerson}
            </if>
            <if test="phone != null  and phone != ''">
                and nsd.phone = #{phone}
            </if>
            <if test="createBy != null  and createBy != ''">
                and nsd.create_by = #{createBy}
            </if>
            <if test="createTime != null  and createTime != ''">
                and nsd.create_time = #{createTime}
            </if>
            <if test="nurseStationName != null  and nurseStationName != ''">
                and ns.nurse_station_name like concat('%', #{nurseStationName},'%')
            </if>
            <if test="nurseStationCode != null  and nurseStationCode != ''">
                and nsd.nurse_station_code = #{nurseStationCode}
            </if>
        </where>
        ORDER BY nsd.create_time DESC
    </select>

    <select id="selectNurseStationDepartmentById"
            resultType="com.xinelu.manage.vo.nursestationdepartment.NurseStationDepartmentVO">
        select nsd.id,
               nsd.nurse_station_id,
               nsd.department_code,
               nsd.department_name,
               nsd.department_person,
               nsd.phone,
               nsd.create_by,
               nsd.create_time,
               nsd.update_by,
               nsd.update_time,
               ns.nurse_station_name,
               ns.nurse_station_code
        from nurse_station_department nsd
                 INNER JOIN nurse_station ns ON ns.id = nsd.nurse_station_id
        where nsd.id = #{id}
    </select>

    <insert id="insertNurseStationDepartmentList" parameterType="java.util.List">
        insert into nurse_station_department
        (
        nurse_station_id,
        department_code,
        department_name,
        department_person,
        phone,
        create_by,
        create_time
        )
        VALUE
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.nurseStationId},
            #{item.departmentCode},
            #{item.departmentName},
            #{item.departmentPerson},
            #{item.phone},
            #{item.createBy},
            #{item.createTime}
            )
        </foreach>
    </insert>

    <select id="getDepartmentName" resultType="int">
        SELECT
        count( 1 )
        FROM
        nurse_station_department
        <where>
            <if test="nurseStationId != null  and nurseStationId != ''">
                and nurse_station_id = #{nurseStationId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and department_name = #{departmentName}
            </if>
        </where>
    </select>

    <select id="getDepartmentPhone" resultType="int">
        SELECT
        count( 1 )
        FROM
        nurse_station_department
        where
        <if test="phone != null  and phone != ''">
            phone = #{phone}
        </if>
    </select>
</mapper>