Newer
Older
smart-metering-front / src / views / customer / customerInfo / customerList.vue
<script lang="ts" setup name="CustomerList">
import { getCurrentInstance, ref } from 'vue'
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { ICustomer, ICustomerQuery } from './customer_interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { printJSON } from '@/utils/printUtils'
import { exportFile } from '@/utils/exportUtils'
import useTemplateDownload from '@/utils/useTemplateDownload'
import { batchImportCustomer, deleteCustomer, exportCustomerList, getCustomerList } from '@/api/customer/customer'
import { getDictByCode } from '@/api/system/dict'
const { proxy } = getCurrentInstance() as any
const $router = useRouter()
// 查询条件
const listQuery: Ref<ICustomerQuery> = ref({
  businessSize: '', // 业务规模
  customerName: '', // 公司名称
  customerNo: '', // 客户编号
  grade: '', // 履约评级
  offset: 1,
  limit: 20,
})
interface dictType {
  id: string
  name: string
  value: string
}
// const companySizeList = ref<dictType[]>([]) // 公司规模列表
const businessSizeList = ref<dictType[]>([]) // 业务规模列表
const gradeList = ref<dictType[]>([]) // 履约评级列表
// const evaluationList = ref<dictType[]>([]) // 整体评价列表
function getDict() {
  // // 获取公司规模
  // getDictByCode('companySize').then((response) => {
  //   companySizeList.value = response.data
  // })
  // 获取业务规模
  getDictByCode('businessSize').then((response) => {
    businessSizeList.value = response.data
  })
  // 获取履约评级
  getDictByCode('grade').then((response) => {
    gradeList.value = response.data
  })
  // // 获取整体评价
  // getDictByCode('evaluation').then((response) => {
  //   evaluationList.value = response.data
  // })
}
getDict()
// 表头
const columns = ref<TableColumn[]>([
  { text: '客户编号', value: 'customerNo', width: '160', align: 'center' },
  { text: '客户名称', value: 'customerName', align: 'center' },
  { text: '公司规模', value: 'companySizeName', width: '110', align: 'center' },
  { text: '业务规模', value: 'businessSizeName', width: '130', align: 'center' },
  { text: '履约评级', value: 'gradeName', width: '90', align: 'center' },
  { text: '整体评价', value: 'evaluationName', align: 'center' },
  { text: '公司地址', value: 'fullAddress', align: 'center' },
  // { text: '备注', value: 'remark', align: 'center' },
])
// 表格数据
const list = ref<ICustomer[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 选中的内容
const checkoutList = ref<string[]>([])
// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.value.offset = 1
  }
  getCustomerList(listQuery.value).then((response) => {
    // 模拟数据
    // response.data.rows = [{ bankAccount: '银行账户', bankAccountNumber: '银行账号', bankName: '工商银行', briefName: '京东', businessContent: '电商', businessScope: 'test1', grade: '1', gradeName: 'A', companySize: '1', companySizeName: '小微企业', businessSize: '1', businessSizeName: '0-10万', evaluation: '1', evaluationName: '优质', companyAddress: '五棵松', companyAreaName: '海淀区', companyCityName: '北京市', companyCountryName: '中国', companyProvinceName: '北京', companyArea: '110007', companyCity: '110000', companyCountry: 'CN', companyProvince: '100000', createTime: '2022-11-29 21:14:50', director: '刘强东', fax: 'test1', id: '1597579843411234817', invoiceAddress: '五棵松', invoiceAreaName: '海淀区', invoiceCityName: '北京市', invoiceCountryName: '中国', invoiceProvinceName: '北京', invoiceyArea: '110007', invoiceCity: '110000', invoiceCountry: 'CN', invoiceProvince: '100000', mailbox: 'test1', minioFileName: 'test', mobile: 'test', phone: '950618', postalCode: 'test', remark: 'test', customerName: '京东集团', customerNo: 'sygf202211290001', taxNumber: 'test', updateTime: '2023-01-10 09:56:57', website: 'test' }]
    list.value = response.data.rows.map((item: ICustomer) => {
      // if (item.addressProvinceName && item.addressCityName) {
      //   item.fullAddress = `${item.addressProvinceName}/${item.addressCityName}`
      // }
      // else if (item.addressProvinceName || item.addressCityName) {
      //   item.fullAddress = item.addressProvinceName || item.addressCityName
      // }
      // else if (item.addressCountryName) {
      //   item.fullAddress = item.addressCountryName
      // }
      // return item
      if (item.addressProvinceName && item.addressCityName && (item.addressProvinceName === item.addressCityName)) {
        item.fullAddress = item.addressCountryName + item.addressProvinceName + item.addressAreaName + item.fullAddress
      }
      else {
        if (item.addressAreaName) {
          item.fullAddress = item.addressCountryName + item.addressProvinceName + item.addressCityName + item.addressAreaName + item.fullAddress
        }
        else {
          item.fullAddress = item.addressCountryName + item.addressProvinceName + item.addressCityName + item.fullAddress
        }
      }
      return item
    })
    total.value = response.data.total
    loadingTable.value = false
  })
}
// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}

// 点击编辑/详情
const handleEdit = (row: ICustomer, pageType: 'edit' | 'detail') => {
  $router.push(`/customerManage/${pageType}/${row.id}`)
}
// 点击删除
const handleDelete = (index: string, row: ICustomer) => {
  ElMessageBox.confirm(
    `确认删除${row.customerName}吗?`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      deleteCustomer({ id: row.id }).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          fetchData(true)
        }
      })
    })
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    businessSize: '', // 业务规模
    customerName: '', // 公司名称
    customerNo: '', // 客户编号
    grade: '', // 履约评级
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData(true)
}

// 添加客户
const add = () => {
  $router.push('/customerManage/add')
}
// 导出
const exportAll = () => {
  const loading = ElLoading.service({
    lock: true,
    text: '下载中请稍后',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (list.value.length > 0) {
    const params = {
      businessSize: listQuery.value.businessSize, // 业务规模
      customerName: listQuery.value.customerName, // 公司名称
      customerNo: listQuery.value.customerNo, // 客户编号
      grade: listQuery.value.grade, // 履约评级
      ids: checkoutList.value,
    }
    exportCustomerList(params).then((res) => {
      const blob = new Blob([res.data])
      exportFile(blob, '客户列表.xlsx')
    })
  }
  else {
    ElMessage.warning('无数据可导出数据')
  }
  loading.close()
}

// 打印列表
function printList() {
  // 打印列
  const properties = columns.value.map((item) => {
    return {
      field: item.value,
      displayName: item.text,
    }
  })
  if (checkoutList.value.length <= 0 && list.value.length > 0) {
    printJSON(list.value, properties, '客户列表')
  }
  else if (checkoutList.value.length > 0) {
    const printList = list.value.filter((item: ICustomer) => checkoutList.value.includes(item.id))
    printJSON(printList, properties, '客户列表')
  }
  else {
    ElMessage.warning('无可打印内容')
  }
}

// -------------------------------------模板下载、批量导入-----------------------------------
// 模板下载
const templateDownload = () => {
  useTemplateDownload('客户模块')
}
const fileRef = ref() // 文件上传input
const onFileChange = (event: any) => {
  // 原生上传
  // console.log(event.target.files)
  // if (event.target.files[0].type === 'application/pdf') {
  if (event.target.files?.length !== 0) {
    // 创建formdata对象
    const fd = new FormData()
    fd.append('multipartFile', event.target.files[0])
    const loading = ElLoading.service({
      lock: true,
      background: 'rgba(255, 255, 255, 0.8)',
    })
    batchImportCustomer(fd).then((res) => {
      if (res.code === 200) {
        ElMessage.success('导入成功')
        fetchData(true)
        loading.close()
        event.target.value = ''
      }
      else {
        ElMessage.error(res.message)
      }
    })
  }
//   }
//   else {
//     ElMessage.error('请上传pdf格式')
//   }
}
// 批量导入
const batchImport = () => {
  fileRef.value.click()
}
// --------------------------------------------------------------------------------------------

fetchData(true)
</script>

<template>
  <app-container>
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.customerNo"
          placeholder="客户编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.customerName"
          placeholder="公司名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.businessSize" placeholder="业务规模" clearable>
          <el-option v-for="item in businessSizeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.grade" placeholder="履约评级" clearable>
          <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/meter/customer/import')" icon="icon-import" title="批量导入" type="primary" @click="batchImport" />
        <icon-button v-if="proxy.hasPerm('/meter/customer/import')" icon="icon-template" title="模板下载" type="primary" @click="templateDownload" />
        <icon-button v-if="proxy.hasPerm('/meter/customer/add')" icon="icon-add" title="新建" type="primary" @click="add" />
        <icon-button v-if="proxy.hasPerm('/meter/customer/export')" icon="icon-export" title="导出" type="primary" @click="exportAll" />
        <icon-button v-if="proxy.hasPerm('/meter/customer/print')" icon="icon-print" title="打印" type="primary" @click="printList" />
      </template>
      <input v-show="false" ref="fileRef" type="file" accept="pdf/*" @change="onFileChange">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" is-showmulti-select @change="changePage" @multi-select="handleSelectionChange"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                v-if="proxy.hasPerm('/meter/customer/update')"
                size="small"
                type="primary"
                link
                @click="handleEdit(row, 'edit')"
              >
                编辑
              </el-button>
              <el-button
                size="small"
                link
                type="primary"
                @click="handleEdit(row, 'detail')"
              >
                详情
              </el-button>
              <el-button
                v-if="proxy.hasPerm('/meter/customer/delete')"
                size="small"
                link
                type="danger"
                @click="handleDelete(row.$index, row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="scss" scoped>
// 样式
</style>