import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { findCrmCustomerPage, saveCrmCustomer, updateCrmCustomer, getCrmCustomerById, findAddressListByCustomerId, saveCrmCustomerAddress, deleteCrmCustomerAddress, } from '@/api/mdmCustomer/index' import type { Customer, CustomerForm, CustomerAddress, UserRole, SearchForm } from '@/types/mdmCustomer/customer' export const useCustomerStore = defineStore('mdmCustomer', () => { // ==================== 列表 ==================== const tableData = ref([]) const total = ref(0) const loading = ref(false) const searchForm = ref({}) const pagination = ref({ pageIndex: 1, pageRows: 10 }) // ==================== 角色 ==================== const currentRole = ref('admin') // ==================== 详情 ==================== const detailVisible = ref(false) const detailData = ref(null) const detailLoading = ref(false) // ==================== 表单弹窗 ==================== const formVisible = ref(false) const formMode = ref<'add' | 'edit'>('add') const formData = ref(emptyForm()) const formVersionNum = ref(null) const submitting = ref(false) // ==================== 地址管理 ==================== const addressList = ref([]) const addressFormVisible = ref(false) const addressFormData = ref<{ addressId?: number | null; customerId?: number | null; addressDetail: string; isDefault: number }>(emptyAddressForm()) const editingAddressIndex = ref(-1) // ==================== 确认弹窗 ==================== const confirmVisible = ref(false) const confirmTitle = ref('') const confirmMessage = ref('') const confirmCallback = ref<(() => void) | null>(null) // ==================== Getters ==================== const statusFilterOptions = computed(() => { if (currentRole.value === 'admin') { return CUSTOMER_STATUS_OPTIONS } // 销售员/经理默认仅查看正常状态 return [{ label: '正常', value: '正常' }] }) // ==================== Actions ==================== function emptyForm(): CustomerForm { return { customerId: null, customerCode: '', customerName: '', shortName: '', tradeType: '', creditCode: '', parentId: null, responsibleUserId: null, customerStatus: '正常', } } function emptyAddressForm() { return { addressId: null, customerId: null, addressDetail: '', isDefault: 0, } } /** 查询客户列表 */ async function fetchList(params?: Partial) { loading.value = true try { const res = await findCrmCustomerPage({ pageIndex: pagination.value.pageIndex, pageRows: pagination.value.pageRows, params: { ...searchForm.value, ...params, // 角色默认筛选 customerStatus: currentRole.value === 'admin' ? (searchForm.value.customerStatus || '') : '正常', }, }) if (res.code === 800) { const d = res.data || {} tableData.value = d.rows || d.data || [] total.value = Number(d.total || d.count) || 0 } } catch (err) { console.error('查询客户列表失败:', err) tableData.value = [] total.value = 0 } finally { loading.value = false } } /** 查询客户详情 */ async function fetchDetail(customerId: number) { detailLoading.value = true try { const res = await getCrmCustomerById(customerId) if (res.code === 800) { detailData.value = res.data addressList.value = res.data?.addressList || [] } } catch (err) { console.error('查询详情失败:', err) } finally { detailLoading.value = false } } /** 打开新增表单 */ function openAddForm() { formMode.value = 'add' formData.value = emptyForm() formVersionNum.value = null formVisible.value = true } /** 打开编辑表单 */ function openEditForm(row: Customer) { formMode.value = 'edit' formData.value = { customerId: row.customerId || null, customerCode: row.customerCode || '', customerName: row.customerName || '', shortName: row.shortName || '', tradeType: row.tradeType || '', creditCode: row.creditCode || '', parentId: row.parentId || null, responsibleUserId: row.responsibleUserId || null, customerStatus: row.customerStatus || '正常', } formVersionNum.value = row.versionNum || null formVisible.value = true // 预加载地址 if (row.customerId) fetchAddressList(row.customerId) } /** 保存客户(新增/编辑) */ async function saveOrUpdateCustomer() { submitting.value = true try { const payload: Record = { ...formData.value } if (formMode.value === 'add') { delete payload.customerCode delete payload.customerId } else { payload.versionNum = formVersionNum.value if (addressList.value.length > 0) { payload.addressList = addressList.value } } const api = formMode.value === 'edit' ? updateCrmCustomer : saveCrmCustomer const res = await api({ params: payload }) if (res.code === 800) { formVisible.value = false await fetchList() return true } return false } finally { submitting.value = false } } /** 加载地址列表 */ async function fetchAddressList(customerId: number) { try { const res = await findAddressListByCustomerId(customerId) if (res.code === 800) { addressList.value = res.data || [] } } catch { addressList.value = [] } } /** 打开地址新增弹窗 */ function openAddressForm(customerId: number) { editingAddressIndex.value = -1 addressFormData.value = { ...emptyAddressForm(), customerId } addressFormVisible.value = true } /** 编辑地址(打开弹窗回填) */ function editAddressItem(addr: CustomerAddress, index: number) { editingAddressIndex.value = index addressFormData.value = { addressId: addr.addressId || null, customerId: addr.customerId, addressDetail: addr.addressDetail || '', isDefault: addr.isDefault || 0, } addressFormVisible.value = true } /** 保存地址 */ async function saveAddress() { try { const res = await saveCrmCustomerAddress({ params: addressFormData.value }) if (res.code === 800) { addressFormVisible.value = false if (formData.value.customerId) await fetchAddressList(formData.value.customerId) return true } return false } catch { return false } } /** 删除地址 */ async function deleteAddress(addressId: number, customerId: number) { try { const res = await deleteCrmCustomerAddress(addressId) if (res.code === 800) { await fetchAddressList(customerId) return true } return false } catch { return false } } /** 切换角色 */ function setRole(role: UserRole) { currentRole.value = role searchForm.value = {} pagination.value.pageIndex = 1 fetchList() } /** 重置搜索 */ function resetSearch() { searchForm.value = {} pagination.value.pageIndex = 1 fetchList() } /** 打开确认弹窗 */ function openConfirm(title: string, message: string, cb: () => void) { confirmTitle.value = title confirmMessage.value = message confirmCallback.value = cb confirmVisible.value = true } /** 执行确认回调 */ function doConfirm() { confirmCallback.value?.() confirmVisible.value = false } return { // state tableData, total, loading, searchForm, pagination, currentRole, detailVisible, detailData, detailLoading, formVisible, formMode, formData, formVersionNum, submitting, addressList, addressFormVisible, addressFormData, editingAddressIndex, confirmVisible, confirmTitle, confirmMessage, confirmCallback, // getters statusFilterOptions, // actions fetchList, fetchDetail, openAddForm, openEditForm, saveOrUpdateCustomer, fetchAddressList, openAddressForm, editAddressItem, saveAddress, deleteAddress, setRole, resetSearch, openConfirm, doConfirm, emptyForm, } }) /** 客户状态选项(全局) */ export const CUSTOMER_STATUS_OPTIONS = [ { label: '正常', value: '正常' }, { label: '停用', value: '停用' }, ] /** 贸易类型选项(全局) */ export const TRADE_TYPE_OPTIONS = [ { label: '内销', value: 'DOMESTIC' }, { label: '外销', value: 'EXPORT' }, ]