<?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.zxdmy.excite.system.mapper.cloud.account.CloudDepartmentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zxdmy.excite.system.entity.cloud.account.CloudDepartment">
        <id column="id" property="id" />
        <result column="department_name" property="departmentName" />
        <result column="department_sort" property="departmentSort" />
        <result column="department_parent_id" property="departmentParentId" />
        <result column="company_id" property="companyId" />

        <result column="create_time" property="createTime" />
        <result column="creator_id" property="creatorId" />
        <result column="creator_name" property="creatorName" />
        <result column="delete_time" property="deleteTime" />
        <result column="delete_id" property="deleteId" />
        <result column="delete_name" property="deleteName" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, department_name, department_sort, department_parent_id,company_id, create_time, creator_id, creator_name, delete_time, delete_id, delete_name
    </sql>

    <select id="queryByPage" resultType="com.zxdmy.excite.system.entity.vo.account.DepartmentVo" >
        SELECT d.*,c.company_name as companyName from cloud_department d
        LEFT JOIN cloud_company c on c.id = d.company_id
        <trim prefix="WHERE" prefixOverrides="AND">
            d.delete_time is null
            <if test="data.departmentName != null and data.departmentName != ''">
                AND d.department_name LIKE CONCAT('%' ,#{data.departmentName}, '%')
            </if>
            <if test="data.companyId != null and data.companyId != ''">
                AND d.company_id = #{data.companyId}
            </if>
        </trim>
        ORDER BY department_sort DESC
        LIMIT #{page},#{size};
    </select>

    <select id="queryByPageCount" resultType="java.lang.Integer">
        select count(*)
        from cloud_department d
        LEFT JOIN cloud_company c on c.id = d.company_id
        <trim prefix="WHERE" prefixOverrides="AND">
            d.delete_time is null
            <if test="data.departmentName != null and data.departmentName != ''">
                AND d.department_name LIKE CONCAT('%' ,#{data.departmentName}, '%')
            </if>
            <if test="data.companyId != null and data.companyId != ''">
                AND d.company_id = #{data.companyId}
            </if>
        </trim>
    </select>

    <select id="getData" resultType="com.zxdmy.excite.system.entity.vo.account.DepartmentVo" >
        select <include refid="Base_Column_List"/>
        from cloud_department
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="data.id != null and data.id != ''">
                AND id = #{data.id}
            </if>
            <if test="data.departmentName != null and data.departmentName != ''">
                AND department_name = #{data.departmentName}
            </if>
        </trim>
    </select>


    <select id="getList" resultType="com.zxdmy.excite.system.entity.cloud.account.CloudDepartment" >
        select id, department_name,department_name as title, department_sort, department_parent_id,company_id, create_time, creator_id, creator_name, delete_time, delete_id, delete_name
        from cloud_department
        <trim prefix="WHERE" prefixOverrides="AND">
            delete_time is null
            <if test="id != null and id != ''">
                AND company_id = #{id}
            </if>
        </trim>
        ORDER BY department_sort DESC
    </select>


    <select id="selectByCompanyId" resultType="com.zxdmy.excite.system.entity.cloud.account.CloudDepartment" >
        SELECT * from cloud_department where company_id = #{companyId}
        ORDER BY department_sort DESC
    </select>

</mapper>
