<?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.PreDealLogMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanPreDealLog">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="wx_enterprise_id" jdbcType="VARCHAR" property="wxEnterpriseId"/>
        <result column="enterprise_id" jdbcType="VARCHAR" property="enterpriseId"/>
        <result column="data_id" jdbcType="VARCHAR" property="dataId"/>
        <result column="data_content" jdbcType="VARCHAR" property="dataContent"/>
        <result column="reason" jdbcType="VARCHAR" property="reason"/>
        <result column="p_data_id" jdbcType="VARCHAR" property="pDataId"/>
        <result column="task_id" jdbcType="VARCHAR" property="taskId"/>
        <result column="data_type" jdbcType="INTEGER" property="dataType"/>
        <result column="status_flag" jdbcType="INTEGER" property="statusFlag"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, wx_enterprise_id,enterprise_id, data_id, p_data_id, task_id, data_type, status_flag,reason, create_time,
    update_time
  </sql>
    <sql id="Base_Column_List_data">
        id, wx_enterprise_id,enterprise_id, data_id,data_content, p_data_id, task_id, data_type, status_flag, create_time,reason,
        update_time
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from tab_haoban_pre_deal_log
    where id = #{id,jdbcType=INTEGER}
  </delete>
    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanPreDealLog">
        insert into tab_haoban_pre_deal_log ( wx_enterprise_id,enterprise_id, data_id,data_content,
      p_data_id, task_id, data_type, 
      status_flag, create_time, update_time
      )
        values (#{wxEnterpriseId,jdbcType=VARCHAR},#{enterpriseId,jdbcType=VARCHAR},
        #{dataId,jdbcType=VARCHAR},#{dataContent}
      #{pDataId,jdbcType=VARCHAR}, #{taskId,jdbcType=VARCHAR}, #{dataType,jdbcType=INTEGER}, 
      #{statusFlag,jdbcType=INTEGER}, now(), now()
      )
  </insert>
    <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanPreDealLog">
        insert into tab_haoban_pre_deal_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id,
            </if>
            <if test="enterpriseId != null">
                enterprise_id,
            </if>
            <if test="dataId != null">
                data_id,
            </if>
            <if test="dataContent != null">
                data_content,
            </if>
            <if test="pDataId != null">
                p_data_id,
            </if>
            <if test="taskId != null">
                task_id,
            </if>
            <if test="dataType != null">
                data_type,
            </if>
            <if test="statusFlag != null">
                status_flag,
            </if>
                create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="wxEnterpriseId != null">
                #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="enterpriseId != null">
                #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="dataId != null">
                #{dataId,jdbcType=VARCHAR},
            </if>
            <if test="dataContent != null">
                #{dataContent},
            </if>
            <if test="pDataId != null">
                #{pDataId,jdbcType=VARCHAR},
            </if>
            <if test="taskId != null">
                #{taskId,jdbcType=VARCHAR},
            </if>
            <if test="dataType != null">
                #{dataType,jdbcType=INTEGER},
            </if>
            <if test="statusFlag != null">
                #{statusFlag,jdbcType=INTEGER},
            </if>
            now(),
            now()
        </trim>
    </insert>


    <insert id="insertSelectiveBatch">
        insert into tab_haoban_pre_deal_log ( wx_enterprise_id,enterprise_id, data_id,data_content,
        p_data_id, task_id, data_type,
        status_flag, create_time, update_time,reason
        )
        values
        <foreach collection="list" separator="," item="item" index="index">
            (#{item.wxEnterpriseId,jdbcType=VARCHAR},#{item.enterpriseId,jdbcType=VARCHAR},
            #{item.dataId,jdbcType=VARCHAR},#{item.dataContent},
            #{item.pDataId,jdbcType=VARCHAR}, #{item.taskId,jdbcType=VARCHAR}, #{item.dataType,jdbcType=INTEGER},
            #{item.statusFlag,jdbcType=INTEGER}, now(), now(),#{item.reason}
            )
        </foreach>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanPreDealLog">
        update tab_haoban_pre_deal_log
        <set>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="enterpriseId != null">
                enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="dataId != null">
                data_id = #{dataId,jdbcType=VARCHAR},
            </if>
            <if test="pDataId != null">
                p_data_id = #{pDataId,jdbcType=VARCHAR},
            </if>
            <if test="taskId != null">
                task_id = #{taskId,jdbcType=VARCHAR},
            </if>
            <if test="dataType != null">
                data_type = #{dataType,jdbcType=INTEGER},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,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>
    <update id="updateByPrimaryKey" parameterType="com.gic.haoban.manage.service.entity.TabHaobanPreDealLog">
    update tab_haoban_pre_deal_log
    set wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
      data_id = #{dataId,jdbcType=VARCHAR},
      p_data_id = #{pDataId,jdbcType=VARCHAR},
      task_id = #{taskId,jdbcType=VARCHAR},
      data_type = #{dataType,jdbcType=INTEGER},
      status_flag = #{statusFlag,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

    <select id="getByDataId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List_data"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId} and data_id=#{dataId}
        <if test="status!=-1">
            and status_flag=#{status}
        </if>
        <if test="dataType!=-1">
            and data_type=#{dataType}
        </if>
    </select>

    <select id="listByPDataId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId} and p_data_id=#{dataId} and data_type=#{dataType}
        <if test="status!=-1">
            and status_flag=#{status}
        </if>
    </select>

    <select id="listReByPDataId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId} and p_data_id=#{dataId} and data_type=#{dataType}
        and status_flag in(0,1,3)
    </select>

    <update id="updateStatusByDataId">
        update tab_haoban_pre_deal_log
        <set>
            status_flag = #{status,jdbcType=INTEGER},
            reason=#{reason},
            update_time = now()
        </set>
        where data_id = #{dataId,jdbcType=INTEGER} and task_id=#{taskId}
        <if test="dataType!=-1">
            and data_type=#{dataType}
        </if>
    </update>

    <select id="checkTask" resultType="Integer">
    select
     count(*)
    from tab_haoban_pre_deal_log
    where task_id = #{taskId} and data_type=#{dataType}
      and status_flag=#{status}
  </select>

    <select id="listByTaskId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId} and data_type=#{dataType}
        and status_flag=#{status}
    </select>

    <select id="countByTaskId" resultType="Integer">
        select
        COUNT(*)
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}
        <if test="dataType!=-1">
            and data_type=#{dataType}
        </if>
        <if test="status!=-1">
            and status_flag=#{status}
        </if>
    </select>


    <select id="countExcepAndPreByTaskId" resultType="Integer">
        select
        COUNT(*)
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}
        <if test="dataType!=-1">
            and data_type=#{dataType}
        </if>
        and status_flag in(0,3)
    </select>

    <select id="listRebuildDepartByTaskId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        tab_haoban_pre_deal_log a
        WHERE
        a.task_id = #{taskId}
        AND data_type = 0
        AND status_flag in(0,3)
        AND EXISTS (
        SELECT
        1
        FROM
        tab_haoban_pre_deal_log b
        WHERE
        b.task_id = #{taskId}
        AND data_type = 0
        AND a.p_data_id = b.data_id
        AND b.status_flag = 2
        )
    </select>
	
	 <select id="listAllByTaskId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}  
    </select>

    <select id="listRebuildByTaskId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId} and data_type=#{dataType}
        and status_flag in(0,3)
    </select>


    <update id="updateExceptionToPre">
        update tab_haoban_pre_deal_log
        <set>

            status_flag = 0,
            update_time = now()
        </set>
        where status_flag = 3 and task_id=#{taskId}
    </update>

    <select id="listExceptionByTask" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List_data"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}
        and status_flag =3
    </select>
    
    <select id="listDataIdByTaskIdAndDataType" resultType="java.lang.String">
        select
          data_id
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}
        and data_type =#{dataType}
    </select>


    <select id="getLastPreDataByTaskId"  resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId,jdbcType=INTEGER} order by update_time desc limit 1
    </select>

    
    <update id="updateFriendStatusByDataId">
        update tab_haoban_pre_deal_log
        <set>
            status_flag = #{status,jdbcType=INTEGER},
            reason=#{reason},
            data_content = #{dataContent},
            update_time = now()
        </set>
        where data_id = #{dataId,jdbcType=INTEGER} and task_id=#{taskId}
    </update>

    <update id="updateFriendStatusAndRelationKeyByDataId">
        update tab_haoban_pre_deal_log
        <set>
            status_flag = #{status,jdbcType=INTEGER},
            reason=#{reason},
            <if test="dataContent !=null">
                data_content = #{dataContent},
            </if>
            relation_key=#{relationKey},
            update_time = now()
        </set>
        where data_id = #{dataId,jdbcType=INTEGER} and task_id=#{taskId} and data_type = #{dataType}
    </update>


    <select id="getByRelationKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List_data"/>
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}
        and relation_key =#{relationKey}
        and data_type =#{dataType}
    </select>

    <select id="checkTaskFriend" resultType="Integer">
        select
        count(*)
        from tab_haoban_pre_deal_log
        where task_id = #{taskId}
        <if test="pDataId!=null">
            and p_data_id=#{pDataId}
        </if>
        and data_type=#{dataType}
        and status_flag in(0,1)
    </select>

    <select id="listReDataIdByPDataId" resultType="String">
        select
         data_id
        from tab_haoban_pre_deal_log
        where task_id = #{taskId} and p_data_id=#{dataId} and data_type=#{dataType}
        and status_flag in(0,1,3)
    </select>
</mapper>