<?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.mmc.iuav.user.dao.RoleDao">


    <resultMap type="com.mmc.iuav.user.entity.RoleInfoDO"
               id="roleInfoResultMap">
        <id property="id" column="id"/>
        <result property="roleNo" column="role_no"/>
        <result property="roleName" column="role_name"/>
        <result property="isDeleted" column="is_deleted"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="backUserAccountId" column="back_user_account_id"/>
        <result property="userName" column="user_name"/>
        <result property="updateTime" column="update_time"/>
        <result property="userName" column="user_name"/>
        <result property="superAdmin" column="super_admin"/>
    </resultMap>

    <resultMap id="userAccountResultMap" type="com.mmc.iuav.user.entity.UserAccountDO">
        <id property="id" column="id"/>
        <result property="uid" column="uid"/>
        <result property="phoneNum" column="phone_num"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="userImg" column="user_img"/>
        <result property="openId" column="open_id"/>
        <result property="unionId" column="union_id"/>
        <result property="userSex" column="user_sex"/>
        <result property="email" column="email"/>
        <result property="source" column="source"/>
        <result property="accountStatus" column="account_status"/>
        <result property="remark" column="remark"/>
        <result property="portType" column="port_type"/>
        <result property="deleted" column="is_deleted"/>
        <result property="createTime" column="createTime"/>
        <result property="updateTime" column="update_time"/>

    </resultMap>

    <select id="countRoleName" resultType="int">
        select count(*)
        from role_info r
        where r.is_deleted=0
        <if test=" roleName != null and roleName != '' ">
            and role_name = #{roleName}
        </if>
    </select>

    <select id="countRoleNameId" resultType="java.lang.Integer">
        select count(*)
        from role_info r
        where r.is_deleted=0
        <if test=" roleName != null and roleName != '' ">
            and role_name = #{roleName}
        </if>
        <if test=" id != null and id != '' ">
            and id != #{id}
        </if>
    </select>

    <insert id="insertRoleInfo" useGeneratedKeys="true"
            keyProperty="id" parameterType="com.mmc.iuav.user.entity.RoleInfoDO">
        insert into role_info(role_no, role_name, back_user_account_id, remark, create_time, update_time)
        values (#{roleNo}, #{roleName}, #{backUserAccountId}, #{remark}, NOW(), NOW())
    </insert>

    <insert id="batchAddMenuIds">
        insert into menu_role_rel
        (
        role_id,menu_id,create_time
        )
        values
        <foreach collection="addMenuIds" item="menuId" separator=",">
            (
            #{roleId},#{menuId},NOW()
            )
        </foreach>
    </insert>

    <insert id="insertUserRoleRel">
        INSERT INTO user_role_rel(back_user_account_id, role_id, create_time)
        VALUES (#{backUserAccountId}, #{roleId}, NOW());
    </insert>

    <update id="updateRoleInfo"
            parameterType="com.mmc.iuav.user.entity.RoleInfoDO">
        UPDATE role_info
        <set>
            <if test=" roleName != null and roleName != '' ">
                role_name = #{roleName},
            </if>
            remark = #{remark},
            update_time=NOW()
        </set>
        where id = #{id}
    </update>

    <update id="updateUserRoleRel">
        UPDATE user_role_rel SET role_id = #{roleId}
        WHERE back_user_account_id = #{backUserAccountId};
    </update>

    <select id="listUserRoleInfo" resultType="int"
            parameterType="int">
        select back_user_account_id
        from user_role_rel
        where role_id = #{roleId}
    </select>

    <delete id="removeRoleInfo" parameterType="java.lang.Integer">
        update role_info set is_deleted = 1 where id = #{id}
    </delete>

    <delete id="batchDeleteMenuIds">
        delete from menu_role_rel where role_id = #{roleId} and menu_id in
        <foreach collection="deleteMenuIds" item="menuId" open="(" separator=","
                 close=")">
            #{menuId}
        </foreach>
    </delete>

    <select id="getRoleInfo" resultMap="roleInfoResultMap"
            parameterType="int">
        select r.id,
               r.role_no,
               r.role_name,
               r.remark,
               r.super_admin,
               r.create_time
        from role_info r
        where r.id = #{id}
          and r.is_deleted = 0
    </select>

    <select id="countPageRoleInfo" resultType="int"
            parameterType="com.mmc.iuav.user.model.qo.RoleInfoQO">
        SELECT count(*)
        FROM
        role_info r
        INNER JOIN back_user_account ua ON r.back_user_account_id = ua.id
        WHERE
        r.is_deleted =0
        <if test="id != null">
            and r.id = #{id}
        </if>
        <if test=" NumberOrName != null and NumberOrName != '' ">
            and (r.role_no =#{NumberOrName}) or
            (r.role_name =#{NumberOrName})
        </if>
    </select>


    <select id="listPageRoleInfo" resultMap="roleInfoResultMap"
            parameterType="com.mmc.iuav.user.model.qo.RoleInfoQO">
        SELECT
        r.id,
        r.role_no,
        r.role_name,
        r.remark,
        r.create_time,
        r.back_user_account_id,
        r.super_admin,
        ua.user_name,
        r.update_time
        FROM
        role_info r
        INNER JOIN back_user_account ua ON r.back_user_account_id = ua.id
        WHERE
        r.is_deleted =0
        <if test="id != null">
            and r.id = #{id}
        </if>
        <if test=" NumberOrName != null and NumberOrName != '' ">
            and (r.role_no =#{NumberOrName}) or
            (r.role_name =#{NumberOrName})
        </if>
        order by r.update_time desc, r.create_time desc
        limit #{pageNo},#{pageSize}
    </select>

    <select id="countNotDelUserAccountByRole" resultType="java.lang.Integer">
        SELECT count(*)
        FROM user_role_rel ur
                 INNER JOIN back_user_account ua ON ur.back_user_account_id = ua.id
        WHERE ur.role_id = #{id}
          AND ua.is_deleted = 0
    </select>

    <select id="listRoleMenuInfo" resultType="com.mmc.iuav.user.entity.MenuInfoDO">
        select m.id, m.pid, m.menu_name, m.dom_type, m.path_info
        from menu_info m
                 inner join menu_role_rel mr on mr.menu_id = m.id
        where mr.role_id = #{roleId}
        order by m.sort ASC
    </select>

    <select id="findList" resultType="java.lang.String">
        SELECT role_no
        FROM role_info
        where is_deleted = 0
        ORDER BY id DESC LIMIT 1;
    </select>

    <select id="getRoleInfoByUserId" resultType="com.mmc.iuav.user.entity.RoleInfoDO">
        select ri.id,ri.role_no,ri.role_name,ri.is_deleted,ri.remark,
        ri.create_time,ri.update_time,ri.back_user_account_id,ri.super_admin
        from role_info ri INNER JOIN user_role_rel urr ON ri.id = urr.role_id
        where urr.back_user_account_id = #{backUserAccountId}
    </select>

</mapper>
