<?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.jeeplus.sys.mapper.OfficeMapper">

    <sql id="officeColumns">
    	a.id as "id",
        a.parent_id as "parent.id",
        a.parent_ids as "parentIds",
        a.name as "name",
        a.sort as "sort",
        a.area as "areaId",
        a.code as "code",
        a.type as "type",
        a.grade as "grade",
        a.address as "address",
        a.zip_code as "zipCode",
        a.master as "master",
        a.phone as "phone",
        a.fax as "fax",
        a.email as "email",
        a.useable as "useable",
        a.primary_person as "primaryPerson",
        a.deputy_person as "deputyPerson",
		a.create_by_id as "createBy.id",
		a.create_time as "createDate",
		a.update_by_id as "updateBy.id",
		a.update_time as "updateDate",
        a.remarks as "remarks",
        a.del_flag as "delFlag",
        a.is_public as "isPublic",
        a.administrator as "administrator",
        a.tenant_id
    </sql>

    <select id="getSecondLevelData" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select
        <include refid="officeColumns"/>
        from sys_office a
        <where>
            a.parent_id in (select id from sys_office where parent_id = 0)
            and a.del_flag = 0
        </where>
    </select>

    <!-- 分页查询用户信息 -->
    <select id="findList" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        SELECT a.id,
               a.parent_id    AS "parent.id",
               a.parent_ids,
               a.area,
               a.code,
               a.name,
               a.sort,
               a.type,
               a.grade,
               a.address,
               a.zip_code,
               a.master,
               a.phone,
               a.fax,
               a.email,
               a.remarks,
               a.create_by_id AS "createBy.id",
               a.create_time,
               a.update_by_id AS "updateBy.id",
               a.update_time,
               a.del_flag,
               a.useable      AS useable,
               a.tenant_id    AS "tenantDTO.id",
               a.administrator,
               su.name as adminName,
               p.name         AS "parent.name"
        FROM sys_office a
                 LEFT JOIN sys_office p ON p.id = a.parent_id
                 LEFT JOIN sys_user su ON su.id = a.administrator
            ${ew.customSqlSegment}
    </select>

    <select id="findValueByParameter" resultType="com.jeeplus.sys.domain.SysParameterValue" >
		SELECT
		a.*
		FROM sys_parameter_value a
		WHERE a.parameter_id=(select s.id from sys_parameter s where s.parameter=#{parameter})
		 and a.branch_office = #{branchOffice}
		 and falt='0'
		 and del_flag = '0'
	</select>
    <select id="getOfficeNameByIds" resultType="java.lang.String">
        select
        name
        from sys_office
        <where>
            del_flag = 0
            and id in
            <foreach collection="idList" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </where>
    </select>
    <select id="getOfficeById" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select
        <include refid="officeColumns"/>
        from sys_office a
        <where>
            a.id = #{id}
            and a.del_flag = 0
        </where>
    </select>

    <select id="getOfficeAllByOfficeName" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select
            a.id,
            a.parent_id AS "parent.id",
            a.parent_ids,
            a.area,
            a.code,
            a.name,
            a.sort,
            a.type,
            a.grade,
            a.address,
            a.zip_code,
            a.master,
            a.phone,
            a.fax,
            a.email,
            a.remarks,
            a.create_by_id AS "createBy.id",
            a.create_time,
            a.update_by_id AS "updateBy.id",
            a.update_time,
            a.del_flag,
            a.useable      AS useable,
            a.tenant_id    AS "tenantDTO.id",
            p.name         AS "parent.name"
        from sys_office a
        LEFT JOIN sys_office p ON p.id = a.parent_id
        <where>
            a.del_flag = 0
            <if test="officeName != null and officeName != ''">
                and (
                a.id = (select id from sys_office where name = #{officeName})
                or
                a.parent_ids like concat('%',(select id from sys_office where name = #{officeName}),'%')
                )
            </if>
        </where>
    </select>
    <select id="selectListByParentId" resultType="com.jeeplus.sys.domain.Office">
        select
        <include refid="officeColumns"/>
        from sys_office a
        <where>
            a.parent_id = #{parentId}
            and a.del_flag = 0
        </where>
    </select>
    <select id="selectListByIsPublic" resultType="com.jeeplus.sys.domain.Office">
        select
        <include refid="officeColumns"/>
        from sys_office a
        <where>
            a.is_public = #{isPublic}
            and a.del_flag = 0
        </where>
    </select>
    <select id="getChildren" resultType="com.jeeplus.sys.domain.Office">
        SELECT
            id,
            is_public,
            area,
            CODE,
            type,
            grade,
            address,
            zip_code,
            MASTER,
            phone,
            fax,
            email,
            useable,
            remarks,
            parent_id,
            parent_ids,
            NAME,
            sort,
            create_time,
            create_by_id,
            update_time,
            update_by_id,
            del_flag,
            tenant_id
        FROM
            sys_office
        WHERE
            del_flag = 0
            and parent_id = #{rootId}
        <if test="companyId != null and companyId != ''">
            and id = #{companyId}
        </if>
    </select>
    <select id="getAccessChildren" resultType="com.jeeplus.sys.domain.Office">
        SELECT
            id,
            is_public,
            area,
            CODE,
            type,
            grade,
            address,
            zip_code,
            MASTER,
            phone,
            fax,
            email,
            useable,
            remarks,
            parent_id,
            parent_ids,
            NAME,
            sort,
            create_time,
            create_by_id,
            update_time,
            update_by_id,
            del_flag,
            tenant_id
        FROM
            sys_office
        WHERE
            del_flag = 0
            and parent_id = #{rootId}
            and NAME LIKE concat('%','评估','%')
    </select>
    <select id="findAccessList" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        SELECT DISTINCT
        a.id,
               a.parent_id    AS "parent.id",
               a.parent_ids,
               a.area,
               a.code,
               a.name,
               a.sort,
               a.type,
               a.grade,
               a.address,
               a.zip_code,
               a.master,
               a.phone,
               a.fax,
               a.email,
               a.remarks,
               a.create_by_id AS "createBy.id",
               a.create_time,
               a.update_by_id AS "updateBy.id",
               a.update_time,
               a.del_flag,
               a.useable      AS useable,
               a.tenant_id    AS "tenantDTO.id",
               p.name         AS "parent.name"
        FROM sys_office a
                 LEFT JOIN sys_office p ON p.id = a.parent_id
                 LEFT JOIN sys_user_post AS user_post ON user_post.user_id = a.id
	             LEFT JOIN sys_post AS post on user_post.post_id = post.id
            ${ew.customSqlSegment}
    </select>
    <select id="getOfficeAllByFinanceOfficeName" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select
        a.id,
        a.parent_id AS "parent.id",
        a.parent_ids,
        a.area,
        a.code,
        a.name,
        a.sort,
        a.type,
        a.grade,
        a.address,
        a.zip_code,
        a.master,
        a.phone,
        a.fax,
        a.email,
        a.remarks,
        a.create_by_id AS "createBy.id",
        a.create_time,
        a.update_by_id AS "updateBy.id",
        a.update_time,
        a.del_flag,
        a.useable      AS useable,
        a.tenant_id    AS "tenantDTO.id",
        p.name         AS "parent.name"
        from sys_office a
        LEFT JOIN sys_office p ON p.id = a.parent_id
        <where>
            a.del_flag = 0
            <if test="officeName != null and officeName != ''">
                and (
                a.parent_ids like concat('%',(select id from sys_office where name = #{officeName}),'%')
                )
            </if>
        </where>
    </select>
    <select id="getFinanceChildren" resultType="com.jeeplus.sys.domain.Office">
        SELECT
            id,
            is_public,
            area,
            CODE,
            type,
            grade,
            address,
            zip_code,
            MASTER,
            phone,
            fax,
            email,
            useable,
            remarks,
            parent_id,
            parent_ids,
            NAME,
            sort,
            create_time,
            create_by_id,
            update_time,
            update_by_id,
            del_flag,
            tenant_id
        FROM
            sys_office
        WHERE
            del_flag = 0
            and parent_id = #{rootId}
            and NAME LIKE concat('%','会计','%')
    </select>
    <select id="getIds" resultType="java.lang.String">
        SELECT id FROM sys_office WHERE parent_id = #{id} and del_flag = 0
    </select>
    <select id="getChild" resultType="com.jeeplus.sys.domain.Office">
        SELECT
            id,
            is_public,
            area,
            CODE,
            type,
            grade,
            address,
            zip_code,
            MASTER,
            phone,
            fax,
            email,
            useable,
            remarks,
            parent_id,
            parent_ids,
            NAME,
            sort,
            create_time,
            create_by_id,
            update_time,
            update_by_id,
            del_flag,
            tenant_id
        FROM
            sys_office
        WHERE
            del_flag = 0
            and parent_id = #{rootId}
    </select>

    <select id="getParentOfficeById" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select
        <include refid="officeColumns"/>
        from sys_office a
        <where>
            a.id = (select parent_id from sys_office where id = #{id})
            and a.del_flag = 0
        </where>
    </select>

    <select id="getOfficeByCompany" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select
        <include refid="officeColumns"/>
        from sys_office a
        <where>
            a.parent_id = (select id from sys_office where name = #{company})
            and a.del_flag = 0
        </where>
    </select>
</mapper>
