<?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.kiisoo.ic.system.mapper.PrivilageAccountDOMapper">

    <select id="selectUserAndRoleInfo" resultType="com.kiisoo.ic.system.bean.AccountBean">
        select t1.id as userId,t1.name as userName, t3.name as roleName, t3.id as roleId, t3.code as roleCode,t1.mobil,t1.abbreviation
        from privilage_user t1, privilage_user_role t2, privilage_role t3
        where t1.id = t2.user_id
        and t2.role_id = t3.id
        and t1.id = #{userId}
        limit 1
    </select>

    <select id="selectAccountIdByRoleAndSearchContent" resultType="com.kiisoo.ic.system.bean.AccountBean">
        select t1.id as accountId,t1.login as account,t2.id as userId,t5.staff_code as staffCode,t2.name as userName,t2.abbreviation,t4.name as roleName,t4.id as roleId,t1.password,t6.id as shopId,t10.name as companyName,t10.code as companyCode,t6.name as shopName,t6.code as shopCode,t4.code as roleCode,t9.id as orgId,t2.mobil
        from privilage_account t1
        join privilage_user t2 on t1.user_id = t2.id and t1.type = 1
        join privilage_user_role t3 on t2.id = t3.user_id
        join privilage_role t4 on t3.role_id = t4.id
        left join poi_store_staff t5 on t5.user_id = t2.id
        left join poi_store t6 on t6.id = t5.store_id
        left join privilage_domain_entity t7 on t7.entity_id = t6.id and t7.type = 3
        left join privilage_organizational_domain t8 on t8.domain_id = t7.domain_id
        left join privilage_organizational t9 on t9.id = t8.org_id and t9.`level` = 3
        left join privilage_organizational t10 on t9.parent_id = t10.id and t10.`level` = 2

        WHERE 1=1
        <if test="roleId != null and roleId != 0 and roleId != ''">
            and t3.role_id = #{roleId}
        </if>
        <if test="searchContent != null and searchContent != ''">
            and
            CONCAT(IFNULL(t1.login,''),IFNULL(t2.name,''))
            LIKE CONCAT('%',#{searchContent},'%')
        </if>
        <if test="companyId != null and companyId != 0 and companyId != ''">
            and t9.parent_id = #{companyId}
        </if>
        <if test="storeId != null and storeId != 0 and storeId != ''">
            and t6.id = #{storeId}
        </if>
    </select>

    <select id="selectAccountIdByRoleAndSearchContentAndYY" resultType="com.kiisoo.ic.system.bean.AccountBean">
        select t1.id as accountId,t1.login as account,t2.id as userId,t2.name as userName,t2.abbreviation,t4.name as roleName,t4.id as roleId,t1.password,t10.name as companyName,t10.code as companyCode,t4.code as roleCode,t10.id as orgId,t2.mobil
        from privilage_account t1
        join privilage_user t2 on t1.user_id = t2.id and t1.type = 1
        join privilage_user_role t3 on t2.id = t3.user_id
        join privilage_role t4 on t3.role_id = t4.id
        join privilage_organizational_member pom on pom.user_id = t2.id
        join privilage_organizational t10 on t10.id = pom.org_id and t10.`level` = 2
        WHERE 1=1
        <if test="roleId != 0">
            and t3.role_id = #{roleId}
        </if>
        <if test="searchContent != null and searchContent != ''">
            and
            CONCAT(IFNULL(t1.login,''),IFNULL(t2.name,''))
            LIKE CONCAT('%',#{searchContent},'%')
        </if>
        <if test="companyId != 0">
            and t10.id = #{companyId}
        </if>
    </select>
    <select id="selectAccountIdByShopId" resultType="java.lang.Long">
        select t3.id from poi_store_staff t1, privilage_user t2, privilage_account t3
        where t1.user_id = t2.id
        and t2.id = t3.user_id
        and t1.store_id = #{shopId}
    </select>
    <delete id="deleteByUserId">
        delete from privilage_account where user_id =#{userId}
    </delete>

    <select id="selectCompanyNameAndCodeByRoleId" resultType="com.kiisoo.ic.system.bean.AccountBean">
        select t10.name as companyName,t10.code as companyCode
        from privilage_account t1
        join privilage_user t2 on t1.user_id = t2.id and t1.type = 1
        join privilage_user_role t3 on t2.id = t3.user_id
        join privilage_role t4 on t3.role_id = t4.id
        join privilage_organizational_member pom on pom.user_id = t2.id
        join privilage_organizational t10 on t10.id = pom.org_id and t10.`level` = 2
        WHERE t2.id = #{userId}
    </select>
</mapper>
