<?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.diagbot.mapper.BasDeptInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.BasDeptInfo">
        <id column="dept_id" property="deptId"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="parent_dept_id" property="parentDeptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="dept_type" property="deptType"/>
        <result column="spell" property="spell"/>
        <result column="station" property="station"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
    </resultMap>
    <select id="getList" resultType="com.diagbot.dto.BasDeptInfoDTO"
            parameterType="com.diagbot.vo.BasDeptInfoVO">
        SELECT DISTINCT
            t.dept_id AS deptId,
            dept_name AS deptName
        FROM
            `bas_dept_info` t
        WHERE
            t.is_deleted = 'N'
        AND (t.station = '住院' or t.station = '质管')
        AND t.hospital_id = #{hospitalId}
        <if test="inputStr !=null and inputStr != ''">
            AND (UPPER(t.spell) LIKE CONCAT('%', UPPER(TRIM(#{inputStr})),'%') OR UPPER(t.dept_name) LIKE CONCAT('%', UPPER(TRIM(#{inputStr})),'%'))
        </if>
        ORDER BY t.dept_name ASC
    </select>

    <select id="getListUser" resultType="com.diagbot.dto.BasDeptInfoDTO"
            parameterType="com.diagbot.vo.BasDeptInfoVO">
        SELECT DISTINCT
        t.dept_id AS deptId,
        dept_name AS deptName
        FROM
        `bas_dept_info` t
        INNER JOIN sys_user_dept t1 ON t1.is_deleted = 'N'
        AND t1.dept_id = t.dept_id
        AND t1.hospital_id = t.hospital_id
        WHERE
        t.is_deleted = 'N'
        AND t.station = '住院'
        AND t.hospital_id = #{hospitalId}
        AND t1.user_id = #{userId}
        <if test="inputStr !=null and inputStr != ''">
            AND (UPPER(t.spell) LIKE CONCAT('%', UPPER(TRIM(#{inputStr})),'%') OR UPPER(t.dept_name) LIKE CONCAT('%', UPPER(TRIM(#{inputStr})),'%'));
        </if>
        ORDER BY t.dept_name ASC
    </select>

    <!-- 查询用户关联科室 -->
    <select id="getDeptByUser" parameterType="com.diagbot.vo.FilterVO" resultType="com.diagbot.dto.DeptBaseDTO">
        SELECT
        b.dept_id as deptId,
        b.dept_name as deptName
        FROM
        sys_user_dept a,
        bas_dept_info b
        WHERE
        a.is_deleted = 'N'
        AND b.is_deleted = 'N'
        AND a.hospital_id = b.hospital_id
        AND a.dept_id = b.dept_id
        AND b.station = '住院'
        <if test="userId!=null">
            AND a.user_id = #{userId}
        </if>
        <if test="hospitalId != null and hospitalId != ''">
            AND a.hospital_id = #{hospitalId}
        </if>
    </select>

    <update id="updateBatchByKey">
        <foreach collection="list" item="item"  separator=";">
            update bas_dept_info
            <set>
                <if test="item.parentDeptId != null">
                    parent_dept_id = #{item.parentDeptId},
                </if>
                <if test="item.deptName != null">
                    dept_name = #{item.deptName},
                </if>
                <if test="item.deptType != null">
                    dept_type = #{item.deptType},
                </if>
                <if test="item.spell != null">
                    spell = #{item.spell},
                </if>
                <if test="item.station != null">
                    station = #{item.station},
                </if>
                <if test="item.gmtModified != null">
                    gmt_modified = #{item.gmtModified},
                </if>
                <if test="item.modifier != null">
                    modifier = #{item.modifier},
                </if>
            </set>
            where dept_id = #{item.deptId} and hospital_id = #{item.hospitalId}
        </foreach>
    </update>
</mapper>
