<?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.RepayListMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.RepayList">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="repay_num" jdbcType="VARCHAR" property="repayNum" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="store_name" jdbcType="VARCHAR" property="storeName" />
    <result column="member_id" jdbcType="BIGINT" property="memberId" />
    <result column="member_name" jdbcType="VARCHAR" property="memberName" />
    <result column="member_identity_card" jdbcType="VARCHAR" property="memberIdentityCard" />
    <result column="mobile_phone" jdbcType="VARCHAR" property="mobilePhone" />
    <result column="repay_date" jdbcType="TIMESTAMP" property="repayDate" />
    <result column="repay_money" jdbcType="DECIMAL" property="repayMoney" />
    <result column="debt_money" jdbcType="DECIMAL" property="debtMoney" />
    <result column="pay_money" jdbcType="DECIMAL" property="payMoney" />
    <result column="is_cancel" jdbcType="INTEGER" property="isCancel" />
    <result column="cancel_date" jdbcType="TIMESTAMP" property="cancelDate" />
    <result column="cancel_id" jdbcType="BIGINT" property="cancelId" />
    <result column="cancel_by" jdbcType="VARCHAR" property="cancelBy" />
    <result column="input_id" jdbcType="BIGINT" property="inputId" />
    <result column="input_by" jdbcType="VARCHAR" property="inputBy" />
    <result column="addition_date" jdbcType="DATE" property="additionDate" />
    <result column="is_addition" jdbcType="INTEGER" property="isAddition" />
  </resultMap>

  <resultMap id="StoreDailyMap" type="com.ssm.manage.extendEntity.StoreDaily">
    <!--
      WARNING - @mbg.generated
    -->

    <result column="cash_people_num" jdbcType="INTEGER" property="cashPeopleNum" />
    <result column="cash_money" jdbcType="DECIMAL" property="cashMoney" />
    <result column="recharge_money" jdbcType="DECIMAL" property="rechargeMoney" />
    <result column="course_order_num" jdbcType="INTEGER" property="courseOrderNum" />
    <result column="recharge_order_num" jdbcType="INTEGER" property="rechargeOrderNum" />
    <result column="repay_money" jdbcType="DECIMAL" property="repayMoney" />
    <result column="refund_money" jdbcType="DECIMAL" property="refundMoney" />
    <result column="rec_negative_money" jdbcType="BIGINT" property="recNegativeMoney" />
    <result column="member_num" jdbcType="INTEGER" property="memberNum" />
    <result column="new_member_num" jdbcType="INTEGER" property="newMemberNum" />

    <result column="id" jdbcType="VARCHAR" property="id" />
    <result column="account_type" jdbcType="VARCHAR" property="accountType" />


    <result column="collection_cash_amount" jdbcType="DECIMAL" property="collectionCashAmount" />
    <result column="collection_recharge_amount" jdbcType="DECIMAL" property="collectionRechargeAmount" />
    <result column="cash_recharge_moneys" jdbcType="DECIMAL" property="cashRechargeMoneys" />
    <result column="type_name" jdbcType="VARCHAR" property="typeName" />
    <result column="type_moneys" jdbcType="DECIMAL" property="typeMoneys" />

    <result column="repay_type_name" jdbcType="VARCHAR" property="repayTypeName" />
    <result column="repay_type_num" jdbcType="INTEGER" property="repayTypeNum" />
    <result column="doc_num" jdbcType="VARCHAR" property="docNum" />
  </resultMap>

  <resultMap id="StoreDailyMap1" type="com.ssm.manage.entity.PayList">
    <result column="type_name" jdbcType="VARCHAR" property="payName" />
    <result column="type_moneys" jdbcType="DECIMAL" property="payMoney" />
    <result column="pay_id" jdbcType="BIGINT" property="payId"/>

  </resultMap>


  <select id="selectDayRecord" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select sum(pay_money) as repay_money from repay_list
    <where>
      store_id = #{storeId, jdbcType = BIGINT}
      <if test="isCancel != null">
        and is_cancel = #{isCancel, jdbcType = INTEGER}
      </if>
      <if test="dateStr != null">
        and repay_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
  </select>

  <select id="selectRepay" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select doc_num,account_type from account_trade_record_${tab}
    <where>
      store_id = #{storeId, jdbcType = BIGINT}
      and doc_type in ('card_repay','promotion_card_repay')
      and account_type in ('course_repay','rec_repay','promotion_course_repay')
      <if test="isCancel != null">
        and order_statu = #{isCancel, jdbcType = INTEGER}
      </if>
      <if test="dateStr != null">
        and input_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
    ORDER BY doc_num
  </select>

  <select id="selectRepayCancel" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select doc_num,account_type from account_trade_record_${tab}
    <where>
      store_id = #{storeId, jdbcType = BIGINT}
      and doc_type in ('card_repay','promotion_card_repay')
      and account_type in ('course_repay_cancel','promotion_course_repay_cancel','rec_repay_cancel')
      <if test="dateStr != null">
        and input_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
    ORDER BY doc_num
  </select>

  <select id="SumRepay" resultMap="StoreDailyMap" >
    select sum(pay_money) as repay_money from repay_list
    where repay_num in
    <foreach collection="docNums" item="docnum" index="index" open="(" close=")" separator=",">
      #{docnum}
    </foreach>
    and is_cancel = 0
  </select>
  <select id="SumRepayByRecharge" resultMap="StoreDailyMap" >
    select sum(repay_trade_list.repay_money) as repay_money from repay_trade_list left join repay_list on repay_trade_list.repay_num = repay_list.repay_num
    where repay_trade_list.repay_num in
    <foreach collection="docNums" item="docnum" index="index" open="(" close=")" separator=",">
      #{docnum}
    </foreach>
    and repay_trade_list.doc_type = 'card_recharge' and repay_list.is_cancel = 0
  </select>
  <select id="SumRepayByCourse" resultMap="StoreDailyMap" >
    select sum(repay_trade_list.repay_money) as repay_money from repay_trade_list left join repay_list on repay_trade_list.repay_num = repay_list.repay_num
    where repay_trade_list.repay_num in
    <foreach collection="docNums" item="docnum" index="index" open="(" close=")" separator=",">
      #{docnum}
    </foreach>
    and (repay_trade_list.doc_type = 'card_use' or repay_trade_list.doc_type = 'card_change')and repay_list.is_cancel = 0

  </select>

  <select id="SumRepayByPromotion" resultMap="StoreDailyMap" >
    select sum(repay_trade_list.repay_money) as repay_money from repay_trade_list left join repay_list on repay_trade_list.repay_num = repay_list.repay_num
    where repay_trade_list.repay_num in
    <foreach collection="docNums" item="docnum" index="index" open="(" close=")" separator=",">
      #{docnum}
    </foreach>
    and repay_trade_list.doc_type = 'promotion_card_use'and repay_list.is_cancel = 0
  </select>


  <select id="selectRepay1" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select doc_id as id,doc_num from account_trade_record_${tab}
    <where>
      store_id = #{storeId, jdbcType = BIGINT}
      and doc_type in ('card_repay','promotion_card_repay')
      and account_type in ('course_repay','rec_repay','promotion_course_repay')
      <if test="isCancel != null">
        and order_statu = #{isCancel, jdbcType = INTEGER}
      </if>
      <if test="dateStr != null">
        and input_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
  </select>

  <select id="selectRepayType" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select a.doc_num,a.account_type as repay_type_name from
    (select doc_num,account_type  from account_trade_record_${tab} where store_id = #{storeId, jdbcType = BIGINT}
    <if test="isCancel != null">
      and order_statu = #{isCancel, jdbcType = INTEGER}
    </if>
    <if test="dateStr != null">
      and input_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
    </if>
    ) as a
    <where>
    account_type in ('course_repay','rec_repay','promotion_course_repay')

    </where>
  </select>


  <select id="selectCashRechargeMoney" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select sum(collection_cash_amount) as collection_cash_amount,sum(collection_recharge_amount) as collection_recharge_amount
    ,sum(collection_cash_amount+collection_recharge_amount) as cash_recharge_moneys,count(id) as course_order_num
    from doc_promotion
    <where>
      store_id = #{storeId, jdbcType = BIGINT}
      <if test="isCancel != null">
        and is_cancel = #{isCancel, jdbcType = INTEGER}
      </if>
      <if test="dateStr != null">
        and doc_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
  </select>

  <select id="selectCashRechargeMoney1" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select id
    from doc_promotion
    <where>
      store_id = #{storeId, jdbcType = BIGINT}
      <if test="isCancel != null">
        and is_cancel = #{isCancel, jdbcType = INTEGER}
      </if>
      <if test="dateStr != null">
        and doc_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
  </select>

  <select id="selectTypeMoneys" resultMap="StoreDailyMap1" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select pay_name as type_name,sum(pay_money) as type_moneys,pay_id  from pay_list
    where
    store_id = #{storeId, jdbcType = BIGINT} and
    doc_id in
    <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
      #{id}
    </foreach>
    and is_cancel = 0
    GROUP BY pay_name

  </select>
    <select id="selectNew" resultMap="BaseResultMap">
      select * from repay_list where SUBSTR(repay_date,1,7) = #{strDate}
    </select>
  <select id="selectByRepayNum" resultMap="BaseResultMap">
    select * from repay_list where repay_num in <foreach collection="repayNumList" close=")" open="(" separator="," item="item">
    #{item}
  </foreach>
  </select>
</mapper>