<?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.order.persistent.mapper.OrderMasterExtMapper">
    <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExt">
        <result column="org_id" jdbcType="INTEGER" property="orgId" />
        <result column="shop_id" jdbcType="INTEGER" property="shopId" />
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
        <result column="order_count" jdbcType="INTEGER" property="orderCount" />
        <result column="recharge_count" jdbcType="INTEGER" property="rechargeCount" />
        <result column="recharge_amount" jdbcType="DECIMAL" property="rechargeAmount" />
    </resultMap>
    <resultMap id="MemberOrderResultMap" type="com.neusoft.smart.pos.order.persistent.domain.OrderMasterMemberExt">
        <result column="org_id" jdbcType="INTEGER" property="orgId" />
        <result column="shop_id" jdbcType="INTEGER" property="shopId" />
        <result column="trade_id" jdbcType="VARCHAR" property="tradeId" />
        <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="order_amount" jdbcType="DECIMAL" property="orderAmount" />
        <result column="real_pay_amount" jdbcType="DECIMAL" property="realPayAmount" />
        <result column="order_status" jdbcType="VARCHAR" property="orderStatus" />
        <result column="order_type" jdbcType="VARCHAR" property="orderType" />
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber" />
        <result column="pay_amount" jdbcType="INTEGER" property="payAmount" />
        <result column="card_balance" jdbcType="INTEGER" property="balance" />
        <result column="member_point" jdbcType="INTEGER" property="memberPoint" />
        <result column="pay_type" jdbcType="VARCHAR" property="payType" />
    </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="queryShopOrderSummary" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExtExample" resultMap="BaseResultMap">
        select
        org_id,
        shop_id,
        ifnull(sum(if(order_type != 3,real_pay_amount,0))/100,0) as total_amount,
        ifnull(sum(if(order_type = 3,real_pay_amount,0))/100,0) as recharge_amount,
        count( IF(order_type != 3, order_number , null))  as order_count,
        count( IF(order_type = 3, order_number , null))  as recharge_count
        from order_master_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        GROUP BY org_id,shop_id
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>
    <select id="queryShopOrderSummaryCount" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExtExample" resultType="java.lang.Integer">
        select count(*)
        from (
        select
        count(*)
        from order_master_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        GROUP BY org_id,shop_id) temp
    </select>

    <select id="queryHasPayAmountSum" parameterType="com.neusoft.smart.pos.payorder.persistent.domain.PayOrderExample" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_amount),0) as payAmount
        from pay_order_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>

    <select id="queryHasRefundAmountSum" parameterType="com.neusoft.smart.pos.payorder.persistent.domain.PayRefundOrderExample" resultType="java.math.BigDecimal">
        select ifnull(sum(refunded_amount),0) as refundAmount
        from pay_refund_order_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>

    <select id="queryShopOrderDetailsTotal" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        select ifnull(sum(real_pay_amount),0) as realAmount,
        IFNULL(sum(order_amount),0) as orderAmount
        from order_master_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <select id="queryShopOrderDetailsTotalAmount" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        select ifnull(sum(real_pay_amount),0)/100 as orderAmount,0 as score
        from order_master_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <select id="queryShopOrderDetailsLeftJoinPayOrderTotalAmount" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        select ifnull(sum( ifnull(gbo.orderAmount,0) ),0) as orderAmount,0 as score
        from
            (
        SELECT
            order_number as orderNumber,
            ifnull(sum(real_pay_amount),0)/100 as orderAmount
        FROM
            order_master_v2 AS o
            LEFT JOIN pay_order_v2 AS p ON o.order_number = p.m_order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
            GROUP BY
        o.order_number) as gbo
    </select>
    <select id="querySpecialPayTypeShopOrderNumbers4Web" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
    SELECT
        order_number as orderNumber
    FROM
        order_master_v2 AS o
        LEFT JOIN pay_order_v2 AS p ON o.order_number = p.m_order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        ORDER BY o.created_at desc
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>
    <select id="countShopOrderDetailsLeftJoinPayOrderCount" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        SELECT
        count(DISTINCT p.m_order_number) AS count
        FROM
        order_master_v2 AS o
        LEFT JOIN pay_order_v2 AS p ON o.order_number = p.m_order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <select id="queryShopPayOrderList" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        SELECT
        pay_type_dict_v2.pay_type AS payType,
        ifnull( payAmount, 0 )AS payAmount,
        ifnull( discountAmount, 0 )AS discountAmount,
        ifnull( payCount, 0 )AS payCount,
        ifnull( refundAmount, 0 )AS refundAmount,
        ifnull( refundCount, 0 )AS refundCount,
        ifnull( ( ifnull(payAmount,0) - ifnull(refundAmount,0) ), 0 ) AS amount
        FROM
        pay_type_dict_v2
        LEFT JOIN (
        SELECT
        ifnull( sum( pay_order_v2.pay_amount ) / 100, 0 ) AS payAmount,
        count( 0 ) AS payCount,
        0 AS discountAmount,
        pay_type AS payType
        FROM
        pay_order_v2
        LEFT JOIN order_master_v2 ON pay_order_v2.m_order_number = order_master_v2.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        AND ( pay_order_v2.pay_status = 2 OR pay_order_v2.pay_status = 5 OR pay_order_v2.pay_status = 4 )
        GROUP BY
        pay_type
        ) AS paid ON pay_type_dict_v2.pay_type = paid.payType
        LEFT JOIN (
        SELECT
        ifnull( sum( refunded_amount ) / 100, 0 ) refundAmount,
        count( 0 ) refundCount,
        pay_type AS payType
        FROM
        pay_refund_order_v2 AS pay_order_v2
        LEFT JOIN order_master_v2 ON pay_order_v2.m_order_number = order_master_v2.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        AND refund_status = 2
        GROUP BY
        pay_type
        ) AS refund ON pay_type_dict_v2.pay_type = refund.payType
    </select>

    <select id="selectLoginLogoutRecordList" parameterType="java.util.Map" resultType="java.util.Map">
        select * from
        (select org_id, shop_id, username, serial_no,
        min(case when status=1  then created_at else '3109-12-30 23:59:59' end) as loginTime,
        max(case when status=2 and is_shift=1  then created_at else 0 end) as shiftTime
        from user_login_record
        where org_id = #{org_id} and created_at &gt; #{start_time} and created_at &lt;= #{end_time} <if test="username != null"> and username = #{username}</if>
        GROUP BY username) as a
        where shiftTime > 0
    </select>

    <select id="selectShiftRecordList" parameterType="java.util.Map" resultType="java.util.Map">
        select org_id, shop_id, username, serial_no, status, created_at, is_shift
        from user_login_record
        where org_id = #{org_id} and created_at &gt; #{start_time} and created_at &lt;= #{end_time} and is_shift=1 and status=2 <if test="username != null"> and username = #{username}</if>
        GROUP BY username
    </select>

    <select id="selectLoginRecord" parameterType="java.util.Map" resultType="java.util.Map">
        select username, created_at
        from user_login_record
        where org_id = #{org_id} and username = #{username} and created_at &gt; #{start_time} and created_at &lt;= #{end_time} and status=1
    </select>

    <select id="selectTradeSummary" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        SELECT
        po.createdBy,
        sum( IF ( po.payType = 6, po.paidAmount, 0 ) ) AS "6payType",
        sum( IF ( po.payType = 24, po.paidAmount, 0 ) ) AS "24payType",
        sum( IF ( po.payType = 31, po.paidAmount, 0 ) ) AS "31payType",
        sum( IF ( po.payType = 32, po.paidAmount, 0 ) ) AS "32payType",
        sum( IF ( po.payType = 6, IFNULL(ro.refundAmount,0), 0 ) ) AS "6payTypeRefund",
        sum( IF ( po.payType = 24, IFNULL(ro.refundAmount,0), 0 ) ) AS "24payTypeRefund",
        sum( IF ( po.payType = 31, IFNULL(ro.refundAmount,0), 0 ) ) AS "31payTypeRefund",
        sum( IF ( po.payType = 32, IFNULL(ro.refundAmount,0), 0 ) ) AS "32payTypeRefund",
        sum( IF ( po.createdHour > 12, po.paidAmount - IFNULL(ro.refundAmount,0), 0 ) ) AS pmPaidAmount,
        sum( IF ( 12 >= po.createdHour, po.paidAmount - IFNULL(ro.refundAmount, 0),0 ) ) AS amPaidAmount,
        sum( po.paidAmount - IFNULL(ro.refundAmount, 0) ) AS paidAmount
        FROM
        (
        SELECT
        o.created_by AS createdBy,
        o.order_number AS orderNumber,
        p.pay_type AS payType,
        p.pay_type_name AS payTypeName,
        sum( IFNULL( pay_amount, 0 ) ) / 100 AS paidAmount,
        HOUR ( o.created_at ) AS createdHour
        FROM
        pay_order_v2 AS p
        LEFT JOIN order_master_v2 AS o ON p.m_order_number = o.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        AND ( p.pay_status = 2 OR p.pay_status = 5 OR p.pay_status = 4 )
        AND p.pay_type != 29
        AND p.pay_type != 30
        GROUP BY
        o.order_number,
        p.pay_type
        ) AS po
        LEFT JOIN (
        SELECT
        o.order_number AS orderNumber,
        r.pay_type AS payType,
        sum( IFNULL( r.refunded_amount, 0 ) ) / 100 AS refundAmount
        FROM
        pay_refund_order_v2 AS r
        LEFT JOIN order_master_v2 AS o ON r.m_order_number = o.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        AND ( r.refund_status = 2 )
        AND r.pay_type != 29
        AND r.pay_type != 30
        GROUP BY
        o.order_number,
        r.pay_type
        ) AS ro ON ro.orderNumber = po.orderNumber and po.payType=ro.payType
        GROUP BY
        createdBy
    </select>

    <select id="queryUsernameByCustomerNumber" parameterType="java.util.Map" resultType="java.util.Map">
        select username
        from user_info
        where id = #{id}
    </select>

    <select id="queryShopDayOrder" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        select org_id as orgId,
        shop_id as shopId,
        DATE_FORMAT(created_at,'%Y-%m-%d') as createdAt,
        ifnull(SUM(real_pay_amount)/100,0) as orderAmount
        from order_master_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        GROUP BY org_id,shop_id,DATE_FORMAT(created_at,'%Y-%m-%d')
        order by shop_id asc
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>

    <select id="queryShopTradeOrderList" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        select
        shop_id as shopId,
        ifnull(sum(if(order_status=5 and order_type != 3,order_amount,0))/100,0) as payAmount,
        ifnull(sum(if(order_status=5 and order_type != 3,(order_amount-real_pay_amount),0))/100,0) as discountAmount,
        count(order_type != 3 or null) as payCount,
        ifnull(sum(if((order_status=8 or order_status=9) and order_type != 3,real_pay_amount,0)),0) refundAmount,
        count(if((order_status=8 or order_status=9) and order_type != 3,true,null)) refundCount,
        ifnull(sum(if(order_status=5 and order_type != 3,real_pay_amount,0)),0) as amount,
        count(if(order_status=5 and order_type = 3,true,null)) rechargeCount,
        ifnull(sum(if(order_status=5 and order_type = 3,real_pay_amount,0)),0) as rechargeAmount
        from order_master_v2
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>

        GROUP BY shop_id
        order by shop_id asc
    </select>

    <select id="queryShopPayTypeTradeList" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        SELECT
            ifnull( refund.shop_id, paid.shop_id ) AS shopId,
            pay_type_dict_v2.pay_type AS payType,
            ifnull( payAmount, 0 ) - ifnull( refundAmount, 0 ) AS payAmount,
            ifnull( payCount, 0 ) + ifnull( refundCount, 0 ) AS payCount
        FROM
            pay_type_dict_v2
            LEFT JOIN (
        SELECT
            shop_id,
            pay_type AS payType,
            ifnull( sum( pay_order_v2.pay_amount ) / 100, 0 ) AS payAmount,
            count( 0 ) AS payCount,
            0 AS discountAmount
        FROM
            pay_order_v2
            LEFT JOIN order_master_v2 ON pay_order_v2.m_order_number = order_master_v2.order_number
                <if test="_parameter != null">
                    <include refid="Example_Where_Clause" />
                </if>
            AND ( pay_order_v2.pay_status = 2 OR pay_order_v2.pay_status = 5 OR pay_order_v2.pay_status = 4 )
        GROUP BY
            pay_type,
            shop_id
            ) AS paid ON pay_type_dict_v2.pay_type = paid.payType
            LEFT JOIN (
            SELECT
                shop_id,
                pay_type AS payType,
                ifnull( sum( refunded_amount ) / 100, 0 ) refundAmount,
                count( 0 ) refundCount
            FROM
                pay_refund_order_v2 AS pay_order_v2
                LEFT JOIN order_master_v2 ON pay_order_v2.m_order_number = order_master_v2.order_number
                <if test="_parameter != null">
                    <include refid="Example_Where_Clause" />
                </if>
                AND refund_status = 2
            GROUP BY
                pay_type,
                shop_id
            ) AS refund ON pay_type_dict_v2.pay_type = refund.payType
            AND paid.shop_id = refund.shop_id
        ORDER BY
            payType,
        shopId ASC
    </select>
    <select id="queryShopOrderDetails" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        SELECT
        o.org_id AS orgId,
        o.shop_id AS shopId,
        o.created_at AS createdAt,
        o.order_number AS orderNumber,
        o.order_sub_type AS orderSubType,
        o.serial_no AS serialNo,
        d.alias AS deviceName,
        o.created_by AS createdBy,
        o.order_amount / 100 AS orderAmount,
        sum( IF ( p.pay_type = 29, pay_amount, 0 ) ) / 100 AS discount,
        ( o.order_amount - sum( IF ( p.pay_type = 30 OR p.pay_type = 29, pay_amount, 0 ) ) ) / 100 AS shouldAmount,
        o.real_pay_amount / 100 AS paidAmount,
        0 AS score,
        IF
        (
        count( DISTINCT IF ( p.pay_type = 30 OR p.pay_type = 29, NULL, p.pay_type ) ) = 1
        AND count( DISTINCT IF ( p.pay_type = 30 OR p.pay_type = 29, p.pay_type, NULL ) ) = 0,
        p.pay_type_name,
        IF
        (
        count( DISTINCT IF ( p.pay_type = 30 OR p.pay_type = 29, NULL, p.pay_type ) ) = 0,
        "-",
        "to query"
        )
        ) AS payTypeName,
        count( DISTINCT IF ( p.pay_type = 30 OR p.pay_type = 29, NULL, p.pay_type ) ) AS payTypeCount,
        count( DISTINCT IF ( p.pay_type = 30 OR p.pay_type = 29, p.pay_type, NULL ) ) AS discountTypeCount,
        o.order_status AS orderStatus
        FROM
        order_master_v2 AS o
        LEFT JOIN device AS d ON d.serial_no = o.serial_no
        LEFT JOIN pay_order_v2 AS p ON o.order_number = p.m_order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        <if test="deviceStatus != null">
            AND d.status= ${deviceStatus}
        </if>
        GROUP BY
        o.order_number
        ORDER BY createdAt desc
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>
    <select id="getOrderPayDetail" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
        pay_type_name as payTypeName,
        concat("(", cast( pay_amount / 100 AS DECIMAL ( 18, 2 )), ")") as payTypeAmountString
        FROM
            pay_order_v2
        WHERE
            m_order_number = #{m_order_number}
            AND pay_type != 30
            AND pay_type != 29
            AND ( pay_status = 2 OR pay_status = 4 OR pay_status = 5 )
    </select>


    <select id="queryMemberOrderDetails" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultMap="MemberOrderResultMap">
        SELECT * FROM order_master_v2
        RIGHT JOIN (
            SELECT * FROM(
                SELECT pay_order_v2.m_order_number AS m_order_number,
                card.card_balance AS card_balance,
                card.card_num AS card_num,
                pay_order_v2.pay_type AS pay_type,
                pay_order_v2.pay_amount AS pay_amount,
                card.card_another_id AS member_card_no,
                card.card_point AS member_point,
                pay_order_v2.member_card_no AS member_card_noy,
                pay_order_v2.trade_id AS trade_id
                FROM card RIGHT JOIN pay_order_v2 ON card.card_num = pay_order_v2.card_num
            ) AS cardOrderTable
        ) AS payOrderTable ON payOrderTable.m_order_number = order_master_v2.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
        ORDER BY order_master_v2.created_at desc
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>

    <select id="queryMemberOrderDetailsCount" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.lang.Integer">
        SELECT count(*) FROM order_master_v2
        RIGHT JOIN (
            SELECT * FROM(
                SELECT pay_order_v2.m_order_number AS m_order_number,
                card.card_balance AS card_balance,
                card.card_num AS card_num,
                pay_order_v2.pay_type AS pay_type,
                pay_order_v2.pay_amount AS pay_amount,
                card.card_another_id AS member_card_no,
                card.card_point AS member_point,
                pay_order_v2.member_card_no AS member_card_noy
                FROM card RIGHT JOIN pay_order_v2 ON card.card_num = pay_order_v2.card_num
            ) AS cardOrderTable
        ) AS payOrderTable ON payOrderTable.m_order_number = order_master_v2.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>

    <select id="queryMemberOrderDetailsTotalAmount" parameterType="com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample" resultType="java.util.Map">
        select ifnull(sum(real_pay_amount),0)/100 as orderAmount,0 as score
        FROM order_master_v2
        RIGHT JOIN (
            SELECT * FROM(
                SELECT pay_order_v2.m_order_number AS m_order_number,
                card.card_balance AS card_balance,
                card.card_num AS card_num,
                pay_order_v2.pay_type AS pay_type,
                pay_order_v2.pay_amount AS pay_amount,
                card.card_another_id AS member_card_no,
                card.card_point AS member_point,
                pay_order_v2.member_card_no AS member_card_noy
                FROM card RIGHT JOIN pay_order_v2 ON card.card_num = pay_order_v2.card_num
            ) AS cardOrderTable
        ) AS payOrderTable ON payOrderTable.m_order_number = order_master_v2.order_number
        <if test="_parameter != null">
            <include refid="Example_Where_Clause" />
        </if>
    </select>

    <select id="queryBalanceAndPoint" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT amount_after, point_after
        FROM card_capital_change
        RIGHT JOIN card_order ON card_capital_change.change_order_id = card_order.card_trade_num
        WHERE out_trade_id = #{trade_id}
    </select>
    <select id="queryShopNameAndOrgName" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT org.name as org_name,
                org.id as org_id,
                shop.name as shop_name
        FROM shop, org
        WHERE shop.org = org.id and shop.id = ${shop_id}
    </select>
</mapper>