addRules.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { useEffect, useState, useContext } from 'react';
  2. import { Form, Input, Radio, Button, Select, message, Breadcrumb, Modal } from 'antd';
  3. import './index.less';
  4. import apiObj from '@api/index';
  5. import backIcon from "@images/back.png";
  6. import FiledContext from './filed-context';
  7. import add from '@images/add-icon.png';
  8. import del from '@images/del-icon.png';
  9. import { getValueFromEvent } from '@utils/index'
  10. const { post, api, xPost } = apiObj;
  11. const { Option } = Select;
  12. const { TextArea } = Input;
  13. function AddRules(props) {
  14. const [form] = Form.useForm();
  15. const { back, userChange } = props;
  16. const [visible, setVisible] = useState(false);
  17. const { type } = useContext(FiledContext);
  18. function handleOk() {
  19. back()
  20. }
  21. function handleCancel() {
  22. setVisible(false)
  23. }
  24. const goback = () => {
  25. if (form.isFieldsTouched()) {
  26. setVisible(true)
  27. } else {
  28. back()
  29. }
  30. };
  31. return (
  32. <>
  33. <Breadcrumb separator="">
  34. <Breadcrumb.Item><img className='back-icon' src={backIcon} onClick={goback} alt="返回上一页" /></Breadcrumb.Item>
  35. <Breadcrumb.Item>字段校验规则维护</Breadcrumb.Item>
  36. <Breadcrumb.Separator />
  37. <Breadcrumb.Item>{type == 1 ? "新增字段校验" : "修改字段校验"}</Breadcrumb.Item>
  38. </Breadcrumb>
  39. <div className="add-rules">
  40. <ContentForm back={goback} form={form} cancel={userChange}></ContentForm>
  41. </div>
  42. <Modal
  43. title="提示"
  44. okText='确定'
  45. cancelText='取消'
  46. width={400}
  47. visible={visible}
  48. onOk={handleOk}
  49. onCancel={handleCancel}
  50. maskClosable={false}
  51. >
  52. <p>您还有内容未保存,确定要退出?</p>
  53. </Modal>
  54. </>
  55. )
  56. }
  57. function ContentForm(props) {
  58. const [columnList, setColumnList] = useState([]);
  59. const [standardValueList, setStandardValueList] = useState([]);
  60. const [regularList, setRegularList] = useState([]);//正则式数据
  61. const [tableList, setTableList] = useState([]);//表名称
  62. const [colList, setColList] = useState([[]]);//字段名称
  63. const [list, setList] = useState([]);
  64. const [tableCname, setTableCname] = useState();
  65. const [tableEname, setTableEname] = useState();
  66. const [columnCname, setcolumnCname] = useState();
  67. const [columnEname, setcolumnEname] = useState();
  68. const [disable, seDisable] = useState([true]);
  69. const { type, detail } = useContext(FiledContext);
  70. const { back, form, cancel } = props;
  71. const initialValues = detail
  72. console.log(type);
  73. const vilidateRules = {
  74. required: '${label}不能为空!',
  75. };
  76. //获取正则式名称
  77. function getRegular(name) {
  78. const param = {
  79. name: name || ''
  80. }
  81. post(api.getRegular, param).then((res) => {
  82. if (res.data.code === 200) {
  83. const data = res.data.data;
  84. setRegularList(data);
  85. }
  86. })
  87. }
  88. function getAddUpColumnName() {
  89. post(api.getAddUpColumnName, {
  90. tableCname: tableCname,
  91. tableEname: tableEname,
  92. columnId: type == 2 ? detail.columnId : null,
  93. columnCname: columnCname,
  94. columnEname: columnEname,
  95. }).then((res) => {
  96. if (res.data.code === 200) {
  97. const data = res.data.data;
  98. let columnList = []
  99. data.forEach((item, i) => {
  100. item.getTableNameDTO.index = i
  101. columnList.push(item.getTableNameDTO)
  102. });
  103. setTableList(columnList);
  104. setList(data)
  105. if (type == 2) {
  106. data.forEach((item, i) => {
  107. detail.columnList.forEach(it => {
  108. if (item.getTableNameDTO.tableCname == it.tableCname) {
  109. let colList = [[]]
  110. colList[0] = data[i].getColumnNameDTOList
  111. setColList([...colList])
  112. }
  113. });
  114. });
  115. }
  116. }
  117. })
  118. }
  119. //表选择
  120. function tablechange(i, value, options, ins) {
  121. const formData = form.getFieldsValue();
  122. if (value) {
  123. colList[i] = list[options.key].getColumnNameDTOList
  124. disable[i] = false
  125. seDisable([...disable])
  126. setColList([...colList])
  127. } else {
  128. colList[i] = []
  129. disable[i] = true
  130. seDisable([...disable])
  131. setColList([...colList])
  132. getAddUpColumnName()
  133. }
  134. let columnList = formData.columnList;
  135. if (ins == 1) {
  136. if (value) {
  137. columnList[i].tableEname = options.val
  138. } else {
  139. columnList[i].tableEname = undefined
  140. }
  141. } else {
  142. if (value) {
  143. columnList[i].tableCname = options.val
  144. } else {
  145. columnList[i].tableCname = undefined
  146. }
  147. }
  148. columnList[i].columnCname = undefined
  149. columnList[i].columnEname = undefined
  150. form.setFieldsValue({
  151. columnList: columnList
  152. });
  153. }
  154. function colchange(i, value, options, ins) {
  155. const formData = form.getFieldsValue();
  156. let columnList = formData.columnList;
  157. if (value) {
  158. columnList[i].id = options.key
  159. } else {
  160. columnList[i].id = undefined
  161. }
  162. if (ins == 1) {
  163. if (value) {
  164. columnList[i].columnEname = options.val
  165. } else {
  166. columnList[i].columnEname = undefined
  167. }
  168. } else {
  169. if (value) {
  170. columnList[i].columnCname = options.val
  171. } else {
  172. columnList[i].columnCname = undefined
  173. }
  174. }
  175. form.setFieldsValue({
  176. columnList: columnList
  177. });
  178. }
  179. //表名称搜索(中文)
  180. function cnameSearch(val) {
  181. setTableCname(val)
  182. getAddUpColumnName()
  183. }
  184. //表名称搜索(英文)
  185. function enameSearch(val) {
  186. setTableEname(val)
  187. getAddUpColumnName()
  188. }
  189. //字段名称搜索(中文)
  190. function colmeSearch(val) {
  191. setcolumnCname(val)
  192. getAddUpColumnName()
  193. }
  194. //字段名称搜索(英文)
  195. function coleneSearch(val) {
  196. setcolumnEname(val)
  197. getAddUpColumnName()
  198. }
  199. //表字段添加
  200. function modifyData(i) {
  201. const formData = form.getFieldsValue();
  202. let columnList = formData.columnList;
  203. if (i == 0) {
  204. columnList.push({
  205. columnCname: undefined,
  206. columnEname: undefined,
  207. ableCname: undefined,
  208. tableEname: undefined
  209. })
  210. } else {
  211. columnList.splice(i, 1)
  212. }
  213. setColumnList([...columnList])
  214. form.setFieldsValue({
  215. columnList: columnList
  216. });
  217. const index = columnList.length - 1
  218. colList.push(...[[]])
  219. setColList([...colList])
  220. disable[index] = true
  221. seDisable([...disable])
  222. }
  223. //标准值维护
  224. function modifyStand(i) {
  225. const formData = form.getFieldsValue();
  226. let standardValueList = formData.standardValueList;
  227. if (i == 0) {
  228. standardValueList.push({
  229. tit: null
  230. })
  231. } else {
  232. standardValueList.splice(i, 1)
  233. }
  234. setStandardValueList([...standardValueList])
  235. form.setFieldsValue({
  236. standardValueList: standardValueList
  237. });
  238. }
  239. //正则式名称搜索
  240. function onSearch(val) {
  241. getRegular(val)
  242. }
  243. //正则式选择获取id
  244. function onChange(value, options) {
  245. form.setFieldsValue({
  246. regularId: options ? options.key : ''
  247. });
  248. }
  249. function handleSave() {
  250. form.validateFields().then((values) => {
  251. addColumnVerify()
  252. }).catch((error) => {
  253. console.log('验证失败', error)
  254. })
  255. }
  256. function addColumnVerify() {
  257. const formData = form.getFieldsValue();
  258. let arr = []
  259. let url
  260. formData.standardValueList.forEach(it => {
  261. if (it.tit) {
  262. arr.push(it.tit)
  263. }
  264. });
  265. formData.standardValueList = arr
  266. if (type == 1) {
  267. url = api.addColumnVerify
  268. } else {
  269. url = api.updateColumnVerify
  270. // formData.id = detail.columnId
  271. }
  272. post(url, formData).then((res) => {
  273. if (res.data.code === 200) {
  274. message.success(res.data.message);
  275. cancel()
  276. } else {
  277. message.error(res.data.message);
  278. }
  279. })
  280. }
  281. useEffect(() => {
  282. getRegular()
  283. getAddUpColumnName()
  284. if (detail) {
  285. const columnList = detail.columnList
  286. const standardValueList = detail.standardValueList
  287. setColumnList(columnList)
  288. setStandardValueList(standardValueList)
  289. list.forEach((item, i) => {
  290. detail.columnList.forEach(it => {
  291. if (item.getTableNameDTO.tableCname == it.tableCname) {
  292. let colList = [[]]
  293. colList[0] = list[i].getColumnNameDTOList
  294. setColList([...colList])
  295. }
  296. });
  297. });
  298. }
  299. if (type == 2) {
  300. seDisable([false])
  301. }
  302. }, []);
  303. return (
  304. <>
  305. <Form
  306. labelCol={{ span: 4 }}
  307. wrapperCol={{ span: 16 }}
  308. layout="horizontal"
  309. validateMessages={vilidateRules}
  310. initialValues={initialValues}
  311. form={form}
  312. >
  313. <Form.Item>
  314. {
  315. columnList.map((it, i) => {
  316. return (
  317. <div className='box' key={i}>
  318. <div className='item'>
  319. <div className='item-box'>
  320. <Form.Item label="表名称(中文)" rules={[{ required: true }]} style={{ width: '50%' }} name={['columnList', i, 'tableCname']} labelAlign="right" >
  321. <Select allowClear showSearch onChange={(value, option) => tablechange(i, value, option, 1)} onSearch={cnameSearch} style={{ width: 160 }} placeholder="请选择" disabled={type ==2}>
  322. {tableList.map((item) => {
  323. return (
  324. <Option value={item.tableCname} val={item.tableEname} key={item.index}>{item.tableCname}</Option>
  325. )
  326. })}
  327. </Select>
  328. </Form.Item>
  329. <Form.Item label="表名称(英文)" rules={[{ required: true }]} name={['columnList', i, 'tableEname']} labelAlign="right">
  330. <Select allowClear showSearch onChange={(value, option) => tablechange(i, value, option, 2)} onSearch={enameSearch} style={{ width: 160 }} placeholder="请选择" disabled={type ==2}>
  331. {tableList.map((item) => {
  332. return (
  333. <Option value={item.tableEname} val={item.tableCname} key={item.index}>{item.tableEname}</Option>
  334. )
  335. })}
  336. </Select>
  337. </Form.Item>
  338. </div>
  339. <div className='item-box'>
  340. <Form.Item label="字段名称(中文)" rules={[{ required: true }]} style={{ width: '50%' }} name={['columnList', i, 'columnCname']} labelAlign="right">
  341. <Select allowClear showSearch onChange={(value, option) => colchange(i, value, option, 1)} onSearch={colmeSearch} style={{ width: 160 }} placeholder="请选择" disabled={disable[i] || type == 2}>
  342. {colList[i].map((item) => {
  343. return (
  344. <Option value={item.columnCname} val={item.columnEname} key={item.id} index={i}>{item.columnCname}</Option>
  345. )
  346. })}
  347. </Select>
  348. </Form.Item>
  349. <Form.Item label="字段名称(英文)" rules={[{ required: true }]} name={['columnList', i, 'columnEname']} labelAlign="right">
  350. <Select allowClear showSearch onChange={(value, option) => colchange(i, value, option, 2)} onSearch={coleneSearch} style={{ width: 160 }} placeholder="请选择" disabled={disable[i] || type == 2}>
  351. {colList[i].map((item) => {
  352. return (
  353. <Option value={item.columnEname} val={item.columnCname} key={item.id} index={i}>{item.columnEname}</Option>
  354. )
  355. })}
  356. </Select>
  357. </Form.Item>
  358. <Form.Item hidden={true} name={['columnList', i, 'id']} noStyle>
  359. <Input />
  360. </Form.Item>
  361. </div>
  362. </div>
  363. <img onClick={() => modifyData(i)} src={i == 0 ? add : del} hidden={type == 2} />
  364. </div>
  365. )
  366. })
  367. }
  368. </Form.Item>
  369. <Form.Item label="是否必填数据" name="isRequired" rules={[{ required: true, message: '请选择是否必填' }]}>
  370. <Radio.Group>
  371. <Radio value={'1'}>是</Radio>
  372. <Radio value={'0'}>否</Radio>
  373. </Radio.Group>
  374. </Form.Item>
  375. <Form.Item label="标准值维护" >
  376. {
  377. standardValueList.map((it, i) => {
  378. return (
  379. <div className='box-2' key={i} >
  380. <Form.Item name={['standardValueList', i, 'tit']} style={{ width: 159 }} getValueFromEvent={getValueFromEvent} rules={[{ max: 30, message: '标准值不能超过30个字符' }]}>
  381. <Input autoComplete='off' placeholder="请输入" />
  382. </Form.Item>
  383. <img onClick={() => modifyStand(i)} src={i == 0 ? add : del} />
  384. </div>
  385. )
  386. })
  387. }
  388. </Form.Item>
  389. <Form.Item label="正则式名称" name="regularName">
  390. <Select showSearch allowClear onSearch={onSearch} onChange={onChange} style={{ width: 160 }} placeholder="请选择">
  391. {regularList.map((item) => {
  392. return (
  393. <Option value={item.name} key={item.id}>{item.name}</Option>
  394. )
  395. })}
  396. </Select>
  397. </Form.Item>
  398. <Form.Item hidden={true} name="regularId" noStyle>
  399. <Input />
  400. </Form.Item>
  401. <Form.Item label="说明" name="description" rules={[{ max: 200, message: '说明不能超过200个字符' }]} getValueFromEvent={getValueFromEvent}>
  402. <TextArea
  403. autoSize={{ minRows: 3, maxRows: 5 }}
  404. placeholder="请输入"
  405. />
  406. </Form.Item>
  407. </Form>
  408. <div className="button-box">
  409. <Button onClick={back}>关闭</Button>
  410. <Button onClick={handleSave} type='primary'>保存</Button>
  411. </div>
  412. </>
  413. )
  414. }
  415. export default AddRules;