<?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.zcloud.mapper.datasource.system.DepartmentMapper">

    <resultMap type="Department" id="departmentResultMap">
        <id column="DEPARTMENT_ID" property="DEPARTMENT_ID"/>
        <result column="NAME" property="NAME"/>
        <result column="PARENT_ID" property="PARENT_ID"/>
    </resultMap>

    <!--表名 -->
    <sql id="tableName">
        OA_DEPARTMENT
    </sql>

    <!-- 字段 -->
    <sql id="Field">
        NAME
        ,
		NAME_EN,
		BIANMA,
		PARENT_ID,
		HEADMAN,
		TEL,
		FUNCTIONS,
		ADDRESS,
		BZ,
		DEPARTMENT_ID,
		CORPINFO_ID,
		LEVEL,
		DEP_ORDER,
		ISSUPERVISE,
		ISDELETE,
		LEADER_CHARGE,
		STATE,
		checkedIds,
		FOREIGNPERSONNEL,
		deptId,
		pDeptId,
		DJBM
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{NAME}
        ,
        #{NAME_EN},
        #{BIANMA},
        #{PARENT_ID},
        #{HEADMAN},
        #{TEL},
        #{FUNCTIONS},
        #{ADDRESS},
        #{BZ},
        #{DEPARTMENT_ID},
        #{CORPINFO_ID},
        #{LEVEL},
        #{DEP_ORDER},
        #{ISSUPERVISE},
        0,
        #{LEADER_CHARGE},
        #{STATE},
        #{checkedIds},
        #{FOREIGNPERSONNEL},
        #{deptId},
        #{pDeptId},
        #{DJBM}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>

    <!-- 删除-->
    <update id="delete" parameterType="pd">
        update
        <include refid="tableName"></include>
        set ISDELETE = 1
        where
        DEPARTMENT_ID = #{DEPARTMENT_ID}
    </update>

    <!-- 修改 -->
    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        NAME = #{NAME},
        NAME_EN = #{NAME_EN},
        HEADMAN = #{HEADMAN},
        BZ = #{BZ},
        TEL = #{TEL},
        FUNCTIONS = #{FUNCTIONS},
        ADDRESS = #{ADDRESS},
        LEVEL = #{LEVEL},
        DEPARTMENT_ID = DEPARTMENT_ID,
        DEP_ORDER = #{DEP_ORDER},
        ISSUPERVISE = #{ISSUPERVISE},
        LEADER_CHARGE = #{LEADER_CHARGE},
        STATE = #{STATE},
        checkedIds = #{checkedIds},
        FOREIGNPERSONNEL = #{FOREIGNPERSONNEL},
        DJBM = #{DJBM}
        where ISDELETE = 0 and
        DEPARTMENT_ID = #{DEPARTMENT_ID}
    </update>

    <select id="saftmanagelist" parameterType="pd" resultType="com.zcloud.entity.PageData">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where 1=1
        and CORPINFO_ID = #{CORPINFO_ID}
        and STATE = #{STATE}
    </select>

    <!-- 导出全部数据 -->
    <select id="islistAll" parameterType="pd" resultType="com.zcloud.entity.PageData">
        select
        f.NAME,u1.name as HEADMAN_NAME,s.name as leName
        from
        <include refid="tableName"></include>f
        LEFT JOIN SYS_DICTIONARIES s on s.BIANMA = f.LEVEL
        LEFT JOIN sys_user u1 on u1.USER_ID = f.HEADMAN
        where 1=1
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            AND
            f.CORPINFO_ID = #{CORPINFO_ID}
        </if>
        order by f.LEVEL ,f.DEP_ORDER asc
    </select>
    <!-- 向下递归查询企业部门（集团部门+子公司部门） -->
    <select id="Dept" parameterType="page" resultType="com.zcloud.entity.PageData">
        SELECT
        f.DEPARTMENT_ID,
        f.PARENT_ID,
        f.NAME DEPT_NAME,
        COUNT(c.DEPARTMENT_ID) NEXTCOUNT
        FROM OA_DEPARTMENT f
        LEFT JOIN OA_DEPARTMENT c ON c.PARENT_ID = f.DEPARTMENT_ID
        WHERE 1 = 1 and f.CORPINFO_ID = #{pd.CORPINFO_ID}
        <if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
            AND f.DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
        </if>
        <if test="pd.PARENT_ID != null and pd.PARENT_ID != ''">
            AND f.PARENT_ID = #{pd.PARENT_ID}
        </if>
        GROUP BY f.DEPARTMENT_ID
        ORDER BY f.DEP_ORDER ASC
    </select>

    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>,
        (select NAME from OA_DEPARTMENT o where t.PARENT_ID=o.DEPARTMENT_ID) as PARENT_NAME
        from
        <include refid="tableName"></include>
        t
        where
        DEPARTMENT_ID = #{DEPARTMENT_ID}
    </select>
    <select id="findByName" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        t ,
        (select NAME from OA_DEPARTMENT o where t.PARENT_ID=o.DEPARTMENT_ID) as PARENT_NAME
        from
        <include refid="tableName"></include>
        t
        where t.ISDELETE = 0 and
        t.NAME = #{NAME} and t.CORPINFO_ID = #{CORPINFO_ID}
        <if test="PARENT_ID != null and PARENT_ID != ''">
            AND t.PARENT_ID = #{PARENT_ID}
        </if>
        <if test="ISEDIT != null and ISEDIT != ''">
            AND t.DEPARTMENT_ID != #{DEPARTMENT_ID}
        </if>
    </select>

    <!--通过条件获取全部 -->
    <select id="findByCorpId" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = 0 and
        f.CORPINFO_ID = #{CORPINFO_ID} and f.PARENT_ID='0'
    </select>

    <!-- 通过编码获取数据 -->
    <select id="findByBianma" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        BIANMA = #{BIANMA}
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where
        ISDELETE = 0
        <if test="pd.DEPARTMENT_ID!= null and pd.DEPARTMENT_ID != ''"><!-- 检索 -->
            and PARENT_ID = #{pd.DEPARTMENT_ID}
        </if>
        <if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
            AND
            CORPINFO_ID = #{pd.CORPINFO_ID}
        </if>

        <if test="pd.keywords != null and pd.keywords != ''"><!-- 关键词检索 -->
            and
            (
            NAME LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
            or
            NAME_EN LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
            or
            BIANMA LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
            )
        </if>
        order by DEP_ORDER asc , NAME
    </select>

    <!-- 通过ID获取其子级列表 -->
    <select id="listSubDepartmentByParentId" parameterType="String" resultMap="departmentResultMap">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where ISDELETE = 0
        <if test="_parameter  != null and _parameter  != ''"><!-- 关键词检索 -->
            and PARENT_ID = #{parentId}
        </if>

        order by DEP_ORDER asc, NAME
    </select>


    <!-- 通过ID获取其子级列表 -->
    <select id="listSubDepByParentId" parameterType="pd" resultMap="departmentResultMap">
        select
        f.*,
        IF(o.SUPERVISIONDEPARTMENT_ID is not null,'false','true') DISABLED

        from
        <include refid="tableName"></include>
        f
        left join oa_supervision_department o on f.DEPARTMENT_ID = o.SUB_DEPARTMENT_ID AND o.SUP_DEPARTMENT_ID =
        #{SUP_DEPARTMENT_ID} and o.isdelete= '0'

        where f.ISDELETE = 0

        <if test="parentId  != null and parentId  != ''"><!-- 关键词检索 -->
            and f.PARENT_ID = #{parentId}
        </if>
        order by f.DEP_ORDER asc, f.NAME
    </select>

    <!-- 通过ID获取其子级列表 -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where ISDELETE = 0
        <if test="parameter  != null and parameter  != ''"><!-- 关键词检索 -->
            and PARENT_ID = #{parameter}
        </if>
        <if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
            AND
            CORPINFO_ID = #{CORPINFO_ID}
        </if>
        <if test="PARENT_ID != null and PARENT_ID != ''"><!-- 关键词检索 -->
            AND
            PARENT_ID = #{PARENT_ID}
        </if>
        order by DEP_ORDER asc ,NAME
    </select>

    <!-- 列表关联部门级别 -->
    <select id="forLevelNamedatalistPage" parameterType="page" resultType="pd">
        select
        f.NAME,
        f.NAME_EN,
        getFullName(f.DEPARTMENT_ID) DEPARTMENTNAME_ALL,
        f.BIANMA,
        f.PARENT_ID,
        f.HEADMAN,
        f.TEL,
        f.FUNCTIONS,
        f.ADDRESS,
        f.BZ,
        f.DEPARTMENT_ID,
        f.CORPINFO_ID,
        f.LEVEL,
        f.ISSUPERVISE,
        f.LEADER_CHARGE,
        f.STATE,
        f.checkedIds,
        f.DJBM,
        f.deptId,
        s.name as leName,
        f.DEP_ORDER
        from OA_DEPARTMENT f
        LEFT JOIN SYS_DICTIONARIES s on s.BIANMA = f.LEVEL
        where
        f.ISDELETE = 0
        <if test="pd.keywords != null and pd.keywords != ''"><!-- 关键词检索 -->
            and
            (
            f.NAME LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
            or
            f.NAME_EN LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
            or
            f.BIANMA LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
            )
        </if>
        <if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
            AND
            f.CORPINFO_ID = #{pd.CORPINFO_ID}
        </if>
        <if test="pd.LEVEL != null and pd.LEVEL != ''"><!-- 关键词检索 -->
            AND
            f.LEVEL = #{pd.LEVEL}
        </if>
        <if test="pd.DEPARTMENT_ID!= null and pd.DEPARTMENT_ID != ''"><!-- 检索 -->
            and f.PARENT_ID = #{pd.DEPARTMENT_ID}
        </if>
        <!--		<if test="pd.SELECT_DEPARTMENT_ID != null and pd.SELECT_DEPARTMENT_ID != ''">&lt;!&ndash; 关键词检索 &ndash;&gt;-->
        <!--			AND-->
        <!--				 (f.PARENT_ID = #{pd.SELECT_DEPARTMENT_ID} or f.DEPARTMENT_ID = #{pd.SELECT_DEPARTMENT_ID})-->
        <!--		</if>-->

        <choose>
            <when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
                <if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
                    and
                    f.DEPARTMENT_ID in
                    <foreach item="item" index="index"
                             collection="pd.DEPT_IDS" open="(" separator="," close=")">
                        '${item}'
                    </foreach>
                </if>
            </when>
            <otherwise>
                <choose>
                    <when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
                        <choose>
                            <when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
                                and
                                f.DEPARTMENT_ID in
                                <foreach item="item" index="index"
                                         collection="pd.DEPT_IDS" open="(" separator="," close=")">
                                    '${item}'
                                </foreach>
                            </when>
                            <otherwise>
                                and
                                f.DEPARTMENT_ID in (
                                select
                                SUB_DEPARTMENT_ID
                                from
                                oa_supervision_department osd
                                where
                                osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
                                and osd.ISDELETE = '0'
                                and osd.CORPINFO_ID = #{pd.CORPINFO_ID}
                                )
                            </otherwise>
                        </choose>
                    </when>
                    <when test="pd.DEPARTMENT_ID == '' and pd.SELECT_DEPARTMENT_ID != null and pd.SELECT_DEPARTMENT_ID != ''">
                        and (f.PARENT_ID = #{pd.SELECT_DEPARTMENT_ID} or f.DEPARTMENT_ID = #{pd.SELECT_DEPARTMENT_ID})
                    </when>
                    <otherwise>
                    </otherwise>
                </choose>
            </otherwise>
        </choose>
        order by DEP_ORDER asc ,NAME
    </select>

    <!-- 查询公司部门总数 -->
    <select id="getDepCount" parameterType="pd" resultType="int">
        SELECT COUNT(1)
        FROM oa_department
        WHERE ISDELETE = 0
          and CORPINFO_ID = #{CORPINFO_ID}
    </select>

    <!-- 查询公司检查过清单的部门总数 -->
    <select id="getWorkedDepCount" parameterType="pd" resultType="int">
        SELECT count(1)
        FROM (SELECT count(s.DEPARTMENT_ID)
              FROM oa_department s
                       LEFT JOIN bus_checkuser cu ON s.DEPARTMENT_ID = cu.DEPARTMENT_ID
                       LEFT JOIN bus_checkrecord f ON f.CHECKRECORD_ID = cu.CHECKRECORD_ID
              WHERE s.CORPINFO_ID = #{CORPINFO_ID}
                AND s.ISDELETE = 0
                AND f.FINISHED = '1'
                AND f.isdelete = '0'
                AND f.CHECKRECORD_ID IS NOT NULL
                AND DATE_FORMAT(f.CHECK_TIME, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')
              GROUP BY s.DEPARTMENT_ID) q
    </select>

    <select id="findBySubId" parameterType="pd" resultType="pd">
        select
        f.*
        from
        oa_supervision_department f
        where f.ISDELETE = 0
        <if test="SUP_DEPARTMENT_ID  != null and SUP_DEPARTMENT_ID  != ''"><!-- 关键词检索 -->
            and f.SUP_DEPARTMENT_ID = #{SUP_DEPARTMENT_ID}
        </if>
    </select>
    <!-- 向下递归查询企业部门（集团部门+子公司部门） -->
    <select id="listDept" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT
        f.DEPARTMENT_ID,
        f.PARENT_ID,
        f.NAME DEPT_NAME,
        COUNT(c.DEPARTMENT_ID) NEXTCOUNT
        FROM OA_DEPARTMENT f
        LEFT JOIN OA_DEPARTMENT c ON c.PARENT_ID = f.DEPARTMENT_ID
        WHERE 1 = 1
        <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
            AND f.DEPARTMENT_ID = #{DEPARTMENT_ID}
        </if>
        <if test="PARENT_ID != null and PARENT_ID != ''">
            AND f.PARENT_ID = #{PARENT_ID}
        </if>
        group by f.DEPARTMENT_ID
        ORDER BY f.DEP_ORDER ASC
    </select>

    <select id="getSonIdsByParid" parameterType="pd" resultType="pd">
        SELECT
        f.DEPARTMENT_ID,
        f.`NAME` as DEPT_NAME,
        queryRecursiveDeptDown ( f.DEPARTMENT_ID ) as ids
        FROM
        oa_department f
        LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = f.CORPINFO_ID
        WHERE
        c.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
        <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
            AND f.DEPARTMENT_ID = #{DEPARTMENT_ID}
        </if>
        <if test="PARENT_ID != null and PARENT_ID != ''">
            AND f.PARENT_ID = #{PARENT_ID}
        </if>
        ORDER BY f.DEP_ORDER ASC
    </select>

    <!-- 向下递归查询企业部门（只查企业端部门表集团部门） -->
    <select id="listTreeManageAndCorp2" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT
        DEPARTMENT_ID id,
        NAME name,
        PARENT_ID pId
        FROM <include refid="tableName"></include> f
    </select>

    <select id="listTreeManageAndCorpNum" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT
        DEPARTMENT_ID id,
        NAME name,
        PARENT_ID pId
        FROM <include refid="tableName"></include> f
        where 1=1
        <if test="corpId != null and corpId.size()>0"><!-- 关键词检索 -->
            and f.CORPINFO_ID in
            <foreach collection="corpId" index="index" item="item" open="(" separator="," close=")">
                #{corpId[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 向下递归查询企业部门 -->
    <select id="listTreeCorpDept" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT
        DEPARTMENT_ID id,
        NAME name,
        PARENT_ID pId
        FROM <include refid="tableName"></include> f
        WHERE f.CORPINFO_ID = #{CORPINFO_ID}
        order by f.DEP_ORDER ASC ,f.NAME ASC
    </select>
    <!-- 向下递归查询企业部门 -->
    <select id="listForDoor" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT
        f.DEPARTMENT_ID,
        NAME
        FROM <include refid="tableName"></include> f
        LEFT JOIN sys_user_examine e on e.DEPARTMENT_ID = f.DEPARTMENT_ID
        WHERE f.CORPINFO_ID = #{CORPINFO_ID}
        <if test="FIELD_ITEM!= null and FIELD_ITEM != ''"><!-- 负责人 -->
            and e.${FIELD_ITEM} = '1'
        </if>
        GROUP BY f.DEPARTMENT_ID
        order by f.DEP_ORDER ASC ,f.NAME ASC
    </select>
    <select id="listDepartAllByCorpinfoId" parameterType="String" resultMap="departmentResultMap">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        where 1=1
        AND CORPINFO_ID = #{CORPINFO_ID}
        order by DEP_ORDER asc, NAME
    </select>
    <!-- 向下递归查询企业部门（只查监管端部门表集团部门） -->
    <select id="listTreeManageAndCorp1" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT DEPARTMENT_ID id,
               NAME          name,
               PARENT_ID     pId
        FROM `qa-gwj-regulatory`.sys_department f
        WHERE FIND_IN_SET(DEPARTMENT_ID, `qa-gwj-regulatory`.queryRecursiveDeptDown(#{DEPARTMENT_ID}))
    </select>
    <!-- 向下递归查询企业部门（集团部门+子公司部门） -->
    <select id="listTreeManageAndCorpForPcPunishThePerson" parameterType="pd" resultType="com.zcloud.entity.PageData">
        SELECT
        DEPARTMENT_ID id,
        NAME name,
        PARENT_ID pId
        FROM `qa-gwj-regulatory`.sys_department f
        WHERE FIND_IN_SET(DEPARTMENT_ID,`qa-gwj-regulatory`.queryRecursiveDeptDown(#{DEPARTMENT_ID}))
        UNION ALL
        SELECT
        DEPARTMENT_ID id,
        NAME name,
        PARENT_ID pId
        FROM <include refid="tableName"></include> f
        <if test=" ids != null">
            <foreach item="item" index="index" collection="ids" open="WHERE" separator="or">
                <if test=" item != null and item != ''">
                    CORPINFO_ID like CONCAT(CONCAT('%', #{item}),'%')
                </if>
            </foreach>
        </if>
    </select>

    <select id="saftpersonlist" parameterType="pd" resultType="com.zcloud.entity.PageData">
        select *
        from `qa-gwj-regulatory`.SYS_DEPARTMENT
        where 1 = 1
          and STATE = '0'
    </select>
    <select id="getDeptAllByCorp" resultType="java.util.Map">
        select
               name,
               NAME_EN,
               BIANMA,
               PARENT_ID,
               PARENT_ID pId,
               HEADMAN,
               TEL,
               FUNCTIONS,
               ADDRESS,
               BZ,
               DEPARTMENT_ID,
               DEPARTMENT_ID id,
               CORPINFO_ID,
               LEVEL,
               DEP_ORDER,
               ISSUPERVISE,
               ISDELETE
               LEADER_CHARGE,
               STATE,
               checkedIds,
               DJBM
        from oa_department
        where ISDELETE = 0
          and CORPINFO_ID = #{CORPINFO_ID}
    </select>
</mapper>
