<?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.BusinessGroupMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.report.persistent.domain.BusinessGroupExt">
    <id column="orgname" jdbcType="VARCHAR" property="orgName" />
  <result column="breakfastamount" jdbcType="DOUBLE" property="breakfastAmount" />
    <result column="breakfastcount" jdbcType="INTEGER" property="breakfastCount" />
    <result column="breakfastaverage" jdbcType="DECIMAL" property="breakfastAverage" />
      <result column="launchamount" jdbcType="DOUBLE" property="launchAmount" />
      <result column="launchcount" jdbcType="INTEGER" property="launchCount" />
      <result column="launchaverage" jdbcType="DECIMAL" property="launchAverage" />
      <result column="supperamount" jdbcType="DOUBLE" property="supperAmount" />
      <result column="suppercount" jdbcType="INTEGER" property="supperCount" />
      <result column="supperaverage" jdbcType="DECIMAL" property="supperAverage" />
      <result column="totalamount" jdbcType="DOUBLE" property="totalAmount" />
      <result column="totalcount" jdbcType="INTEGER" property="totalCount" />
      <result column="totalaverage" jdbcType="DECIMAL" property="totalAverage" />
  </resultMap>


  <select id="selectBusGroupByExample" parameterType="com.neusoft.smart.pos.report.persistent.domain.BusinessGroupExample" resultMap="BaseResultMap">
SELECT

      a.name orgname,

      SUM(if (SUBSTRING(b.created_at,12,2) &lt;= '10', b.real_pay_amount, 0)) breakfastamount,
      COUNT(if (SUBSTRING(b.created_at,12,2) &lt;= '10' AND b.real_pay_amount > 0, true, null)) breakfastcount,
      (cast(SUM(if (SUBSTRING(b.created_at,12,2) &lt;='10', b.real_pay_amount, 0))/COUNT(if (SUBSTRING(b.created_at,12,2) &lt;= '10' AND b.real_pay_amount > 0, true, NULL)) AS DECIMAL(10,2))) breakfastaverage,

      SUM(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15'), b.real_pay_amount, 0)) launchamount,
      COUNT(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15') AND b.real_pay_amount > 0, true, null)) launchcount,
      (cast(SUM(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15'), b.real_pay_amount, 0))/COUNT(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15') AND b.real_pay_amount > 0, true, NULL)) AS DECIMAL(10,2))) launchaverage,

      SUM(if (SUBSTRING(b.created_at,12,2) &gt; '15', b.real_pay_amount, 0)) supperamount,
      COUNT(if (SUBSTRING(b.created_at,12,2) &gt; '15' AND b.real_pay_amount > 0, true, null)) suppercount,
      (cast(SUM(if (SUBSTRING(b.created_at,12,2) &gt; '15', b.real_pay_amount, 0))/COUNT(if (SUBSTRING(b.created_at,12,2) &gt; '15' AND b.real_pay_amount > 0, true, NULL)) AS DECIMAL(10, 2))) supperaverage,

      SUM(b.real_pay_amount) totalamount,
      COUNT(if (b.real_pay_amount > 0, true, null)) totalcount,
      (cast(SUM(b.real_pay_amount)/COUNT(if (b.real_pay_amount > 0, true, null)) AS DECIMAL(10,2))) totalaverage

      FROM org a, order_master_v2 b, organization_group h

      WHERE b.org_id = a.id
      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 = a.id AND h.entity_table_name = 'org' AND b.order_type = '1'

      <if test="orgName != null and orgName !=''">
          and   a.name like  '%${orgName}%'
      </if>

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

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

  </select>
    <select id="queryMemberOrderDetailsCount" parameterType="com.neusoft.smart.pos.report.persistent.domain.BusinessGroupExample" resultType="java.lang.Integer">
        SELECT count(*) FROM (  SELECT

      a.name orgname,

        SUM(if (SUBSTRING(b.created_at,12,2) &lt;= '10', b.real_pay_amount, 0)) breakfastamount,
        COUNT(if (SUBSTRING(b.created_at,12,2) &lt;= '10' AND b.real_pay_amount > 0, true, null)) breakfastcount,
        (cast(SUM(if (SUBSTRING(b.created_at,12,2) &lt;='10', b.real_pay_amount, 0))/COUNT(if (SUBSTRING(b.created_at,12,2) &lt;= '10' AND b.real_pay_amount > 0, true, NULL)) AS DECIMAL(10,2))) breakfastaverage,

        SUM(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15'), b.real_pay_amount, 0)) launchamount,
        COUNT(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15') AND b.real_pay_amount > 0, true, null)) launchcount,
        (cast(SUM(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15'), b.real_pay_amount, 0))/COUNT(if ((SUBSTRING(b.created_at,12,2) &gt; '10' AND SUBSTRING(b.created_at,12,2) &lt;= '15') AND b.real_pay_amount > 0, true, NULL)) AS DECIMAL(10,2))) launchaverage,

        SUM(if (SUBSTRING(b.created_at,12,2) &gt; '15', b.real_pay_amount, 0)) supperamount,
        COUNT(if (SUBSTRING(b.created_at,12,2) &gt; '15' AND b.real_pay_amount > 0, true, null)) suppercount,
        (cast(SUM(if (SUBSTRING(b.created_at,12,2) &gt; '15', b.real_pay_amount, 0))/COUNT(if (SUBSTRING(b.created_at,12,2) &gt; '15' AND b.real_pay_amount > 0, true, NULL)) AS DECIMAL(10, 2))) supperaverage,

        SUM(b.real_pay_amount) totalamount,
        COUNT(if (b.real_pay_amount > 0, true, null)) totalcount,
        (cast(SUM(b.real_pay_amount)/COUNT(if (b.real_pay_amount > 0, true, null)) AS DECIMAL(10,2))) totalaverage

        FROM org a, order_master_v2 b, organization_group h

        WHERE b.org_id = a.id
        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 = a.id AND h.entity_table_name = 'org' AND b.order_type = '1'

        <if test="orgName != null and orgName !=''">
            and   a.name like  '%${orgName}%'
        </if>

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

      GROUP BY a.id order by a.id


        ) total


    </select>
</mapper>