<?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.EmailTaskInfoMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.EmailTaskInfoDO">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="task_name" property="taskName"/>
        <result column="task_type" property="taskType"/>
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="task_status" property="taskStatus"/>
        <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.EmailTaskInfoDO" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into PPW_EMAIL.email_task_info(email, task_name,task_type, start_time, end_time, task_status,
                                     isvalid, creatorid, createtime, updaterid, updatetime)
        values (#{itemDo.email}, #{itemDo.taskName}, #{itemDo.taskType}, #{itemDo.startTime}, #{itemDo.endTime}, #{itemDo.taskStatus},
                #{itemDo.isvalid}, #{itemDo.creatorId}, #{itemDo.createTime}, #{itemDo.updaterId}, #{itemDo.updateTime})
    </insert>
    <update id="updateTaskStatus">
        update PPW_EMAIL.email_task_info set task_status=#{taskStatus},updatetime=sysdate() where email=#{email} and start_time=#{startTime} and end_time=#{endTime}
    </update>
    <update id="updateTaskStatusById">
        update PPW_EMAIL.email_task_info set task_status=#{taskStatus},updatetime=#{updateTime},end_time=#{endTime} where id=#{id}
    </update>
    <delete id="deleteTaskLog">
        update PPW_EMAIL.email_task_info set isvalid=0,updatetime=sysdate(),updaterid=#{userId} where isvalid=1
        and id in
        <foreach collection="idList" index="index" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>
    <select id="searchTaskList" resultMap="BaseResultMap"
            parameterType="com.simuwang.base.pojo.dto.query.TaskPageQuery">
        select task.id,task.email, task.task_name,task.task_type, task.start_time, task.end_time, task.task_status,
        task.isvalid, task.creatorid, task.createtime, task.updaterid, task.updatetime,u.user_name
        from PPW_EMAIL.email_task_info task
        left join PPW_EMAIL.sys_user u
        on task.creatorid=u.user_id
        where task.isvalid=1
        <if test="email != null and email !=''">
            and task.email like concat('%',#{email},'%')
        </if>
        <if test="taskName != null and taskName !=''">
            and task.task_name like concat('%',#{taskName},'%')
        </if>
        <if test="startDate != null and startDate !=''">
            and task.updatetime >= #{startDate}
        </if>
        <if test="taskType != null">
            and task.task_type = #{taskType}
        </if>
        <if test="endDate != null and endDate !=''">
            and task.updatetime <![CDATA[ <= ]]> #{endDate}
        </if>
        order by task.updatetime desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countTaskList" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.TaskPageQuery">
        select count(1)
        from PPW_EMAIL.email_task_info task
        left join PPW_EMAIL.sys_user u
        on task.creatorid=u.user_id
        where task.isvalid=1
        <if test="email != null and email !=''">
            and task.email like concat('%',#{email},'%')
        </if>
        <if test="taskName != null and taskName !=''">
            and task.task_name like concat('%',#{taskName},'%')
        </if>
        <if test="startDate != null and startDate !=''">
            and task.updatetime >= #{startDate}
        </if>
        <if test="endDate != null and endDate !=''">
            and task.updatetime <![CDATA[ <= ]]> #{endDate}
        </if>
        <if test="taskType != null">
            and task.task_type = #{taskType}
        </if>
    </select>

</mapper>