<?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.MessageInfoDAO">
    <resultMap id="MessageInfoMap" type="com.jpsoft.shinestar.modules.base.entity.MessageInfo">
        <id property="id" column="id_"/>
        <result property="title" column="title_"/>
        <result property="content" column="content_"/>
        <result property="status" column="status_"/>
        <result property="type" column="type_"/>
        <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"/>
    </resultMap>
    <resultMap id="MessagePersonMap" extends="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.PersonInfoMap"
               type="com.jpsoft.shinestar.modules.base.entity.MessagePerson">
        <result property="companyStructName" column="company_struct_name"/>
        <result property="roleName" column="role_name"/>
        <result property="receiverId" column="receiver_id"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.MessageInfo">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_message_info
	    (id_,title_,content_,status_,type_,del_flag,create_by,create_time,update_by,update_time)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{title,jdbcType=VARCHAR}
			,#{content,jdbcType=VARCHAR}
			,#{status,jdbcType= NUMERIC }
			,#{type,jdbcType=VARCHAR}
			,#{delFlag,jdbcType= NUMERIC }
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_message_info where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.MessageInfo">
        update base_message_info
        <set>
            <if test="title!=null">
                title_=#{title,jdbcType=VARCHAR},
            </if>
            <if test="content!=null">
                content_=#{content,jdbcType=VARCHAR},
            </if>
            <if test="status!=null">
                status_=#{status,jdbcType= NUMERIC },
            </if>
            <if test="type!=null">
                type_=#{type,jdbcType=VARCHAR},
            </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>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="MessageInfoMap">
        select * from base_message_info
        where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_message_info where id_=#{0}
    </select>
    <select id="list" resultMap="MessageInfoMap">
        select * from base_message_info
    </select>
    <select id="search" parameterType="hashmap" resultMap="MessageInfoMap">
        <![CDATA[
			select
			id_,title_,status_,type_,del_flag,create_by,create_time,update_by,update_time
			from base_message_info
		]]>
        <where>
            del_flag=0
            <if test="searchParams.title != null">
                and title_ like #{searchParams.title}
            </if>
            <if test="searchParams.type != null">
                and type_ = #{searchParams.type}
            </if>
            <if test="searchParams.status != null">
                and status_ = #{searchParams.status}
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="personSearch" resultMap="MessagePersonMap">
        select
        a.id_,
        a.sex_,
        a.phone_,
        a.name_,
        c.name_ as company_name,
        d.structure_name as company_struct_name,
        e.person_role_name as role_name,
        f.id_ as receiver_id
        from base_person_info a
        inner join base_person_popedom b on a.id_ = b.person_id and b.del_flag=0
        inner join base_company_info c on b.company_id = c.id_ and c.del_flag=0
        left join base_company_structure_info d on b.company_structure_id = d.id_ and d.del_flag=0
        left join base_person_role_info e on b.person_role_id = e.id_ and e.del_flag=0
        left join base_message_receiver f on f.person_id = a.id_ and f.message_id = #{searchParams.messageId} and f.del_flag=0
        where a.del_flag=0
        <if test="searchParams.companyId != null">
            and b.company_id = #{searchParams.companyId}
        </if>
        <if test="searchParams.companyCode != null">
            and c.code_ like #{searchParams.companyCode}
        </if>
        <if test="searchParams.companyStructureId != null">
            and b.company_structure_id = #{searchParams.companyStructureId}
        </if>
        <if test="searchParams.roleId != null">
            and b.person_role_id = #{searchParams.roleId}
        </if>
        <if test="searchParams.personName != null">
            and a.name_ like #{searchParams.personName}
        </if>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
</mapper>