<?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.smppw.modaq.domain.mapper.EmailParseInfoMapper">
    <resultMap id="BaseResultMap" type="com.smppw.modaq.domain.entity.EmailParseInfoDO">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="email_key" property="emailKey"/>
        <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="fail_reason" property="failReason"/>
        <result column="attr_size" property="attrSize"/>
        <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"/>
        <result column="sender_email" property="senderEmail"/>
    </resultMap>


    <insert id="insert" parameterType="com.smppw.modaq.domain.entity.EmailParseInfoDO" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into mo_email_parse_info(email,email_key, sender_email, email_date, parse_date, email_title, email_type, parse_status, fail_reason,
                                        attr_size,isvalid, creatorid, createtime, updaterid, updatetime)
        values (#{itemDo.email}, #{itemDo.emailKey}, #{itemDo.senderEmail}, #{itemDo.emailDate}, #{itemDo.parseDate}, #{itemDo.emailTitle}, #{itemDo.emailType}, #{itemDo.parseStatus},
                #{itemDo.failReason}, #{itemDo.attrSize}, #{itemDo.isvalid}, #{itemDo.creatorId}, #{itemDo.createTime}, #{itemDo.updaterId}, #{itemDo.updateTime})
    </insert>

    <update id="updateParseStatus">
        update mo_email_parse_info
        set parse_status = #{parseStatus},
            fail_reason = #{failReason}
        where isvalid = 1
          and id = #{id}
    </update>
    <sql id="selectEmailParse">
        SELECT
            epi.id,
            epi.email,
            epi.email_key,
            epi.email_date,
            epi.parse_date,
            epi.email_title,
            epi.email_type,
            epi.parse_status,
            epi.fail_reason,
            epi.attr_size,
            epi.isvalid,
            epi.creatorid,
            epi.createtime,
            epi.updatetime,
            epi.updaterid,
            epi.sender_email
        FROM
            mo_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>-->
<!--            <if test="emailStartDate !=null and emailStartDate != ''">-->
<!--                and epi.email_date <![CDATA[ >= ]]> #{emailStartDate}-->
<!--            </if>-->
<!--            <if test="emailEndDate !=null and emailEndDate != ''">-->
<!--                and epi.email_date <![CDATA[ <= ]]> #{emailEndDate}-->
<!--            </if>-->
<!--            <if test="id !=null">-->
<!--                and epi.id = #{id}-->
<!--            </if>-->
<!--            <if test="email !=null and email !=''">-->
<!--                and epi.email like concat('%',#{email},'%')-->
<!--            </if>-->
<!--            <if test="senderEmail !=null and senderEmail !=''">-->
<!--                and epi.sender_email like concat('%',#{senderEmail},'%')-->
<!--            </if>-->
<!--        </where>-->
<!--    </sql>-->
<!--    <select id="searchEmailList" resultMap="BaseResultMap">-->
<!--        <include refid="selectEmailParse"/>-->
<!--        <include refid="sqlwhereSearch"/>-->
<!--        <choose>-->
<!--            <when test="sort != null and sort !='' and sort=='parseDate' and order != null and order !=''">-->
<!--                order by epi.parse_date ${order}-->
<!--            </when>-->
<!--            <when test="sort != null and sort !='' and sort=='emailDate'  and order != null and order !=''">-->
<!--                order by epi.email_date ${order}-->
<!--            </when>-->
<!--            <otherwise>-->
<!--                order by epi.parse_date desc-->
<!--            </otherwise>-->
<!--        </choose>-->
<!--     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 mo_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_key, email_date, parse_date, email_title, email_type, sender_email,parse_status,attr_size-->
<!--        from mo_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 mo_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>-->
<!--    <select id="searchEmailDataBoard" resultType="java.util.Map"-->
<!--            parameterType="com.simuwang.base.pojo.dto.query.DataboardQuery">-->
<!--        select parse_status as "parse_status",count(id) as "total" from PPW_EMAIL.email_parse_info-->
<!--        where isvalid=1-->
<!--        <if test="startDate != null  and startDate != ''">-->
<!--            and email_date >= #{startDate}-->
<!--        </if>-->
<!--        <if test="endDate != null and endDate != ''">-->
<!--            and email_date <![CDATA[ <= ]]> #{endDate}-->
<!--        </if>-->
<!--        <if test="email !=null and email !=''">-->
<!--            and email like concat('%',#{email},'%')-->
<!--        </if>-->
<!--        <if test="senderEmail !=null and senderEmail !=''">-->
<!--            and sender_email like concat('%',#{senderEmail},'%')-->
<!--        </if>-->
<!--        <if test="emailType !=null">-->
<!--            and email_type = #{emailType}-->
<!--        </if>-->
<!--        group by parse_status-->
<!--        order by parse_status-->
<!--    </select>-->
<!--    <select id="searchEmailTypeCount" resultType="java.util.Map"-->
<!--            parameterType="com.simuwang.base.pojo.dto.query.DataboardQuery">-->
<!--        select email_type as "email_type",count(id)  as "total" from PPW_EMAIL.email_parse_info-->
<!--        where isvalid=1-->
<!--        <if test="startDate != null  and startDate != ''">-->
<!--            and email_date >= #{startDate}-->
<!--        </if>-->
<!--        <if test="endDate != null and endDate != ''">-->
<!--            and email_date <![CDATA[ <= ]]> #{endDate}-->
<!--        </if>-->
<!--        <if test="email !=null and email !=''">-->
<!--            and email like concat('%',#{email},'%')-->
<!--        </if>-->
<!--        <if test="senderEmail !=null and senderEmail !=''">-->
<!--            and sender_email like concat('%',#{senderEmail},'%')-->
<!--        </if>-->
<!--        group by email_type-->
<!--        order by email_type-->
<!--    </select>-->
<!--    <select id="countpdfNoData" resultType="java.lang.Long">-->
<!--        select count(1) from PPW_EMAIL.email_parse_info-->
<!--        where isvalid=1-->
<!--        <if test="item.startDate != null  and item.startDate != ''">-->
<!--            and email_date >= #{item.startDate}-->
<!--        </if>-->
<!--        <if test="item.endDate != null and item.endDate != ''">-->
<!--            and email_date <![CDATA[ <= ]]> #{item.endDate}-->
<!--        </if>-->
<!--        <if test="item.email !=null and item.email !=''">-->
<!--            and email like concat('%',#{item.email},'%')-->
<!--        </if>-->
<!--        <if test="item.senderEmail !=null and item.senderEmail !=''">-->
<!--            and sender_email like concat('%',#{item.senderEmail},'%')-->
<!--        </if>-->
<!--        <if test="item.emailType !=null">-->
<!--            and email_type = #{item.emailType}-->
<!--        </if>-->
<!--        <if test="errorInfo !=null">-->
<!--            and fail_reason like concat('%',#{errorInfo},'%')-->
<!--        </if>-->
<!--    </select>-->
<!--    <select id="countEmailTotal" resultType="java.lang.Long">-->
<!--        select count(1) from mo_email_parse_info where isvalid=1-->
<!--        <if test="emailType !=null">-->
<!--            and email_type = #{emailType}-->
<!--        </if>-->
<!--    </select>-->

<!--    <select id="searchEmail" parameterType="com.smppw.modaq.domain.entity.EmailParseInfoDO" resultMap="BaseResultMap">-->
<!--        select *-->
<!--        from mo_email_parse_info where isvalid = 1-->
<!--        <if test="senderEmail !=null and senderEmail != ''">-->
<!--            and sender_email = #{senderEmail}-->
<!--        </if>-->
<!--        <if test="email != null  and email != ''">-->
<!--            and email = #{email}-->
<!--        </if>-->
<!--        <if test="emailDate != null">-->
<!--            and email_date = #{emailDate}-->
<!--        </if>-->
<!--        <if test="emailTitle != null  and emailTitle != ''">-->
<!--            and email_title = #{emailTitle}-->
<!--        </if>-->
<!--        limit 1-->
<!--    </select>-->

    <select id="countEmailByInfoAndStatus" resultType="java.lang.Integer">
        select count(1)
        from mo_email_parse_info t
        where t.sender_email = #{senderAddress}
          and t.email = #{emailAddress}
          and t.email_date = #{emailDate}
          and t.email_title = #{emailTitle}
          and t.parse_status = 1
    </select>
</mapper>