140 lines
3.2 KiB
Plaintext
140 lines
3.2 KiB
Plaintext
import supa from '@/components/supadb/aksupainstance.uts'
|
|
|
|
/**
|
|
* 优惠券模板模型
|
|
*/
|
|
export type CouponTemplate = {
|
|
id?: string
|
|
cid?: number
|
|
merchant_id?: string
|
|
name: string
|
|
description: string | null
|
|
coupon_type: number // 1:满减, 2:折扣, 3:免运费
|
|
discount_type: number // 1:固定金额, 2:百分比
|
|
discount_value: number
|
|
min_order_amount: number
|
|
max_discount_amount: number | null
|
|
total_quantity: number | null
|
|
per_user_limit: number
|
|
usage_limit: number
|
|
applicable_products: any[]
|
|
applicable_categories: any[]
|
|
start_time: string
|
|
end_time: string
|
|
status: number // 1:正常, 2:暂停, 3:已结束
|
|
created_at?: string
|
|
updated_at?: string
|
|
}
|
|
|
|
export type CouponQuery = {
|
|
name?: string | null
|
|
type?: number | null
|
|
status?: number | null
|
|
page?: number
|
|
pageSize?: number
|
|
}
|
|
|
|
function getCurrentUid(): string | null {
|
|
try {
|
|
const session = supa.getSession()
|
|
return session?.user?.getString('id') ?? null
|
|
} catch (e) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 分页获取优惠券模板列表
|
|
*/
|
|
export async function fetchAdminCoupons(query?: CouponQuery): Promise<{ total: number; items: CouponTemplate[] }> {
|
|
let q = supa.from('ml_coupon_templates').select('*', { count: 'exact' })
|
|
|
|
if (query?.name != null && query.name !== '') {
|
|
q = q.ilike('name', `%${query.name}%`)
|
|
}
|
|
if (query?.type != null) {
|
|
q = q.eq('coupon_type', query.type)
|
|
}
|
|
if (query?.status != null) {
|
|
q = q.eq('status', query.status)
|
|
}
|
|
|
|
const p = query?.page ?? 1
|
|
const ps = query?.pageSize ?? 20
|
|
const from = (p - 1) * ps
|
|
const to = from + ps - 1
|
|
|
|
const { data, error, count } = await q
|
|
.order('created_at', { ascending: false })
|
|
.range(from, to)
|
|
.execute()
|
|
|
|
if (error != null) {
|
|
console.error('获取优惠券列表失败:', error)
|
|
return { total: 0, items: [] as CouponTemplate[] }
|
|
}
|
|
|
|
return {
|
|
total: count ?? 0,
|
|
items: (data ?? []) as CouponTemplate[]
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 保存优惠券模板(新增/更新)
|
|
*/
|
|
export async function saveCouponTemplate(tpl: CouponTemplate): Promise<boolean> {
|
|
const uid = getCurrentUid()
|
|
if (uid == null) return false
|
|
|
|
const { error } = await supa
|
|
.from('ml_coupon_templates')
|
|
.upsert({
|
|
...tpl,
|
|
merchant_id: uid,
|
|
updated_at: new Date().toISOString()
|
|
})
|
|
.execute()
|
|
|
|
if (error != null) {
|
|
console.error('保存优惠券模板失败:', error)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* 切换优惠券状态
|
|
*/
|
|
export async function toggleCouponStatus(id: string, isOpen: boolean): Promise<boolean> {
|
|
const nextStatus = isOpen ? 1 : 2 // 1:正常, 2:暂停
|
|
const { error } = await supa
|
|
.from('ml_coupon_templates')
|
|
.update({ status: nextStatus, updated_at: new Date().toISOString() })
|
|
.eq('id', id)
|
|
.execute()
|
|
|
|
if (error != null) {
|
|
console.error('更新优惠券状态失败:', error)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* 删除优惠券模板
|
|
*/
|
|
export async function deleteCouponTemplate(id: string): Promise<boolean> {
|
|
const { error } = await supa
|
|
.from('ml_coupon_templates')
|
|
.delete()
|
|
.eq('id', id)
|
|
.execute()
|
|
|
|
if (error != null) {
|
|
console.error('删除优惠券失败:', error)
|
|
return false
|
|
}
|
|
return true
|
|
}
|