<?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.yd.dal.mapper.customer.AclPractitionerPotentialAssignedTrackMapper">
  <resultMap id="BaseResultMap" type="com.yd.dal.entity.customer.AclPractitionerPotentialAssignedTrack">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="practitioner_potential_id" jdbcType="BIGINT" property="practitionerPotentialId" />
    <result column="practitioner_assigned_id" jdbcType="BIGINT" property="practitionerAssignedId" />
    <result column="track_status_id" jdbcType="BIGINT" property="trackStatusId" />
    <result column="notice" jdbcType="VARCHAR" property="notice" />
    <result column="is_active" jdbcType="INTEGER" property="isActive" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="BIGINT" property="createdBy" />
    <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
    <result column="updated_by" jdbcType="BIGINT" property="updatedBy" />
    <result column="updater_type" jdbcType="INTEGER" property="updaterType" />
    <result column="creator_type" jdbcType="INTEGER" property="creatorType" />
    <result column="is_lasted" jdbcType="INTEGER" property="isLasted" />
    <result column="track_score" jdbcType="INTEGER" property="trackScore" />
    <result column="track_time" jdbcType="TIMESTAMP" property="trackTime" />
  </resultMap>
  <sql id="Base_Column_List">
    id, practitioner_potential_id, practitioner_assigned_id, track_status_id, track_score,notice,
    is_active, created_at, created_by, updated_at, updated_by, creator_type, updater_type,is_lasted,track_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from ag_acl_practitioner_potential_assigned_track
    where id = #{id,jdbcType=BIGINT}
  </select>
  <select id="findByPractitionerIdAndPotentialIdAndTrackStatusId"
          resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from ag_acl_practitioner_potential_assigned_track
    where practitioner_assigned_id = #{practitionerId,jdbcType=BIGINT}
    AND practitioner_potential_id = #{potentialId,jdbcType=BIGINT}
    AND track_status_id = #{trackStatusId,jdbcType=BIGINT}
    AND to_days(track_time) = to_days(#{trackDate,jdbcType=TIMESTAMP})
  </select>
  <select id="findByPotentialIdAndIsLasted"
          resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from ag_acl_practitioner_potential_assigned_track
    where practitioner_potential_id = #{potentialId,jdbcType=BIGINT}
    AND is_lasted = #{isLasted,jdbcType=INTEGER}
  </select>
  <select id="findByPractitionerIdAndPotentialId"
          resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from ag_acl_practitioner_potential_assigned_track
    where practitioner_assigned_id = #{practitionerId,jdbcType=BIGINT}
    AND practitioner_potential_id = #{potentialId,jdbcType=BIGINT}
    order by track_time desc
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from ag_acl_practitioner_potential_assigned_track
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.customer.AclPractitionerPotentialAssignedTrack" useGeneratedKeys="true">
    insert into ag_acl_practitioner_potential_assigned_track (practitioner_potential_id, practitioner_assigned_id,
                                                              track_status_id, track_score,notice, is_active,
                                                              created_at, created_by, updated_at,
                                                              updated_by, updater_type,creator_type,is_lasted,track_time )
    values (#{practitionerPotentialId,jdbcType=BIGINT}, #{practitionerAssignedId,jdbcType=BIGINT},
            #{trackStatusId,jdbcType=BIGINT},#{trackScore,jdbcType=INTEGER}, #{notice,jdbcType=VARCHAR}, #{isActive,jdbcType=INTEGER},
            #{createdAt,jdbcType=TIMESTAMP}, #{createdBy,jdbcType=BIGINT}, #{updatedAt,jdbcType=TIMESTAMP},
            #{updatedBy,jdbcType=BIGINT},#{updaterType,jdbcType=INTEGER}, #{creatorType,jdbcType=INTEGER},#{isLasted,jdbcType=INTEGER},#{trackTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.customer.AclPractitionerPotentialAssignedTrack" useGeneratedKeys="true">
    insert into ag_acl_practitioner_potential_assigned_track
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="practitionerPotentialId != null">
        practitioner_potential_id,
      </if>
      <if test="practitionerAssignedId != null">
        practitioner_assigned_id,
      </if>
      <if test="trackStatusId != null">
        track_status_id,
      </if>
      <if test="trackScore != null">
        track_score,
      </if>
      <if test="notice != null">
        notice,
      </if>
      <if test="isActive != null">
        is_active,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="updatedAt != null">
        updated_at,
      </if>
      <if test="updatedBy != null">
        updated_by,
      </if>
      <if test="creatorType != null">
        creator_type,
      </if>
      <if test="updaterType != null">
        updater_type,
      </if>
      <if test="isLasted != null">
        is_lasted,
      </if>
      <if test="trackTime != null">
        track_time,
      </if>
      <if test="scheduleTaskTrackingId != null">
        schedule_task_tracking_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="practitionerPotentialId != null">
        #{practitionerPotentialId,jdbcType=BIGINT},
      </if>
      <if test="practitionerAssignedId != null">
        #{practitionerAssignedId,jdbcType=BIGINT},
      </if>
      <if test="trackStatusId != null">
        #{trackStatusId,jdbcType=BIGINT},
      </if>
      <if test="trackScore != null">
        #{trackScore,jdbcType=INTEGER},
      </if>
      <if test="notice != null">
        #{notice,jdbcType=VARCHAR},
      </if>
      <if test="isActive != null">
        #{isActive,jdbcType=INTEGER},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="updatedAt != null">
        #{updatedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="updatedBy != null">
        #{updatedBy,jdbcType=BIGINT},
      </if>
      <if test="creatorType != null">
        #{creatorType,jdbcType=INTEGER},
      </if>
      <if test="updaterType != null">
        #{updaterType,jdbcType=INTEGER},
      </if>
      <if test="isLasted != null">
        #{isLasted,jdbcType=INTEGER},
      </if>
      <if test="trackTime != null">
        #{trackTime,jdbcType=TIMESTAMP},
      </if>
      <if test="scheduleTaskTrackingId != null">
        #{scheduleTaskTrackingId,jdbcType=BIGINT},
      </if>
    </trim>
  </insert>
  <insert id="insertList" parameterType="java.util.List" useGeneratedKeys="true" >
    insert into ag_acl_practitioner_potential_assigned_track(practitioner_potential_id, practitioner_assigned_id,
    track_status_id, track_score,notice, is_active,
    created_at, created_by, updated_at,
    updated_by, updater_type,creator_type,is_lasted,track_time )
    values
    <foreach collection="potentialAssignedTrackList" item="item" index="index" separator=",">
      (
      #{item.practitionerPotentialId},
      #{item.practitionerAssignedId},
      #{item.trackStatusId},
      #{item.trackScore},
      #{item.notice},
      #{item.isActive},
      #{item.createdAt},
      #{item.createdBy},
      #{item.updatedAt},
      #{item.updatedBy},
      #{item.updaterType},
      #{item.creatorType},
      #{item.isLasted},
      #{item.trackTime}
      )
    </foreach>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.yd.dal.entity.customer.AclPractitionerPotentialAssignedTrack">
    update ag_acl_practitioner_potential_assigned_track
    <set>
      <if test="practitionerPotentialId != null">
        practitioner_potential_id = #{practitionerPotentialId,jdbcType=BIGINT},
      </if>
      <if test="practitionerAssignedId != null">
        practitioner_assigned_id = #{practitionerAssignedId,jdbcType=BIGINT},
      </if>
      <if test="trackStatusId != null">
        track_status_id = #{trackStatusId,jdbcType=BIGINT},
      </if>
      <if test="trackScore != null">
        track_score = #{trackScore,jdbcType=INTEGER},
      </if>
      <if test="notice != null">
        notice = #{notice,jdbcType=VARCHAR},
      </if>
      <if test="isActive != null">
        is_active = #{isActive,jdbcType=INTEGER},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=BIGINT},
      </if>
      <if test="updatedAt != null">
        updated_at = #{updatedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="updatedBy != null">
        updated_by = #{updatedBy,jdbcType=BIGINT},
      </if>
      <if test="updaterType != null">
        updater_type = #{updaterType,jdbcType=TIMESTAMP},
      </if>
      <if test="creatorType != null">
        creator_type = #{creatorType,jdbcType=BIGINT},
      </if>
      <if test="isLasted != null">
        is_lasted = #{isLasted,jdbcType=BIGINT},
      </if>
      <if test="trackTime != null">
        track_time = #{trackTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yd.dal.entity.customer.AclPractitionerPotentialAssignedTrack">
    update ag_acl_practitioner_potential_assigned_track
    set practitioner_potential_id = #{practitionerPotentialId,jdbcType=BIGINT},
        practitioner_assigned_id = #{practitionerAssignedId,jdbcType=BIGINT},
        track_status_id = #{trackStatusId,jdbcType=BIGINT},
        notice = #{notice,jdbcType=VARCHAR},
        is_active = #{isActive,jdbcType=INTEGER},
        created_at = #{createdAt,jdbcType=TIMESTAMP},
        created_by = #{createdBy,jdbcType=BIGINT},
        updated_at = #{updatedAt,jdbcType=TIMESTAMP},
        updated_by = #{updatedBy,jdbcType=BIGINT},
        updater_type = #{updaterType,jdbcType=INTEGER},
        creator_type = #{creatorType,jdbcType=INTEGER},
        is_lasted = #{isLasted,jdbcType=INTEGER},
        track_time = #{trackTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateAll" parameterType="java.util.List">
    <foreach collection="assignedTrackUpdateList" item = "item" index="index" separator=";">
      update ag_acl_practitioner_potential_assigned_track
      <set>
        <if test="item.practitionerPotentialId != null">
          practitioner_potential_id = #{item.practitionerPotentialId,jdbcType=BIGINT},
        </if>
        <if test="item.practitionerAssignedId != null">
          practitioner_assigned_id = #{item.practitionerAssignedId,jdbcType=BIGINT},
        </if>
        <if test="item.trackStatusId != null">
          track_status_id = #{item.trackStatusId,jdbcType=BIGINT},
        </if>
        <if test="item.trackScore != null">
          track_score = #{item.trackScore,jdbcType=INTEGER},
        </if>
        <if test="item.notice != null">
          notice = #{item.notice,jdbcType=VARCHAR},
        </if>
        <if test="item.isActive != null">
          is_active = #{item.isActive,jdbcType=INTEGER},
        </if>
        <if test="item.createdAt != null">
          created_at = #{item.createdAt,jdbcType=TIMESTAMP},
        </if>
        <if test="item.createdBy != null">
          created_by = #{item.createdBy,jdbcType=BIGINT},
        </if>
        <if test="item.updatedAt != null">
          updated_at = #{item.updatedAt,jdbcType=TIMESTAMP},
        </if>
        <if test="item.updatedBy != null">
          updated_by = #{item.updatedBy,jdbcType=BIGINT},
        </if>
        <if test="item.creatorType != null">
          creator_type = #{item.creatorType,jdbcType=INTEGER},
        </if>
        <if test="item.updaterType != null">
          updater_type = #{item.updaterType,jdbcType=INTEGER},
        </if>
        <if test="item.isLasted != null">
          is_lasted = #{item.isLasted,jdbcType=INTEGER},
        </if>
        <if test="item.trackTime != null">
          track_time = #{item.trackTime,jdbcType=TIMESTAMP},
        </if>
      </set>
      where id = #{item.id,jdbcType=BIGINT}
    </foreach>
  </update>
    <select id="findScoreByPractitionerIdAndDate" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from ag_acl_practitioner_potential_assigned_track
      where practitioner_assigned_id = #{practitionerId,jdbcType=BIGINT} and track_score > 0
      <choose>
        <when test="time == 'today'">
          and to_days(track_time) = to_days(now())
        </when>
        <when test="time == 'week'">
          and yearweek(date_format(track_time,'%Y-%m-%d') = yearweek(NOW())
        </when>
        <when test="time == 'month'">
          and date_format(track_time,'%Y%m') = date_format(curdate(),'%Y%m')
        </when>
        <when test="time == 'quarter'">
          and QUARTER(track_time) = QUARTER(NOW()) AND year(track_time)=year(now())
        </when>
        <otherwise>
          and YEAR(track_time) = YEAR(now())
        </otherwise>
      </choose>

    </select>

  <delete id="deleteScheduleTrackById" parameterType="java.lang.Long">
    delete from ag_acl_practitioner_potential_assigned_track
    where schedule_task_tracking_id = #{scheduleTrackId,jdbcType=BIGINT}
  </delete>
</mapper>