<?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.marketing.MktLeadsGoalsMapper">
  <resultMap id="BaseResultMap" type="com.yd.dal.entity.marketing.MktLeadsGoals">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="goals_type" jdbcType="INTEGER" property="goalsType" />
    <result column="practitioner_id" jdbcType="BIGINT" property="practitionerId" />
    <result column="subordinate_system_id" jdbcType="BIGINT" property="subordinateSystemId" />
    <result column="subordinate_system_name" jdbcType="VARCHAR" property="subordinateSystemName" />
    <result column="current_year" jdbcType="INTEGER" property="currentYear" />
    <result column="statistic_time_unit" jdbcType="INTEGER" property="statisticTimeUnit" />
    <result column="seq_time" jdbcType="INTEGER" property="seqTime" />
    <result column="premium" jdbcType="DECIMAL" property="premium" />
    <result column="commission" jdbcType="DECIMAL" property="commission" />
    <result column="pieces" jdbcType="INTEGER" property="pieces" />
    <result column="piece_average_premium" jdbcType="DECIMAL" property="pieceAveragePremium" />
    <result column="current_version" jdbcType="TIMESTAMP" property="currentVersion" />
    <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" />
  </resultMap>
  <sql id="Base_Column_List">
    id, goals_type, practitioner_id, subordinate_system_id, subordinate_system_name, 
    current_year, statistic_time_unit, seq_time, premium, commission, pieces, piece_average_premium, 
    current_version, is_active, created_at, created_by, updated_at, updated_by
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from ag_mkt_leads_goals
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from ag_mkt_leads_goals
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.marketing.MktLeadsGoals" useGeneratedKeys="true">
    insert into ag_mkt_leads_goals (goals_type, practitioner_id, subordinate_system_id, 
      subordinate_system_name, current_year, statistic_time_unit, 
      seq_time, premium, commission, 
      pieces, piece_average_premium, current_version, 
      is_active, created_at, created_by, 
      updated_at, updated_by)
    values (#{goalsType,jdbcType=INTEGER}, #{practitionerId,jdbcType=BIGINT}, #{subordinateSystemId,jdbcType=BIGINT}, 
      #{subordinateSystemName,jdbcType=VARCHAR}, #{currentYear,jdbcType=INTEGER}, #{statisticTimeUnit,jdbcType=INTEGER},
      #{seqTime,jdbcType=INTEGER}, #{premium,jdbcType=DECIMAL}, #{commission,jdbcType=DECIMAL}, 
      #{pieces,jdbcType=INTEGER}, #{pieceAveragePremium,jdbcType=DECIMAL}, #{currentVersion,jdbcType=TIMESTAMP},
      #{isActive,jdbcType=INTEGER}, #{createdAt,jdbcType=TIMESTAMP}, #{createdBy,jdbcType=BIGINT}, 
      #{updatedAt,jdbcType=TIMESTAMP}, #{updatedBy,jdbcType=BIGINT})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.marketing.MktLeadsGoals" useGeneratedKeys="true">
    insert into ag_mkt_leads_goals
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="goalsType != null">
        goals_type,
      </if>
      <if test="practitionerId != null">
        practitioner_id,
      </if>
      <if test="subordinateSystemId != null">
        subordinate_system_id,
      </if>
      <if test="subordinateSystemName != null">
        subordinate_system_name,
      </if>
      <if test="currentYear != null">
        current_year,
      </if>
      <if test="statisticTimeUnit != null">
        statistic_time_unit,
      </if>
      <if test="seqTime != null">
        seq_time,
      </if>
      <if test="premium != null">
        premium,
      </if>
      <if test="commission != null">
        commission,
      </if>
      <if test="pieces != null">
        pieces,
      </if>
      <if test="pieceAveragePremium != null">
        piece_average_premium,
      </if>
      <if test="currentVersion != null">
        current_version,
      </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>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="goalsType != null">
        #{goalsType,jdbcType=INTEGER},
      </if>
      <if test="practitionerId != null">
        #{practitionerId,jdbcType=BIGINT},
      </if>
      <if test="subordinateSystemId != null">
        #{subordinateSystemId,jdbcType=BIGINT},
      </if>
      <if test="subordinateSystemName != null">
        #{subordinateSystemName,jdbcType=VARCHAR},
      </if>
      <if test="currentYear != null">
        #{currentYear,jdbcType=INTEGER},
      </if>
      <if test="statisticTimeUnit != null">
        #{statisticTimeUnit,jdbcType=INTEGER},
      </if>
      <if test="seqTime != null">
        #{seqTime,jdbcType=INTEGER},
      </if>
      <if test="premium != null">
        #{premium,jdbcType=DECIMAL},
      </if>
      <if test="commission != null">
        #{commission,jdbcType=DECIMAL},
      </if>
      <if test="pieces != null">
        #{pieces,jdbcType=INTEGER},
      </if>
      <if test="pieceAveragePremium != null">
        #{pieceAveragePremium,jdbcType=DECIMAL},
      </if>
      <if test="currentVersion != null">
        #{currentVersion,jdbcType=TIMESTAMP},
      </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>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.yd.dal.entity.marketing.MktLeadsGoals">
    update ag_mkt_leads_goals
    <set>
      <if test="goalsType != null">
        goals_type = #{goalsType,jdbcType=INTEGER},
      </if>
      <if test="practitionerId != null">
        practitioner_id = #{practitionerId,jdbcType=BIGINT},
      </if>
      <if test="subordinateSystemId != null">
        subordinate_system_id = #{subordinateSystemId,jdbcType=BIGINT},
      </if>
      <if test="subordinateSystemName != null">
        subordinate_system_name = #{subordinateSystemName,jdbcType=VARCHAR},
      </if>
      <if test="currentYear != null">
        current_year = #{currentYear,jdbcType=INTEGER},
      </if>
      <if test="statisticTimeUnit != null">
        statistic_time_unit = #{statisticTimeUnit,jdbcType=INTEGER},
      </if>
      <if test="seqTime != null">
        seq_time = #{seqTime,jdbcType=INTEGER},
      </if>
      <if test="premium != null">
        premium = #{premium,jdbcType=DECIMAL},
      </if>
      <if test="commission != null">
        commission = #{commission,jdbcType=DECIMAL},
      </if>
      <if test="pieces != null">
        pieces = #{pieces,jdbcType=INTEGER},
      </if>
      <if test="pieceAveragePremium != null">
        piece_average_premium = #{pieceAveragePremium,jdbcType=DECIMAL},
      </if>
      <if test="currentVersion != null">
        current_version = #{currentVersion,jdbcType=TIMESTAMP},
      </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>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yd.dal.entity.marketing.MktLeadsGoals">
    update ag_mkt_leads_goals
    set goals_type = #{goalsType,jdbcType=INTEGER},
      practitioner_id = #{practitionerId,jdbcType=BIGINT},
      subordinate_system_id = #{subordinateSystemId,jdbcType=BIGINT},
      subordinate_system_name = #{subordinateSystemName,jdbcType=VARCHAR},
      current_year = #{currentYear,jdbcType=INTEGER},
      statistic_time_unit = #{statisticTimeUnit,jdbcType=INTEGER},
      seq_time = #{seqTime,jdbcType=INTEGER},
      premium = #{premium,jdbcType=DECIMAL},
      commission = #{commission,jdbcType=DECIMAL},
      pieces = #{pieces,jdbcType=INTEGER},
      piece_average_premium = #{pieceAveragePremium,jdbcType=DECIMAL},
      current_version = #{currentVersion,jdbcType=TIMESTAMP},
      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}
    where id = #{id,jdbcType=BIGINT}
  </update>
    <update id="updateIsActiveIsNull">
      UPDATE ag_mkt_leads_goals
      SET is_active = 0
      WHERE practitioner_id = #{practitionerId,jdbcType=BIGINT}
        AND current_year = #{year,jdbcType=INTEGER}
        and goals_type = #{goalsType,jdbcType=INTEGER}
        AND is_active = 1;
    </update>
    <select id="selectByMktLeadsGoals" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from ag_mkt_leads_goals
      <where>
        <if test="id != null">
          id = #{id,jdbcType=BIGINT}
        </if>
        <if test="practitionerId != null">
          and practitioner_id = #{practitionerId,jdbcType=BIGINT}
        </if>
        <if test="currentYear != null">
          and current_year = #{currentYear,jdbcType=INTEGER}
        </if>
        <if test="statisticTimeUnit != null">
          and statistic_time_unit = #{statisticTimeUnit,jdbcType=INTEGER}
        </if>
        <if test="seqTime != null">
          and seq_time = #{seqTime,jdbcType=INTEGER}
        </if>
        <if test="premium != null">
          and premium = #{premium,jdbcType=INTEGER}
        </if>
        <if test="commission != null">
          and commission = #{commission,jdbcType=INTEGER}
        </if>
        <if test="pieces != null">
          and pieces = #{pieces,jdbcType=INTEGER}
        </if>
        <if test="pieceAveragePremium != null">
          and piece_average_premium = #{pieceAveragePremium,jdbcType=INTEGER}
        </if>
        <if test="currentVersion != null">
          and current_version = #{currentVersion,jdbcType=TIMESTAMP}
        </if>
        <if test="isActive != null">
          and is_active = #{isActive,jdbcType=INTEGER}
        </if>
        <if test="createdAt != null">
          and created_at = #{createdAt,jdbcType=TIMESTAMP}
        </if>
        <if test="createdBy != null">
          and created_by = #{createdBy,jdbcType=BIGINT}
        </if>
        <if test="updatedAt != null">
          and updated_at = #{updatedAt,jdbcType=TIMESTAMP}
        </if>
        <if test="updatedBy != null">
          and updated_by = #{updatedBy,jdbcType=BIGINT}
        </if>
        <if test="subordinateSystemId != null">
          and subordinate_system_id = #{subordinateSystemId,jdbcType=BIGINT}
        </if>
        <if test="subordinateSystemName != null">
          and subordinate_system_name = #{subordinateSystemName,jdbcType=VARCHAR}
        </if>
        <if test="goalsType != null">
          and goals_type = #{goalsType,jdbcType=INTEGER}
        </if>
      </where>
    </select>
    <select id="selectByPractitionerIds" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from ag_mkt_leads_goals
      where practitioner_id in
      <foreach collection="practitionerIds" item="practitionerId" index="index" open="(" close=")" separator=",">
        #{practitionerId,jdbcType=BIGINT}
      </foreach>
      and is_active = 1
      and current_year = #{year,jdbcType=INTEGER}
      and goals_type = 1
    </select>
    <select id="findByTypeAndTimeUnitAndSubordinateSystemId" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from ag_mkt_leads_goals
      where goals_type = #{goalsType,jdbcType=INTEGER}
      and statistic_time_unit = #{statisticTimeUnit,jdbcType=INTEGER}
      and subordinate_system_id = #{subordinateSystemId,jdbcType=BIGINT}
      and is_active = 1
    </select>
    <select id="findByTypeAndTimeUnitAndPractitionerId" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from ag_mkt_leads_goals
      where goals_type = #{goalsType,jdbcType=INTEGER}
      and statistic_time_unit = #{statisticTimeUnit,jdbcType=INTEGER}
      and practitioner_id = #{practitionerId,jdbcType=BIGINT}
      and is_active = 1
    </select>
</mapper>