<?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.ConsumptionDetailsMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.report.persistent.domain.ConsumptionDetailsExt">
      <id column="consumetime" jdbcType="TIMESTAMP" property="consumeTime" />
      <result column="consumeamount" jdbcType="FLOAT" property="consumeAmount" />
      <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="serialno" jdbcType="VARCHAR" property="serialNo" />
      <result column="deviceName" jdbcType="VARCHAR" property="deviceName" />

  </resultMap>


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

      SELECT
      a.created_at consumetime,
      a.pay_amount consumeamount,
      (case
      when SUBSTRING(a.created_at,12,2) &lt;= '10' then '上午'
      when (SUBSTRING(a.created_at,12,2) > '10' AND SUBSTRING(a.created_at,12,2) &lt;= '15') then '中午'
      ELSE '下午' END) mealtimes,
      b.name orgname,
      c.name shopname,
      a.pay_type paytype,
      d.serial_no serialno
      , k.alias deviceName
      FROM pay_order_v2 a, org b, shop c, order_master_v2 d, organization_group h, device k
      WHERE a.m_order_number = d.order_number AND d.org_id = b.id AND d.shop_id = c.id AND d.order_type IN ('1', '4') AND a.pay_type IN ('4', '5') AND a.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 = b.id AND h.entity_table_name = 'org'  AND k.serial_no = d.serial_no AND k.`status` = 0
      <if test="serialNo != null and serialNo !=''" >
          and  d.serial_no like '%${serialNo}%'
      </if>
      <if test="orgName != null and orgName !=''">
          and   b.name like  '%${orgName}%'
      </if>

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


      <if test="mealTimes != null and mealTimes !=''">
          <if test="mealTimes =='上午'">
              and SUBSTRING(a.created_at,12,2)  &lt;= '10'
          </if>
          <if test="mealTimes =='中午'">
              and SUBSTRING(a.created_at,12,2) > '10' AND SUBSTRING(a.created_at,12,2) &lt;= '15'
          </if>
          <if test="mealTimes =='下午'">
              and SUBSTRING(a.created_at,12,2) > '15' AND SUBSTRING(a.created_at,12,2) &lt;= '24'
          </if>
      </if>

      <if test="payType != null  and payType !=''">
          and  a.pay_type = ${payType}
      </if>


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

  </select>
    <select id="queryMemberOrderDetailsCount" parameterType="com.neusoft.smart.pos.report.persistent.domain.ConsumptionDetailsExample" resultType="java.lang.Integer">
        SELECT count(*) FROM (   SELECT
        a.created_at consumetime,
        a.pay_amount consumeamount,
        (case
        when SUBSTRING(a.created_at,12,2) &lt;= '10' then '上午'
        when (SUBSTRING(a.created_at,12,2) > '10' AND SUBSTRING(a.created_at,12,2) &lt;= '15') then '中午'
        ELSE '下午' END) mealtimes,
        b.name orgname,
        c.name shopname,
        a.pay_type paytype,
        d.serial_no serialno
        FROM pay_order_v2 a, org b, shop c, order_master_v2 d, organization_group h
        WHERE a.m_order_number = d.order_number AND d.org_id = b.id AND d.shop_id = c.id AND d.order_type IN ('1', '4') AND a.pay_type IN ('4', '5') AND a.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 = b.id AND h.entity_table_name = 'org'
        <if test="serialNo != null and serialNo !=''" >
            and  d.serial_no like '%${serialNo}%'
        </if>
        <if test="orgName != null and orgName !=''">
            and   b.name like  '%${orgName}%'
        </if>

        <if test="startTime != null  and endTime != null  and startTime !='' and endTime !='' " >
            and a.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 a.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>
        <if test="mealTimes != null and mealTimes !=''">
            <if test="mealTimes =='上午'">
                and SUBSTRING(a.created_at,12,2)  &lt;= '10'
            </if>
            <if test="mealTimes =='中午'">
                and SUBSTRING(a.created_at,12,2) > '10' AND SUBSTRING(a.created_at,12,2) &lt;= '15'
            </if>
            <if test="mealTimes =='下午'">
                and SUBSTRING(a.created_at,12,2) > '15' AND SUBSTRING(a.created_at,12,2) &lt;= '24'
            </if>
        </if>

        <if test="payType != null  and payType !=''">
            and  a.pay_type = ${payType}
        </if>


        ORDER BY a.created_at desc

        ) total


    </select>
</mapper>