monitor1.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import { Card, Input, Radio, Select, Space, Spin, Tag, TimePicker, Tooltip } from "antd";
  2. import React, { useCallback, useEffect, useState } from "react";
  3. import { ColumnHeightOutlined, ColumnWidthOutlined, EyeInvisibleOutlined, EyeOutlined, RedoOutlined } from "@ant-design/icons";
  4. import useEcharts from '@/Hook/useEcharts'
  5. import { columnsMonitor } from './tableMonitorConfig1'
  6. import { useModel } from 'umi'
  7. import { ListHourProps, ListType } from '@/services/adMonitor/adMonitor'
  8. import { qiliangpaihanghour } from './config1'
  9. import Details from './components/Details'
  10. import './table.less'
  11. import moment from "moment";
  12. import TableData from "@/pages/launchSystemNew/components/TableData";
  13. import FilterQuery from "./components/FilterQuery";
  14. import RuleAccountLog from "@/components/EarlyWarning/ruleAccountLog";
  15. import { LineField } from "./config";
  16. interface newListType extends ListType {
  17. totalTimeUnit: 'total' | 'minute' | 'hour' | 'day',
  18. planTimeUnit: 'minute' | 'hour' | 'day'
  19. }
  20. /**
  21. * 今日起量监控
  22. * @param props
  23. * @returns
  24. */
  25. function Monitor(props: { onChange: () => void }) {
  26. const { onChange } = props
  27. const { getCostTrendList, getCostTopList, getListForHour, getMinuteList, getAdqAccountList } = useModel('useAdMonitor.useMonitor')
  28. const { getPicherList } = useModel('useOperating.useWxGroupList')
  29. // 变量开始
  30. const [queryForm, setQueryForm] = useState<newListType>({ totalTimeUnit: 'day', planTimeUnit: 'hour', sysUserId: JSON.parse(sessionStorage.getItem('SYSUSERID') || '[]'), pageNum: 1, pageSize: 20 }) // 搜索变量//startTime: moment().format('YYYY-MM-DD'), endTime: moment().format('YYYY-MM-DD'),
  31. const { BarMonitor, LineMonitor } = useEcharts()
  32. const [barDis, setBarDis] = useState<any[]>([])
  33. const [lineDis, setLineDis] = useState<any[]>([])
  34. const [lineTitle, setLineTitle] = useState<string>('广告总消耗趋势')
  35. const [px, setPx] = useState<boolean>(false)//设置顶部图形的排列方式
  36. const [visible, setVisible] = useState<boolean>(false) // 详情弹窗控制
  37. const [aId, setAId] = useState<any>()
  38. const [showEacharts, setShowEacharts] = useState<boolean>(true)
  39. const [queryForHour, setQueryForHour] = useState<ListHourProps>({ pageNum: 1, pageSize: 20 })
  40. const [filterQuery, setFilterQuery] = useState<any>({})
  41. const [logVisible, setLogVisible] = useState<boolean>(false)
  42. const [adgroupId, setAdgroupId] = useState<string>('')
  43. const [adgroupName, setAdgroupName] = useState<string>('')
  44. const [accountIdRule, setAccountIdRule] = useState<string>('')
  45. const [trendColumns, setTrendColumns] = useState<string[]>(['cost'])
  46. const { totalTimeUnit, planTimeUnit, adgroup, accountId, sysUserId, groupAccountIds } = queryForm
  47. const configName = '起量广告排行明细New'
  48. const { getGroupList, groupListInit } = useModel('useLaunchAdq.useAdAuthorize')
  49. useEffect(() => {
  50. groupListInit()
  51. }, [])
  52. useEffect(() => {
  53. getList()
  54. }, [queryForHour, filterQuery, queryForm?.sysUserId, queryForm?.accountId, queryForm?.adgroup, queryForm?.groupAccountIds])
  55. const getList = () => {
  56. let message = localStorage.getItem(`myAdMonitorConfig1.0.1_${configName}`)
  57. if (message) {
  58. message = JSON.parse(message)
  59. }
  60. let isAccountId = true
  61. let isAdgroupId = true
  62. let isAdgroupName = true
  63. let columns: string[] = []
  64. if (message && Array.isArray(message)) {
  65. message.forEach((item: { serverIndex: any; dataIndex: string; }) => {
  66. if (!['event'].includes(item.dataIndex)) {
  67. if (item.dataIndex === 'account_id') {
  68. isAccountId = false
  69. } else if (item.dataIndex === 'adgroup_id') {
  70. isAdgroupId = false
  71. } else if (item.dataIndex === 'adgroup_name') {
  72. isAdgroupName = false
  73. }
  74. columns.push(item?.serverIndex || 'adgroup_data.' + item.dataIndex)
  75. }
  76. })
  77. } else {
  78. qiliangpaihanghour.forEach((item: any) => {
  79. item?.data?.forEach((d: { default: any, serverIndex: string, dataIndex: string }) => {
  80. if (d.default && !['event'].includes(d.dataIndex)) {
  81. if (d.dataIndex === 'account_id') {
  82. isAccountId = false
  83. } else if (d.dataIndex === 'adgroup_id') {
  84. isAdgroupId = false
  85. } else if (item.dataIndex === 'adgroup_name') {
  86. isAdgroupName = false
  87. }
  88. columns.push(d?.serverIndex || 'adgroup_data.' + d.dataIndex)
  89. }
  90. })
  91. })
  92. }
  93. if (isAccountId) {
  94. columns.push('adgroup_data.account_id')
  95. }
  96. if (isAdgroupId) {
  97. columns.push('adgroup_data.adgroup_id')
  98. }
  99. if (isAdgroupName) {
  100. columns.push('adgroup_data.adgroup_name')
  101. }
  102. let params = { ...queryForHour, ...filterQuery }
  103. if (queryForm?.sysUserId) {
  104. params.sysUserIds = queryForm?.sysUserId
  105. }
  106. if (queryForm?.accountId && queryForm?.accountId?.length > 0) {
  107. params.accountIdStr = queryForm?.accountId.toString()
  108. }
  109. if (queryForm?.adgroup) {
  110. params.adgroupIdStr = queryForm?.adgroup
  111. }
  112. if (queryForm?.groupAccountIds) {
  113. params.groupAccountIds = queryForm?.groupAccountIds
  114. }
  115. params.columns = columns
  116. getListForHour.run(params)
  117. }
  118. // 获取投手
  119. useEffect(() => {
  120. !getPicherList.data && getPicherList.run()
  121. }, [])
  122. // 获取广告账号
  123. useEffect(() => {
  124. !getAdqAccountList.data && getAdqAccountList.run()
  125. }, [])
  126. // // 获取排行数据,柱图
  127. useEffect(() => {
  128. getPlanCostList()
  129. }, [totalTimeUnit, accountId, sysUserId, groupAccountIds])
  130. // 获取今日计划总消耗图谱,折线
  131. useEffect(() => {
  132. getTootalCostList()
  133. }, [planTimeUnit, adgroup, accountId, sysUserId, groupAccountIds, trendColumns])
  134. /** 获取折线图 */
  135. const getTootalCostList = useCallback(async () => {
  136. let { totalTimeUnit, planTimeUnit, pageNum, pageSize, adgroup, sysUserId, accountId, ...newQueryForm } = queryForm
  137. let params = adgroup ? { ...newQueryForm, adgroupIdStr: adgroup } : newQueryForm
  138. let res = await getCostTrendList.run({ ...params, timeUnit: planTimeUnit, sysUserIds: sysUserId, accountIdStr: accountId?.join(), trendColumns })
  139. let data = trendColumns.map((field) => {
  140. let value: any = {}
  141. res?.data?.forEach((item: any, index: number) => {
  142. if (index === 0) value.legendName = LineField[field];
  143. value[item?.trend_unit] = item?.[field]
  144. });
  145. return value
  146. })
  147. setLineDis(() => data)
  148. setLineTitle(() => `广告总${LineField[trendColumns[0]]}趋势`)
  149. }, [queryForm, lineDis, trendColumns])
  150. /** 获取柱状图 */
  151. const getPlanCostList = useCallback(async () => {
  152. let { totalTimeUnit, planTimeUnit, pageNum, pageSize, sysUserId, accountId, ...newQueryForm } = queryForm
  153. let { adgroup, ...planQueryFrom } = newQueryForm
  154. let res = await getCostTopList.run({ ...planQueryFrom, timeUnit: totalTimeUnit, sysUserIds: sysUserId, accountId: accountId?.join(), topN: 10 })
  155. let data = res?.data?.map((item: { adgroupId: number, cost: number, adgroupName: string, accountId: number }) => {
  156. return { name: item.adgroupId.toString(), value: item.cost, adName: item.adgroupName, accountId: item.accountId }
  157. })
  158. data = data?.sort((a: any, b: any) => {
  159. var value1 = a['value'];
  160. var value2 = b['value'];
  161. return value2 - value1;
  162. })
  163. setBarDis(() => data)
  164. }, [queryForm, barDis])
  165. // 详情
  166. const details = (data: any) => {
  167. setAId(data)
  168. setVisible(true)
  169. }
  170. // 计划详情
  171. const planDetail = (data: any) => {
  172. sessionStorage.setItem('ADIDORNAME', JSON.stringify(data))
  173. onChange && onChange()
  174. }
  175. //全部接口刷新
  176. const refresh = () => {
  177. getCostTrendList.refresh()
  178. getCostTopList.refresh()
  179. getListForHour.refresh()
  180. getMinuteList.refresh()
  181. }
  182. // 接口自动刷新10分钟一次
  183. useEffect(() => {
  184. let time = setInterval(() => {
  185. refresh()
  186. }, 1000 * 60 * 10)
  187. return () => {
  188. clearInterval(time)
  189. }
  190. }, [])
  191. //图形排列样式改变重新获取数据刷新图形
  192. const set = useCallback((b) => {
  193. setPx(b)
  194. getCostTrendList.refresh()
  195. getCostTopList.refresh()
  196. }, [getCostTopList, getCostTrendList, trendColumns])
  197. // 处理折线图数据
  198. const timePickerHandle = async (values: any, formatString: [string, string]) => {
  199. let res = await getCostTrendList.data
  200. let data = trendColumns.map((field) => {
  201. let value: any = {}
  202. res?.data?.forEach((item: any, index: number) => {
  203. if (index === 0) value.legendName = LineField[field];
  204. value[item?.trend_unit] = item?.[field]
  205. });
  206. return value
  207. })
  208. if (values) {
  209. let date = moment().format('YYYY-MM-DD')
  210. let strTime = formatString[0]
  211. let endTime = formatString[1]
  212. let dateTimeStr = `${date} ${strTime}:00`
  213. let dateTimeEnd = `${date} ${endTime}:00`
  214. let newData: any[] = data.map(item => {
  215. let { legendName, ...otherData } = item
  216. let newItem: any = { legendName }
  217. for (const key in otherData) {
  218. if (Object.prototype.hasOwnProperty.call(otherData, key)) {
  219. const value = otherData[key];
  220. if (moment(dateTimeStr) <= moment(key) && moment(key) <= moment(dateTimeEnd)) {
  221. newItem[key] = value
  222. }
  223. }
  224. }
  225. return newItem
  226. })
  227. setLineDis(() => newData)
  228. } else {
  229. setLineDis(() => data)
  230. }
  231. }
  232. const log = (value: any) => {
  233. setAccountIdRule(value.account_id)
  234. setAdgroupId(value.adgroup_id)
  235. setAdgroupName(value.adgroup_name)
  236. setLogVisible(true)
  237. }
  238. return <Space direction='vertical' style={{ width: '100%' }} className="monitor">
  239. <Card hoverable bodyStyle={{ padding: '12px 16px' }}>
  240. <div style={{ display: 'flex', justifyContent: 'space-between' }}>
  241. <Space>
  242. <Select
  243. showSearch
  244. value={queryForm.sysUserId}
  245. style={{ minWidth: 180, maxWidth: 250 }}
  246. mode='multiple'
  247. maxTagCount={1}
  248. allowClear
  249. placeholder="请选择投手"
  250. disabled={queryForm?.adgroup || queryForm?.accountId?.length > 0}
  251. onChange={(value: number[]) => {
  252. setQueryForm({ ...queryForm, sysUserId: value, pageNum: 1 })
  253. sessionStorage.setItem('SYSUSERID', value ? JSON.stringify(value) : '')
  254. }}
  255. filterOption={(input, option) =>
  256. (option?.children as any).toLowerCase().indexOf(input.toLowerCase()) >= 0
  257. }
  258. >
  259. {getPicherList?.data?.map((item: { nickname: string, userId: number }, index: number) =>
  260. <Select.Option
  261. value={item.userId}
  262. key={item.userId + '' + index}
  263. >
  264. {item.nickname}
  265. </Select.Option>
  266. )}
  267. </Select>
  268. <Select
  269. showSearch
  270. mode='multiple'
  271. maxTagCount={1}
  272. value={queryForm.accountId}
  273. style={{ minWidth: 220, maxWidth: 250 }}
  274. allowClear
  275. placeholder="请选择广告账号"
  276. onChange={(value: number[]) => {
  277. setQueryForm({ ...queryForm, accountId: value, pageNum: 1 })
  278. }}
  279. >
  280. {getAdqAccountList?.data?.data?.map((item: { id: number, accountId: number }) => <Select.Option
  281. value={item.accountId}
  282. key={item.id}
  283. >
  284. {item.accountId}
  285. </Select.Option>)}
  286. </Select>
  287. <Input
  288. value={queryForm?.accountId?.toString()}
  289. placeholder="输入广告账号"
  290. onChange={(e) => {
  291. setQueryForm({ ...queryForm, accountId: e.target.value ? e.target.value?.split(/[,,\s\n]+/) : undefined })
  292. }}
  293. allowClear
  294. />
  295. <Input
  296. value={queryForm.campaign}
  297. placeholder="计划ID"
  298. onChange={(e) => {
  299. let value = e.target.value
  300. setQueryForm({ ...queryForm, campaign: value })
  301. }}
  302. allowClear
  303. />
  304. <Input
  305. value={queryForm.adgroup}
  306. placeholder="广告ID"
  307. onChange={(e) => {
  308. setQueryForm({ ...queryForm, adgroup: e.target.value })
  309. }}
  310. allowClear
  311. />
  312. <Select
  313. showSearch
  314. mode='multiple'
  315. maxTagCount={1}
  316. value={queryForm.groupAccountIds}
  317. style={{ minWidth: 150 }}
  318. allowClear
  319. placeholder="请选择账号分组"
  320. onChange={(value: number[]) => {
  321. setQueryForm({ ...queryForm, groupAccountIds: value, pageNum: 1 })
  322. }}
  323. >
  324. {getGroupList?.data?.map((item: { groupId: number, groupName: number }) => <Select.Option
  325. value={item.groupId}
  326. key={item.groupId}
  327. >
  328. {item.groupName}
  329. </Select.Option>)}
  330. </Select>
  331. </Space>
  332. <Space>
  333. <Tag onClick={() => setShowEacharts(!showEacharts)}>{showEacharts ? <><EyeInvisibleOutlined /> 隐藏</> : <><EyeOutlined /> 显示</>}</Tag>
  334. <Tag color="#2db7f5" onClick={refresh}><RedoOutlined /> 刷新</Tag>
  335. </Space>
  336. </div>
  337. </Card>
  338. {showEacharts && <Card hoverable bodyStyle={{ padding: '12px 16px' }}>
  339. <span style={{ position: 'absolute', top: 10, zIndex: 10 }}>
  340. {px ?
  341. <Tooltip title='左右排列'><Tag color='#f50' onClick={() => { set(false) }}><ColumnWidthOutlined /><span style={{ fontSize: 10 }}>左右排列</span></Tag></Tooltip>
  342. :
  343. <Tooltip title='上下排列'><Tag color='#f50' onClick={() => { set(true) }} ><ColumnHeightOutlined /><span style={{ fontSize: 10 }}>上下排列</span></Tag></Tooltip>
  344. }
  345. </span>
  346. <div className={!px ? 'charts' : 'charts charts100'}>
  347. <div>
  348. <div className="selectTime">
  349. <Space>
  350. <span style={{ fontSize: 10, color: '#999' }}>刷新时间:{getCostTopList?.data?.reqTime}</span>
  351. <Radio.Group value={queryForm.totalTimeUnit} buttonStyle="solid" size='small' onChange={(e) => { setQueryForm({ ...queryForm, totalTimeUnit: e.target.value }) }}>
  352. <Radio.Button value="total">总</Radio.Button>
  353. <Radio.Button value="day">天</Radio.Button>
  354. <Radio.Button value="hour">小时</Radio.Button>
  355. <Radio.Button value="minute">5min</Radio.Button>
  356. </Radio.Group>
  357. </Space>
  358. </div>
  359. {getCostTopList?.loading ? <Spin /> : <BarMonitor style={{ width: '100%', height: '100%' }} data={barDis} xName="今日消耗" yName="广告名称" onChange={(value: string, accountId) => { console.log(value, accountId); setQueryForm({ ...queryForm, adgroup: value }) }} planID={queryForm?.adgroup} />}
  360. </div>
  361. <div>
  362. <div className="selectTime">
  363. <Space>
  364. <span style={{ fontSize: 10, color: '#999' }}>刷新时间:{getCostTrendList?.data?.reqTime}</span>
  365. <Radio.Group value={queryForm.planTimeUnit} buttonStyle="solid" size='small' onChange={(e) => { setQueryForm({ ...queryForm, planTimeUnit: e.target.value }) }}>
  366. <Radio.Button value="hour">小时</Radio.Button>
  367. <Radio.Button value="minute">5min</Radio.Button>
  368. </Radio.Group>
  369. {queryForm.planTimeUnit === 'minute' && <TimePicker.RangePicker
  370. size="small"
  371. format='HH:mm'
  372. minuteStep={5}
  373. onChange={timePickerHandle}
  374. />}
  375. <Select
  376. showSearch
  377. mode='multiple'
  378. maxTagCount={1}
  379. value={trendColumns}
  380. style={{ minWidth: 130 }}
  381. allowClear
  382. placeholder="请选择图表字段"
  383. onChange={(value: string[]) => {
  384. setTrendColumns(value?.length ? value : ['cost'])
  385. }}
  386. size="small"
  387. >
  388. {Object.keys(LineField).map((key) => <Select.Option value={key} key={key}>
  389. {LineField[key]}
  390. </Select.Option>)}
  391. </Select>
  392. </Space>
  393. </div>
  394. {getCostTrendList?.loading ? <Spin /> : <LineMonitor style={{ width: '100%', height: '100%' }} series smooth data={lineDis} title={trendColumns?.length > 1 ? undefined : lineTitle} />}
  395. </div>
  396. </div>
  397. </Card>}
  398. <div className={'MYtable'}>
  399. <TableData
  400. refreshData={getList}
  401. bodyStyle={{ padding: '12px 16px' }}
  402. gutter={[0, 12]}
  403. columns={columnsMonitor(planDetail, details, (value) => { setQueryForm({ ...queryForm, adgroup: value }); }, log)}
  404. dataSource={getListForHour?.data?.data?.records}
  405. loading={getListForHour?.loading}
  406. ajax={getListForHour}
  407. leftChild={
  408. <Space>
  409. <FilterQuery onChange={(data) => {
  410. setFilterQuery(data)
  411. }} />
  412. </Space>
  413. }
  414. myKey={'adgroup_id'}
  415. fixed={{ left: 0, right: 2 }}
  416. total={getListForHour?.data?.data?.total}
  417. onChange={(props: any) => {
  418. let { sortData, pagination } = props
  419. let { current, pageSize } = pagination
  420. let newQueryForm = JSON.parse(JSON.stringify(queryForHour))
  421. newQueryForm.pageNum = current
  422. newQueryForm.pageSize = pageSize
  423. if (sortData && JSON.stringify('sortData') !== '{}') {
  424. let { field, order } = sortData // descend 降序 大到小 ascend 升序 小到大
  425. if (order) {
  426. newQueryForm.sortColumn = field
  427. newQueryForm.sortAsc = order === 'ascend'
  428. } else {
  429. Object.keys(newQueryForm).forEach(key => {
  430. if (key === 'sortColumn' || key === 'sortAsc') {
  431. delete newQueryForm[key]
  432. }
  433. })
  434. }
  435. } else {
  436. Object.keys(newQueryForm).forEach(key => {
  437. if (key === 'sortField' || key === 'sort') {
  438. delete newQueryForm[key]
  439. }
  440. })
  441. }
  442. setQueryForHour({ ...newQueryForm })
  443. }}
  444. page={queryForHour.pageNum}
  445. pageSize={queryForHour.pageSize}
  446. scroll={{ y: 750 }}
  447. config={qiliangpaihanghour}
  448. configName={configName}
  449. />
  450. </div>
  451. {visible && <Details visible={visible} onClose={() => { setVisible(false) }} data={aId} />}
  452. {logVisible && <RuleAccountLog accountId={accountIdRule} adgroupName={adgroupName} adgroupId={adgroupId} visible={logVisible} onClose={() => setLogVisible(false)} />}
  453. </Space>
  454. }
  455. export default React.memo(Monitor)