<?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.ruoyi.system.mapper.PosStoreEzpayMapper">

    <!-- 门店 ezPay 开通管理列表：列名别名对齐 VO 属性，resultType 自动映射 -->
    <select id="selectEzpayStoreList" parameterType="com.ruoyi.system.domain.vo.PosStoreEzpayVo"
            resultType="com.ruoyi.system.domain.vo.PosStoreEzpayVo">
        SELECT
            s.id              AS storeId,
            s.pos_name        AS posName,
            s.user_id         AS userId,
            u.user_name       AS userName,
            s.is_stall        AS isStall,
            s.is_night_market AS isNightMarket,
            s.night_market_id AS nightMarketId,
            s.invoice_exempt  AS invoiceExempt,
            IFNULL(e.ezpay_status, 0)  AS ezpayStatus,
            IFNULL(e.is_enabled, 1)    AS isEnabled,
            e.ubn             AS ubn,
            CASE WHEN e.merchant_id IS NULL OR e.merchant_id = '' THEN 0 ELSE 1 END AS hasMerchantId,
            e.merchant_id     AS merchantId,
            e.apply_time      AS applyTime,
            e.approved_time   AS approvedTime,
            e.last_verify_result AS lastVerifyResult
        FROM pos_store s
        LEFT JOIN pos_store_ezpay e ON e.store_id = s.id
        LEFT JOIN info_user u ON u.user_id = s.user_id
        <where>
            s.del_flag = '0'
            <if test="isStall != null"> AND s.is_stall = #{isStall} </if>
            <if test="posName != null and posName != ''"> AND s.pos_name LIKE concat('%', #{posName}, '%') </if>
            <if test="invoiceExempt != null"> AND s.invoice_exempt = #{invoiceExempt} </if>
            <if test="ezpayStatus != null"> AND IFNULL(e.ezpay_status, 0) = #{ezpayStatus} </if>
            <if test="quickFilter != null and quickFilter == 'needApply'">
                AND s.invoice_exempt = 0 AND IFNULL(e.ezpay_status, 0) = 0
            </if>
            <if test="quickFilter != null and quickFilter == 'notEnabled'">
                AND s.invoice_exempt = 0 AND IFNULL(e.ezpay_status, 0) IN (0, 1)
            </if>
        </where>
        ORDER BY s.id DESC
    </select>

    <!-- 门店 ezPay 详情 -->
    <select id="selectEzpayStoreDetail" parameterType="Long"
            resultType="com.ruoyi.system.domain.vo.PosStoreEzpayVo">
        SELECT
            s.id              AS storeId,
            s.pos_name        AS posName,
            s.user_id         AS userId,
            u.user_name       AS userName,
            s.is_stall        AS isStall,
            s.is_night_market AS isNightMarket,
            s.night_market_id AS nightMarketId,
            s.invoice_exempt  AS invoiceExempt,
            IFNULL(e.ezpay_status, 0)  AS ezpayStatus,
            IFNULL(e.is_enabled, 1)    AS isEnabled,
            e.ubn             AS ubn,
            CASE WHEN e.merchant_id IS NULL OR e.merchant_id = '' THEN 0 ELSE 1 END AS hasMerchantId,
            e.merchant_id     AS merchantId,
            e.hash_key        AS hashKey,
            e.hash_iv         AS hashIv,
            e.company_id      AS companyId,
            e.apply_time      AS applyTime,
            e.approved_time   AS approvedTime,
            e.last_verify_result AS lastVerifyResult,
            e.remark          AS remark
        FROM pos_store s
        LEFT JOIN pos_store_ezpay e ON e.store_id = s.id
        LEFT JOIN info_user u ON u.user_id = s.user_id
        WHERE s.id = #{storeId}
    </select>

    <!-- 更新门店免用发票标记 -->
    <update id="updateInvoiceExempt">
        UPDATE pos_store SET invoice_exempt = #{exempt} WHERE id = #{storeId}
    </update>

    <!-- 商家上传统编：按 store_id upsert（依赖 uk_store_id） -->
    <insert id="upsertUbn">
        INSERT INTO pos_store_ezpay (store_id, ubn, ezpay_status, is_enabled, create_time, update_time)
        VALUES (#{storeId}, #{ubn}, 0, 1, NOW(), NOW())
        ON DUPLICATE KEY UPDATE ubn = VALUES(ubn), update_time = NOW()
    </insert>

</mapper>
