<?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.dsk.biz.mapper.BusinessBacklogMapper">

    <resultMap type="com.dsk.biz.domain.BusinessBacklog" id="BusinessBacklogResult">
        <result property="id" column="id"/>
        <result property="businessId" column="business_id"/>
        <result property="target" column="target"/>
        <result property="task" column="task"/>
        <result property="finishTime" column="finish_time"/>
        <result property="state" column="state"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectBusinessBacklogVo">
        select id,
               business_id,
               target,
               task,
               finish_time,
               due_time,
               if(due_time &lt; now() and state = 0, 2 ,state) state,
               create_time,
               update_time
        from business_backlog
    </sql>

    <select id="selectBusinessBacklogList" parameterType="com.dsk.biz.domain.bo.BusinessBacklogListDto"
            resultType="com.dsk.biz.domain.vo.BusinessBacklogListVo">
        <include refid="selectBusinessBacklogVo"/>
        where business_id = #{dto.businessId}
        <if test="dto.state != null "> and state = #{dto.state} </if>
        <if test="dto.startTime != null "> and(due_time &gt;= #{dto.startTime} or due_time is null)  </if>
        <if test="dto.endTime != null "> and due_time &lt;= #{dto.endTime} </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectBusinessBacklogById" resultMap="BusinessBacklogResult">
        <include refid="selectBusinessBacklogVo"/>
        where id = #{id}
    </select>

    <insert id="insertBusinessBacklog" parameterType="com.dsk.biz.domain.BusinessBacklog"
            useGeneratedKeys="true" keyProperty="id">
        insert into business_backlog
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessId != null">business_id,</if>
            <if test="target != null">target,</if>
            <if test="task != null">task,</if>
            <if test="dueTime != null">due_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="businessId != null">#{businessId},</if>
            <if test="target != null">#{target},</if>
            <if test="task != null">#{task},</if>
            <if test="dueTime != null">#{dueTime},</if>
        </trim>
    </insert>

    <update id="updateBusinessBacklog" parameterType="com.dsk.biz.domain.BusinessBacklog">
        update business_backlog set finish_time = #{finishTime}, state = #{state} where id = #{id}
    </update>

    <delete id="deleteBusinessBacklogById" parameterType="Long">
        delete
        from business_backlog
        where id = #{id}
    </delete>

    <delete id="deleteBusinessBacklogByIds" parameterType="String">
        delete from business_backlog where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="overdueCount" resultType="java.lang.Integer">
        select count(id) from business_backlog where business_id = #{businessId} and due_time &lt; now() and state = 0
    </select>
</mapper>
