<?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.simuwang.base.mapper.EmailParseInfoMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.EmailParseInfoDO">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="sender_email" property="senderEmail"/>
        <result column="email_date" property="emailDate" />
        <result column="parse_date" property="parseDate" />
        <result column="email_title" property="emailTitle"/>
        <result column="email_type" property="emailType"/>
        <result column="parse_status" property="parseStatus"/>
        <result column="isvalid" property="isvalid"/>
        <result column="creatorid" property="creatorId"/>
        <result column="createtime" property="createTime"/>
        <result column="updaterid" property="updaterId"/>
        <result column="updatetime" property="updateTime"/>
    </resultMap>


    <insert id="insert" parameterType="com.simuwang.base.pojo.dos.EmailParseInfoDO" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into PPW_EMAIL.email_parse_info(email, email_date, parse_date, email_title, email_type, parse_status,
                                     isvalid, creatorid, createtime, updaterid, updatetime)
        values (#{itemDo.email}, #{itemDo.emailDate}, #{itemDo.parseDate}, #{itemDo.emailTitle}, #{itemDo.emailType}, #{itemDo.parseStatus},
                #{itemDo.isvalid}, #{itemDo.creatorId}, #{itemDo.createTime}, #{itemDo.updaterId}, #{itemDo.updateTime})
    </insert>

    <update id="updateParseStatus">
        update PPW_EMAIL.email_parse_info
        set parse_status = #{parseStatus}
        where isvalid = 1
          and id = #{id}
    </update>
    <resultMap id="TableResultMap" type="com.simuwang.base.pojo.vo.EmailParseInfoVO">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="parse_date" property="parseDate" />
        <result column="email_title" property="emailTitle"/>
        <result column="email_type" property="emailType"/>
        <result column="parse_status" property="parseStatus"/>
    </resultMap>
    <sql id="selectEmailParse">
        SELECT
            epi.id,
            epi.email,
            epi.email_date,
            epi.parse_date,
            epi.email_title,
            epi.email_type,
            epi.parse_status,
            epi.isvalid,
            epi.creatorid,
            epi.createtime,
            epi.updatetime,
            epi.updaterid
        FROM
            PPW_EMAIL.email_parse_info epi
    </sql>
    <!-- 查询条件 -->
    <sql id="sqlwhereSearch">
        <where>
            epi.isvalid=1
            <if test="emailTitle !=null and emailTitle !=''">
                and epi.email_title like concat('%',#{emailTitle},'%')
            </if>
            <if test="emailType !=null">
                and epi.email_type = #{emailType}
            </if>
            <if test="parseStatus !=null">
                and epi.parse_status = #{parseStatus}
            </if>
            <if test="startDate !=null and startDate != ''">
                and epi.parse_date <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate !=null and endDate != ''">
                and epi.parse_date <![CDATA[ <= ]]> #{endDate}
            </if>
        </where>
    </sql>
    <select id="searchEmailList" resultMap="BaseResultMap">
        <include refid="selectEmailParse"/>
        <include refid="sqlwhereSearch"/>
     limit #{offset},#{pageSize}
    </select>
    <select id="searchEmailById" resultMap="BaseResultMap">
        <include refid="selectEmailParse"/>
        where epi.isvalid =1 and epi.id =#{id}
    </select>

    <select id="searchEmailCount" resultType="java.lang.Integer">
        select count(1) from PPW_EMAIL.email_parse_info where isvalid=1
        <if test="parseStatus != null and parseStatus != -1">
            and parse_status=#{parseStatus}
        </if>
        <if test="parseStatus != null and parseStatus == -1">
            and parse_status != 1
        </if>
        <if test="startDate != null  and startDate != ''">
            and parse_date >= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and parse_date <![CDATA[ <= ]]> #{endDate}
        </if>
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select id, email, email_date, parse_date, email_title, email_type
        from PPW_EMAIL.email_parse_info
        where isvalid = 1
          and id = #{id}
    </select>
    <select id="countEmailList" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.EmailParseQuery">
        select count(*) from PPW_EMAIL.email_parse_info epi
        <include refid="sqlwhereSearch"/>
    </select>

    <update id="updateParseTime">
        update PPW_EMAIL.email_parse_info
        set updatetime = #{parseDate},
            parse_date = #{parseDate}
        where isvalid = 1
          and id = #{id}
    </update>

    <select id="queryValuationEmailByFileId" resultType="com.simuwang.base.pojo.dto.EmailInfoDTO">
        select t1.id as id,
               t1.email as email,
               t1.email_date as emailDate,
               t1.email_title as emailTitle,
               t1.email_type as emailType,
               t2.id as fileId,
               t2.file_name as fileName,
               t2.file_path as filePath
        from PPW_EMAIL.email_parse_info t1
        join PPW_EMAIL.email_file_info t2 on t1.id = t2.email_id and t2.isvalid = 1
        where t1.isvalid = 1
          and t1.email_type = 2
          and t2.id in
        <foreach collection="fileIdList" item="fileId" index="index" open="(" separator="," close=")">
        #{fileId}
        </foreach>
    </select>

</mapper>