<?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.neusoft.smart.pos.report.persistent.mapper.RechargeRecordMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.report.persistent.domain.RechargeRecordExt">
    <id column="orderNumber" jdbcType="VARCHAR" property="orderNumber" />
    <result column="realPayAmount" jdbcType="VARCHAR" property="realPayAmount" />
    <result column="cardNum" jdbcType="VARCHAR" property="cardNum" />
    <result column="memberName" jdbcType="VARCHAR" property="memberName" />
    <result column="balance" jdbcType="VARCHAR" property="balance" />
    <result column="createdAt" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="balanceBefore" jdbcType="VARCHAR" property="balanceBefore" />
    <result column="payType" jdbcType="INTEGER" property="payType" />
    <result column="payTypeName" jdbcType="VARCHAR" property="payTypeName" />
    <result column="chargeShop" jdbcType="VARCHAR" property="chargeShop" />
    <result column="chargeShopId" jdbcType="INTEGER" property="chargeShopId" />
    <result column="serialNo" jdbcType="VARCHAR" property="serialNo" />
    <result column="chargeOrg" jdbcType="VARCHAR" property="chargeOrg" />
    <result column="deviceName" jdbcType="VARCHAR" property="deviceName" />
    <result column="orgId" jdbcType="INTEGER" property="orgId" />
    <result column="orderStatus" jdbcType="INTEGER" property="orderStatus" />
    <result column="memberCardNo" jdbcType="VARCHAR" property="memberCardNo" />
    <result column="orgName" jdbcType="VARCHAR" property="orgName" />
    <result column="operator" jdbcType="VARCHAR" property="operator" />
  </resultMap>


  <select id="selectByExample" parameterType="com.neusoft.smart.pos.report.persistent.domain.RechargeRecordExample" resultMap="BaseResultMap">
    SELECT DISTINCT a.order_number orderNumber, a.order_amount realPayAmount, b.card_num cardNum, b.card_other_id memberName, k.amount_after balance,
    g.name chargeOrg, c.created_at createdAt, k. amount_before balanceBefore, c.pay_type payType, c.pay_type_name payTypeName, d.name chargeShop, d.id chargeShopId, a. serial_no serialNo, b. card_another_id memberCardNo, m.alias deviceName , g.id orgId, g.name orgName,a.created_by operator, a.order_status orderStatus
    FROM order_master_v2 a, card b, pay_order_v2 c, shop d, organization_group e, organization_group_relation f, org g, card_capital_change k, card_order l, device m
    WHERE e.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
    and a.order_type = '3' AND k.change_type = 0 AND k.change_order_id = l.card_trade_num AND l.out_trade_id = c.trade_id AND c.m_order_number = a.order_number
    AND c.card_num = b.card_num AND a.shop_id = d.id AND d.org = g.id AND e.entity_table_id = g.id AND e.entity_table_name = 'org'
    AND a.order_amount > 0 AND m.serial_no = a.serial_no AND m.`status` = 0

    <if test="serialNo != null and serialNo !=''">
      and  a.serial_no like '%${serialNo}%'
    </if>
    <if test="orderNumber != null and orderNumber !='' ">
      and  a.order_number like '%${orderNumber}%'
    </if>
    <if test="memberName != null and memberName !=''">
      and  b.card_other_id like '%${memberName}%'
    </if>
    <if test="deviceName != null and deviceName !=''">
      and  m.alias like '%${deviceName}%'
    </if>
    <if test="cardNum != null and cardNum !=''">
      and b.card_num  like '%${cardNum}%'
    </if>

    <if test="orgName != null and orgName !=''">
      and  g.`name` like  '%${orgName}%'
    </if>
    <if test="memberCardNo != null and memberCardNo !=''">
      and  b.card_another_id like  '%${memberCardNo}%'
    </if>
    <if test="payTypeName != null and payTypeName !=''">
      and  c.pay_type_name like  '%${payTypeName}%'
    </if>

    <if test="orderStatus != null and orderStatus !=''">
      and  a.order_status =  ${orderStatus}
    </if>
    <if test="chargeOrg != null and chargeOrg !=''">
      and  g.`name`  like  '%${chargeOrg}%'
    </if>


    <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
      and c.created_at  between DATE_FORMAT( concat('${startTime}',' 00-00-00'),'%Y-%m-%d %H:%i:%s') and DATE_FORMAT( concat('${endTime}',' 23-59-59'),'%Y-%m-%d %H:%i:%s')
    </if>

    <if test="(startTime == null or  startTime =='') and (endTime == null  or endTime =='') " >
      and c.created_at  between DATE_FORMAT( curdate(),'%Y-%m-%d %H:%i:%s') and DATE_FORMAT( concat(curdate(),' 23-59-59'),'%Y-%m-%d %H:%i:%s')
    </if>

      ORDER BY c.created_at asc
    <if test="offset != null  and limit != null ">
       limit  ${offset}, ${limit}
    </if>


  </select>
  <select id="queryMemberOrderDetailsCount" parameterType="com.neusoft.smart.pos.report.persistent.domain.RechargeRecordExample" resultType="java.lang.Integer">
    SELECT count(*) FROM (   SELECT DISTINCT a.order_number orderNumber, a.order_amount realPayAmount, b.card_num cardNum, b.card_other_id memberName, k.amount_after balance,
    g.name chargeOrg, c.created_at createdAt, k. amount_before balanceBefore, c.pay_type payType, c.pay_type_name payTypeName, d.name chargeShop, d.id chargeShopId, a. serial_no serialNo, b. card_another_id memberCardNo, m.alias deviceName , g.id orgId, g.name orgName, a.order_status orderStatus
    FROM order_master_v2 a, card b, pay_order_v2 c, shop d, organization_group e, organization_group_relation f, org g, card_capital_change k, card_order l, device m
    WHERE e.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
    and a.order_type = '3' AND k.change_type = 0 AND k.change_order_id = l.card_trade_num AND l.out_trade_id = c.trade_id AND c.m_order_number = a.order_number
    AND c.card_num = b.card_num AND a.shop_id = d.id AND d.org = g.id AND e.entity_table_id = g.id AND e.entity_table_name = 'org'
    AND a.order_amount > 0 AND m.serial_no = a.serial_no AND m.`status` = 0
    <if test="serialNo != null and serialNo !=''">
      and  a.serial_no like '%${serialNo}%'
    </if>
    <if test="orderNumber != null and orderNumber !='' ">
      and  a.order_number like '%${orderNumber}%'
    </if>
    <if test="memberName != null and memberName !=''">
      and  b.card_other_id like '%${memberName}%'
    </if>
    <if test="deviceName != null and deviceName !=''">
      and  m.alias like '%${deviceName}%'
    </if>
    <if test="cardNum != null and cardNum !=''">
      and b.card_num  like '%${cardNum}%'
    </if>

    <if test="orgName != null and orgName !=''">
      and  g.`name` like  '%${orgName}%'
    </if>
    <if test="memberCardNo != null and memberCardNo !=''">
      and  b.card_another_id like  '%${memberCardNo}%'
    </if>
    <if test="payTypeName != null and payTypeName !=''">
      and  c.pay_type_name like  '%${payTypeName}%'
    </if>

    <if test="orderStatus != null and orderStatus !=''">
      and  a.order_status =  ${orderStatus}
    </if>
    <if test="chargeOrg != null and chargeOrg !=''">
      and  g.`name`  like  '%${chargeOrg}%'
    </if>

    <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
      and c.created_at  between DATE_FORMAT( concat('${startTime}',' 00-00-00'),'%Y-%m-%d %H:%i:%s') and DATE_FORMAT( concat('${endTime}',' 23-59-59'),'%Y-%m-%d %H:%i:%s')
    </if>

    <if test="(startTime == null or  startTime =='') and (endTime == null  or endTime =='') " >
      and c.created_at  between DATE_FORMAT( curdate(),'%Y-%m-%d %H:%i:%s') and DATE_FORMAT( concat(curdate(),' 23-59-59'),'%Y-%m-%d %H:%i:%s')
    </if>

    ORDER BY c.created_at asc

    ) total


  </select>



</mapper>