<?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.dayou.mapper.MarketStatMapper">

   <select id="marketStat" resultType="com.dayou.vo.MarketStatVO" parameterType="com.dayou.dto.MarketStatDTO">
      SELECT
       ifnull(( SELECT sum(ifnull(sale_target,0)) FROM user_target
          <where>
              <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">and user_id in
                  <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
               #{userId}
           </foreach>
       </if> </where> ),0) AS yearSaleTarget,
       ifnull(( SELECT sum(ifnull(payment_target,0)) FROM user_target
      <where>
       <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
           and user_id in
           <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
               #{userId}
           </foreach>
       </if> </where>),0) AS yearPaymentTarget,
      ( SELECT count(*) FROM business_opportunity WHERE state = '跟进中' AND deleted = 0
            <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
                and user_id in
                <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
                    #{userId}
                </foreach>
            </if>
        ) AS businessNum,
      ( SELECT
      ifnull(sum(amount ),0)
      FROM
      item
      WHERE
      deleted = 0
      AND upload_date is not null
      AND upload_date &gt;= concat( YEAR ( now())- 1, '-12-23' )
      AND upload_date &lt;= concat( YEAR ( now()), '-12-22' )
      <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
         and user_id in
         <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
            #{userId}
         </foreach>
      </if>
        ) AS yearSaleDone,
      ( SELECT
       ifnull( sum( pc.amount ), 0 )
       FROM
       payment_collection pc
       LEFT JOIN item i ON i.id = pc.item_id
       WHERE
       pc.deleted = 0
       AND pc.payment_date &gt;= concat(
       YEAR (
       now())-1,
       '-12-23'
       )
       AND pc.payment_date &lt;=  concat(
       YEAR (
       now()),
       '-12-22'
       )
      <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
         and i.user_id in
         <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
            #{userId}
         </foreach>
      </if>
                                                         ) AS yearPaymentDone,
          ( SELECT ifnull(sum( pc.amount ),0) FROM payment_collection pc left join item i on i.id = pc.item_id WHERE pc.deleted = 0 AND pc.payment_date &gt;= #{lastMonth23}
      AND pc.payment_date &lt;= concat( date_format( LAST_DAY( now()), '%Y-%m-' ), '22' )
      <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
         and i.user_id in
         <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
            #{userId}
         </foreach>
      </if>
        ) AS monthPaymentDone,
       ifnull(( select sum(estimate_amount) from business_opportunity where deleted = 0 and state = '跟进中'
       <if test="dto!=null and dto.userIds!=null and dto.userIds.size!=0">
           and user_id in
           <foreach collection="dto.userIds" open="(" close=")" separator="," item="userId">
               #{userId}
           </foreach>
       </if>
       ),0) AS estimateAmount
   </select>

    <select id="currentMonthPayment" resultType="java.math.BigDecimal">
        SELECT ifnull(sum( pc.amount ),0) FROM payment_collection pc left join item i on i.id = pc.item_id WHERE pc.deleted = 0 AND pc.payment_date >= #{lastMonth23}
            AND pc.payment_date &lt;= concat( date_format( LAST_DAY( now()), '%Y-%m-' ), '22' ) and i.user_id = #{userId}
    </select>

    <select id="currentYearPayment" resultType="java.math.BigDecimal">
        SELECT
            ifnull( sum( pc.amount ), 0 )
        FROM
            payment_collection pc
                LEFT JOIN item i ON i.id = pc.item_id
        WHERE
            pc.deleted = 0
          AND pc.payment_date &gt;= concat(
            YEAR (
       now())-1,
                '-12-23'
            )
          AND pc.payment_date &lt;=  concat(
            YEAR (
       now()),
                '-12-22'
            )
          and i.user_id = #{userId}
    </select>

    <select id="currentYearTeamPayment" parameterType="java.util.Set" resultType="java.math.BigDecimal">
        SELECT
            ifnull( sum( pc.amount ), 0 )
        FROM
            payment_collection pc
                LEFT JOIN item i ON i.id = pc.item_id
        WHERE
            pc.deleted = 0
          AND pc.payment_date &gt;= concat(
            YEAR (
       now())-1,
                '-12-23'
            )
          AND pc.payment_date &lt;=  concat(
            YEAR (
       now()),
                '-12-22'
            )
          and i.user_id in (
        <foreach collection="userIds" item="item" index="index" separator=",">
            #{item}
        </foreach>
              )
    </select>
</mapper>
