<?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.trade.persistent.ext.mapper.BillDetailExtMapper">
  <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.trade.persistent.domain.BillDetail">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="bill_number" jdbcType="VARCHAR" property="billNumber" />
    <result column="order_number" jdbcType="VARCHAR" property="orderNumber" />
    <result column="org_id" jdbcType="INTEGER" property="orgId" />
    <result column="shop_id" jdbcType="INTEGER" property="shopId" />
    <result column="serial_no" jdbcType="VARCHAR" property="serialNo" />
    <result column="device_name" jdbcType="VARCHAR" property="deviceName" />
    <result column="customer_number" jdbcType="VARCHAR" property="customerNumber" />
    <result column="seq_no" jdbcType="INTEGER" property="seqNo" />
    <result column="member_card_no" jdbcType="VARCHAR" property="memberCardNo" />
    <result column="member_name" jdbcType="VARCHAR" property="memberName" />
    <result column="card_num" jdbcType="VARCHAR" property="cardNum" />
    <result column="card_hid_code" jdbcType="VARCHAR" property="cardHidCode" />
    <result column="order_amount" jdbcType="DECIMAL" property="orderAmount" />
    <result column="real_pay_amount" jdbcType="DECIMAL" property="realPayAmount" />
    <result column="discount_amount" jdbcType="DECIMAL" property="discountAmount" />
    <result column="profit_amount" jdbcType="DECIMAL" property="profitAmount" />
    <result column="profit_rate" jdbcType="INTEGER" property="profitRate" />
    <result column="pay_type" jdbcType="VARCHAR" property="payType" />
    <result column="paid_time" jdbcType="TIMESTAMP" property="paidTime" />
    <result column="order_type" jdbcType="VARCHAR" property="orderType" />
    <result column="location_no" jdbcType="VARCHAR" property="locationNo" />
    <result column="retrvl_ref_no" jdbcType="VARCHAR" property="retrvlRefNo" />
    <result column="ori_trace_no" jdbcType="VARCHAR" property="oriTraceNo" />
    <result column="trans_date" jdbcType="VARCHAR" property="transDate" />
    <result column="ori_batch_no" jdbcType="VARCHAR" property="oriBatchNo" />
    <result column="score" jdbcType="INTEGER" property="score" />
    <result column="version" jdbcType="INTEGER" property="version" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="VARCHAR" property="createdBy" />
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy" />
    <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>

  <select id="queryShopPayTypeProfit" parameterType="com.neusoft.smart.pos.trade.persistent.domain.BillDetailProfitExample" resultType="java.util.Map">
    select
    ifnull(sum(profit_amount),0) as billAmount,
    count(0) as payCount
    from bill_detail_profit
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>

  <select id="queryBillNumberList" parameterType="java.util.Map" resultType="java.util.Map">
    select bill_number as billNumber
    from bill_master
    where left(bill_number,6) = #{month}
    and org_id = #{orgId}
    and shop_id = #{shopId}
  </select>

  <select id="queryBillDetailProfit" parameterType="com.neusoft.smart.pos.trade.persistent.domain.BillDetailProfitExample" resultType="java.util.Map">
      select subject_name as subjectName,
              sum(profit_amount) as profitAmount
      from bill_detail_profit
      <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
      </if>
      GROUP BY subject_name
  </select>

  <!-- 集团主体列表查询方法 -->
  <select id="querySubjectNameListByOrgId" parameterType="java.util.Map" resultType="java.util.Map">
    select rd.subject_name as subjectName
    from bill_settlement_rule r
    left join bill_settlement_rule_detail rd on r.id = rd.bill_rule_id
    where r.org_id = #{orgId}
    and r.rule_status = '1'
    GROUP BY rd.subject_name
  </select>
</mapper>