<?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.ssm.manage.mapper.StoreStatisticMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.extendEntity.ProjectStatistic">
    <!--
      WARNING - @mbg.generated
    -->
    <result column="project_id" jdbcType="BIGINT" property="projectId"/>
    <result column="project_name" jdbcType="VARCHAR" property="projectName"/>
    <result column="course_money" jdbcType="BIGINT" property="courseMoney"/>
    <result column="repay_course_money" jdbcType="BIGINT" property="repayCourseMoney"/>
    <result column="refund_course_money" jdbcType="BIGINT" property="refundCourseMoney"/>
    <result column="cash_money" jdbcType="BIGINT" property="cashMoney"/>
    <result column="project_statistic_money" jdbcType="BIGINT" property="projectStatisticMoney"/>
    <result column="doc_count" jdbcType="INTEGER" property="docCount"/>
  </resultMap>

  <resultMap id="BaseResultMap1" type="com.ssm.manage.extendEntity.ProjectLists">
    <!--
      WARNING - @mbg.generated
    -->
    <result column="store_id" jdbcType="BIGINT" property="storeId"/>
    <result column="store_name" jdbcType="VARCHAR" property="storeName"/>
    <result column="trade_id" jdbcType="BIGINT" property="tradeId"/>
    <result column="trade_name" jdbcType="VARCHAR" property="tradeName"/>
    <result column="count_num" jdbcType="BIGINT" property="countNum"/>
    <result column="count_money" jdbcType="BIGINT" property="countMoney"/>
  </resultMap>

  <resultMap id="MemberResultMap" type="com.ssm.manage.extendEntity.MemberStatistic">
    <!--
      WARNING - @mbg.generated
    -->
    <result column="member_id" jdbcType="BIGINT"  property="memberId"/>
    <result column="member_name" jdbcType="VARCHAR"  property="memberName"/>
    <result column="member_phone" jdbcType="VARCHAR"  property="memberPhone"/>
    <result column="rec_statistic_money" jdbcType="BIGINT"  property="recStatisticMoney"/>
    <result column="rec_repay_money" jdbcType="BIGINT"  property="recRepayMoney"/>
    <result column="rec_refund_money" jdbcType="BIGINT"  property="recRefundMoney"/>
    <result column="course_statistic_money" jdbcType="BIGINT"  property="courseStatisticMoney"/>
    <result column="course_repay_money" jdbcType="BIGINT"  property="courseRepayMoney"/>
    <result column="course_refund_money" jdbcType="BIGINT"  property="courseRefundMoney"/>
    <result column="cash_statistic_money" jdbcType="BIGINT"  property="cashStatisticMoney"/>

  </resultMap>

<select id="selectMemberList" parameterType="com.ssm.manage.query.AccountTradeRecordRequest" resultMap="MemberResultMap">
  select  a.member_id,a.member_name,a.member_phone,
--   a.rec_statistic_money,a.rec_repay_money,a.rec_refund_money,a.course_statistic_money,a.course_repay_money,a.course_refund_money,
  a.cash_statistic_money from
  (select member_id,member_name,member_phone,
  sum(CASE WHEN account_type = 'cash' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END ) -
  sum(CASE WHEN account_type = 'cash_cancel' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END ) +
  sum(CASE WHEN account_type = 'recharge_consume' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END ) -
  sum(CASE WHEN account_type = 'recharge_consume_cancel' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END ) +
  sum(CASE WHEN account_type = 'project_consume' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END ) -
  sum(CASE WHEN account_type = 'project_consume_cancel' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END ) -
  sum(CASE WHEN account_type = 'refund_cash_audit' and doc_type = 'refund_cash' THEN account_spend_money ELSE 0 END ) +
  sum(CASE WHEN account_type = 'refund_cash_unaudit' and doc_type = 'refund_cash' THEN account_in_money ELSE 0 END )as cash_statistic_money

  from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and member_id is not null and  input_date like concat(#{date, jdbcType = VARCHAR},'%') group by member_id) a order by a.cash_statistic_money desc
</select>

  <select id="selectProjectList" parameterType="com.ssm.manage.query.AccountTradeRecordRequest" resultMap="BaseResultMap">
   select trade_id as project_id, trade_name as project_name,
    <if test="type == 1">
      sum(CASE WHEN account_type = 'project' and doc_type = 'card_use' THEN account_in_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'project_cancel' and doc_type = 'card_use' THEN account_in_money ELSE 0  END) +

      sum(CASE WHEN account_type = 'course_repay' and doc_type = 'card_repay' THEN account_in_money ELSE 0  END ) -
      sum(CASE WHEN account_type = 'course_repay_cancel' and doc_type = 'card_repay' THEN account_in_money  ELSE 0 END ) -
      sum(CASE WHEN account_type = 'refund_course_audit' and doc_type = 'refund_course' THEN account_spend_money  ELSE 0 END ) +
      sum(CASE WHEN account_type = 'refund_course_unaudit' and doc_type = 'refund_course' THEN account_in_money ELSE 0 END ) +
      sum(CASE WHEN account_type = 'project' and doc_type = 'promotion' THEN account_in_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'project_cancel' and doc_type = 'promotion' THEN account_spend_money ELSE 0  END) as project_statistic_money
      from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and input_date like concat(#{date, jdbcType = VARCHAR},'%') and doc_type in ('card_use','card_repay','refund_course','promotion') and account_type in ('project','project_cancel','course_repay','course_repay_cancel','refund_course_audit','refund_course_unaudit') group by trade_id order by project_statistic_money desc
    </if>
    <if test="type == 2">
      sum(CASE WHEN account_type = 'project_consume' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'project_consume_cancel' and doc_type = 'store_cash' THEN account_in_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'refund_cash_audit' and doc_type = 'refund_cash' THEN account_spend_money  ELSE 0 END ) +
      sum(CASE WHEN account_type = 'refund_cash_unaudit' and doc_type = 'refund_cash' THEN account_in_money ELSE 0 END )as project_statistic_money,
      count(distinct doc_num) count
      from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and input_date like concat(#{date, jdbcType = VARCHAR},'%') and doc_type in ('store_cash','refund_cash') and account_type in ('project_consume','project_consume_cancel','refund_cash_audit','refund_cash_unaudit')group by trade_id order by project_statistic_money desc
    </if>
<!--    <if test="type == 3">-->
<!--      sum(CASE WHEN account_type = 'project' and doc_type = 'card_use' THEN account_in_money ELSE 0 END) - -->
<!--      sum(CASE WHEN account_type = 'project_cancel' and doc_type = 'card_use' THEN account_in_money ELSE 0 END) +-->

<!--      sum(CASE WHEN account_type = 'course_repay' and doc_type = 'card_repay' THEN account_in_money ELSE 0 END ) - -->
<!--      sum(CASE WHEN account_type = 'course_repay_cancel' and doc_type = 'card_repay' THEN account_in_money ELSE 0 END ) - -->
<!--      sum(CASE WHEN account_type = 'refund_course_audit' and doc_type = 'refund_course' THEN trade_money / trade_count * account_spend_count ELSE 0 END ) +-->
<!--      sum(CASE WHEN account_type = 'refund_course_unaudit' and doc_type = 'refund_course' THEN trade_money / trade_count * account_spend_count ELSE 0 END ) - -->
<!--      sum(CASE WHEN account_type = 'project_consume' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END) +-->
<!--      sum(CASE WHEN account_type = 'project_consume_cancel' and doc_type = 'store_cash' THEN account_in_money ELSE 0 END) as project_statistic_money-->
<!--      from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and input_date like concat(#{date, jdbcType = VARCHAR},'%') and doc_type in ('card_use','card_repay','refund_course','store_cash')-->
<!--      and account_type in ('project','project_cancel','course_repay','course_repay_cancel','refund_course_audit','refund_course_unaudit','project_consume','project_consume_cancel')group by trade_id order by project_statistic_money desc-->
<!--    </if>-->

  </select>

  <select id="selectProjectLists" parameterType="com.ssm.manage.query.AccountTradeRecordRequest" resultMap="BaseResultMap">
    select trade_id as project_id, trade_name as project_name,
    <if test="type == 1">
      sum(CASE WHEN account_type = 'project' and doc_type = 'card_use' THEN account_in_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'project_cancel' and doc_type = 'card_use' THEN account_in_money ELSE 0  END) +

      sum(CASE WHEN account_type = 'course_repay' and doc_type = 'card_repay' THEN account_in_money ELSE 0  END ) -
      sum(CASE WHEN account_type = 'course_repay_cancel' and doc_type = 'card_repay' THEN account_in_money  ELSE 0 END ) -
      sum(CASE WHEN account_type = 'refund_course_audit' and doc_type = 'refund_course' THEN account_spend_money  ELSE 0 END ) +
      sum(CASE WHEN account_type = 'refund_course_unaudit' and doc_type = 'refund_course' THEN account_in_money ELSE 0 END ) +
      sum(CASE WHEN account_type = 'project' and doc_type = 'promotion' THEN account_in_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'project_cancel' and doc_type = 'promotion' THEN account_spend_money ELSE 0  END) as project_statistic_money
      from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and input_date between #{date, jdbcType = VARCHAR} and  #{endDate, jdbcType = VARCHAR} and doc_type in ('card_use','card_repay','refund_course','promotion') and account_type in ('project','project_cancel','course_repay','course_repay_cancel','refund_course_audit','refund_course_unaudit') group by trade_id order by project_statistic_money desc
    </if>
    <if test="type == 2">
      sum(CASE WHEN account_type = 'project_consume' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'project_consume_cancel' and doc_type = 'store_cash' THEN account_in_money ELSE 0 END) -
      sum(CASE WHEN account_type = 'refund_cash_audit' and doc_type = 'refund_cash' THEN account_spend_money  ELSE 0 END ) +
      sum(CASE WHEN account_type = 'refund_cash_unaudit' and doc_type = 'refund_cash' THEN account_in_money ELSE 0 END )as project_statistic_money,
      count(distinct doc_num) count
      from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and input_date between #{date, jdbcType = VARCHAR} and  #{endDate, jdbcType = VARCHAR} and doc_type in ('store_cash','refund_cash') and account_type in ('project_consume','project_consume_cancel','refund_cash_audit','refund_cash_unaudit')group by trade_id order by project_statistic_money desc
    </if>
    <!--    <if test="type == 3">-->
    <!--      sum(CASE WHEN account_type = 'project' and doc_type = 'card_use' THEN account_in_money ELSE 0 END) - -->
    <!--      sum(CASE WHEN account_type = 'project_cancel' and doc_type = 'card_use' THEN account_in_money ELSE 0 END) +-->

    <!--      sum(CASE WHEN account_type = 'course_repay' and doc_type = 'card_repay' THEN account_in_money ELSE 0 END ) - -->
    <!--      sum(CASE WHEN account_type = 'course_repay_cancel' and doc_type = 'card_repay' THEN account_in_money ELSE 0 END ) - -->
    <!--      sum(CASE WHEN account_type = 'refund_course_audit' and doc_type = 'refund_course' THEN trade_money / trade_count * account_spend_count ELSE 0 END ) +-->
    <!--      sum(CASE WHEN account_type = 'refund_course_unaudit' and doc_type = 'refund_course' THEN trade_money / trade_count * account_spend_count ELSE 0 END ) - -->
    <!--      sum(CASE WHEN account_type = 'project_consume' and doc_type = 'store_cash' THEN account_spend_money ELSE 0 END) +-->
    <!--      sum(CASE WHEN account_type = 'project_consume_cancel' and doc_type = 'store_cash' THEN account_in_money ELSE 0 END) as project_statistic_money-->
    <!--      from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT} and input_date like concat(#{date, jdbcType = VARCHAR},'%') and doc_type in ('card_use','card_repay','refund_course','store_cash')-->
    <!--      and account_type in ('project','project_cancel','course_repay','course_repay_cancel','refund_course_audit','refund_course_unaudit','project_consume','project_consume_cancel')group by trade_id order by project_statistic_money desc-->
    <!--    </if>-->

  </select>


  <select id="selectProjectLists1"  resultMap="BaseResultMap1">
    select store_id,store_name,trade_id,trade_name,account_type,doc_type,SUM(trade_count) AS count_num ,SUM(account_in_money) AS count_money  from
      (
           <foreach collection="date" index="index"  item="date" separator="UNION ALL">
             select store_id,store_name,trade_id,trade_name,trade_count ,account_in_money,account_type,doc_type  from account_trade_record_${date}
           </foreach>
      ) aa
    where account_type='project' and doc_type in  ('card_use','promotion','card_change') and  store_id in
    <foreach collection="storeId" index="index" item="item" open="(" separator="," close=")">
      #{item, jdbcType = BIGINT}
    </foreach>
    GROUP BY trade_name,store_name ORDER BY count_money desc

  </select>

  <select id="selectProjectLists2"  resultMap="BaseResultMap1">
    select store_id,store_name,course_project_id as trade_id,course_project_name as trade_name,sum(course_consume_price) as count_money,sum(course_consume_count)as count_num
    from course_account where course_consume_count>0 and store_id = #{storeId, jdbcType = BIGINT}
    <if test="allowValid!=null and  allowValid!=''">
    and ( ${allowValid} )
    </if>

    GROUP BY course_project_name,store_name ORDER BY count_money desc
  </select>

  <select id="aa">
    select store_id,store_name,course_project_id,course_project_name,course_consume_price,course_consume_count,end_time from course_account where end_time>now() or  end_time is null and course_consume_count>0
  </select>

  <select id="selectProjectLists3"  resultMap="BaseResultMap1">
    select store_id,store_name,trade_id,trade_name,account_type,doc_type,SUM(trade_count) AS count_num ,SUM(trade_money) AS count_money  from
    (
    <foreach collection="date" index="index"  item="date" separator="UNION ALL">
      select store_id,store_name,trade_id,trade_name,trade_count ,trade_money,account_type,doc_type  from account_trade_record_${date}
    </foreach>
    ) aa
    where account_type='project_consume' and doc_type = 'store_cash' and  store_id in
    <foreach collection="storeId" index="index" item="item" open="(" separator="," close=")">
      #{item, jdbcType = BIGINT}
    </foreach>
    GROUP BY trade_name,store_name ORDER BY count_money desc

  </select>


</mapper>