index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import Tables from "@/components/Tables"
  2. import { useAjax } from "@/Hook/useAjax"
  3. import { getDataSourceApi, sysDataSourceApi } 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 '../goodsModal/index.less'
  8. import columns from './tableConfig'
  9. /**
  10. * 获取数据源
  11. * @returns
  12. */
  13. interface Props {
  14. promotedObjectType?: string
  15. visible?: boolean,
  16. onClose?: () => void,
  17. onChange?: (data: any) => void,
  18. data: any
  19. }
  20. const DataSourceModal: React.FC<Props> = (props) => {
  21. /************************/
  22. const { visible, onClose, data: data1, onChange, promotedObjectType } = props
  23. const [tableData, setTableData] = useState<any[]>([])//table数据
  24. const [selectAdz, setSelectAdz] = useState<number>(1) // 选择广告主
  25. const [data, setData] = useState<any>(data1)
  26. const getDataSource = useAjax((params) => getDataSourceApi(params))
  27. const sysDataSource = useAjax((params) => sysDataSourceApi(params))
  28. /************************/
  29. useEffect(() => {
  30. if (data?.length > 0) {
  31. getList([data[selectAdz - 1].adAccountId])
  32. } else {
  33. setTableData([])
  34. }
  35. }, [selectAdz])
  36. // 获取数据源
  37. const getList = (params: number[]) => {
  38. getDataSource.run({ accountIds: params, promotedObjectType }).then(res => {
  39. if (res && Object.keys(res)?.indexOf(params[0].toString()) !== -1) {
  40. setTableData(res[params[0]]?.map((item: { userActionSetId: string }) => ({ ...item, id: item?.userActionSetId })))
  41. } else {
  42. setTableData([])
  43. }
  44. })
  45. }
  46. const handleOk = () => {
  47. onChange && onChange(data)
  48. }
  49. // 同步数据源
  50. const synDataSourceList = () => {
  51. sysDataSource.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]['userActionSetsList'] = selectedRows
  66. setData([...newData])
  67. }
  68. /** 一键设置 */
  69. const setOnekey = () => {
  70. let dataSourceIds: number[] = data[selectAdz - 1]['userActionSetsList']?.map((item: { userActionSetId: number }) => item.userActionSetId)
  71. let newData = JSON.parse(JSON.stringify(data))
  72. const hide = message.loading(`正在设置...`, 0, () => {
  73. message.success('设置成功');
  74. });
  75. getDataSource.run({ accountIds: newData?.filter((item: { adAccountId: number }) => item.adAccountId !== data[selectAdz - 1].adAccountId)?.map((item: { adAccountId: number }) => item?.adAccountId), promotedObjectType }).then(res => {
  76. if (res && typeof res === 'object') {
  77. Object.keys(res).forEach((key: string) => {
  78. let values = dataSourceIds.map(id => {
  79. let value = res[key]?.find((item: { userActionSetId: number }) => item.userActionSetId === id)
  80. if (value) {
  81. return { ...value, id: value.userActionSetId }
  82. }
  83. return undefined
  84. }).filter(item => item)
  85. if (values.length > 0) {
  86. newData = newData.map((item: { adAccountId: string }) => {
  87. if (item.adAccountId.toString() === key.toString()) {
  88. return { ...item, userActionSetsList: values }
  89. }
  90. return item
  91. })
  92. }
  93. })
  94. setData(newData)
  95. }
  96. message.success('设置完成');
  97. hide()
  98. })
  99. }
  100. return <Modal
  101. title={<Space>
  102. <span>数据源</span>
  103. <Button size="small" onClick={() => { synDataSourceList() }} type='link' loading={sysDataSource?.loading}>同步数据源</Button>
  104. </Space>}
  105. visible={visible}
  106. onCancel={() => { onClose && onClose() }}
  107. onOk={handleOk}
  108. width={1100}
  109. className={style.SelectPackage}
  110. bodyStyle={{ padding: '0 10px 0 10px' }}
  111. >
  112. <div className={style.content}>
  113. <div className={style.left}>
  114. <h4 className={style.title}>媒体账户</h4>
  115. {data?.map((item: { adAccountId: number, id: number }, index: number) => (
  116. <div key={index} onClick={() => { handleSelectAdz(index + 1, item) }} className={`${style.accItem} ${selectAdz === index + 1 && style.select} `}>
  117. {item?.adAccountId}
  118. {data[index].userActionSetsList?.length > 0 && <CheckOutlined style={{ color: '#1890ff' }} />}
  119. </div>
  120. ))}
  121. </div>
  122. <div className={style.right}>
  123. <Space style={{ marginBottom: 10 }} align="end" size={0}>
  124. <Button icon={<SyncOutlined />} type='link' loading={getDataSource?.loading} onClick={() => { getList([data[selectAdz - 1].adAccountId]) }}></Button>
  125. {data.length > 1 && <Button disabled={!data[selectAdz - 1]['userActionSetsList']?.length} onClick={setOnekey} type="link" loading={getDataSource.loading}>
  126. <Space>
  127. <span>一键设置</span>
  128. <Tooltip color="#FFF" overlayInnerStyle={{ color: '#000' }} title="设置其它账号有相同ID的数据源为那个账号的数据源(注意需要数据源ID相同,否则不设置)">
  129. <QuestionCircleOutlined />
  130. </Tooltip>
  131. </Space>
  132. </Button>}
  133. </Space>
  134. <Tables
  135. columns={columns()}
  136. dataSource={tableData}
  137. size="small"
  138. loading={getDataSource?.loading}
  139. scroll={{ y: 300 }}
  140. bordered
  141. rowSelection={{
  142. type: 'checkbox',
  143. selectedRowKeys: data[selectAdz - 1]?.userActionSetsList?.map((item: any) => item?.id?.toString()),
  144. onChange: onChangeTable
  145. }}
  146. />
  147. </div>
  148. </div>
  149. </Modal>
  150. }
  151. export default React.memo(DataSourceModal)