<?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.RepayTradeListMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.RepayTradeList">
    <!--
      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="doc_id" jdbcType="BIGINT" property="docId" />
    <result column="doc_num" jdbcType="VARCHAR" property="docNum" />
    <result column="doc_type" jdbcType="VARCHAR" property="docType" />
    <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="repay_date" jdbcType="TIMESTAMP" property="repayDate" />
    <result column="repay_money" jdbcType="DECIMAL" property="repayMoney" />
    <result column="pay_money" jdbcType="DECIMAL" property="payMoney" />
    <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="sale_one_achievement_amount" jdbcType="DECIMAL" property="saleOneAchievementAmount" />
    <result column="sale_two_achievement_amount" jdbcType="DECIMAL" property="saleTwoAchievementAmount" />
    <result column="sale_three_achievement_amount" jdbcType="DECIMAL" property="saleThreeAchievementAmount" />
    <result column="sale_four_achievement_amount" jdbcType="DECIMAL" property="saleFourAchievementAmount" />
    <result column="sale_one_commission_amount" jdbcType="DECIMAL" property="saleOneCommissionAmount" />
    <result column="sale_two_commission_amount" jdbcType="DECIMAL" property="saleTwoCommissionAmount" />
    <result column="sale_three_commission_amount" jdbcType="DECIMAL" property="saleThreeCommissionAmount" />
    <result column="sale_four_commission_amount" jdbcType="DECIMAL" property="saleFourCommissionAmount" />
    <result column="achievement_amount" jdbcType="DECIMAL" property="achievementAmount" />
    <result column="commission_amount" jdbcType="DECIMAL" property="commissionAmount" />
    <result column="card_achievement_amount" jdbcType="DECIMAL" property="cardAchievementAmount" />
    <result column="card_commission_amount" jdbcType="DECIMAL" property="cardCommissionAmount" />
    <result column="account_id" jdbcType="BIGINT" property="accountId" />
    <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="debt_id" jdbcType="BIGINT" property="debtId" />
    <result column="pay_recharge_money" jdbcType="DECIMAL" property="payRechargeMoney" />
    <result column="belonging_store_id" jdbcType="BIGINT" property="belongingStoreId" />
    <result column="belonging_store_name" jdbcType="VARCHAR" property="belongingStoreName" />
    <result property="beforePrice" column="before_rice" jdbcType="NUMERIC"/>
    <result property="beforeRestMoney" column="before_rest_money" jdbcType="NUMERIC"/>
    <result property="beforeConsumeMoney" column="before_consume_money" jdbcType="NUMERIC"/>
    <result property="beforeGiftRestMoney" column="before_gift_rest_money" jdbcType="NUMERIC"/>
    <result property="beforeGiftConsumeMoney" column="before_gift_consume_money" jdbcType="NUMERIC"/>
    <result property="beforeRestCount" column="before_rest_count" jdbcType="INTEGER"/>
    <result property="beforeConsumeCount" column="before_consume_count" jdbcType="INTEGER"/>

    <result property="backPrice" column="back_rice" jdbcType="NUMERIC"/>
    <result property="backRestMoney" column="back_rest_money" jdbcType="NUMERIC"/>
    <result property="backConsumeMoney" column="back_consume_money" jdbcType="NUMERIC"/>
    <result property="backGiftRestMoney" column="back_gift_rest_money" jdbcType="NUMERIC"/>
    <result property="backGiftConsumeMoney" column="back_gift_consume_money" jdbcType="NUMERIC"/>
    <result property="backRestCount" column="back_rest_count" jdbcType="INTEGER"/>
    <result property="backConsumeCount" column="back_consume_count" jdbcType="INTEGER"/>
    <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" />
    <result column="sale_five_achievement_amount" jdbcType="DECIMAL" property="saleFiveAchievementAmount" />
    <result column="sale_six_achievement_amount" jdbcType="DECIMAL" property="saleSixAchievementAmount" />
    <result column="sale_seven_achievement_amount" jdbcType="DECIMAL" property="saleSevenAchievementAmount" />
    <result column="sale_eight_achievement_amount" jdbcType="DECIMAL" property="saleEightAchievementAmount" />
    <result column="sale_five_commission_amount" jdbcType="DECIMAL" property="saleFiveCommissionAmount" />
    <result column="sale_six_commission_amount" jdbcType="DECIMAL" property="saleSixCommissionAmount" />
    <result column="sale_seven_commission_amount" jdbcType="DECIMAL" property="saleSevenCommissionAmount" />
    <result column="sale_eight_commission_amount" jdbcType="DECIMAL" property="saleEightCommissionAmount" />
  </resultMap>


  <select id="selectList" parameterType="com.ssm.manage.query.RepayListRequest" resultMap="BaseResultMap">
    select b.* from repay_list a inner join repay_trade_list b on  a.repay_num = b.repay_num
    where a.store_id = #{storeId, jdbcType = BIGINT}
    and b.store_id = #{storeId, jdbcType = BIGINT}
    and a.is_cancel = #{isCancel, jdbcType = INTEGER}

    <if test="docType != null">
      and b.doc_type = #{docType, jdbcType = VARCHAR}
    </if>
    <if test="dateStr != null">
      and a.repay_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
    </if>
    <if test="isGift!=null">
      and b.back_gift_rest_money-b.before_gift_rest_money>0
    </if>
    <if test="startTime != null">
      and a.repay_date between #{startTime} and #{endTime}
    </if>
  </select>

  <select id="selectCoursePayList" parameterType="com.ssm.manage.query.RepayListRequest" resultMap="BaseResultMap">
    select distinct(b.repay_num) pay_num,b.* from repay_list a inner join repay_trade_list b on  a.repay_num = b.repay_num INNER join  repay_account_list c on b.id = c.repay_trade_id
    where  a.store_id = #{storeId, jdbcType = BIGINT}
    and b.store_id = #{storeId, jdbcType = BIGINT}
    and a.is_cancel = #{isCancel, jdbcType = INTEGER}
    and c.repay_trade_id is not null
    <if test="docType != null">
      and b.doc_type = #{docType, jdbcType = VARCHAR}
    </if>
    <if test="dateStr != null">
      and a.repay_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
    </if>
    <if test="isGift!=null">
      and b.back_gift_rest_money-b.before_gift_rest_money>0
    </if>
    <if test="accountType!=null">
      and c.account_type = #{accountType}
    </if>
  </select>

  <select id="selectFKJ" parameterType="com.ssm.manage.query.RepayTradeRequest" resultMap="BaseResultMap">
    select * from repay_trade_list
    where store_id = #{storeId, jdbcType = BIGINT}
    and DATE_FORMAT(repay_date, '%Y-%m-%d' ) = DATE_FORMAT(#{repayDate} , '%Y-%m-%d' );
  </select>

  <select id="queryByDocId" parameterType="com.ssm.manage.query.RepayTradeRequest" resultMap="BaseResultMap">
    select * from repay_trade_list r1 left join repay_list r2 on r1.repay_num = r2.repay_num
    where r1.doc_id = #{docId, jdbcType = BIGINT} and r2.is_cancel = 0
  </select>

  <select id="queryByDocNum" parameterType="com.ssm.manage.query.RepayTradeRequest" resultMap="BaseResultMap">
    select * from repay_trade_list
    where doc_num = #{docNum, jdbcType = VARCHAR}
  </select>

  <select id="selectLists" parameterType="com.ssm.manage.query.RepayListRequest" resultMap="BaseResultMap">
    select b.* from repay_list a, repay_trade_list b
    where b.store_id = #{storeId, jdbcType = BIGINT}
    and a.is_cancel = #{isCancel, jdbcType = INTEGER}
    and a.repay_num = b.repay_num
    <if test="docType != null">
      and b.doc_type = #{docType, jdbcType = VARCHAR}
    </if>
    <if test="dateStr != null">
      and a.repay_date between #{startTime, jdbcType = TIMESTAMP} and #{endTime, jdbcType = TIMESTAMP}
    </if>
  </select>

  <select id="selectListBetween" parameterType="com.ssm.manage.query.RepayListRequest" resultMap="BaseResultMap">
    select b.* from repay_list a, repay_trade_list b
    where b.store_id = #{storeId, jdbcType = BIGINT}
    and a.repay_num = b.repay_num
      and a.repay_date between #{startTime, jdbcType = TIMESTAMP} and #{endTime, jdbcType = TIMESTAMP}
  </select>

  <update id="updateStaff" parameterType="com.ssm.manage.entity.RepayTradeList" >
    update repay_trade_list
    <set>
      <!--<if test="saleOneNumber != null">-->
      sale_one_number = #{saleOneNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleTwoNumber != null">-->
      sale_two_number = #{saleTwoNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleThreeNumber != null">-->
      sale_three_number = #{saleThreeNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleFourNumber != null">-->
      sale_four_number = #{saleFourNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleOneStaffId != null">-->
      sale_one_staff_id = #{saleOneStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="saleTwoStaffId != null">-->
      sale_two_staff_id = #{saleTwoStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="saleThreeStaffId != null">-->
      sale_three_staff_id = #{saleThreeStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="saleFourStaffId != null">-->
      sale_four_staff_id = #{saleFourStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="saleOneStaffName != null">-->
      sale_one_staff_name = #{saleOneStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleTwoStaffName != null">-->
      sale_two_staff_name = #{saleTwoStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleThreeStaffName != null">-->
      sale_three_staff_name = #{saleThreeStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleFourStaffName != null">-->
      sale_four_staff_name = #{saleFourStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleOneIdentity != null">-->
      sale_one_identity = #{saleOneIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleTwoIdentity != null">-->
      sale_two_identity = #{saleTwoIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleThreeIdentity != null">-->
      sale_three_identity = #{saleThreeIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleFourIdentity != null">-->
      sale_four_identity = #{saleFourIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleOneRatio != null">-->
      sale_one_ratio = #{saleOneRatio, jdbcType = DECIMAL},
      <!--</if>
      <if test="saleTwoRatio != null">-->
      sale_two_ratio = #{saleTwoRatio, jdbcType = DECIMAL},
      <!--</if>
      <if test="saleThreeRatio != null">-->
      sale_three_ratio = #{saleThreeRatio, jdbcType = DECIMAL},
      <!--</if>
      <if test="saleFourRatio != null"> </if>-->
      sale_four_ratio = #{saleFourRatio, jdbcType = DECIMAL},




      <!--<if test="saleOneNumber != null">-->
      sale_five_number = #{saleFiveNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleSixNumber != null">-->
      sale_six_number = #{saleSixNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleSevenNumber != null">-->
      sale_seven_number = #{saleSevenNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleEightNumber != null">-->
      sale_eight_number = #{saleEightNumber, jdbcType = VARCHAR},
      <!--</if>
      <if test="salefiveStaffId != null">-->
      sale_five_staff_id = #{saleFiveStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="salesixStaffId != null">-->
      sale_six_staff_id = #{saleSixStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="salesevenStaffId != null">-->
      sale_seven_staff_id = #{saleSevenStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="saleeightStaffId != null">-->
      sale_eight_staff_id = #{saleEightStaffId, jdbcType = BIGINT},
      <!--</if>
      <if test="salefiveStaffName != null">-->
      sale_five_staff_name = #{saleFiveStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="salesixStaffName != null">-->
      sale_six_staff_name = #{saleSixStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="salesevenStaffName != null">-->
      sale_seven_staff_name = #{saleSevenStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleeightStaffName != null">-->
      sale_eight_staff_name = #{saleEightStaffName, jdbcType = VARCHAR},
      <!--</if>
      <if test="salefiveIdentity != null">-->
      sale_five_identity = #{saleFiveIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="salesixIdentity != null">-->
      sale_six_identity = #{saleSixIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="salesevenIdentity != null">-->
      sale_seven_identity = #{saleSevenIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="saleeightIdentity != null">-->
      sale_eight_identity = #{saleEightIdentity, jdbcType = VARCHAR},
      <!--</if>
      <if test="salefiveRatio != null">-->
      sale_five_ratio = #{saleFiveRatio, jdbcType = DECIMAL},
      <!--</if>
      <if test="salesixRatio != null">-->
      sale_six_ratio = #{saleSixRatio, jdbcType = DECIMAL},
      <!--</if>
      <if test="salesevenRatio != null">-->
      sale_seven_ratio = #{saleSevenRatio, jdbcType = DECIMAL},
      <!--</if>
      <if test="saleeightRatio != null"> </if>-->
      sale_eight_ratio = #{saleEightRatio, jdbcType = DECIMAL}

    </set>
    <where>
      id = #{id, jdbcType = BIGINT}
    </where>
  </update>

  <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" />
    <result column="hk_cooperateAmount" jdbcType="DECIMAL" property="hkCooperateAmount"/>
  </resultMap>
  <select id="selectrepayCooperate" resultMap="StoreDailyMap" parameterType="com.ssm.manage.query.StoreDailyRequest">
    select sum(r1.repay_money) as repay_money,sum(r1.achievement_amount) as hk_cooperateAmount  from repay_trade_list r1
        left join repay_list r2 on r2.repay_num = r1.repay_num
    <where>
      r1.store_id = #{storeId, jdbcType = BIGINT}
      <if test="isCancel != null">
        and r2.is_cancel = #{isCancel, jdbcType = INTEGER}
      </if>
      <if test="dateStr != null">
        and r1.repay_date like concat( #{dateStr, jdbcType = VARCHAR}, '%')
      </if>
    </where>
  </select>
  <select id="selectListNew" resultMap="BaseResultMap">
    select * from repay_trade_list where SUBSTR(repay_date,1,7) = #{strDate} and store_id in
                                   <foreach collection="storeIds" open="(" close=")" separator="," item="item">
                                     #{item}
                                   </foreach>
  </select>
    <select id="selectFKJByDate" parameterType="com.ssm.manage.query.RepayTradeRequest" resultMap="BaseResultMap">
      select * from repay_trade_list
      where store_id = #{storeId, jdbcType = BIGINT}
        and repay_date between #{startTime} and #{endTime}
    </select>
  <select id="selectListByStartTimeAndEndTime" resultMap="BaseResultMap">
    select * from repay_trade_list a left join repay_list b on a.repay_num = b.repay_num
        where b.store_id = #{storeId}
          and b.is_cancel = 0
          and b.repay_date between #{startTime}
          and #{endTime}
          and a.doc_type in ('promotion_card_use','card_use','card_change')
  </select>

</mapper>