<?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.dxhy.admin.sys.dao.SysDeptDao">

    <select id="queryList" resultType="com.dxhy.admin.sys.entity.SysDeptEntity">
        select t1.*,(select t2.name from sys_dept t2 where t2.dept_id=t1.parent_id)parentName from sys_dept t1
        where (t1.org_type != '8' and t1.org_type != '9')
        <if test="company != null and company != '' and company != 'null'">
            and t1.company = #{company}
        </if>
    </select>

    <select id="queryListUser" resultType="com.dxhy.admin.sys.entity.SysDeptEntity">
        select t1.*,(select t2.name from sys_dept t2 where t2.dept_id=t1.parent_id)parentName from sys_dept t1 where
        and t1.org_type != '7' and t1.org_type != '8' and t1.org_type != '9' and t1.org_type != '3'
        <if test="company != null and company != '' and company != 'null'">
            and t1.company = #{company}
        </if>
    </select>

    <select id="queryListParent" resultType="com.dxhy.admin.sys.entity.SysDeptEntity">
        select t1.*,(select t2.name from sys_dept t2 where t2.dept_id=t1.parent_id)parentName from sys_dept t1 where
        (t1.dept_type = '0' or t1.dept_type = '1')
        <if test="company != null and company != '' and company != 'null'">
            and t1.company = #{company}
        </if>
    </select>

    <select id="queryDetpIdList" resultType="long">
        select dept_id
        from sys_dept
        where parent_id = #{parentId}
    </select>

    <select id="queryDeptParentId" resultType="com.dxhy.admin.sys.entity.SysDeptEntity">
        select *
        from sys_dept
        where parent_id = #{parentId}
    </select>

    <select id="queryDeptList" resultType="com.dxhy.admin.sys.entity.SysDeptEntity">
        select *
        from sys_dept
        where cj_status = '1'
          and org_type = '5'
    </select>

    <update id="updateDeptCompany">
        update
            sys_dept
        set qs_status  = #{qsStatus},
            db_name    = #{dbName},
            ace_key    = #{aceKey},
            ace_id     = #{aceId},
            is_collext = #{isCollext}
        where company = #{company}
          and org_type != '1'

    </update>
    <select id="selectDeptByCompany" resultType="com.dxhy.admin.sys.entity.SysDeptEntity" databaseId="mysql">
        select * from sys_dept
        where company = #{company}
        <if test="orgType!=null and orgType !=''">
            and org_type = #{orgType}
        </if>
        order by org_code
    </select>
<!--    group by taxno-->
    <select id="selectDeptByCompany" resultType="com.dxhy.admin.sys.entity.SysDeptEntity" databaseId="oracle">
        select * from sys_dept a
        where
        a.company=#{company}
        <if test="orgType!=null and orgType !=''">
            and a.org_type = #{orgType}
        </if>
        <if test='orgType!="1"'>
            and a.dept_id = (select max(dept_id) from sys_dept b where a.taxno=b.taxno )
        </if>
        order by a.org_code
    </select>

    <select id="getOrgCodeList" parameterType="long" resultType="string">
        select d.org_code
        from sys_user_org o left join sys_dept d
        on o.dept_id = d.dept_id
        where o.userid = #{userId}
    </select>


</mapper>
