<?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.SysUserOrgDao">

    <select id="queryOrgIdList" resultType="com.dxhy.admin.sys.entity.SysUserOrgEntity" databaseId="mysql">
        select uorg.id,
               uorg.userid,
               uorg.dept_id,
               dept.taxno,
               dept.taxname,
               dept.company,
               dept.upgrade_status
        from sys_user_org uorg
                 left join sys_dept dept on uorg.dept_id = dept.dept_id
        where uorg.userid = #{userId}
          and uorg.taxno is not null
          and uorg.taxno != ''
          and dept.org_type = '5'
        group by dept.taxno
        order by dept.taxno asc
    </select>
    <select id="queryOrgIdList" resultType="com.dxhy.admin.sys.entity.SysUserOrgEntity" databaseId="oracle">
        select uorg.id,
               uorg.userid,
               uorg.dept_id,
               dept.taxno,
               dept.taxname,
               dept.company,
               dept.upgrade_status
        from sys_user_org uorg
                 left join sys_dept dept on uorg.dept_id = dept.dept_id
        where uorg.userid = #{userId}
          and uorg.taxno is not null
          and dept.org_type = '5'
          and dept.dept_id = (select max(dept_id) from sys_dept sys where dept.taxno = sys.taxno)
        order by dept.taxno asc
    </select>

    <select id="queryOrgCodes" resultType="com.dxhy.admin.sys.entity.SysDeptEntity" databaseId="mysql">
        select dept.*
        from sys_user_org uorg
                 left join sys_dept dept on uorg.dept_id = dept.dept_id
        where uorg.userid = #{userId}
          and uorg.taxno is not null
          and dept.org_type = '5'
        order by dept.org_code asc
    </select>
<!--    group by dept.taxno-->
    <select id="queryOrgCodes" resultType="com.dxhy.admin.sys.entity.SysDeptEntity" databaseId="oracle">
        select dept.*
        from sys_user_org uorg
                 left join sys_dept dept on uorg.dept_id = dept.dept_id
        where uorg.userid = #{userId}
          and uorg.taxno is not null
          and dept.org_type = '5'
          and dept.dept_id = (select max(dept_id) from sys_dept sys where dept.taxno = sys.taxno)
        order by dept.org_code asc

    </select>

    <delete id="deleteBatch">
        delete from sys_user_role where role_id in
        <foreach item="roleId" collection="array" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </delete>

    <update id="updateDeptId">
        update sys_user_org
        set taxno   = #{taxno},
            taxname = #{taxname}
        where dept_id = #{deptId}
    </update>

    <select id="getOrgsByUserId" parameterType="long" resultType="com.dxhy.admin.sys.entity.SysCompanyEntity">
        select dept.org_code as companyCode,dept.name as companyName from sys_user_org org
        inner join sys_dept dept on org.dept_id = dept.dept_id
        where org.userid = #{userId}
    </select>

</mapper>
