<?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.PersonaConsumptionMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.report.persistent.domain.PersonaConsumptionExt">
    <id column="HIDNumber" jdbcType="VARCHAR" property="hidNumber" />
      <result column="employeenumber" jdbcType="VARCHAR" property="employeeNumber" />
  <result column="employeename" jdbcType="VARCHAR" property="employeeName" />
    <result column="consumetime" jdbcType="DATE" property="consumeTime" />
    <result column="depositamount" jdbcType="DECIMAL" property="depositAmount" />
      <result column="cosumeamount" jdbcType="DECIMAL" property="cosumeAmount" />
      <result column="balance" jdbcType="DECIMAL" property="balance" />
      <result column="mealtimes" jdbcType="VARCHAR" property="mealTimes" />
      <result column="orgname" jdbcType="VARCHAR" property="orgName" />
      <result column="shopname" jdbcType="VARCHAR" property="shopName" />
      <result column="paytype" jdbcType="INTEGER" property="payType" />
      <result column="deviceName" jdbcType="VARCHAR" property="deviceName" />
  </resultMap>


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

      SELECT
      a.card_num HIDNumber,
      b.card_another_id employeenumber,
      b.card_other_id employeename,
      a.created_time consumetime,
      (case a.change_type
      when 0 then a.amount_change
      ELSE '' END ) depositamount,
      (case a.change_type
      when 7 then a.amount_change
      ELSE '' END ) cosumeamount,
      a.amount_after balance,
      (case
      when SUBSTRING(a.change_time,12,2) &lt;= '10' then '上午'
      when (SUBSTRING(a.change_time,12,2) > '10' AND SUBSTRING(a.change_time,12,2) &lt;= '15') then '中午'
      ELSE '下午' END) mealtimes,
      c.name orgname,
      d.name shopname,
      f.pay_type paytype
      , m.alias deviceName
      FROM card_capital_change a, card b, org c, shop d, card_order e, pay_order_v2 f, order_master_v2 g, organization_group h, device m
      WHERE a.card_num = b.card_num AND a.change_order_id = e.card_trade_num AND e.out_trade_id = f.trade_id
      AND f.m_order_number = g.order_number AND g.shop_id = d.id AND g.org_id = c.id AND e.pay_status IN ('2', '3')
      AND h.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
      AND h.entity_table_id = c.id AND h.entity_table_name = 'org' AND m.serial_no = g.serial_no AND m.`status` = 0
      <if test="orgName != null and orgName !=''">
          and   c.name like  '%${orgName}%'
      </if>
      <if test="employeeNumber != null and employeeNumber !=''">
          and   b.card_another_id like  '%${employeeNumber}%'
      </if>

      <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
          and a.created_time 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 a.created_time  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 a.created_time desc

      <if test="offset != null and limit != null ">
           limit ${offset}, ${limit}
      </if>

  </select>
    <select id="queryMemberOrderDetailsCount" parameterType="com.neusoft.smart.pos.report.persistent.domain.PersonaConsumptionExample" resultType="java.lang.Integer">
        SELECT count(*) FROM (SELECT
      a.card_num HIDNumber,
      b.card_other_id employeename,
      a.created_time consumetime,
      (case a.change_type
      when 0 then a.amount_change
      ELSE '' END ) depositamount,
      (case a.change_type
      when 7 then a.amount_change
      ELSE '' END ) cosumeamount,
      a.amount_after balance,
      (case
      when SUBSTRING(a.change_time,12,2) &lt;= '10' then '上午'
      when (SUBSTRING(a.change_time,12,2) > '10' AND SUBSTRING(a.change_time,12,2) &lt;= '15') then '中午'
      ELSE '下午' END) mealtimes,
      c.name orgname,
      d.name shopname,
      f.pay_type paytype

      FROM card_capital_change a, card b, org c, shop d, card_order e, pay_order_v2 f, order_master_v2 g, organization_group h
      WHERE a.card_num = b.card_num AND a.change_order_id = e.card_trade_num AND e.out_trade_id = f.trade_id
      AND f.m_order_number = g.order_number AND g.shop_id = d.id AND g.org_id = c.id AND e.pay_status IN ('2', '3')
      AND h.id IN (SELECT ogr0.childid from organization_group_relation ogr0 WHERE ogr0.parentid = ${orgGroupId} UNION SELECT ${orgGroupId} AS Field)
      AND h.entity_table_id = c.id AND h.entity_table_name = 'org'
        <if test="orgName != null and orgName !=''">
            and   c.name like  '%${orgName}%'
        </if>
        <if test="employeeNumber != null and employeeNumber !=''">
            and   b.card_another_id like  '%${employeeNumber}%'
        </if>
        <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
            and a.created_time 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 a.created_time  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>

        ) total


    </select>
</mapper>