<?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.mosty.common.core.business.mapper.SysDeptMapper">

    <resultMap id="BaseResultMap" type="com.mosty.common.core.business.entity.SysDept">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="orgCode" column="org_code" jdbcType="VARCHAR"/>
        <result property="orgName" column="org_name" jdbcType="VARCHAR"/>
        <result property="rootPath" column="root_path" jdbcType="VARCHAR"/>
        <result property="orgType" column="org_type" jdbcType="VARCHAR"/>
        <result property="orgLevel" column="org_level" jdbcType="VARCHAR"/>
        <result property="orgBizType" column="org_biz_type" jdbcType="VARCHAR"/>
        <result property="orgNo" column="org_no" jdbcType="VARCHAR"/>
        <result property="parentId" column="parent_id" jdbcType="BIGINT"/>
        <result property="bmpyszm" column="bmpyszm" jdbcType="VARCHAR"/>
        <result property="orgJc" column="org_jc" jdbcType="VARCHAR"/>
        <result property="orgQc" column="org_qc" jdbcType="VARCHAR"/>
        <result property="address" column="address" jdbcType="VARCHAR"/>
        <result property="linkTel" column="link_tel" jdbcType="VARCHAR"/>
        <result property="linkMan" column="link_man" jdbcType="VARCHAR"/>
        <result property="linkManTel" column="link_man_tel" jdbcType="VARCHAR"/>
        <result property="webUrl" column="web_url" jdbcType="VARCHAR"/>
        <result property="email" column="email" jdbcType="VARCHAR"/>
        <result property="xzqh" column="xzqh" jdbcType="VARCHAR"/>
        <result property="bz" column="bz" jdbcType="VARCHAR"/>
        <result property="xtZxbz" column="xt_zxbz" jdbcType="INTEGER"/>
        <result property="xtCjsj" column="xt_cjsj" jdbcType="TIMESTAMP"/>
        <result property="xtLrsj" column="xt_lrsj" jdbcType="TIMESTAMP"/>
        <result property="xtLrrxm" column="xt_lrrxm" jdbcType="VARCHAR"/>
        <result property="xtLrrid" column="xt_lrrid" jdbcType="BIGINT"/>
        <result property="xtLrrbm" column="xt_lrrbm" jdbcType="VARCHAR"/>
        <result property="xtLrrbmid" column="xt_lrrbmid" jdbcType="VARCHAR"/>
        <result property="xtLrip" column="xt_lrip" jdbcType="VARCHAR"/>
        <result property="xtZhxgsj" column="xt_zhxgsj" jdbcType="TIMESTAMP"/>
        <result property="xtZhxgrxm" column="xt_zhxgrxm" jdbcType="VARCHAR"/>
        <result property="xtZhxgid" column="xt_zhxgid" jdbcType="BIGINT"/>
        <result property="xtZhxgrbm" column="xt_zhxgrbm" jdbcType="VARCHAR"/>
        <result property="xtZhxgrbmid" column="xt_zhxgrbmid" jdbcType="BIGINT"/>
        <result property="xtZhxgrip" column="xt_zhxgrip" jdbcType="VARCHAR"/>
        <result property="hasChildren" column="hasChildren" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,org_code,org_name,root_path,
        org_type,org_level,org_biz_type,
        org_no,parent_id,bmpyszm,
        org_jc,org_qc,address,
        link_tel,link_man,link_man_tel,
        web_url,email,xzqh,
        bz,xt_zxbz,xt_zxyz,
        xt_cjsj,xt_lrsj,xt_lrrxm,
        xt_lrrid,xt_lrrbm,xt_lrrbmid,
        xt_lrip,xt_zhxgsj,xt_zhxgrxm,
        xt_zhxgid,xt_zhxgrbm,xt_zhxgrbmid,
        xt_zhxgrip
    </sql>
    <select id="selectUserDeptList" resultType="com.mosty.common.core.login.dto.DeptDTO">
        select a.id        as deptId,
               a.root_path as path,
               a.org_name  as deptName,
               a.org_code  as deptCode,
               a.org_level as deptLevel,
               a.org_biz_type deptBizType,
               a.org_jc deptJc
        from sys_dept a
                 inner join sys_user_dept b
                            on
                                a.id = b.dept_id
        where b.user_id = #{userId}
          and a.xt_zxbz = 0
        order by a.id asc
    </select>
    <select id="selectUserPageByDept" resultType="com.mosty.common.core.business.entity.vo.UserDeptVO">
        select b.dept_id,
        b.user_id,
        a.user_name,
        a.login_name,
        a.xt_zxbz,
        a.xt_lrsj
        from sys_user a
        inner join sys_user_dept b
        on
        a.id = b.user_id
        WHERE
        a.xt_zxbz = 0
        <if test="param.deptId!=null and param.deptId!=''">
            and b.dept_id = #{param.deptId}
        </if>
        <if test="param.userId!=null and param.userId!=''">
            and b.user_id = #{param.userId}
        </if>
        order by a.id asc
    </select>
    <select id="selectRolePageByDept" resultType="com.mosty.common.core.business.entity.vo.RoleDeptVO">
        select b.id,
        b.dept_id,
        b.role_id,
        a.role_name,
        a.role_desc,
        a.xt_zxbz,
        a.xt_lrsj
        from sys_role a
        inner join sys_role_dept b
        on
        a.id = b.role_id
        WHERE
        a.xt_zxbz = 0
        <if test="param.deptId!=null and param.deptId!=''">
           AND b.dept_id = #{param.deptId}
        </if>
        <if test="param.roleId!=null and param.roleId!=''">
           AND b.role_id = #{param.roleId}
        </if>
        order by a.id asc
    </select>

    <select id="getDeptByUserId" resultMap="BaseResultMap">
        select * from sys_user_dept a, sys_dept b where a.dept_id = b.id and a.user_id = #{id}
        and b.xt_zxbz = '0'
    </select>

    <select id="getDeptListByParentId" resultMap="BaseResultMap">
        select a.*,
        case when (select count(1) from sys_dept b where b.xt_zxbz = '0' and parent_id = a.id) > 0 then 'true'
        else 'false' end hasChildren
        from sys_dept a where xt_zxbz = '0'
        <if test="parentid != null">
            and a.parent_id = #{parentid}
        </if>
        <if test="deptname != null and deptname != ''">
            and a.org_name like concat('%',#{deptname},'%')
        </if>
    </select>

    <select id="getDeptListByOrgcode" resultMap="BaseResultMap">
        select a.*,
        case when (select count(1) from sys_dept b where b.xt_zxbz = '0' and parent_id = a.id) > 0 then 'true'
        else 'false' end hasChildren
        from sys_dept a where xt_zxbz = '0'
        <if test="deptCode != null and deptCode != ''">
            and a.org_code = #{deptCode}
        </if>
        <if test="deptname != null and deptname != ''">
            and a.org_name like concat('%',#{deptname},'%')
        </if>
        <if test="deptId != null">
            and a.id = #{deptId}
        </if>
    </select>
</mapper>
