<?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.willalp.system.mapper.SysUserOrganizationMapper">

    <sql id="columns">
         id as id, login_name as loginName, organization_code as organizationCode, user_type as userType,
         integral as integral, effective_date as effectiveDate, is_default as isDefault,
         quota as quota, deposit as deposit
    </sql>

    <sql id="where">
        <where>
            <if test="@sqlt.Ognl@isNotEmpty(loginName)">
               and login_name = #{loginName}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(organizationCode)">
                and organization_code = #{organizationCode}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(userType)">
                and user_type = #{userType}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(integral)">
                and integral = #{integral}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(effectiveDate)">
                and effective_date = #{effectiveDate}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(isDefault)">
                and is_default = #{isDefault}
            </if>

        </where>
    </sql>

    <select id="selectSysUserOrganizationList" resultType="com.willalp.system.domain.SysUserOrganization">
        select
        a.id as id, a.login_name as loginName, a.organization_code as organizationCode, a.user_type as userType,
        a.integral as integral, a.effective_date as effectiveDate, a.is_default as isDefault,
        a.quota as quota, a.deposit as deposit ,b.nick_name as nickName
        from sys_user_organization a join sys_user b on a.login_name = b.user_name
        <where>
            <if test="@sqlt.Ognl@isNotEmpty(loginName)">
                and a.login_name = #{loginName}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(organizationCode)">
                and a.organization_code = #{organizationCode}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(userType)">
                and a.user_type = #{userType}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(integral)">
                and a.integral = #{integral}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(effectiveDate)">
                and a.effective_date = #{effectiveDate}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(isDefault)">
                and a.is_default = #{isDefault}
            </if>

        </where>
    </select>

    <select id="queryOrganizationByUserId" resultType="com.willalp.system.domain.SysUserOrganization">
        select a.id as id,
           a.login_name as loginName,
           a.organization_code as organizationCode,
           a.user_type as userType,
           a.integral as integral,
           a.effective_date as effectiveDate,
           a.is_default as isDefault,
           b.nick_name as nickName,
           a.quota as quota,
           a.deposit as deposit
        from sys_user_organization a, sys_user b
        where a.login_name = b.user_name
        and b.user_id = #{userId}
    </select>

    <select id="getOrganizationByUserName" resultType="com.willalp.system.domain.resp.OrganizationListResp">
        SELECT
            a.organization_code AS organizationCode,
            b.dept_name AS organizationName,
            a.is_default AS isDefault
        FROM
            sys_user_organization a
            JOIN sys_dept b ON a.organization_code = b.dept_code
        Where
            a.login_name = #{userName}
    </select>

    <select id="queryOrganizationByLoginName" resultType="com.willalp.system.domain.SysUserOrganization">
        select a.id as id,
               a.login_name as loginName,
               a.organization_code as organizationCode,
               a.user_type as userType,
               a.integral as integral,
               a.effective_date as effectiveDate,
               a.is_default as isDefault,
               b.nick_name as nickName,
               a.quota as quota,
               a.deposit as deposit
        from sys_user_organization a, sys_user b
        where a.login_name = b.user_name
          and a.login_name = #{loginName}
    </select>
</mapper>
