<?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.dxhy.core.dao.mailGather.MailGatherLogDao">

    <resultMap type="com.dxhy.core.model.mailGather.MailGatherLogVo" id="MailGatherLogMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="subject" column="subject" jdbcType="VARCHAR"/>
        <result property="fromAddress" column="from_address" jdbcType="VARCHAR"/>
        <result property="receiveAddress" column="receive_address" jdbcType="VARCHAR"/>
        <result property="sentDate" column="sent_date" jdbcType="VARCHAR"/>
        <result property="fileType" column="file_type" jdbcType="VARCHAR"/>
        <result property="fpdm" column="fpdm" jdbcType="VARCHAR"/>
        <result property="fphm" column="fphm" jdbcType="VARCHAR"/>
        <result property="msgUid" column="msg_uid" jdbcType="VARCHAR"/>
        <result property="ocrType" column="ocr_type" jdbcType="VARCHAR"/>
        <result property="errorMsg" column="error_msg" jdbcType="VARCHAR"/>
        <result property="fileName" column="file_name" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="MailGatherLogMap">
        select
          id, subject, from_address, receive_address, sent_date, file_type, fpdm, fphm, msg_uid, ocr_type, error_msg, file_name, create_time
        from mail_gather_log
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultType= "com.dxhy.core.model.mailGather.MailGatherLogVo" parameterType="java.util.Map">
        SELECT
        m.create_time, e.dept_name, e.username,  m.from_address, m.file_name, m.file_type, m.ocr_type, m.error_msg
        FROM
        mail_gather_log m
        LEFT JOIN email_maintain e ON m.from_address = e.email_address
        <where>
            <if test="deptId != null and deptId != ''">
                and e.dept_id = #{deptId}
            </if>
            <if test="username != null and username != ''">
                and e.username = #{username}
            </if>
            <if test="userId != null and userId != ''">
                and e.user_id = #{userId}
            </if>
            <if test="createUser != null and createUser != ''">
                and e.create_user = #{createUser}
            </if>
            <if test="ocrType != null and ocrType != ''">
                and m.ocr_type = #{ocrType}
            </if>
            <if test="startTime !=null">
                and m.create_time >= #{startTime}
            </if>
            <if test="endTime !=null">
                and #{endTime} >= m.create_time
            </if>
        </where>
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from mail_gather_log
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="subject != null and subject != ''">
                and subject = #{subject}
            </if>
            <if test="fromAddress != null and fromAddress != ''">
                and from_address = #{from}
            </if>
            <if test="receiveAddress != null and receiveAddress != ''">
                and receive_address = #{receiveAddress}
            </if>
            <if test="sentDate != null and sentDate != ''">
                and sent_date = #{sentDate}
            </if>
            <if test="fileType != null and fileType != ''">
                and file_type = #{fileType}
            </if>
            <if test="fpdm != null and fpdm != ''">
                and fpdm = #{fpdm}
            </if>
            <if test="fphm != null and fphm != ''">
                and fphm = #{fphm}
            </if>
            <if test="msgUid != null and msgUid != ''">
                and msg_uid = #{msgUid}
            </if>
            <if test="ocrType != null and ocrType != ''">
                and ocr_type = #{ocrType}
            </if>
            <if test="errorMsg != null and errorMsg != ''">
                and error_msg = #{errorMsg}
            </if>
            <if test="fileName != null and fileName != ''">
                and file_name = #{fileName}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into mail_gather_log(subject, from_address, receive_address, sent_date, file_type, fpdm, fphm, msg_uid, ocr_type, error_msg, file_name, create_time)
        values (#{subject}, #{fromAddress}, #{receiveAddress}, #{sentDate}, #{fileType}, #{fpdm}, #{fphm}, #{msgUid}, #{ocrType}, #{errorMsg}, #{fileName}, #{createTime})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into mail_gather_log(subject, from_address, receive_address, sent_date, file_type, fpdm, fphm, msg_uid, ocr_type, error_msg, file_name, create_time)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.subject}, #{entity.fromAddress}, #{entity.receiveAddress}, #{entity.sentDate}, #{entity.fileType}, #{entity.fpdm}, #{entity.fphm}, #{entity.msgUid}, #{entity.ocrType}, #{entity.errorMsg}, #{entity.fileName}, #{entity.createTime})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into mail_gather_log(subject, from_address, receive_address, sent_date, file_type, fpdm, fphm, msg_uid, ocr_type, error_msg, file_name, create_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.subject}, #{entity.fromAddress}, #{entity.receiveAddress}, #{entity.sentDate}, #{entity.fileType}, #{entity.fpdm}, #{entity.fphm}, #{entity.msgUid}, #{entity.ocrType}, #{entity.errorMsg}, #{entity.fileName}, #{entity.createTime})
        </foreach>
        on duplicate key update
        subject = values(subject),
        from_address = values(fromAddress),
        receive_address = values(receive_address),
        sent_date = values(sent_date),
        file_type = values(file_type),
        fpdm = values(fpdm),
        fphm = values(fphm),
        msg_uid = values(msg_uid),
        ocr_type = values(ocr_type),
        error_msg = values(error_msg),
        file_name = values(file_name),
        create_time = values(create_time)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update mail_gather_log
        <set>
            <if test="subject != null and subject != ''">
                subject = #{subject},
            </if>
            <if test="fromAddress != null and fromAddress != ''">
                from_address = #{fromAddress},
            </if>
            <if test="receiveAddress != null and receiveAddress != ''">
                receive_address = #{receiveAddress},
            </if>
            <if test="sentDate != null and sentDate != ''">
                sent_date = #{sentDate},
            </if>
            <if test="fileType != null and fileType != ''">
                file_type = #{fileType},
            </if>
            <if test="fpdm != null and fpdm != ''">
                fpdm = #{fpdm},
            </if>
            <if test="fphm != null and fphm != ''">
                fphm = #{fphm},
            </if>
            <if test="msgUid != null and msgUid != ''">
                msg_uid = #{msgUid},
            </if>
            <if test="ocrType != null and ocrType != ''">
                ocr_type = #{ocrType},
            </if>
            <if test="errorMsg != null and errorMsg != ''">
                error_msg = #{errorMsg},
            </if>
            <if test="fileName != null and fileName != ''">
                file_name = #{fileName},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from mail_gather_log where id = #{id}
    </delete>

</mapper>

