import { axios } from "timi-web-api"; import type { RecycleExchange, RecycleExchangeActionRequest, RecycleExchangeCreateRequest, RecycleExchangeDetail, RecycleExchangePage, RecycleExchangeRule, RecycleExchangeRulePage, RecycleExchangeStatRow, RecycleItem, RecycleItemPage, RecyclePageResult, RecycleStatRow } from "@/types/Recycle"; import { pendingNoticeEvents } from "@/utils/pendingNoticeEvents"; import { getCurrentStoreId, withCurrentStoreId } from "@/utils/store"; const CONFIG_URI = "/gao/recycle/config"; const EXCHANGE_URI = "/gao/recycle/exchange"; const STAT_URI = "/gao/recycle/stat"; /** 配置接口的 storeId 走查询参数,可省略时由后端按登录用户门店范围解析。 */ function configParams(page: RecycleItemPage | RecycleExchangeRulePage): Record { return { storeId: page.storeId || getCurrentStoreId() || undefined, status: page.status, keyword: page.keyword || undefined, recycleItemId: "recycleItemId" in page ? page.recycleItemId : undefined, skuId: "skuId" in page ? page.skuId : undefined, offset: page.offset, limit: page.limit }; } /** 兑换单状态变化会改变待核销数量,通知顶部的待处理提示重新取数。 */ function notifyPendingNoticeChanged(result: RecycleExchange): RecycleExchange { pendingNoticeEvents.emit("changed", getCurrentStoreId()); return result; } // ---------- 配置 ---------- /** 回收物配置列表,包含启用和停用项,支持按名称与状态筛选。 */ async function itemList(page: RecycleItemPage): Promise> { return await axios.post(`${CONFIG_URI}/items`, undefined, { params: configParams(page) }); } /** 回收物配置详情,回填服务端拼接好的来源产品与 SKU 名称。 */ async function itemDetail(id: string): Promise { return await axios.post(`${CONFIG_URI}/item/detail`, undefined, { params: { storeId: getCurrentStoreId() || undefined, id } }); } /** 保存回收物配置:新增不传 id,修改必须提交完整配置。 */ async function itemSave(item: RecycleItem): Promise { return await axios.post(`${CONFIG_URI}/item/save`, withCurrentStoreId(item)); } /** 规则列表,返回全部状态,按 sort 升序、创建时间倒序。 */ async function ruleList(page: RecycleExchangeRulePage): Promise> { return await axios.post(`${CONFIG_URI}/rules`, undefined, { params: configParams(page) }); } /** 启用规则列表,创建兑换单时使用。回收物或产品后来停用时仍可能出现,以服务端校验为准。 */ async function activeRuleList(page: RecycleExchangeRulePage): Promise> { return await axios.post(`${CONFIG_URI}/rules/active`, undefined, { params: configParams(page) }); } /** 兑换规则详情,回填回收物、产品、SKU 与业务单位名称。 */ async function ruleDetail(id: string): Promise { return await axios.post(`${CONFIG_URI}/rule/detail`, undefined, { params: { storeId: getCurrentStoreId() || undefined, id } }); } /** 保存规则配置:新增不传 id,修改必须提交完整配置。 */ async function ruleSave(rule: RecycleExchangeRule): Promise { return await axios.post(`${CONFIG_URI}/rule/save`, withCurrentStoreId(rule)); } // ---------- 兑换单 ---------- /** 创建兑换单。requestNo 为幂等号,重试必须原样复用。 */ async function create(request: RecycleExchangeCreateRequest): Promise { const result = await axios.post(`${EXCHANGE_URI}/create`, withCurrentStoreId(request)); return notifyPendingNoticeChanged(result); } /** 兑换单分页,按业务时间、创建时间及 ID 倒序。 */ async function page(request: RecycleExchangePage): Promise> { return await axios.post(`${EXCHANGE_URI}/page`, withCurrentStoreId(request)); } async function detail(id: string): Promise { return await axios.post(`${EXCHANGE_URI}/detail`, undefined, { params: { id } }); } /** * 核销待核销单。成功返回的兑换单状态可能已被并发操作改变,页面应重新查询详情。 * 门店由服务端按兑换单自身归属解析,请求不需要带 storeId。 */ async function verify(request: RecycleExchangeActionRequest): Promise { const result = await axios.post(`${EXCHANGE_URI}/verify`, request); return notifyPendingNoticeChanged(result); } /** 撤销待核销单,recycleReturned 决定回收物是否全部退还客户。门店同理由服务端取。 */ async function cancel(request: RecycleExchangeActionRequest): Promise { const result = await axios.post(`${EXCHANGE_URI}/cancel`, request); return notifyPendingNoticeChanged(result); } /** 冲正已核销单。库存商品必须 stockReturned 且至少一张 PHOTO。门店同理由服务端取。 */ async function revoke(request: RecycleExchangeActionRequest): Promise { const result = await axios.post(`${EXCHANGE_URI}/revoke`, request); return notifyPendingNoticeChanged(result); } // ---------- 统计 ---------- /** 回收统计,时间区间为 [startAt, endAt)。 */ async function statRecycle(startAt: number, endAt: number): Promise { return await axios.post(`${STAT_URI}/recycle`, undefined, { params: { storeId: getCurrentStoreId() || undefined, startAt, endAt } }); } /** 兑换统计,时间区间为 [startAt, endAt)。 */ async function statExchange(startAt: number, endAt: number): Promise { return await axios.post(`${STAT_URI}/exchange`, undefined, { params: { storeId: getCurrentStoreId() || undefined, startAt, endAt } }); } export default { itemList, itemDetail, itemSave, ruleList, activeRuleList, ruleDetail, ruleSave, create, page, detail, verify, cancel, revoke, statRecycle, statExchange };