<?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.gic.haoban.manage.service.dao.mapper.PendingTaskMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabPendingTask">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="enterprise_id" jdbcType="VARCHAR" property="enterpriseId"/>
        <result column="relation_id" jdbcType="VARCHAR" property="relationId"/>
        <result column="business_id" jdbcType="VARCHAR" property="businessId"/>
        <result column="task_type" jdbcType="INTEGER" property="taskType"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="task_status_flag" jdbcType="INTEGER" property="taskStatusFlag"/>
        <result column="overdue_time" jdbcType="TIMESTAMP" property="overdueTime"/>
        <result column="invalid_time" jdbcType="TIMESTAMP" property="invalidTime"/>
        <result column="store_id" jdbcType="VARCHAR" property="storeId"/>
        <result column="clerk_id" jdbcType="VARCHAR" property="clerkId"/>
        <result column="finish_time" jdbcType="TIMESTAMP" property="finishTime"/>
        <result column="finish_flag" jdbcType="INTEGER" property="finishFlag"/>
        <result column="overdue_flag" jdbcType="INTEGER" property="overdueFlag"/>
        <result column="delete_flag" jdbcType="INTEGER" property="deleteFlag"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, enterprise_id, relation_id,business_id, task_type, title, description, task_status_flag,
    overdue_time, invalid_time, store_id, clerk_id, finish_time, finish_flag, overdue_flag, 
    delete_flag, create_time, update_time
  </sql>
    
    
    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabPendingTask">
    insert into tab_haoban_pending_task (id, enterprise_id, relation_id,
      task_type, title, description, 
      task_status_flag, overdue_time, invalid_time, 
      store_id, clerk_id, finish_time, 
      finish_flag, overdue_flag, delete_flag, 
      create_time, update_time)
    values (#{id,jdbcType=INTEGER}, #{enterpriseId,jdbcType=VARCHAR}, #{relationId,jdbcType=VARCHAR},
      #{taskType,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, 
      #{taskStatusFlag,jdbcType=INTEGER}, #{overdueTime,jdbcType=TIMESTAMP}, #{invalidTime,jdbcType=TIMESTAMP}, 
      #{storeId,jdbcType=VARCHAR}, #{clerkId,jdbcType=VARCHAR}, #{finishTime,jdbcType=TIMESTAMP}, 
      #{finishFlag,jdbcType=INTEGER}, #{overdueFlag,jdbcType=INTEGER}, #{deleteFlag,jdbcType=INTEGER}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  	
    <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabPendingTask">
        insert into tab_haoban_pending_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="enterpriseId != null">
                enterprise_id,
            </if>
            <if test="relationId != null">
                relation_id,
            </if>
            <if test="businessId != null">
                business_id,
            </if>
            <if test="taskType != null">
                task_type,
            </if>
            <if test="title != null">
                title,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="taskStatusFlag != null">
                task_status_flag,
            </if>
            <if test="overdueTime != null">
                overdue_time,
            </if>
            <if test="invalidTime != null">
                invalid_time,
            </if>
            <if test="storeId != null">
                store_id,
            </if>
            <if test="clerkId != null">
                clerk_id,
            </if>
            <if test="finishTime != null">
                finish_time,
            </if>
            <if test="finishFlag != null">
                finish_flag,
            </if>
            <if test="overdueFlag != null">
                overdue_flag,
            </if>
            <if test="deleteFlag != null">
                delete_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="enterpriseId != null">
                #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="relationId != null">
                #{relationId,jdbcType=VARCHAR},
            </if>
            <if test="businessId != null">
                #{businessId,jdbcType=VARCHAR},
            </if>
            <if test="taskType != null">
                #{taskType,jdbcType=INTEGER},
            </if>
            <if test="title != null">
                #{title,jdbcType=VARCHAR},
            </if>
            <if test="description != null">
                #{description,jdbcType=VARCHAR},
            </if>
            <if test="taskStatusFlag != null">
                #{taskStatusFlag,jdbcType=INTEGER},
            </if>
            <if test="overdueTime != null">
                #{overdueTime,jdbcType=TIMESTAMP},
            </if>
            <if test="invalidTime != null">
                #{invalidTime,jdbcType=TIMESTAMP},
            </if>
            <if test="storeId != null">
                #{storeId,jdbcType=VARCHAR},
            </if>
            <if test="clerkId != null">
                #{clerkId,jdbcType=VARCHAR},
            </if>
            <if test="finishTime != null">
                #{finishTime,jdbcType=TIMESTAMP},
            </if>
            <if test="finishFlag != null">
                #{finishFlag,jdbcType=INTEGER},
            </if>
            <if test="overdueFlag != null">
                #{overdueFlag,jdbcType=INTEGER},
            </if>
            <if test="deleteFlag != null">
                #{deleteFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabPendingTask">
        update tab_haoban_pending_task
        <set>
            <if test="enterpriseId != null">
                enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="relationId != null">
                relation_id = #{relationId,jdbcType=VARCHAR},
            </if>
            <if test="businessId != null">
                business_id = #{businessId,jdbcType=VARCHAR},
            </if>
            <if test="taskType != null">
                task_type = #{taskType,jdbcType=INTEGER},
            </if>
            <if test="title != null">
                title = #{title,jdbcType=VARCHAR},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
            <if test="taskStatusFlag != null">
                task_status_flag = #{taskStatusFlag,jdbcType=INTEGER},
            </if>
            <if test="overdueTime != null">
                overdue_time = #{overdueTime,jdbcType=TIMESTAMP},
            </if>
            <if test="invalidTime != null">
                invalid_time = #{invalidTime,jdbcType=TIMESTAMP},
            </if>
            <if test="storeId != null">
                store_id = #{storeId,jdbcType=VARCHAR},
            </if>
            <if test="clerkId != null">
                clerk_id = #{clerkId,jdbcType=VARCHAR},
            </if>
            <if test="finishTime != null">
                finish_time = #{finishTime,jdbcType=TIMESTAMP},
            </if>
            <if test="finishFlag != null">
                finish_flag = #{finishFlag,jdbcType=INTEGER},
            </if>
            <if test="overdueFlag != null">
                overdue_flag = #{overdueFlag,jdbcType=INTEGER},
            </if>
            <if test="deleteFlag != null">
                delete_flag = #{deleteFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    

    <select id="getByRelationId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pending_task
        where relation_id = #{relationId}
    </select>

    <select id="listByRelationId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pending_task
        where relation_id in
              <foreach collection="relationIds" open="(" separator="," close=")" item="item">
                  #{item}
              </foreach>
    </select>

    <select id="listPendingTask" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pending_task
        where enterprise_id = #{enterpriseId}
          and store_id =#{storeId}
          and task_status_flag=0
          and delete_flag=0
        <if test="finishFlag != -1">
            and finish_flag = #{finishFlag}
        </if>
        <if test="overdueFlag != -1">
            and overdue_flag = #{overdueFlag}
        </if>
        <if test="clerkId != null">
            and clerk_id = #{clerkId}
        </if>
        <if test="taskType!=-1">
            and task_type = #{taskType}
        </if>
        order by overdue_time desc
    </select>

    <select id="listPendingTaskByCreateTime" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pending_task
        where enterprise_id = #{enterpriseId}
        and store_id =#{storeId}
        and task_status_flag=0
        and delete_flag=0
        and finish_flag =0
        <if test="clerkId != null">
            and clerk_id = #{clerkId}
        </if>
        <if test="startDate != null">
            and create_time <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null">
            and create_time <![CDATA[ <= ]]> #{endDate}
        </if>
        order by create_time desc limit 20
    </select>

    <update id="deleteByRelationIds">
        update tab_haoban_pending_task
        set
        delete_flag = 1,
        update_time = now()
        where relation_id in
        <foreach collection="relationIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <update id="updateInvalidTimeByBusinessId">
        update tab_haoban_pending_task
        set
            update_time = now(),
            invalid_time = #{invalidTime},
            overdue_time = #{invalidTime}
        where business_id = #{businessId}
    </update>

    <update id="deleteByBusinessId">
        update tab_haoban_pending_task
        set
            delete_flag = 1,
            update_time = now()
        where business_id = #{businessId} and delete_flag=0
    </update>

    <insert id="insertBatch" >
        insert into tab_haoban_pending_task ( enterprise_id, relation_id,business_id, task_type, title, description,
          task_status_flag, overdue_time, invalid_time,
          store_id, clerk_id, finish_time,
          finish_flag, overdue_flag, delete_flag,
          create_time, update_time)
        values
        <foreach collection="list" item="item" separator=",">
            ( #{item.enterpriseId,jdbcType=VARCHAR}, #{item.relationId,jdbcType=VARCHAR},#{item.businessId,jdbcType=VARCHAR},
            #{item.taskType,jdbcType=INTEGER}, #{item.title,jdbcType=VARCHAR}, #{item.description,jdbcType=VARCHAR},
            0, #{item.overdueTime,jdbcType=TIMESTAMP}, #{item.invalidTime,jdbcType=TIMESTAMP},
            #{item.storeId,jdbcType=VARCHAR}, #{item.clerkId,jdbcType=VARCHAR}, #{item.finishTime,jdbcType=TIMESTAMP},
            0, 0, 0,
            now(), now())
        </foreach>

    </insert>

    <update id="updateDelToNormalByRelationIds">
        update tab_haoban_pending_task
        set
            delete_flag=0,
            update_time = now(),
            invalid_time = #{invalidTime},
            overdue_time = #{invalidTime}
        where
              relation_id in
            <foreach collection="relationIds" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
    </update>
</mapper>