import { create } from 'zustand'; import { devtools } from 'zustand/middleware'; import dayjs from 'dayjs' import { fetchJSON, postForm, postJSON } from '@/utils/request'; import { HT_HOST } from '@/config'; import { groupBy } from '@/utils/commons'; export const searchAgencyAction = async (param) => { const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/products_search`, param); return errcode !== 0 ? [] : result; }; /** * 搜索所有产品, 返回产品列表 * ! 只有审核通过, 已发布的 * @param {object} params { keyword, use_year, product_types, travel_agency_id, city } */ export const searchPublishedProductsAction = async (param) => { const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/web_products_search`, param); return errcode !== 0 ? [] : result; }; export const copyAgencyDataAction = async (postbody) => { const formData = new FormData(); Object.keys(postbody).forEach((key) => { formData.append(key, postbody[key]); }); const { errcode, result } = await postForm(`${HT_HOST}/Service_BaseInfoWeb/agency_products_copy`, formData); return errcode === 0 ? true : false; }; export const getAgencyProductsAction = async (param) => { const _param = { ...param, use_year: String(param.use_year || '').replace('all', ''), audit_state: String(param.audit_state || '').replace('all', '') }; const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/travel_agency_products`, _param); return errcode !== 0 ? { agency: {}, products: [] } : result; }; /** * @param {object} body { id, travel_agency_id, extras: [{id, title, code}] } */ export const addProductExtraAction = async (body) => { // console.log('addProductExtraAction', body); // return true; // test: 先不更新到HT const { errcode, result } = await postJSON(`${HT_HOST}/Service_BaseInfoWeb/products_extras_add`, body); return errcode === 0 ? true : false; }; /** * */ export const delProductExtrasAction = async (body) => { // return true; // test: 先不更新到HT const { errcode, result } = await postJSON(`${HT_HOST}/Service_BaseInfoWeb/products_extras_del`, body); return errcode === 0 ? true : false; }; /** * 获取指定产品的附加项目 * @param {object} param { id, travel_agency_id, use_year } */ export const getAgencyProductExtrasAction = async (param) => { const _param = { ...param, use_year: (param.use_year || '').replace('all', '') }; const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/products_extras`, _param); return errcode !== 0 ? [] : result; }; /** * 审核一条价格 */ export const postProductsQuoteAuditAction = async (auditState, quoteRow) => { const postbody = { audit_state: auditState, id: quoteRow.id, travel_agency_id: quoteRow.travel_agency_id, }; const formData = new FormData(); Object.keys(postbody).forEach((key) => { formData.append(key, postbody[key]); }); const json = await postForm(`${HT_HOST}/Service_BaseInfoWeb/quotation_audit`, formData); return json; // return errcode !== 0 ? {} : result; }; /** * 供应商提交审核 */ export const postAgencyAuditAction = async (travel_agency_id, use_year) => { const postbody = { use_year, travel_agency_id, }; const formData = new FormData(); Object.keys(postbody).forEach((key) => { formData.append(key, postbody[key]); }); const { errcode, result } = await postForm(`${HT_HOST}/Service_BaseInfoWeb/agency_submit`, formData); return { errcode, result, success: errcode === 0 }; // const { errcode, result } = json; // return errcode !== 0 ? {} : result; }; /** * 保存一个产品 */ export const postProductsSaveAction = async (products) => { const { errcode, result } = await postJSON(`${HT_HOST}/Service_BaseInfoWeb/agency_product_save`, products); return { errcode, result, success: errcode === 0 }; } const initialState = { loading: false, searchValues: {}, // 客服首页: 搜索条件 agencyList: [], // 客服首页: 搜索结果 activeAgency: {}, // 审核/编辑 页: 当前的供应商 agencyProducts: {}, // 审核/编辑 页: 供应商产品列表 editingProduct: {}, // 编辑页: 当前编辑的产品 quotationList: [], // 编辑页: 当前产品报价列表 editing: false, switchParams: {}, // 头部切换参数 }; export const useProductsStore = create( devtools((set, get) => ({ // 初始化状态 ...initialState, // state actions setLoading: (loading) => set({ loading }), setSearchValues: (searchValues) => set({ searchValues }), setAgencyList: (agencyList) => set({ agencyList }), setActiveAgency: (activeAgency) => set({ activeAgency }), setAgencyProducts: (agencyProducts) => set({ agencyProducts }), // TODO:产品和价格会分开查询编辑, setEditingProduct: (product) => { set(() => ({ editingProduct: product, quotationList: product?.quotation })) }, setEditing: (editing) => set({ editing }), setSwitchParams: (switchParams) => set({ switchParams }), appendNewProduct: (productItem) => { const { setActiveAgency, agencyProducts, setAgencyProducts } = get(); const typeGroup = agencyProducts[productItem.info.product_type_id] || []; const newIndex = typeGroup.findIndex((item) => item.info.id === productItem.info.id); if (newIndex !== -1) { typeGroup.splice(newIndex, 1, productItem); } else { typeGroup.unshift(productItem); } return set({ agencyProducts: { ...agencyProducts, [productItem.info.product_type_id]: typeGroup }, }); }, reset: () => set(initialState), newEmptyQuotation: () => ({ id: null, adult_cost: 0, child_cost: 0, currency: 'RMB', unit_id: '0', group_size_min: 1, group_size_max: 10, use_dates: [ dayjs().startOf('M'), dayjs().endOf('M') ], weekdays: '5, 6' }), // TODO:添加价格后,重新读取列表 appendQuotationList: (newList) => { set((state) => ({ quotationList: [...state.quotationList, ...newList] })) }, saveOrUpdateQuotation: (formValues) => { const { quotationList } = get() console.info('formValues: ', formValues) const prevList = quotationList.filter(q => q.id === formValues.id) console.info(prevList) if (prevList.length > 0) { const prevQuotation = prevList[0] console.info('formValues: ', formValues) console.info('prevQuotation: ', prevQuotation) for (const [key, value] of Object.entries(formValues)) { const prevValue = prevQuotation[key] const hasChanged = prevValue === value console.log(`${key}: ${prevValue} - ${value} (${hasChanged})`) } } }, // side effects searchAgency: async (param) => { const { setLoading, setAgencyList } = get(); setLoading(true); const res = await searchAgencyAction(param); setAgencyList(res); setLoading(false); }, getAgencyProducts: async (param) => { const { setLoading, setActiveAgency, setAgencyProducts, editingProduct, setEditingProduct } = get(); setLoading(true); setAgencyProducts({}); // setEditingProduct({}); const res = await getAgencyProductsAction(param); const productsData = groupBy(res.products, (row) => row.info.product_type_id); setAgencyProducts(productsData); setActiveAgency(res.agency); if (editingProduct?.info?.id) { const item = (productsData[editingProduct.info.product_type_id] || []).find((item) => item.info.id === editingProduct.info.id); setEditingProduct(item); } else { setEditingProduct({}); } setLoading(false); }, getAgencyProductExtras: async (param) => { const res = await getAgencyProductExtrasAction(param); // todo: }, })) ); export default useProductsStore;