<?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.MessageSendConditionDAO">
    <resultMap id="MessageSendConditionMap" type="com.jpsoft.shinestar.modules.base.entity.MessageSendCondition">
        <id property="id" column="id_"/>
        <result property="messageId" column="message_id"/>
        <result property="companyId" column="company_id"/>
        <result property="includeChildren" column="include_children"/>
        <result property="companyStructureId" column="company_structure_id"/>
        <result property="personRoleId" column="person_role_id"/>
        <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="delFlag" column="del_flag"/>
        <result property="companyName" column="company_name"/>
        <result property="companyStructName" column="company_struct_name"/>
        <result property="roleName" column="role_name"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.MessageSendCondition">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_message_send_condition
	    (id_,message_id,company_id,include_children,company_structure_id,person_role_id,create_by,create_time,update_by,update_time,del_flag)
		values
		(
            #{id,jdbcType=VARCHAR}
            ,#{messageId,jdbcType=VARCHAR}
            ,#{companyId,jdbcType=VARCHAR}
            ,#{includeChildren,jdbcType= NUMERIC }
            ,#{companyStructureId,jdbcType=VARCHAR}
            ,#{personRoleId,jdbcType=VARCHAR}
            ,#{createBy,jdbcType=VARCHAR}
            ,#{createTime,jdbcType= TIMESTAMP }
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{updateTime,jdbcType= TIMESTAMP }
            ,#{delFlag,jdbcType= NUMERIC }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_message_send_condition where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.MessageSendCondition">
        update base_message_send_condition
        <set>
            <if test="messageId!=null">
                message_id=#{messageId,jdbcType=VARCHAR},
            </if>
            <if test="companyId!=null">
                company_id=#{companyId,jdbcType=VARCHAR},
            </if>
            <if test="includeChildren!=null">
                include_children=#{includeChildren,jdbcType= NUMERIC },
            </if>
            <if test="companyStructureId!=null">
                company_structure_id=#{companyStructureId,jdbcType=VARCHAR},
            </if>
            <if test="personRoleId!=null">
                person_role_id=#{personRoleId,jdbcType=VARCHAR},
            </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="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="MessageSendConditionMap">
        select
        id_,message_id,company_id,include_children,company_structure_id,person_role_id,create_by,create_time,update_by,update_time,del_flag
        from base_message_send_condition where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_message_send_condition where id_=#{0}
    </select>
    <select id="list" resultMap="MessageSendConditionMap">
        select * from base_message_send_condition
    </select>
    <select id="search" parameterType="hashmap" resultMap="MessageSendConditionMap">
        <![CDATA[
			select b.*,
            c.name_ as company_name,
            d.structure_name as company_struct_name,
            e.person_role_name as role_name
			from base_message_send_condition b
            inner join base_company_info c on b.company_id = c.id_
            left join base_company_structure_info d on b.company_structure_id = d.id_
            left join base_person_role_info e on b.person_role_id = e.id_
		]]>
        <where>
            b.del_flag=0
            <if test="searchParams.messageId != null">
                and b.message_id = #{searchParams.messageId}
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="findNewMessage" resultType="java.lang.String">
        select distinct(message_id)
        from base_message_send_condition a
        where a.del_flag=0
        and find_in_set(a.company_id,#{companyCode})
        and (a.company_structure_id is null or a.company_structure_id = #{companyStructureId})
        and (a.person_role_id is null or a.person_role_id=#{personRoleId})
        and not exists(
            select b.message_id from base_message_receiver b
            where b.person_id=#{personId}
            and b.del_flag=0
            and b.message_id = a.message_id
        )
    </select>
</mapper>