<?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.RefundRecordMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.report.persistent.domain.RefundRecordExt">
    <id column="ordernumber" jdbcType="INTEGER" property="orderNumber" />
      <id column="refundamount" jdbcType="FLOAT" property="refundAount" />
  <result column="HIDnumber" jdbcType="VARCHAR" property="hidNumber" />
    <result column="employename" jdbcType="VARCHAR" property="employeName" />
      <result column="refundtime" jdbcType="TIMESTAMP" property="refundTime" />
    <result column="pay_type" jdbcType="INTEGER" property="payType" />
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="seller_org_id" jdbcType="INTEGER" property="sellerOrgId" />
    <result column="original_order_number" jdbcType="VARCHAR" property="originalOrderNumber" />
      <result column="orgname" jdbcType="VARCHAR" property="orgName" />
      <result column="serialno" jdbcType="VARCHAR" property="serialNo" />
      <result column="deviceName" jdbcType="VARCHAR" property="deviceName" />
  </resultMap>


  <select id="selectRefundByExample" parameterType="com.neusoft.smart.pos.report.persistent.domain.RefundRecordExample" resultMap="BaseResultMap">

      SELECT refundnumber, refundamount,path,employeename,DATE_FORMAT(refundtime,'%Y-%m-%d %H:%i:%s') refundtime,HIDnumber,pay_type,id,seller_org_id, original_order_number,
      orgname,serialno,deviceName  from (
      SELECT
      DISTINCT a.id refundnumber, a.refunded_amount refundamount, b.`name` path, a.created_at refundtime,
      (case d.pay_type
      when '13' then g.card_num
      ELSE '' END) HIDnumber,
      (case d.pay_type
      when '13' then g.card_other_id
      ELSE '' END) employeename,
      d.pay_type, b.id, a.seller_org_id, a.original_order_number ,b.`name` orgname,a.serial_no serialno, k.alias deviceName
      FROM order_refund_v2 a, org b, pay_order_v2 d, card_capital_change e, card_order f, card g, organization_group h, organization_group_relation i, device k
      WHERE
      a.seller_org_id IN
      (SELECT DISTINCT m.entity_table_id FROM organization_group m
      WHERE m.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
      AND m.entity_table_name = 'org')
      AND b.id = a.seller_org_id
      and d.m_order_number = a.original_order_number AND d.pay_status = '2' AND a.refund_status = '3'
      AND (d.pay_type = '13' AND g.card_id = e.card_id AND f.out_trade_id = d.trade_id AND e.change_order_id = f.card_trade_num) AND k.serial_no = a.serial_no AND k.`status` = 0
      UNION
      SELECT
      DISTINCT a.id refundnumber, a.refunded_amount refundamount,  b.`name` path,  a.created_at refundtime,  '' HIDnumber,  '' employeename,  d.pay_type,
      b.id, a.seller_org_id,  a.original_order_number ,b.`name` orgName,a.serial_no serialno, k.alias deviceName
      FROM order_refund_v2 a, org b, pay_order_v2 d, organization_group h, organization_group_relation i, device k
      WHERE
      a.seller_org_id IN
      (SELECT DISTINCT m.entity_table_id FROM organization_group m
      WHERE m.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
      AND m.entity_table_name = 'org') AND b.id = a.seller_org_id
      and d.m_order_number = a.original_order_number AND d.pay_status = '2' AND a.refund_status = '3' AND k.serial_no = a.serial_no AND k.`status` = 0
      AND (d.pay_type = '6' OR d.pay_type = '4' OR d.pay_type = '5') ) ss where 1 = 1

      <if test="serialNo != null  and serialNo !=''">
          and  ss.serialno like '%${serialNo}%'
      </if>
      <if test="path != null  and path !=''">
          and  ss.path  like '%${path}%'
      </if>
      <if test="orgName != null and orgName !=''">
          and  ss.orgName like  '%${orgName}%'
      </if>

      <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
          and ss.refundtime  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 ss.refundtime  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>


      <if test="offset != null and limit != null ">
          order by ss.refundnumber asc limit ${offset}, ${limit}
      </if>
  </select>
    <select id="queryMemberOrderDetailsCount" parameterType="com.neusoft.smart.pos.report.persistent.domain.RefundRecordExample" resultType="java.lang.Integer">
        SELECT count(*) FROM (   SELECT refundnumber, refundamount,path,refundtime,HIDnumber,pay_type,id,seller_org_id, original_order_number,
        orgname,serialno  from (
        SELECT
        DISTINCT a.id refundnumber, a.refunded_amount refundamount, b.`name` path, a.created_at refundtime,
        (case d.pay_type
        when '13' then g.card_num
        ELSE '' END) HIDnumber,
        (case d.pay_type
        when '13' then g.card_other_id
        ELSE '' END) employeename,
        d.pay_type, b.id, a.seller_org_id, a.original_order_number ,b.`name` orgname,a.serial_no serialno
        FROM order_refund_v2 a, org b, pay_order_v2 d, card_capital_change e, card_order f, card g, organization_group h, organization_group_relation i
        WHERE
        a.seller_org_id IN
        (SELECT DISTINCT m.entity_table_id FROM organization_group m
        WHERE m.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
        AND m.entity_table_name = 'org')
        AND b.id = a.seller_org_id
        and d.m_order_number = a.original_order_number AND d.pay_status = '2' AND a.refund_status = '3'
        AND (d.pay_type = '13' AND g.card_id = e.card_id AND f.out_trade_id = d.trade_id AND e.change_order_id = f.card_trade_num)
        UNION
        SELECT
        DISTINCT a.id refundnumber, a.refunded_amount refundamount,  b.`name` path,  a.created_at refundtime,  '' HIDnumber,  '' employeename,  d.pay_type,
        b.id, a.seller_org_id,  a.original_order_number ,b.`name` orgName,a.serial_no serialno
        FROM order_refund_v2 a, org b, pay_order_v2 d, organization_group h, organization_group_relation i
        WHERE
        a.seller_org_id IN
        (SELECT DISTINCT m.entity_table_id FROM organization_group m
        WHERE m.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
        AND m.entity_table_name = 'org') AND b.id = a.seller_org_id
        and d.m_order_number = a.original_order_number AND d.pay_status = '2' AND a.refund_status = '3'
        AND (d.pay_type = '6' OR d.pay_type = '4' OR d.pay_type = '5') ) ss where 1 = 1

        <if test="serialNo != null and serialNo !=''">
            and  ss.serialno like '%${serialNo}%'
        </if>
        <if test="path != null  and path !=''">
            and  ss.path  like '%${path}%'
        </if>
        <if test="orgName != null and orgName !=''">
            and  ss.orgName like  '%${orgName}%'
        </if>

        <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
            and ss.refundtime 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 ss.refundtime  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 ss.refundnumber asc

        ) total


    </select>
</mapper>

