<?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.MktLeadsAssignedsMapper">
  <resultMap id="BaseResultMap" type="com.yd.dal.entity.marketing.MktLeadsAssigneds">
    <!--@mbg.generated-->
    <!--@Table ag_mkt_leads_assigneds-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="assigned_practitioner_id" jdbcType="BIGINT" property="assignedPractitionerId" />
    <result column="customer_id" jdbcType="BIGINT" property="customerId" />
    <result column="FYP" jdbcType="DECIMAL" property="fyp" />
    <result column="FYC" jdbcType="DECIMAL" property="fyc" />
    <result column="time_to_close" jdbcType="TIMESTAMP" property="timeToClose" />
    <result column="pieces" jdbcType="INTEGER" property="pieces" />
    <result column="remark" jdbcType="LONGVARCHAR" property="remark" />
    <result column="customer_name" jdbcType="VARCHAR" property="customerName" />
    <result column="customer_mobile" jdbcType="VARCHAR" property="customerMobile" typeHandler="com.yd.util.deshandler.DESTypeHandler"/>
    <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">
    <!--@mbg.generated-->
    id, assigned_practitioner_id, customer_id, FYP, FYC, time_to_close, pieces, remark, 
    is_active, created_at, created_by, updated_at, updated_by
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from ag_mkt_leads_assigneds
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    <!--@mbg.generated-->
    delete from ag_mkt_leads_assigneds
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.yd.dal.entity.marketing.MktLeadsAssigneds" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into ag_mkt_leads_assigneds (assigned_practitioner_id, customer_id, 
      FYP, FYC, time_to_close, 
      pieces, remark, is_active, 
      created_at, created_by, updated_at, 
      updated_by)
    values (#{assignedPractitionerId,jdbcType=BIGINT}, #{customerId,jdbcType=BIGINT}, 
      #{fyp,jdbcType=DECIMAL}, #{fyc,jdbcType=DECIMAL}, #{timeToClose,jdbcType=TIMESTAMP}, 
      #{pieces,jdbcType=INTEGER}, #{remark,jdbcType=LONGVARCHAR}, #{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.MktLeadsAssigneds" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into ag_mkt_leads_assigneds
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="assignedPractitionerId != null">
        assigned_practitioner_id,
      </if>
      <if test="customerId != null">
        customer_id,
      </if>
      <if test="fyp != null">
        FYP,
      </if>
      <if test="fyc != null">
        FYC,
      </if>
      <if test="timeToClose != null">
        time_to_close,
      </if>
      <if test="pieces != null">
        pieces,
      </if>
      <if test="remark != null">
        remark,
      </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="assignedPractitionerId != null">
        #{assignedPractitionerId,jdbcType=BIGINT},
      </if>
      <if test="customerId != null">
        #{customerId,jdbcType=BIGINT},
      </if>
      <if test="fyp != null">
        #{fyp,jdbcType=DECIMAL},
      </if>
      <if test="fyc != null">
        #{fyc,jdbcType=DECIMAL},
      </if>
      <if test="timeToClose != null">
        #{timeToClose,jdbcType=TIMESTAMP},
      </if>
      <if test="pieces != null">
        #{pieces,jdbcType=INTEGER},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=LONGVARCHAR},
      </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.MktLeadsAssigneds">
    <!--@mbg.generated-->
    update ag_mkt_leads_assigneds
    <set>
      <if test="assignedPractitionerId != null">
        assigned_practitioner_id = #{assignedPractitionerId,jdbcType=BIGINT},
      </if>
      <if test="customerId != null">
        customer_id = #{customerId,jdbcType=BIGINT},
      </if>
      <if test="fyp != null">
        FYP = #{fyp,jdbcType=DECIMAL},
      </if>
      <if test="fyc != null">
        FYC = #{fyc,jdbcType=DECIMAL},
      </if>
      <if test="timeToClose != null">
        time_to_close = #{timeToClose,jdbcType=TIMESTAMP},
      </if>
      <if test="pieces != null">
        pieces = #{pieces,jdbcType=INTEGER},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=LONGVARCHAR},
      </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="customerName != null">
        customer_name = #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="customerMobile != null">
        customer_mobile = #{customerMobile,jdbcType=VARCHAR,typeHandler=com.yd.util.deshandler.DESTypeHandler},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yd.dal.entity.marketing.MktLeadsAssigneds">
    <!--@mbg.generated-->
    update ag_mkt_leads_assigneds
    set assigned_practitioner_id = #{assignedPractitionerId,jdbcType=BIGINT},
      customer_id = #{customerId,jdbcType=BIGINT},
      FYP = #{fyp,jdbcType=DECIMAL},
      FYC = #{fyc,jdbcType=DECIMAL},
      time_to_close = #{timeToClose,jdbcType=TIMESTAMP},
      pieces = #{pieces,jdbcType=INTEGER},
      remark = #{remark,jdbcType=LONGVARCHAR},
      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>

  <select id="findByMktLeadsAssigneds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from ag_mkt_leads_assigneds
    <where>
      <if test="id != null">
        id = #{id}
      </if>
      <if test="assignedPractitionerId != null">
        and assigned_practitioner_id = #{assignedPractitionerId,jdbcType=BIGINT}
      </if>
      <if test="customerId != null">
        and customer_id = #{customerId,jdbcType=BIGINT}
      </if>
      <if test="fyp != null">
        and FYP = #{fyp,jdbcType=DECIMAL}
      </if>
      <if test="fyc != null">
        and FYC = #{fyc,jdbcType=DECIMAL}
      </if>
      <if test="timeToClose != null">
        and time_to_close = #{timeToClose,jdbcType=TIMESTAMP}
      </if>
      <if test="pieces != null">
        and pieces = #{pieces,jdbcType=INTEGER}
      </if>
      <if test="remark != null">
        and remark = #{remark,jdbcType=LONGVARCHAR}
      </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>
    </where>
  </select>
    <select id="findByPractitionerIdAndThisWeek" resultMap="BaseResultMap">
      select
      <include refid="Base_Column_List" />
      from ag_mkt_leads_assigneds
      where
      assigned_practitioner_id = #{practitionerId,jdbcType=BIGINT}
      and YEARWEEK(date_format(created_at,'%Y-%m-%d')) = YEARWEEK(now())
    </select>
    <select id="countPractitionerIdsAndThisWeek" resultType="integer">
      select
        count(a.id)
      from
        ag_mkt_leads_assigneds a
      where
        a.assigned_practitioner_id in
      <foreach collection="practitionerIds" item="practitionerId" index="index" open="(" close=")" separator=",">
        #{practitionerId,jdbcType=BIGINT}
      </foreach>
      and YEARWEEK(date_format(a.created_at,'%Y-%m-%d')) = YEARWEEK(now())
      and not EXISTS (
          select
            t.leads_assigned_id
          from
            ag_mkt_leads_assigned_track t
          where
            t.md_drop_option_id = #{refusedId,jdbcType=BIGINT}
            and a.id = t.leads_assigned_id
      )
    </select>
    <select id="performanceForecastForTeam" resultType="hashmap">
      select sum(case when date_format(time_to_close,'%Y-%m')=date_format(now(),'%Y-%m') then FYC end) totalFYCMonth,
             sum(case when date_format(time_to_close,'%Y-%m')=date_format(now(),'%Y-%m') then FYP end) totalFYPMonth,
             sum(case when date_format(time_to_close,'%Y-%m')=date_format(now(),'%Y-%m') then pieces end) totalPiecesMonth,
             sum(case when QUARTER(time_to_close)=QUARTER(now()) then FYC end) totalFYCQuarter,
             sum(case when QUARTER(time_to_close)=QUARTER(now()) then FYP end) totalFYPQuarter,
             sum(case when QUARTER(time_to_close)=QUARTER(now()) then pieces end) totalPiecesQuarter,
             sum(case when YEAR(time_to_close)=YEAR(NOW()) then FYC end) totalFYCYear,
             sum(case when YEAR(time_to_close)=YEAR(NOW()) then FYP end) totalFYPYear,
             sum(case when YEAR(time_to_close)=YEAR(NOW()) then pieces end) totalPiecesYear
      from ag_mkt_leads_assigneds a
      where a.is_active=1
      and a.assigned_practitioner_id in
      <foreach collection="practitionerIds" item="practitionerId" index="index" open="(" close=")" separator=",">
        #{practitionerId,jdbcType=BIGINT}
      </foreach>
      and not EXISTS (select t.leads_assigned_id
                       from ag_mkt_leads_assigned_track t
                       where t.md_drop_option_id = #{refusedId,jdbcType=BIGINT}
                       and t.practitioner_id in
                      <foreach collection="practitionerIds" item="practitionerId" index="index" open="(" close=")" separator=",">
                        #{practitionerId,jdbcType=BIGINT}
                      </foreach>
                       and a.id = t.leads_assigned_id)
    </select>
  <select id="selectByPractitionerIdRemoveRefused" resultMap="BaseResultMap">
      select *
      from ag_mkt_leads_assigneds a
      where a.assigned_practitioner_id in
          <foreach collection="practitionerIds" item="practitionerId" index="index" open="(" close=")" separator=",">
            #{practitionerId,jdbcType=BIGINT}
          </foreach>
      and not EXISTS (select t.leads_assigned_id
                from ag_mkt_leads_assigned_track t
                where t.md_drop_option_id = #{dropOptionsId,jdbcType=BIGINT}
                  and a.id = t.leads_assigned_id)
  </select>
</mapper>