adPlanList.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import { useAjax } from '@/Hook/useAjax'
  2. import { Col, Row, message, Space, Button, Switch, notification, Modal, Tooltip, Dropdown, Menu } from 'antd'
  3. import React, { useEffect, useCallback, useState, useRef } from 'react'
  4. import TableData from '../../components/TableData'
  5. import { putAdqAdgroupsSync, delListAdqAdgroupsApi, newEditAdqAdgroupsDataApi, editAdqAdgroupsDataApi, putAdqAdgroupsSyncBatch, putModifyCustomAudienceApi, getPutUserApi, delUserTagApi } from '@/services/launchAdq/adq'
  6. import { CopyOutlined, DeleteOutlined, DownOutlined, ExclamationCircleOutlined, FieldTimeOutlined, FormOutlined, PauseCircleOutlined, PlayCircleOutlined, QuestionCircleOutlined, SyncOutlined, TransactionOutlined } from '@ant-design/icons'
  7. import UpdateAd from './updateAd'
  8. import Copy from './copy'
  9. import { planAdConfig } from '../config'
  10. import Log from '../log'
  11. import SetEarlyWarning from '@/components/EarlyWarning/setEarlyWarning'
  12. import CrowdPackModal from '../../components/crowdPackModal'
  13. import './index.less'
  14. import tablePlanConfig from './tablePlanListConfig'
  15. import { AdListProps, getAdListApi } from '@/services/adMonitor/adMonitor'
  16. import FilterQuery from './FilterQuery'
  17. import Details from '@/pages/adMonitor/adMonitorList/components/Details'
  18. import AdExpandedRowRender from './adExpandedRowRender'
  19. import { useSize } from 'ahooks'
  20. import RuleAccountLog from '@/components/EarlyWarning/ruleAccountLog'
  21. import PlanTag from './planTag'
  22. const AdPlanList: React.FC<{ userId: string }> = (props) => {
  23. /***********************/
  24. const { userId } = props
  25. const [selectedRows, setSelectedRows] = useState<any[]>([])
  26. const [update, setUpdate] = useState<{ visible: boolean, title: string }>({ visible: false, title: '' })
  27. const [model, setModel] = useState(true)
  28. const [copyData, setCopyData] = useState<{ visible: boolean }>({ visible: false })
  29. const [detailShow, setDetailShow] = useState<boolean>(false)
  30. const [detailData, setDetailData] = useState<any>({})
  31. const [czjlShow, setCzjlShow] = useState(false)
  32. const [cpVisible, setCpVisible] = useState(false)
  33. const [accountCreateLogs, setAccountCreateLogs] = useState<{ adAccountId: number, id: number, customAudienceList?: any[], excludedCustomAudienceList?: any[] }[]>([])
  34. const [queryForm, setQueryForm] = useState<AdListProps>({ pageNum: 1, pageSize: 20, columns: [], isDeleted: false })
  35. const [filterForm, setFilterForm] = useState<AdListProps>()
  36. const getAdList = useAjax((params) => getAdListApi(params), { formatResult: true })
  37. const syncAjax = useAjax((adAccountId) => putAdqAdgroupsSync(adAccountId))
  38. const delListAdqAdgroups = useAjax((params) => delListAdqAdgroupsApi(params))
  39. const editAdqAdgroupsData = useAjax((params) => newEditAdqAdgroupsDataApi(params))
  40. const editAdqAdgroups = useAjax((params) => editAdqAdgroupsDataApi(params))
  41. const putModifyCustomAudience = useAjax((params) => putModifyCustomAudienceApi(params))
  42. const putAdqAdgroupsSyncBatchApi = useAjax((params) => putAdqAdgroupsSyncBatch(params))
  43. const getPutUser = useAjax((params) => getPutUserApi(params))
  44. const delUserTag = useAjax((params) => delUserTagApi(params))
  45. const [logVisible, setLogVisible] = useState<boolean>(false)
  46. const [adgroupId, setAdgroupId] = useState<string>('')
  47. const [adgroupName, setAdgroupName] = useState<string>('')
  48. const [accountIdRule, setAccountIdRule] = useState<string>('')
  49. const [tagVisible, setTagVisible] = useState<boolean>(false)
  50. const [tagData, setTagData] = useState<any>({})
  51. const configName = '广告列表New'
  52. const ref = useRef(null)
  53. const size = useSize(ref)
  54. const [scrollLeft, setScrollLeft] = useState<number>(0)
  55. /************************/
  56. useEffect(() => {
  57. getList()
  58. }, [filterForm, queryForm])
  59. const getList = () => {
  60. let message = localStorage.getItem(`myAdMonitorConfig1.0.0_${configName}`)
  61. if (message) {
  62. message = JSON.parse(message)
  63. }
  64. let columns: string[] = []
  65. if (message && Array.isArray(message)) {
  66. message.forEach((item: { serverIndex: any; dataIndex: string; }) => {
  67. if (!['cz', 'cost_speed'].includes(item.dataIndex)) {
  68. columns.push(item?.serverIndex || 'adgroup_data.' + item.dataIndex)
  69. }
  70. })
  71. } else {
  72. planAdConfig.forEach((item: any) => {
  73. item?.data?.forEach((d: { default: any, serverIndex: string, dataIndex: string }) => {
  74. if (d.default && !['cz', 'cost_speed'].includes(d.dataIndex)) {
  75. columns.push(d?.serverIndex || 'adgroup_data.' + d.dataIndex)
  76. }
  77. })
  78. })
  79. }
  80. getAdList.run({ ...queryForm, ...filterForm, columns })
  81. }
  82. useEffect(() => {
  83. getPutUser.run({ userId })
  84. }, [userId])
  85. // 同步
  86. const sync = useCallback(() => {
  87. let arr = [...new Set(selectedRows?.map(item => item.accountId))]
  88. syncAjax.run({ accountIdList: arr }).then(res => {
  89. res && getAdList.refresh()
  90. res ? message.success('同步成功!') : message.error('同步失败!')
  91. })
  92. }, [getAdList, selectedRows])
  93. /** 删除 */
  94. const deleteHandle = (type: 0 | 1, adgroupId?: number) => {
  95. const hide = message.loading('删除中。。。', 0)
  96. delListAdqAdgroups.run({ adgroupIds: type === 1 ? selectedRows.map(item => item.adgroup_id) : [adgroupId] }).then(res => {
  97. hide()
  98. message.success('删除成功')
  99. setSelectedRows([])
  100. getAdList.refresh()
  101. }).catch(() => hide())
  102. }
  103. /** 修改排期出价 */
  104. const editScheduling = () => {
  105. setUpdate({ visible: true, title: '批量修改' })
  106. }
  107. /** 修改排期出价 */
  108. const editDeepConversion = () => {
  109. setUpdate({ visible: true, title: '批量修改深度优化' })
  110. }
  111. // 单个启停
  112. const onChange = () => {
  113. getAdList.refresh()
  114. setSelectedRows([])
  115. }
  116. // 批量启停
  117. const adStatus = (type: 'play' | 'suspend') => {
  118. let params: any = {}
  119. if (type === 'play') {
  120. params.configuredStatus = 'AD_STATUS_NORMAL'
  121. params.adgroupIds = selectedRows.filter((item: { configuredStatus: string, adgroup_id: number }) => item.configuredStatus === 'AD_STATUS_SUSPEND').map(item => item.adgroup_id)
  122. } else {
  123. params.configuredStatus = 'AD_STATUS_SUSPEND'
  124. params.adgroupIds = selectedRows.filter((item: { configuredStatus: string, adgroup_id: number }) => item.configuredStatus === 'AD_STATUS_NORMAL').map(item => item.adgroup_id)
  125. }
  126. if (params.adgroupIds.length === 0) {
  127. message.warn(`所以账号都是${type === 'play' ? '启动' : '暂停'}状态,无需${type === 'play' ? '启动' : '暂停'}操作`)
  128. return
  129. }
  130. editAdqAdgroupsData.run(params).then(res => {
  131. message.success(`${type === 'play' ? '启动' : '暂停'}成功: ${res.success},失败: ${res.fail}`)//
  132. if (res?.fail) {
  133. notification.error({
  134. message: `${type === 'play' ? '启动' : '暂停'}失败`,
  135. description: `成功: ${res.success},修改失败${res.fail}条,失败的请到任务列表查看`,
  136. duration: 0
  137. });
  138. }
  139. getAdList.refresh()
  140. setSelectedRows([])
  141. })
  142. }
  143. //同步广告
  144. const syncAd = useCallback(() => {
  145. const hide = message.loading('同步中。。。', 0)
  146. putAdqAdgroupsSyncBatchApi.run({ adgroupIds: selectedRows?.map(item => item.adgroup_id) }).then(res => {
  147. hide()
  148. if (res) {
  149. message.success('同步成功!')
  150. getAdList.refresh()
  151. }
  152. }).catch(() => hide())
  153. }, [selectedRows])
  154. // 批量复制
  155. const copyHandle = () => {
  156. setCopyData({ visible: true })
  157. }
  158. const handleSave = (row: any) => {
  159. const hide = message.loading(`广告“${row.adgroup_id}”广告名称修改成<${row.adgroup_name}>,修改中`, 0, () => {
  160. message.success('修改成功');
  161. });
  162. editAdqAdgroups.run({ adgroupIds: [row.adgroup_id], adgroupName: row.adgroup_name }).then(res => {
  163. message.success('修改广告名称成功')
  164. getAdList.refresh()
  165. hide()
  166. })
  167. }
  168. const handleSaveDaily = (row: any) => {
  169. console.log('row--->', row)
  170. const hide = message.loading(`广告“${row.adgroup_id}”广告预算修改成<${row.daily_budget}元>,修改中`, 0, () => {
  171. message.success('修改成功');
  172. });
  173. editAdqAdgroups.run({ adgroupIds: [row.adgroup_id], dailyBudget: row.daily_budget * 100 }).then(res => {
  174. message.success('修改广告预算成功')
  175. getAdList.refresh()
  176. hide()
  177. })
  178. }
  179. const details = (data: any) => {
  180. setDetailData(data)
  181. setDetailShow(true)
  182. }
  183. // 设置人群包
  184. const setRqb = () => {
  185. const { accountId } = selectedRows[0]
  186. setAccountCreateLogs([{ id: accountId, adAccountId: accountId }])
  187. setCpVisible(true)
  188. }
  189. // 确认提交人群包
  190. const handleRqb = (value: any[]) => {
  191. if ((value[0]?.customAudienceList && value[0]?.customAudienceList?.length > 0) || (value[0]?.excludedCustomAudienceList && value[0]?.excludedCustomAudienceList?.length > 0)) {
  192. let { adAccountId, customAudienceList = [], excludedCustomAudienceList = [] } = value[0]
  193. let adgroupIds = selectedRows.map((item: { adgroup_id: number }) => item.adgroup_id)
  194. let customAudienceIds = customAudienceList.map((item: { id: number }) => item.id)
  195. let excludedCustomAudienceIds = excludedCustomAudienceList.map((item: { id: number }) => item.id)
  196. const hide = message.loading('正在修改。。。', 0)
  197. setAccountCreateLogs([])
  198. setCpVisible(false)
  199. let params: any = { adAccountId, adgroupIds };
  200. if (excludedCustomAudienceIds.length > 0) {
  201. params['excludedCustomAudienceIds'] = excludedCustomAudienceIds
  202. }
  203. if (customAudienceIds?.length > 0) {
  204. params['customAudienceIds'] = customAudienceIds
  205. }
  206. putModifyCustomAudience.run(params).then(res => {
  207. hide()
  208. setSelectedRows([])
  209. message.success('修改成功,请到腾讯广告平台查看')
  210. }).catch(err => hide())
  211. } else {
  212. message.error('请选择用户群')
  213. }
  214. }
  215. useEffect(() => {
  216. const headerBodyScroll = (e: any) => {
  217. let el = document.querySelector(`.expandClassname .expendTable .ant-table-body`);
  218. if (el) {
  219. setScrollLeft(e.target.scrollLeft)
  220. }
  221. }
  222. document.querySelector(`.expandClassname .expendTable .ant-table-body`)?.addEventListener('scroll', headerBodyScroll);
  223. () => {
  224. document.querySelector(`.expandClassname .expendTable .ant-table-body`)?.removeEventListener('scroll', headerBodyScroll);
  225. }
  226. }, [])
  227. const log = (value: any) => {
  228. setAccountIdRule(value.account_id)
  229. setAdgroupId(value.adgroup_id)
  230. setAdgroupName(value.adgroup_name)
  231. setLogVisible(true)
  232. }
  233. const handleTag = (value: any) => {
  234. setTagData(value)
  235. setTagVisible(true)
  236. }
  237. const delTag = (value: any) => {
  238. delUserTag.run({ accountId: value.account_id, adgroupId: value.adgroup_id }).then(res => {
  239. if (res) {
  240. message.success('删除成功')
  241. getAdList.refresh()
  242. }
  243. })
  244. }
  245. return <div>
  246. {/* 打标签 */}
  247. {tagVisible && <PlanTag
  248. visible={tagVisible}
  249. data={tagData}
  250. onClose={() => setTagVisible(false)}
  251. onChange={() => {
  252. getAdList.refresh()
  253. setTagVisible(false)
  254. }}
  255. />}
  256. {/* 修改广告 */}
  257. {update.visible && <UpdateAd
  258. {...update}
  259. selectedRows={selectedRows}
  260. onChange={() => {
  261. setUpdate({ visible: false, title: '' })
  262. getAdList.refresh()
  263. setSelectedRows([])
  264. }}
  265. onClose={() => { setUpdate({ visible: false, title: '' }) }}
  266. />}
  267. {/* 复制广告 */}
  268. {copyData.visible && <Copy selectedRows={selectedRows} {...copyData} onClose={() => setCopyData({ visible: false })} onChange={() => { setCopyData({ visible: false }); getAdList.refresh(); setSelectedRows([]) }} />}
  269. <Row gutter={[6, 6]} align='middle' style={{ marginBottom: 10 }}>
  270. <FilterQuery
  271. queryForm={queryForm}
  272. setQueryForm={setQueryForm}
  273. onChange={(value) => {
  274. setFilterForm({ ...value, pageNum: 1 })
  275. }}
  276. />
  277. </Row>
  278. <div ref={ref} className='expandClassname'>
  279. <TableData
  280. refreshData={getList}
  281. isCard={false}
  282. className='expendTable'
  283. columns={() => tablePlanConfig(onChange, details, handleSave, handleSaveDaily, log, handleTag, delTag)}
  284. ajax={getAdList}
  285. syncAjax={sync}
  286. fixed={{ left: 2, right: 4 }}
  287. dataSource={getAdList?.data?.data?.records}
  288. loading={getAdList?.loading || syncAjax?.loading}
  289. scroll={{ y: 560 }}
  290. total={getAdList?.data?.data?.total}
  291. page={getAdList?.data?.data?.current}
  292. pageSize={getAdList?.data?.data?.size}
  293. myKey={'adgroup_id'}
  294. gutter={[0, 10]}
  295. config={planAdConfig}
  296. configName={configName}
  297. rowClassName={(record) => {
  298. if (record?.tag_value === 100) {
  299. return 'row_error'
  300. }else if (record?.tag_value === 90) {
  301. return 'row_warning'
  302. } else {
  303. return ''
  304. }
  305. }}
  306. leftChild={<Space direction='vertical'>
  307. <Row gutter={[10, 10]} align='middle'>
  308. <Col>
  309. <Switch checkedChildren="普通" unCheckedChildren="ROI" checked={model} onChange={(checked) => { setModel(checked); setSelectedRows([]) }} style={model ? {} : { background: '#67c23a' }} />
  310. </Col>
  311. {!model && <Col><Button type='primary' icon={<TransactionOutlined />} disabled={selectedRows.length === 0} onClick={editDeepConversion}>修改深度优化ROI</Button></Col>}
  312. <Col><Button type='primary' style={{ background: '#1890ff' }} icon={<CopyOutlined />} disabled={selectedRows.length === 0} onClick={copyHandle}>复制</Button></Col>
  313. <Col><Button type='primary' style={{ background: '#67c23a', borderColor: '#67c23a' }} loading={editAdqAdgroupsData.loading} icon={<PlayCircleOutlined />} disabled={selectedRows.length === 0} onClick={() => adStatus('play')}>启动</Button></Col>
  314. <Col><Button type='primary' style={{ background: '#e6a23c', borderColor: '#e6a23c' }} loading={editAdqAdgroupsData.loading} icon={<PauseCircleOutlined />} disabled={selectedRows.length === 0} onClick={() => adStatus('suspend')}>暂停</Button></Col>
  315. <Col><SetEarlyWarning selectedRows={selectedRows} onChange={() => getAdList.refresh()} /></Col>
  316. <Col>
  317. <Dropdown overlay={<Menu
  318. onClick={(e) => {
  319. switch (e.key) {
  320. case '1':
  321. editScheduling()
  322. break
  323. case '2':
  324. setRqb()
  325. break
  326. case '3':
  327. syncAd()
  328. break
  329. case '4':
  330. Modal.confirm({
  331. title: '删除',
  332. content: '确定删除?',
  333. icon: <ExclamationCircleOutlined />,
  334. okType: 'danger',
  335. onOk() {
  336. deleteHandle(1)
  337. }
  338. });
  339. break
  340. }
  341. }}
  342. items={[
  343. {
  344. key: '1',
  345. disabled: selectedRows.length === 0,
  346. label: <Button type="link" size='small' style={{ padding: 0 }} icon={<FieldTimeOutlined />} disabled={selectedRows.length === 0}>修改排期出价名称</Button>,
  347. },
  348. {
  349. key: '2',
  350. disabled: selectedRows.length > 0 ? !selectedRows.every((item: { accountId: number }) => item.accountId === selectedRows[0].accountId) : true,
  351. label: <Button type="link" size='small' style={{ padding: 0 }} icon={<FormOutlined />} loading={putModifyCustomAudience.loading} disabled={selectedRows.length > 0 ? !selectedRows.every((item: { accountId: number }) => item.accountId === selectedRows[0].accountId) : true}>
  352. 修改人群包
  353. <Tooltip title="2023/4/20 16:20:00刷新页面后平台创建的广告可修改,或者其它平台使用定向包创建广告可用">
  354. <QuestionCircleOutlined />
  355. </Tooltip>
  356. </Button>,
  357. },
  358. {
  359. key: '3',
  360. disabled: selectedRows.length === 0,
  361. label: <Button type="link" size='small' style={{ padding: 0 }} loading={putAdqAdgroupsSyncBatchApi.loading} icon={<SyncOutlined />} disabled={selectedRows.length === 0}>同步广告</Button>,
  362. },
  363. {
  364. key: '4',
  365. disabled: selectedRows.length === 0,
  366. label: <Button danger type="link" size='small' style={{ padding: 0 }} loading={delListAdqAdgroups.loading} icon={<DeleteOutlined />} disabled={selectedRows.length === 0}>删除</Button>,
  367. },
  368. ]} />}>
  369. <a onClick={(e) => e.preventDefault()}>
  370. <Space>
  371. 更多
  372. <DownOutlined />
  373. </Space>
  374. </a>
  375. </Dropdown>
  376. </Col>
  377. <Col>
  378. <Button type='dashed' onClick={() => { setCzjlShow(true) }}>操作记录</Button>
  379. </Col>
  380. </Row>
  381. </Space>}
  382. rowSelection={{
  383. selectedRowKeys: selectedRows.map(item => item.adgroup_id.toString()),
  384. getCheckboxProps: (record: any) => ({
  385. disabled: model ?
  386. record.status === 'STATUS_DELETED' :
  387. record.status === 'STATUS_DELETED' ||
  388. !(!model &&
  389. JSON.parse(record?.deep_conversion_spec_json)?.deepConversionWorthSpec?.goal === 'GOAL_1DAY_PURCHASE_ROAS'
  390. )
  391. }),
  392. onSelect: (record: { adgroup_id: number, mpName: string }, selected: boolean) => {
  393. if (selected) {
  394. selectedRows.push({ ...record })
  395. setSelectedRows([...selectedRows])
  396. } else {
  397. let newSelectAccData = selectedRows.filter((item: { adgroup_id: number }) => item.adgroup_id !== record.adgroup_id)
  398. setSelectedRows([...newSelectAccData])
  399. }
  400. },
  401. onSelectAll: (selected: boolean, selectedRowss: { adgroup_id: number }[], changeRows: { adgroup_id: number }[]) => {
  402. if (selected) {
  403. let newSelectAccData = [...selectedRows]
  404. changeRows.forEach((item: { adgroup_id: number }) => {
  405. let index = newSelectAccData.findIndex((ite: { adgroup_id: number }) => ite.adgroup_id === item.adgroup_id)
  406. if (index === -1) {
  407. newSelectAccData.push({ ...item })
  408. }
  409. })
  410. setSelectedRows([...newSelectAccData])
  411. } else {
  412. let newSelectAccData = selectedRows.filter((item: { adgroup_id: number }) => {
  413. let index = changeRows.findIndex((ite: { adgroup_id: number }) => ite.adgroup_id === item.adgroup_id)
  414. if (index !== -1) {
  415. return false
  416. } else {
  417. return true
  418. }
  419. })
  420. setSelectedRows([...newSelectAccData])
  421. }
  422. }
  423. }}
  424. onChange={(props: any) => {
  425. let { sortData, pagination } = props
  426. let { current, pageSize } = pagination
  427. let newQueryForm = JSON.parse(JSON.stringify(queryForm))
  428. newQueryForm.pageNum = current
  429. newQueryForm.pageSize = pageSize
  430. if (sortData && JSON.stringify('sortData') !== '{}') {
  431. let { field, order } = sortData // descend 降序 大到小 ascend 升序 小到大
  432. if (order) {
  433. newQueryForm.sortColumn = field
  434. newQueryForm.sortAsc = order === 'ascend'
  435. } else {
  436. Object.keys(newQueryForm).forEach(key => {
  437. if (key === 'sortColumn' || key === 'sortAsc') {
  438. delete newQueryForm[key]
  439. }
  440. })
  441. }
  442. } else {
  443. Object.keys(newQueryForm).forEach(key => {
  444. if (key === 'sortField' || key === 'sort') {
  445. delete newQueryForm[key]
  446. }
  447. })
  448. }
  449. setQueryForm({ ...newQueryForm })
  450. }}
  451. expandedRowRender={(data) => <AdExpandedRowRender data={data} scrollLeft={scrollLeft} width={size?.width} />}
  452. />
  453. </div>
  454. {detailShow && <Details visible={detailShow} onClose={() => { setDetailShow(false) }} data={detailData} />}
  455. {czjlShow && <Modal
  456. visible={czjlShow}
  457. onCancel={() => { setCzjlShow(false) }}
  458. onOk={() => { setCzjlShow(false) }}
  459. width={1200}
  460. footer={null}
  461. title={"广告操作记录"}
  462. >
  463. <Log {...props} />
  464. </Modal>}
  465. {cpVisible && <CrowdPackModal visible={cpVisible} data={accountCreateLogs} onClose={() => setCpVisible(false)} onChange={(e) => { handleRqb(e) }} />}
  466. {logVisible && <RuleAccountLog accountId={accountIdRule} adgroupName={adgroupName} adgroupId={adgroupId} visible={logVisible} onClose={() => setLogVisible(false)} />}
  467. </div>
  468. }
  469. export default AdPlanList