<?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.elephant.framework.galaxy.aroad.module.info.mapper.InfoInformationsMapper">

  <resultMap id="infoInformationsMap" type="com.elephant.framework.galaxy.aroad.module.info.entity.InfoInformations">
        <id property="infoId" column="info_id"/>
        <result property="templateId" column="template_id"/>
        <result property="infoTitle" column="info_title"/>
        <result property="infoContent" column="info_content"/>
        <result property="infoRemark" column="info_remark"/>
        <result property="reporterId" column="reporter_id"/>
        <result property="reportingTime" column="reporting_time"/>
  </resultMap>

    <resultMap id="infoMap" type="com.elephant.framework.galaxy.aroad.module.info.dto.InfoDto">
        <id property="infoId" column="info_id"/>
        <result property="templateId" column="template_id"/>
        <result property="infoTitle" column="info_title"/>
        <result property="infoContent" column="info_content"/>
        <result property="infoRemark" column="info_remark"/>
        <result property="reporterId" column="reporter_id"/>
        <result property="reporter" column="reporter"/>
        <result property="reportingTime" column="reporting_time"/>
        <result property="templateName" column="template_name"/>
        <result property="templateType" column="template_type"/>
        <result property="receiverId" column="receiver_id"/>
        <result property="receiverName" column="receiver_name"/>
    </resultMap>


  <insert id="saveInformations" useGeneratedKeys="true" keyProperty="infoId">
    insert into
      info_informations(template_id, info_title,info_content,info_remark,reporter_id,reporter,reporting_time)
    values
      (#{templateId}, #{infoTitle},#{infoContent},#{infoRemark},#{reporterId},#{reporter},#{reportingTime})
  </insert>

  <!--报给我的信息-->
  <select id="getReportedToMe" resultMap="infoMap">
      select
        i.info_id,i.template_id,i.info_title,i.info_content,i.info_remark,i.reporter_id,i.reporter,i.reporting_time,
        t.template_name,t.template_type,
        r.receiver_id,r.receiver_name
      from `info_templates` t
      right join
        info_informations i
      on t.template_id = i.template_id
      inner join
        info_receivers r
      on i.info_id = r.info_id
      where r.receiver_id = #{receiverId}
  </select>

  <!--我上报的信息-->
  <select id="getIReported" resultMap="infoMap">
      select
        i.info_id,i.template_id,i.info_title,i.info_content,i.info_remark,i.reporter_id,i.reporter,i.reporting_time,
        t.template_name,t.template_type,
        r.receiver_id,group_concat(r.receiver_name) as receiver_name
      from `info_templates` t
      right join
        info_informations i
      on t.template_id = i.template_id
      inner join
        info_receivers r
      on i.info_id = r.info_id
      where i.reporter_id = #{reporterId}
      group by i.info_id
  </select>
</mapper>
