<?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.DebtListMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.DebtList">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <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="doc_id" jdbcType="BIGINT" property="docId" />
    <result column="doc_num" jdbcType="VARCHAR" property="docNum" />
    <result column="doc_type" jdbcType="VARCHAR" property="docType" />
    <result column="doc_date" jdbcType="TIMESTAMP" property="docDate" />
    <result column="trade_id" jdbcType="BIGINT" property="tradeId" />
    <result column="trade_name" jdbcType="VARCHAR" property="tradeName" />
    <result column="trade_money" jdbcType="DECIMAL" property="tradeMoney" />
    <result column="debt_money" jdbcType="DECIMAL" property="debtMoney" />
    <result column="sales_achievement" jdbcType="DECIMAL" property="salesAchievement" />
    <result column="sales_commission" jdbcType="DECIMAL" property="salesCommission" />
    <result column="card_sales_achievement" jdbcType="DECIMAL" property="cardSalesAchievement" />
    <result column="card_sales_commission" jdbcType="DECIMAL" property="cardSalesCommission" />
    <result column="type_ratio" jdbcType="DECIMAL" property="typeRatio" />
    <result column="sale_one_number" jdbcType="VARCHAR" property="saleOneNumber" />
    <result column="sale_two_number" jdbcType="VARCHAR" property="saleTwoNumber" />
    <result column="sale_three_number" jdbcType="VARCHAR" property="saleThreeNumber" />
    <result column="sale_four_number" jdbcType="VARCHAR" property="saleFourNumber" />
    <result column="sale_one_staff_id" jdbcType="BIGINT" property="saleOneStaffId" />
    <result column="sale_two_staff_id" jdbcType="BIGINT" property="saleTwoStaffId" />
    <result column="sale_three_staff_id" jdbcType="BIGINT" property="saleThreeStaffId" />
    <result column="sale_four_staff_id" jdbcType="BIGINT" property="saleFourStaffId" />
    <result column="sale_one_staff_name" jdbcType="VARCHAR" property="saleOneStaffName" />
    <result column="sale_two_staff_name" jdbcType="VARCHAR" property="saleTwoStaffName" />
    <result column="sale_three_staff_name" jdbcType="VARCHAR" property="saleThreeStaffName" />
    <result column="sale_four_staff_name" jdbcType="VARCHAR" property="saleFourStaffName" />
    <result column="sale_one_identity" jdbcType="VARCHAR" property="saleOneIdentity" />
    <result column="sale_two_identity" jdbcType="VARCHAR" property="saleTwoIdentity" />
    <result column="sale_three_identity" jdbcType="VARCHAR" property="saleThreeIdentity" />
    <result column="sale_four_identity" jdbcType="VARCHAR" property="saleFourIdentity" />
    <result column="sale_one_ratio" jdbcType="DECIMAL" property="saleOneRatio" />
    <result column="sale_two_ratio" jdbcType="DECIMAL" property="saleTwoRatio" />
    <result column="sale_three_ratio" jdbcType="DECIMAL" property="saleThreeRatio" />
    <result column="sale_four_ratio" jdbcType="DECIMAL" property="saleFourRatio" />
    <result column="manage_staff_id" jdbcType="BIGINT" property="manageStaffId" />
    <result column="manage_staff_name" jdbcType="VARCHAR" property="manageStaffName" />
    <result column="manage_identity" jdbcType="VARCHAR" property="manageIdentity" />
    <result column="manage_number" jdbcType="VARCHAR" property="manageNumber" />
    <result column="account_id" jdbcType="BIGINT" property="accountId" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <result column="repay_count" jdbcType="INTEGER" property="repayCount" />
    <result column="repay_consume_count" jdbcType="INTEGER" property="repayConsumeCount" />
    <result column="repay_money" jdbcType="DECIMAL" property="repayMoney" />
    <result column="repay_consume_money" jdbcType="DECIMAL" property="repayConsumeMoney" />
    <result column="sale_five_number" jdbcType="VARCHAR" property="saleFiveNumber" />
    <result column="sale_six_number" jdbcType="VARCHAR" property="saleSixNumber" />
    <result column="sale_seven_number" jdbcType="VARCHAR" property="saleSevenNumber" />
    <result column="sale_eight_number" jdbcType="VARCHAR" property="saleEightNumber" />
    <result column="sale_five_staff_id" jdbcType="BIGINT" property="saleFiveStaffId" />
    <result column="sale_six_staff_id" jdbcType="BIGINT" property="saleSixStaffId" />
    <result column="sale_seven_staff_id" jdbcType="BIGINT" property="saleSevenStaffId" />
    <result column="sale_eight_staff_id" jdbcType="BIGINT" property="saleEightStaffId" />
    <result column="sale_five_staff_name" jdbcType="VARCHAR" property="saleFiveStaffName" />
    <result column="sale_six_staff_name" jdbcType="VARCHAR" property="saleSixStaffName" />
    <result column="sale_seven_staff_name" jdbcType="VARCHAR" property="saleSevenStaffName" />
    <result column="sale_eight_staff_name" jdbcType="VARCHAR" property="saleEightStaffName" />
    <result column="sale_five_identity" jdbcType="VARCHAR" property="saleFiveIdentity" />
    <result column="sale_six_identity" jdbcType="VARCHAR" property="saleSixIdentity" />
    <result column="sale_seven_identity" jdbcType="VARCHAR" property="saleSevenIdentity" />
    <result column="sale_eight_identity" jdbcType="VARCHAR" property="saleEightIdentity" />
    <result column="sale_five_ratio" jdbcType="DECIMAL" property="saleFiveRatio" />
    <result column="sale_six_ratio" jdbcType="DECIMAL" property="saleSixRatio" />
    <result column="sale_seven_ratio" jdbcType="DECIMAL" property="saleSevenRatio" />
    <result column="sale_eight_ratio" jdbcType="DECIMAL" property="saleEightRatio" />
  </resultMap>

  <sql id="baseColumnList">
    id,
    store_id,
    store_name,
    member_id,
    member_name,
    member_identity_card,
    mobile_phone,
    doc_id,
    doc_num,
    doc_type,
    doc_date,
    trade_id,
    trade_name,
    trade_money,
    debt_money,
    sales_achievement,
    sales_commission,
    card_sales_achievement,
    card_sales_commission,
    type_ratio,
    sale_one_number,
    sale_two_number,
    sale_three_number,
    sale_four_number,
    sale_one_staff_id,
    sale_two_staff_id,
    sale_three_staff_id,
    sale_four_staff_id,
    sale_one_staff_name,
    sale_two_staff_name,
    sale_three_staff_name,
    sale_four_staff_name,
    sale_one_identity,
    sale_two_identity,
    sale_three_identity,
    sale_four_identity,
    sale_one_ratio,
    sale_two_ratio,
    sale_three_ratio,
    sale_four_ratio,
    manage_staff_id,
    manage_staff_name,
    manage_identity,
    manage_number,
    account_id,
    state,
    repay_count,
    repay_consume_count,
    repay_money,
    repay_consume_money,
        sale_five_number,
sale_six_number			,
sale_seven_number		,
sale_eight_number		,
sale_five_staff_id		,
sale_six_staff_id		,
sale_seven_staff_id		,
sale_eight_staff_id		,
sale_five_staff_name	,
sale_six_staff_name		,
sale_seven_staff_name	,
sale_eight_staff_name	,
sale_five_identity		,
sale_six_identity		,
sale_seven_identity		,
sale_eight_identity		,
sale_five_ratio	,
sale_six_ratio	,
sale_seven_ratio,
sale_eight_ratio
  </sql>

  <insert id="insertBatch" parameterType="java.util.ArrayList">
    insert  into debt_list (<include refid="baseColumnList"/>)
    <foreach collection="list" item="item" separator="," index="index">
      (
      #{item.id},
      #{item.storeId},
      #{item.storeName},
      #{item.memberId},
      #{item.memberName},
      #{item.memberIdentityCard},
      #{item.mobilePhone},
      #{item.docId},
      #{item.docNum},
      #{item.docType},
      #{item.docDate},
      #{item.tradeId},
      #{item.tradeName},
      #{item.tradeMoney},
      #{item.debtMoney},
      #{item.salesAchievement},
      #{item.salesCommission},
      #{item.cardSalesAchievement},
      #{item.cardSalesCommission},
      #{item.typeRatio},
      #{item.saleOneNumber},
      #{item.saleTwoNumber},
      #{item.saleThreeNumber},
      #{item.saleFourNumber},
      #{item.saleOneStaffId},
      #{item.saleTwoStaffId},
      #{item.saleThreeStaffId},
      #{item.saleFourStaffId},
      #{item.saleOneStaffName},
      #{item.saleTwoStaffName},
      #{item.saleThreeStaffName},
      #{item.saleFourStaffName},
      #{item.saleOneIdentity},
      #{item.saleTwoIdentity},
      #{item.saleThreeIdentity},
      #{item.saleFourIdentity},
      #{item.saleOneRatio},
      #{item.saleTwoRatio},
      #{item.saleThreeRatio},
      #{item.saleFourRatio},
      #{item.manageStaffId},
      #{item.manageStaffName},
      #{item.manageIdentity},
      #{item.manageNumber},
      #{item.accountId},
      #{item.state},
      #{item.repayCount},
      #{item.repayConsumeCount},
      #{item.repayMoney},
      #{item.repayConsumeMoney},
      sale_five_number = #{item.saleFiveNumber, jdbcType = VARCHAR},
      sale_six_number = #{item.saleSixNumber, jdbcType = VARCHAR},
      sale_seven_number = #{item.saleSevenNumber, jdbcType = VARCHAR},
      sale_eight_number = #{item.saleEightNumber, jdbcType = VARCHAR},
      sale_five_staff_id = #{item.saleFiveStaffId, jdbcType = BIGINT},
      sale_six_staff_id = #{item.saleSixStaffId, jdbcType = BIGINT},
      sale_seven_staff_id = #{item.saleSevenStaffId, jdbcType = BIGINT},
      sale_eight_staff_id = #{item.saleEightStaffId, jdbcType = BIGINT},
      sale_five_staff_name = #{item.saleFiveStaffName, jdbcType = VARCHAR},
      sale_six_staff_name = #{item.saleSixStaffName, jdbcType = VARCHAR},
      sale_seven_staff_name = #{item.saleSevenStaffName, jdbcType = VARCHAR},
      sale_eight_staff_name = #{item.saleEightStaffName, jdbcType = VARCHAR},
      sale_five_identity = #{item.saleFiveIdentity, jdbcType = VARCHAR},
      sale_six_identity = #{item.saleSixIdentity, jdbcType = VARCHAR},
      sale_seven_identity = #{item.saleSevenIdentity, jdbcType = VARCHAR},
      sale_eight_identity = #{item.saleEightIdentity, jdbcType = VARCHAR},
      sale_five_ratio = #{item.saleFiveRatio, jdbcType = DECIMAL},
      sale_six_ratio = #{item.saleSixRatio, jdbcType = DECIMAL},
      sale_seven_ratio = #{item.saleSevenRatio, jdbcType = DECIMAL},
      sale_eight_ratio = #{item.saleEightRatio, jdbcType = DECIMAL}
      )
    </foreach>
  </insert>
  <resultMap id="selectByDay" type="com.ssm.manage.response.DebtDayVo">
    <result column="store_id" property="storeId"/>
    <result column="store_name" property="storeName"/>
    <result column="date" property="date"/>
    <result column="card_repay_money" property="cardRepayMoney"/>
    <result column="recharge_repay_money" property="rechargeRepayMoney"/>
  </resultMap>
    <select id="selectByDay" resultMap="selectByDay">
      select store_id,store_name,date(repay_date) as repay_date,sum(if(doc_type = 'card_use',repay_money,0)
      +if(doc_type = 'promotion_card_use',repay_money,0)
      +if(doc_type = 'card_change',repay_money,0)
      )as card_repay_money,
          sum(if(doc_type = 'card_recharge',repay_money,0))as recharge_repay_money
      from repay_trade_list where repay_num in
      (select repay_num from repay_list
                            where is_cancel = '0' and store_id in
                            <foreach collection="storeIds" item="item" open="(" close=")" separator=",">
                              #{item}
                            </foreach>
                            )
                            and repay_date between #{startTime} and #{endTime}
                            and store_id in <foreach collection="storeIds" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
      group by store_id,store_name,date(repay_date)
    </select>
</mapper>