<?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="cn.wisenergy.mapper.WorkDeptMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.wisenergy.model.app.WorkDept">
        <id column="id" property="id"/>
        <result column="dept_name" property="deptName"/>
        <result column="oa_dept_id" property="oaDeptId"/>
        <result column="centre_id" property="centreId"/>
        <result column="dept_manager_id" property="deptManagerId"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="sort" property="sort"/>
    </resultMap>

    <resultMap id="CentreDeptMap" type="cn.wisenergy.model.dto.OrganizationStructureDto">
        <id column="centre_id" property="centreId"/>
        <result column="centre_name" property="centreName"/>
        <collection property="deptUserDtos" ofType="cn.wisenergy.model.dto.DeptUserDto">
            <id column="dept_id" property="deptId"/>
            <result column="dept_name" property="deptName"/>
        </collection>
    </resultMap>

    <resultMap id="ProjectAndOrderType" type="cn.wisenergy.model.dto.DeptOfProjectAndOrderType">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <collection property="projectManagerDtos" ofType="cn.wisenergy.model.dto.ProjectManagerDto">
            <id column="project_id" property="id"/>
            <result column="project_name" property="projectName"/>
        </collection>
    </resultMap>

    <sql id="table">
        work_dept
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        dept_name,oa_dept_id,dept_manager_id,centre_id, create_time, modify_time,sort
    </sql>

    <sql id="criteria">
        <if test="id != null">AND id = #{id}</if>
        <if test="deptName != null">AND dept_name = #{deptName}</if>
        <if test="oaDeptId != null">AND oa_dept_id = #{oaDeptId}</if>
        <if test="centreId != null">AND centre_id = #{centreId}</if>
        <if test="deptManagerId != null">AND dept_manager_id = #{deptManagerId}</if>
        <if test="createTime != null">AND create_time = #{createTime}</if>
        <if test="modifyTime != null">AND modify_time = #{modifyTime}</if>
    </sql>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,dept_name, oa_dept_id,centre_id, dept_name, create_time, modify_time,sort
    </sql>
    <update id="updateManagerIdISNullById">
        UPDATE
        <include refid="table"/>
        SET dept_manager_id = NULL
        WHERE id = #{id}
    </update>

    <select id="getDeptByCondition" resultMap="BaseResultMap" parameterType="integer">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <include refid="criteria"/>
        </where>
        order by sort
    </select>

    <select id="getAllDeptByAllCentre" resultMap="CentreDeptMap">
select c.id as 'centre_id',c.centre_name as 'centre_name',d.id as 'dept_id',d.dept_name as 'dept_name'
from work_centre c LEFT JOIN work_dept d ON c.id = d.centre_id
ORDER BY c.id,d.sort

    </select>
    <select id="getById" resultMap="BaseResultMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where id =#{deptId}

    </select>

    <select id="getDeptByManagerId" resultType="cn.wisenergy.model.app.WorkDept">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        where dept_manager_id = #{userId}
    </select>


    <select id="getDeptOfProjectAndOrderType" resultMap="ProjectAndOrderType">
select d.id 'dept_id',d.dept_name,p.id 'project_id',p.project_name
from work_dept d LEFT JOIN work_project p on d.id = p.dept_id
<where>
        <if test="userId !=null">
        AND   p.manager_id = #{userId}
        </if>
        <if test="deptId !=null">
        OR p.dept_id = #{deptId}
        </if>
</where>
ORDER BY d.sort,p.id

    </select>

    <select id="getOaDeptNoByUserId" resultMap="BaseResultMap">
    select d.id,<include refid="cols_exclude_id"/>
    from work_dept d join work_user_dept ud on d.id = ud.dept_id
    where ud.user_id = #{userId}
    </select>

    <select id="getAllDept" resultType="cn.wisenergy.model.app.WorkDept">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
    </select>

</mapper>