<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.PersonRoleIconDAO">
    <resultMap id="PersonRoleIconMap" type="com.jpsoft.shinestar.modules.base.entity.PersonRoleIcon">
        <id property="id" column="id_"/>
        <result property="personRoleId" column="person_role_id"/>
        <result property="iconId" column="icon_id"/>
        <result property="commonStatus" column="common_status"/>
        <result property="defaultStatus" column="default_status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark_"/>
        <result property="iconName" column="icon_name"/>
        <result property="sortNo" column="sort_no"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonRoleIcon">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_person_role_icon
	    (id_,person_role_id,icon_id,common_status,default_status,del_flag,create_by,create_time,update_by,update_time,remark_,sort_no)
		values
		(
            #{id,jdbcType=VARCHAR}
            ,#{personRoleId,jdbcType=VARCHAR}
            ,#{iconId,jdbcType=VARCHAR}
            ,#{commonStatus,jdbcType= BOOLEAN }
            ,#{defaultStatus,jdbcType=BOOLEAN }
            ,#{delFlag,jdbcType= NUMERIC }
            ,#{createBy,jdbcType=VARCHAR}
            ,#{createTime,jdbcType= TIMESTAMP }
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{updateTime,jdbcType= TIMESTAMP }
            ,#{remark,jdbcType=VARCHAR}
            ,#{sortNo,jdbcType= NUMERIC }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_person_role_icon where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="deleteCommonByPersonRoleId">
        update base_person_role_icon set del_flag=1,update_time=now()
        where person_role_id=#{personRoleId} and common_status=1
    </update>
    <update id="deleteDefaultIconByPersonRoleId">
        update base_person_role_icon set del_flag=1,update_time=now()
        where person_role_id=#{personRoleId} and default_status=1
    </update>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonRoleIcon">
        update base_person_role_icon
        <set>
            <if test="personRoleId!=null">
                person_role_id=#{personRoleId,jdbcType=VARCHAR},
            </if>
            <if test="iconId!=null">
                icon_id=#{iconId,jdbcType=VARCHAR},
            </if>
            <if test="commonStatus!=null">
                common_status=#{commonStatus,jdbcType=BOOLEAN },
            </if>
            <if test="defaultStatus!=null">
                default_status=#{defaultStatus,jdbcType= BOOLEAN },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="remark!=null">
                remark_=#{remark,jdbcType=VARCHAR},
            </if>
            <if test="sortNo!=null">
                sort_no=#{sortNo,jdbcType= NUMERIC },
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="PersonRoleIconMap">
        select * from base_person_role_icon where id_=#{0} and del_flag = 0
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_person_role_icon where id_=#{0}
    </select>
    <select id="list" resultMap="PersonRoleIconMap">
        select * from base_person_role_icon
    </select>
    <select id="search" parameterType="hashmap" resultMap="PersonRoleIconMap">
        <![CDATA[
			select a.* from
			base_person_role_icon a,
			base_icon_info b
		]]>
        <where>
            a.icon_id = b.id_
            and a.del_flag = 0
            and b.del_flag = 0
            <if test="searchParams.id != null">
                and a.id_ = #{searchParams.id}
            </if>
            <if test="searchParams.useEnable != null">
                and b.use_enable = #{searchParams.useEnable}
            </if>
            <if test="searchParams.personRoleId != null">
                and a.person_role_id = #{searchParams.personRoleId}
            </if>
            <if test="searchParams.defaultStatus != null">
                and a.default_status = #{searchParams.defaultStatus}
            </if>
            <if test="searchParams.inIconId != null">
                <foreach collection="searchParams.inIconId" index="index" item="item" open="and a.icon_id in("
                         separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="findByPersonRoleId" resultMap="PersonRoleIconMap">
        <![CDATA[
		select a.*,b.icon_name as icon_name from base_person_role_icon a,base_icon_info b
		where a.icon_id = b.id_
		and  a.person_role_id = #{personRoleId}
		and a.del_flag = 0
		and b.del_flag = 0
		order by a.sort_no asc
		]]>
    </select>

    <select id="findByPersonRoleIdAndCommonStatus" resultMap="PersonRoleIconMap">
        <![CDATA[
		select  * from base_person_role_icon
		where del_flag = 0
		and person_role_id = #{personRoleId}
		and common_status = #{commonStatus}
        order by sort_no asc
		]]>
    </select>
    <select id="findCommonByPersonRoleId" resultMap="PersonRoleIconMap">
        <![CDATA[
		select  * from base_person_role_icon
		where del_flag = 0
		and person_role_id = #{personRoleId}
		and common_status = 1
		order by sort_no asc
		]]>
    </select>
    <select id="findDefaultIconByPersonRoleId" resultMap="PersonRoleIconMap">
        <![CDATA[
		select  * from base_person_role_icon
		where del_flag = 0
		and person_role_id = #{personRoleId}
		and default_status = 1
		order by sort_no asc
		]]>
    </select>

	<select id="findByPersonRoleIdAndIconId" resultMap="PersonRoleIconMap">
		<![CDATA[
		select * from base_person_role_icon
		where del_flag = 0
		and person_role_id = #{personRoleId}
		and icon_id = #{iconId}
		limit 1
		]]>
	</select>

    <select id="findByIconName" resultMap="PersonRoleIconMap">
        <![CDATA[
        select a.*,b.icon_name as icon_name from base_person_role_icon a left join base_icon_info b
        on a.icon_id = b.id_
        where b.icon_name = #{iconName}
        and a.del_flag = 0
        and b.del_flag = 0
        ]]>
    </select>
</mapper>