index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import Tables from "@/components/Tables"
  2. import { useAjax } from "@/Hook/useAjax"
  3. import { getGoodsApi, synGoodsApi } from "@/services/launchAdq/createAd"
  4. import { CheckOutlined, SyncOutlined } from "@ant-design/icons"
  5. import { Button, Modal, Space } from "antd"
  6. import React, { useEffect, useState } from "react"
  7. import style from './index.less'
  8. import columns from './tableConfig'
  9. /**
  10. * 获取商品列表
  11. * @returns
  12. */
  13. interface Props {
  14. visible?: boolean,
  15. onClose?: () => void,
  16. onChange?: (data: any) => void,
  17. data: any
  18. }
  19. const GoodsModal: React.FC<Props> = (props) => {
  20. /************************/
  21. const { visible, onClose, data: data1, onChange } = props
  22. const [tableData, setTableData] = useState<any[]>([])//table数据
  23. const [selectAdz, setSelectAdz] = useState<number>(1) // 选择广告主
  24. const [data, setData] = useState<any>(data1)
  25. const getGoods = useAjax((params) => getGoodsApi(params))
  26. const synGoods = useAjax((params) => synGoodsApi(params))
  27. /************************/
  28. useEffect(() => {
  29. if (data?.length > 0) {
  30. getList([data[selectAdz - 1].adAccountId])
  31. } else {
  32. setTableData([])
  33. }
  34. }, [selectAdz])
  35. // 获取商品列表
  36. const getList = (params: number[]) => {
  37. getGoods.run(params).then(res => {
  38. console.log('res===>', res)
  39. if (res && Object.keys(res)?.indexOf(params[0].toString()) !== -1) {
  40. setTableData(res[params[0]]?.map((item: { productOuterId: string }) => ({ ...item, id: Number(item?.productOuterId?.replace(/\D/ig, '')) })))
  41. } else {
  42. setTableData([])
  43. }
  44. })
  45. }
  46. const handleOk = () => {
  47. onChange && onChange(data)
  48. }
  49. // 同步商品库
  50. const synGoodsList = () => {
  51. synGoods.run(data?.map((item: { id: number }) => item?.id)).then(res => {
  52. getList([data[selectAdz - 1].adAccountId])
  53. })
  54. }
  55. /** 设置选中广告主 */
  56. const handleSelectAdz = (value: number, item: any) => {
  57. if (value === selectAdz) {
  58. return
  59. }
  60. setSelectAdz(value)
  61. }
  62. /** 表格选折 */
  63. const onChangeTable = (selectedRowKeys: React.Key[], selectedRows: any) => {
  64. let newData = JSON.parse(JSON.stringify(data))
  65. newData[selectAdz - 1]['productList'] = selectedRows
  66. setData([...newData])
  67. }
  68. return <Modal
  69. title={<Space>
  70. <span>商品库</span>
  71. <Button size="small" onClick={() => { synGoodsList() }} loading={synGoods?.loading}>同步商品库</Button>
  72. </Space>}
  73. visible={visible}
  74. onCancel={() => { onClose && onClose() }}
  75. onOk={handleOk}
  76. width={1100}
  77. className={style.SelectPackage}
  78. bodyStyle={{ padding: '0 10px 0 10px' }}
  79. >
  80. <div className={style.content}>
  81. <div className={style.left}>
  82. <h4 className={style.title}>广告主账户</h4>
  83. {data?.map((item: { adAccountId: number, id: number }, index: number) => (
  84. <div key={index} onClick={() => { handleSelectAdz(index + 1, item) }} className={`${style.accItem} ${selectAdz === index + 1 && style.select} `}>
  85. {item?.adAccountId}
  86. {data[index].productList?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
  87. </div>))}
  88. </div>
  89. {/* <div className={style.left}>
  90. <h4 className={style.title}>商品库</h4>
  91. {goodsData?.map((item: { name: string, goodsList: any[], catalogId: number }, index: number) => (
  92. <div key={index} onClick={() => { handleGoods(index + 1, { ...queryFormDetail, godsId: goodsData[index].catalogId }) }} className={`${style.accItem} ${selectGoods === index + 1 && style.select} `}>
  93. {item?.name}
  94. {orientPackage[selectAdz - 1]?.goods?.key === item?.catalogId && orientPackage[selectAdz - 1].goods?.data?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
  95. </div>))
  96. }
  97. </div> */}
  98. <div className={style.right}>
  99. <Space style={{ marginBottom: 10 }}>
  100. <Button icon={<SyncOutlined />} type='link' loading={getGoods?.loading} onClick={() => { getList([data[selectAdz - 1].adAccountId]) }}></Button>
  101. </Space>
  102. <Tables
  103. columns={columns()}
  104. dataSource={tableData}
  105. size="small"
  106. loading={getGoods?.loading}
  107. scroll={{ y: 300 }}
  108. bordered
  109. rowSelection={{
  110. type: 'radio',
  111. selectedRowKeys: data[selectAdz - 1]?.productList?.map((item: any) => item?.id?.toString()),
  112. onChange: onChangeTable
  113. }}
  114. />
  115. </div>
  116. </div>
  117. </Modal>
  118. }
  119. export default React.memo(GoodsModal)