index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Tables from "@/components/Tables"
  2. import { useAjax } from "@/Hook/useAjax"
  3. import { getGoodsApi, synGoodsApi } from "@/services/launchAdq/createAd"
  4. import { CheckOutlined, QuestionCircleOutlined, SyncOutlined } from "@ant-design/icons"
  5. import { Button, message, Modal, Space, Tooltip } 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: { adAccountId: number }) => item?.adAccountId)).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. // 清空已选
  69. const clearGoods = () => {
  70. let newData = JSON.parse(JSON.stringify(data))
  71. newData[selectAdz - 1]['productList'] = []
  72. setData([...newData])
  73. }
  74. /** 一键设置 */
  75. const setOnekey = () => {
  76. let goodsNames: string[] = data[selectAdz - 1]['productList']?.map((item: { productName: string }) => item.productName)
  77. let newData = JSON.parse(JSON.stringify(data))
  78. const hide = message.loading(`正在设置...`, 0, () => {
  79. message.success('设置成功');
  80. });
  81. getGoods.run(newData?.filter((item: { adAccountId: number }) => item.adAccountId !== data[selectAdz - 1].adAccountId)?.map((item: { adAccountId: number }) => item?.adAccountId)).then(res => {
  82. if (res && typeof res === 'object') {
  83. Object.keys(res).forEach((key: string) => {
  84. let values = goodsNames.map(name => {
  85. let value = res[key]?.find((item: { productName: string }) => item.productName === name)
  86. if (value) {
  87. return { ...value, id: Number(value?.productOuterId?.replace(/\D/ig, '')) }
  88. }
  89. return undefined
  90. }).filter(item => item)
  91. if (values.length > 0) {
  92. newData = newData.map((item: { adAccountId: string }) => {
  93. if (item.adAccountId.toString() === key.toString()) {
  94. return { ...item, productList: values }
  95. }
  96. return item
  97. })
  98. }
  99. })
  100. setData(newData)
  101. }
  102. message.success('设置完成');
  103. hide()
  104. })
  105. }
  106. return <Modal
  107. title={<Space>
  108. <span>商品库</span>
  109. <Button size="small" onClick={() => { synGoodsList() }} type="link" loading={synGoods?.loading}>同步商品库</Button>
  110. </Space>}
  111. visible={visible}
  112. onCancel={() => { onClose && onClose() }}
  113. onOk={handleOk}
  114. width={1100}
  115. className={style.SelectPackage}
  116. bodyStyle={{ padding: '0 10px 0 10px' }}
  117. >
  118. <div className={style.content}>
  119. <div className={style.left}>
  120. <h4 className={style.title}>媒体账户</h4>
  121. {data?.map((item: { adAccountId: number, id: number }, index: number) => (
  122. <div key={index} onClick={() => { handleSelectAdz(index + 1, item) }} className={`${style.accItem} ${selectAdz === index + 1 && style.select} `}>
  123. {item?.adAccountId}
  124. {data[index].productList?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
  125. </div>
  126. ))}
  127. </div>
  128. <div className={style.right}>
  129. <Space style={{ marginBottom: 10 }} align="end" size={0}>
  130. <Button icon={<SyncOutlined />} type='link' loading={getGoods?.loading} onClick={() => { getList([data[selectAdz - 1].adAccountId]) }}>刷新</Button>
  131. {data?.length > 1 && <Button disabled={!data[selectAdz - 1]['productList']?.length} onClick={setOnekey} type="link" loading={getGoods.loading}>
  132. <Space>
  133. <span>一键设置</span>
  134. <Tooltip color="#FFF" overlayInnerStyle={{ color: '#000' }} title="设置其它账号有相同名称的商品为那个账号的商品(注意需要用户商品称相同,否则不设置)">
  135. <QuestionCircleOutlined />
  136. </Tooltip>
  137. </Space>
  138. </Button>}
  139. {data[selectAdz - 1]?.productList?.length > 0 && <Button type='link' onClick={() => { clearGoods() }}>清空</Button>}
  140. </Space>
  141. <Tables
  142. columns={columns()}
  143. dataSource={tableData}
  144. size="small"
  145. loading={getGoods?.loading}
  146. scroll={{ y: 300 }}
  147. bordered
  148. defaultPageSize={100}
  149. rowSelection={{
  150. type: 'radio',
  151. selectedRowKeys: data[selectAdz - 1]?.productList?.map((item: any) => item?.id?.toString()),
  152. onChange: onChangeTable
  153. }}
  154. />
  155. </div>
  156. </div>
  157. </Modal>
  158. }
  159. export default React.memo(GoodsModal)