index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2023-03-03 11:30:33
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2023-04-07 10:12:37
  6. * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/pubDicTypeMana/index.tsx
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import BMSPagecontainer from '@/components/BMSPageContainer';
  10. import { BMSTable } from '@/components/BMSTable';
  11. import { createFromIconfontCN } from '@ant-design/icons';
  12. import { ActionType, ProFormRadio } from '@ant-design/pro-components';
  13. import { ModalForm, ProFormDependency, ProFormDigit, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-form'
  14. import { ProColumns } from '@ant-design/pro-table';
  15. import { Input, message, Popconfirm } from 'antd';
  16. import { useEffect, useRef, useState } from 'react';
  17. import { addData, delData, editData, getManaIndic, getReportListTableData, getReportListType } from './service';
  18. import './style.less';
  19. const IconFont = createFromIconfontCN({
  20. scriptUrl: '//at.alicdn.com/t/c/font_1927152_4nm5kxbv4m3.js',
  21. });
  22. export default function ReportListMana() {
  23. const [tableDataFilterParams, set_tableDataFilterParams] = useState<any | undefined>();
  24. const [tableDataSearchKeywords, set_tableDataSearchKeywords] = useState<string>('');
  25. const tableRef = useRef<ActionType>();
  26. const columns: ProColumns[] = [
  27. {
  28. title: '列名称',
  29. dataIndex: 'name',
  30. },
  31. {
  32. title: '列标题',
  33. dataIndex: 'headerText',
  34. },
  35. {
  36. title: '列类型',
  37. dataIndex: 'columnTypeName',
  38. // render:(_:any)=>_ == 1?'指标':'自定义SQL'
  39. },
  40. {
  41. title: '取数类型',
  42. dataIndex: 'dataSource',
  43. render: (_: any) => _ == 1 ? '指标' : '自定义SQL'
  44. },
  45. {
  46. title: '取数来源',
  47. width: 300,
  48. ellipsis: true,
  49. dataIndex: 'sql',
  50. render: (_: any, record: any) => {
  51. if (record.sql) return record.sql;
  52. if (record.indicatorCodeName) return record.indicatorCodeName
  53. }
  54. },
  55. {
  56. title: '操作',
  57. key: 'option',
  58. width: 120,
  59. valueType: 'option',
  60. render: (_: any, record: any) => {
  61. return [
  62. <UpDataActBtn key={'act'} record={record} type='EDIT' />,
  63. <Popconfirm
  64. title="是否确认删除?"
  65. key="del"
  66. onConfirm={() => delTableData(record)}
  67. >
  68. <a>删除</a>
  69. </Popconfirm>
  70. ]
  71. },
  72. },
  73. ]
  74. const getTableData = async (params: any) => {
  75. const resp = await getReportListTableData(params);
  76. if (resp) {
  77. return {
  78. data: resp.list,
  79. success: true,
  80. total: resp.totalCount,
  81. pageSize: resp.pageSize,
  82. totalPage: resp.totalPage,
  83. }
  84. }
  85. return []
  86. }
  87. const delTableData = async (record: any) => {
  88. const resp = await delData(record.id);
  89. if (resp) {
  90. message.success('操作成功!');
  91. tableRef.current?.reload();
  92. // message.success('操作成功!');
  93. }
  94. }
  95. const updateTable = async (formVal: any, type: 'EDIT' | "ADD") => {
  96. if (type == 'ADD') {
  97. if (formVal.dataSource == 1) {
  98. //
  99. let result = {
  100. name: formVal.name,
  101. headerText: formVal.headerText,
  102. dataSource: formVal.dataSource,
  103. columnType: formVal.columnType,
  104. sql:'',
  105. indicatorCode: formVal.indicatorCode.key,
  106. indicatorCodeName: formVal.indicatorCode.label,
  107. }
  108. const resp = await addData({ ...result });
  109. if (resp) {
  110. tableRef.current?.reload();
  111. message.success('操作成功!');
  112. }
  113. }
  114. if(formVal.dataSource == 2){
  115. //sql
  116. let result = {
  117. name: formVal.name,
  118. headerText: formVal.headerText,
  119. dataSource: formVal.dataSource,
  120. columnType: formVal.columnType,
  121. sql: formVal.sql,
  122. indicatorCode:'',
  123. indicatorCodeName:''
  124. }
  125. const resp = await addData({ ...result });
  126. if (resp) {
  127. tableRef.current?.reload();
  128. message.success('操作成功!');
  129. }
  130. }
  131. }
  132. if (type == 'EDIT') {
  133. if (formVal.dataSource == 1){
  134. //指标
  135. const result = {
  136. id: formVal.id,
  137. name: formVal.name,
  138. headerText: formVal.headerText,
  139. dataSource: formVal.dataSource,
  140. columnType: formVal.columnType,
  141. sql:'',
  142. indicatorCode: formVal.indicatorCode.key,
  143. indicatorCodeName: formVal.indicatorCode.label,
  144. }
  145. const resp = await editData({ ...result });
  146. if (resp) {
  147. tableRef.current?.reload();
  148. message.success('操作成功!');
  149. }
  150. }
  151. if(formVal.dataSource == 2){
  152. //sql
  153. const result = {
  154. id: formVal.id,
  155. name: formVal.name,
  156. headerText: formVal.headerText,
  157. dataSource: formVal.dataSource,
  158. columnType: formVal.columnType,
  159. sql: formVal.sql,
  160. indicatorCode:'',
  161. indicatorCodeName:''
  162. }
  163. const resp = await editData({ ...result });
  164. if (resp) {
  165. tableRef.current?.reload();
  166. message.success('操作成功!');
  167. }
  168. }
  169. }
  170. return true;
  171. }
  172. const formRef = useRef();
  173. const UpDataActBtn = ({ record, type }: { record: any, type: 'EDIT' | 'ADD' }) => {
  174. return (
  175. <ModalForm
  176. title={`${type == 'EDIT' ? '编辑' : '新增'}报表列`}
  177. width={352}
  178. formRef={formRef}
  179. initialValues={type == 'EDIT' ? { ...record, indicatorCode: { label: record.indicatorCodeName, value: record.indicatorCode } } : {}}
  180. trigger={
  181. type == 'EDIT' ? <a key="edit" >编辑</a> : <span className='add'>新增</span>
  182. }
  183. onFinish={(val) => {
  184. console.log({ val });
  185. return updateTable(type == 'EDIT' ? { ...record, ...val, sql: val.sql ? val.sql : '', } : { ...val }, type);
  186. }}
  187. colProps={{ span: 24 }}
  188. grid
  189. >
  190. <ProFormText
  191. name="name"
  192. label="列名称:"
  193. placeholder="请输入"
  194. rules={[{ required: true, message: '列名称不能为空!' }]}
  195. />
  196. <ProFormText
  197. name="headerText"
  198. label="列标题:"
  199. placeholder="请输入"
  200. rules={[{ required: true, message: '列标题不能为空!' }]}
  201. />
  202. <ProFormSelect
  203. name="columnType"
  204. label="列类型:"
  205. placeholder="请选择"
  206. request={async () => {
  207. const resp = await getReportListType();
  208. if (resp) {
  209. return resp.list.map((a: any) => ({ label: a.name, value: a.code }))
  210. }
  211. }}
  212. rules={[{ required: true, message: '列类型不能为空!' }]}
  213. />
  214. <ProFormSelect
  215. name="dataSource"
  216. label="取数类型:"
  217. placeholder="请选择"
  218. options={[
  219. { label: '指标', value: 1 },
  220. { label: '自定义SQL', value: 2 }
  221. ]}
  222. rules={[{ required: true, message: '取数类型不能为空!' }]}
  223. />
  224. <ProFormDependency name={['dataSource']}>
  225. {
  226. ({ dataSource }) => dataSource == 1 && (
  227. <ProFormSelect
  228. name="indicatorCode"
  229. label="取数来源:"
  230. placeholder="请选择"
  231. fieldProps={{
  232. labelInValue: true
  233. }}
  234. request={async () => {
  235. const resp = await getManaIndic();
  236. if (resp) {
  237. return resp.map((a: any) => ({ label: a.name, value: a.code }))
  238. }
  239. }}
  240. rules={[{ required: true, message: '数据来源不能为空!' }]}
  241. />
  242. )
  243. }
  244. </ProFormDependency>
  245. <ProFormDependency name={['dataSource']}>
  246. {
  247. ({ dataSource }) => dataSource == 2 && (
  248. <ProFormTextArea name={'sql'} label='SQL:' rules={[{ required: true, message: 'SQL不能为空!' }]} />
  249. )
  250. }
  251. </ProFormDependency>
  252. </ModalForm>
  253. )
  254. }
  255. const tableDataSearchHandle = (paramName: string) => {
  256. set_tableDataFilterParams({
  257. ...tableDataFilterParams,
  258. [`${paramName}`]: tableDataSearchKeywords
  259. })
  260. }
  261. useEffect(() => {
  262. }, [])
  263. return (
  264. <BMSPagecontainer className='ReportListMana' title={false}>
  265. <div className='toolBar'>
  266. <div className='filter'>
  267. <div className='filterItem'>
  268. <span className='label' style={{ whiteSpace: 'nowrap' }}> 检索:</span>
  269. <Input placeholder={'请输入列标题'} allowClear
  270. suffix={
  271. <IconFont type="iconsousuo" onClick={() => tableDataSearchHandle('columnName')} />
  272. }
  273. onChange={(e) => {
  274. set_tableDataSearchKeywords(e.target.value);
  275. if (e.target.value.length == 0) {
  276. set_tableDataFilterParams({
  277. ...tableDataFilterParams,
  278. columnName: ''
  279. });
  280. }
  281. }}
  282. onPressEnter={(e) => {
  283. set_tableDataFilterParams({
  284. ...tableDataFilterParams,
  285. columnName: (e.target as HTMLInputElement).value
  286. });
  287. }}
  288. />
  289. </div>
  290. </div>
  291. <div className='btnGroup'>
  292. <UpDataActBtn record type='ADD' />
  293. </div>
  294. </div>
  295. <div style={{ marginTop: 16 }}>
  296. <BMSTable columns={columns as ProColumns[]} actionRef={tableRef} rowKey='id' params={tableDataFilterParams} request={(params) => getTableData(params)} />
  297. </div>
  298. </BMSPagecontainer>
  299. )
  300. }