<?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.gxwebsoft.glt.mapper.GltUserTicketMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*, u.nickname, u.avatar, u.phone, m.name AS templateName, o.pay_price AS payPrice, o.order_status as orderStatus
        FROM glt_user_ticket a
        LEFT JOIN gxwebsoft_core.sys_user u ON a.user_id = u.user_id
        LEFT JOIN glt_ticket_template m ON a.template_id = m.id
        <!-- 使用 order_id + tenant_id 关联，避免 order_no 跨租户/重复导致 a.id 数据被 JOIN 放大 -->
        LEFT JOIN shop_order o ON a.order_no = o.order_no AND a.tenant_id = o.tenant_id AND o.deleted = 0
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.templateId != null">
                AND a.template_id = #{param.templateId}
            </if>
            <if test="param.goodsId != null">
                AND a.goods_id = #{param.goodsId}
            </if>
            <if test="param.orderId != null">
                AND a.order_id = #{param.orderId}
            </if>
            <if test="param.orderNo != null">
                AND a.order_no LIKE CONCAT('%', #{param.orderNo}, '%')
            </if>
            <if test="param.orderStatus != null">
              AND o.order_status = #{param.orderStatus}
            </if>
            <if test="param.orderGoodsId != null">
                AND a.order_goods_id = #{param.orderGoodsId}
            </if>
            <if test="param.totalQty != null">
                AND a.total_qty = #{param.totalQty}
            </if>
            <if test="param.availableQty != null">
                AND a.available_qty = #{param.availableQty}
            </if>
            <if test="param.frozenQty != null">
                AND a.frozen_qty = #{param.frozenQty}
            </if>
            <if test="param.usedQty != null">
                AND a.used_qty = #{param.usedQty}
            </if>
            <if test="param.releasedQty != null">
                AND a.released_qty = #{param.releasedQty}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.keywords != null">
                AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
                  OR a.user_id = #{param.keywords}
                  OR a.order_no = #{param.keywords}
                )
              </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.glt.entity.GltUserTicket">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.glt.entity.GltUserTicket">
        <include refid="selectSql"></include>
    </select>

    <!-- 我的可用水票总数（sum(available_qty)） -->
    <select id="sumAvailableQtyByUserId" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(available_qty), 0)
        FROM glt_user_ticket
        WHERE user_id = #{userId}
          AND tenant_id = #{tenantId}
          AND status = 0
          AND deleted = 0
    </select>

</mapper>
