addRules.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 [disable, seDisable] = useState([true]);
  65. const { type, detail } = useContext(FiledContext);
  66. const { back, form, cancel } = props;
  67. const initialValues = detail
  68. const vilidateRules = {
  69. required: '${label}不能为空!',
  70. };
  71. //获取正则式名称
  72. function getRegular(name) {
  73. const param = {
  74. name: name || ''
  75. }
  76. post(api.getRegular, param).then((res) => {
  77. if (res.data.code === 200) {
  78. const data = res.data.data;
  79. setRegularList(data);
  80. }
  81. })
  82. }
  83. function getAddUpColumnName(tableCname, tableEname, columnCname, columnEname) {
  84. post(api.getAddUpColumnName, {
  85. tableCname: tableCname,
  86. tableEname: tableEname,
  87. columnCname: columnCname,
  88. columnEname: columnEname,
  89. }).then((res) => {
  90. if (res.data.code === 200) {
  91. const data = res.data.data;
  92. let columnList = []
  93. data.forEach((item, i) => {
  94. item.getTableNameDTO.index = i
  95. columnList.push(item.getTableNameDTO)
  96. });
  97. setTableList(columnList);
  98. setList(data)
  99. if (type == 2) {
  100. data.forEach((item, i) => {
  101. detail.columnList.forEach(it => {
  102. if (item.getTableNameDTO.tableCname == it.tableCname) {
  103. let colList = [[]]
  104. colList[0] = data[i].getColumnNameDTOList
  105. setColList([...colList])
  106. }
  107. });
  108. });
  109. }
  110. }
  111. })
  112. }
  113. //表选择
  114. function tablechange(i, value, options,ins) {
  115. const formData = form.getFieldsValue();
  116. if (value) {
  117. colList[i] = list[options.key].getColumnNameDTOList
  118. disable[i] = false
  119. seDisable([...disable])
  120. setColList([...colList])
  121. } else {
  122. colList[i] = []
  123. disable[i] = true
  124. seDisable([...disable])
  125. setColList([...colList])
  126. }
  127. let columnList = formData.columnList;
  128. if(ins == 1){
  129. columnList[i].tableEname = options.val
  130. }else{
  131. columnList[i].tableCname = options.val
  132. }
  133. columnList[i].columnCname = undefined
  134. columnList[i].columnEname = undefined
  135. form.setFieldsValue({
  136. columnList: columnList
  137. });
  138. console.log(form.getFieldsValue());
  139. }
  140. function colchange(i, value, options,ins) {
  141. const formData = form.getFieldsValue();
  142. let columnList = formData.columnList;
  143. columnList[i].id = options.key
  144. if(ins == 1){
  145. columnList[i].columnEname = options.val
  146. }else{
  147. columnList[i].columnCname = options.val
  148. }
  149. form.setFieldsValue({
  150. columnList: columnList
  151. });
  152. }
  153. //表名称搜索(中文)
  154. function cnameSearch(val) {
  155. getAddUpColumnName(val, '', '', '')
  156. }
  157. //表名称搜索(英文)
  158. function enameSearch(val) {
  159. getAddUpColumnName('', val, '', '')
  160. }
  161. //字段名称搜索(中文)
  162. function colmeSearch(val) {
  163. getAddUpColumnName(val, '', '', '')
  164. }
  165. //字段名称搜索(英文)
  166. function coleneSearch(val) {
  167. getAddUpColumnName('', val, '', '')
  168. }
  169. //表字段添加
  170. function modifyData(i) {
  171. const formData = form.getFieldsValue();
  172. let columnList = formData.columnList;
  173. if (i == 0) {
  174. columnList.push({
  175. columnCname: undefined,
  176. columnEname: undefined,
  177. ableCname: undefined,
  178. tableEname: undefined
  179. })
  180. } else {
  181. columnList.splice(i, 1)
  182. }
  183. setColumnList([...columnList])
  184. form.setFieldsValue({
  185. columnList: columnList
  186. });
  187. const index = columnList.length - 1
  188. colList.push(...[[]])
  189. setColList([...colList])
  190. disable[index] = true
  191. seDisable([...disable])
  192. }
  193. //标准值维护
  194. function modifyStand(i) {
  195. const formData = form.getFieldsValue();
  196. let standardValueList = formData.standardValueList;
  197. if (i == 0) {
  198. standardValueList.push({
  199. tit: null
  200. })
  201. } else {
  202. standardValueList.splice(i, 1)
  203. }
  204. setStandardValueList([...standardValueList])
  205. form.setFieldsValue({
  206. standardValueList: standardValueList
  207. });
  208. }
  209. //正则式名称搜索
  210. function onSearch(val) {
  211. getRegular(val)
  212. }
  213. //正则式选择获取id
  214. function onChange(value, options) {
  215. form.setFieldsValue({
  216. regularId: options ? options.key : ''
  217. });
  218. }
  219. function handleSave() {
  220. form.validateFields().then((values) => {
  221. addColumnVerify()
  222. }).catch((error) => {
  223. console.log('验证失败', error)
  224. })
  225. }
  226. function addColumnVerify() {
  227. const formData = form.getFieldsValue();
  228. let arr = []
  229. let url
  230. formData.standardValueList.forEach(it => {
  231. if (it.tit) {
  232. arr.push(it.tit)
  233. }
  234. });
  235. formData.standardValueList = arr
  236. if (type == 1) {
  237. url = api.addColumnVerify
  238. } else {
  239. url = api.updateColumnVerify
  240. // formData.id = detail.columnId
  241. }
  242. post(url, formData).then((res) => {
  243. if (res.data.code === 200) {
  244. message.success(res.data.message);
  245. cancel()
  246. } else {
  247. message.error(res.data.message);
  248. }
  249. })
  250. }
  251. useEffect(() => {
  252. getRegular()
  253. getAddUpColumnName()
  254. if (detail) {
  255. const columnList = detail.columnList
  256. const standardValueList = detail.standardValueList
  257. setColumnList(columnList)
  258. setStandardValueList(standardValueList)
  259. list.forEach((item, i) => {
  260. detail.columnList.forEach(it => {
  261. if (item.getTableNameDTO.tableCname == it.tableCname) {
  262. let colList = [[]]
  263. colList[0] = list[i].getColumnNameDTOList
  264. setColList([...colList])
  265. }
  266. });
  267. });
  268. }
  269. if (type == 2) {
  270. seDisable([false])
  271. }
  272. }, []);
  273. return (
  274. <>
  275. <Form
  276. labelCol={{ span: 4 }}
  277. wrapperCol={{ span: 16 }}
  278. layout="horizontal"
  279. validateMessages={vilidateRules}
  280. initialValues={initialValues}
  281. form={form}
  282. >
  283. <Form.Item>
  284. {
  285. columnList.map((it, i) => {
  286. return (
  287. <div className='box' key={i}>
  288. <div className='item'>
  289. <div className='item-box'>
  290. <Form.Item label="表名称(中文)" rules={[{ required: true }]} style={{ width: '50%' }} name={['columnList', i, 'tableCname']} labelAlign="right" >
  291. <Select allowClear showSearch onChange={(value, option) => tablechange(i, value, option,1)} onSearch={cnameSearch} style={{ width: 160 }} placeholder="请选择">
  292. {tableList.map((item) => {
  293. return (
  294. <Option value={item.tableCname} val={item.tableEname} key={item.index}>{item.tableCname}</Option>
  295. )
  296. })}
  297. </Select>
  298. </Form.Item>
  299. <Form.Item label="表名称(英文)" rules={[{ required: true }]} name={['columnList', i, 'tableEname']} labelAlign="right">
  300. <Select allowClear showSearch onChange={(value, option) => tablechange(i, value, option,2)} onSearch={enameSearch} style={{ width: 160 }} placeholder="请选择" >
  301. {tableList.map((item) => {
  302. return (
  303. <Option value={item.tableEname} val={item.tableCname} key={item.index}>{item.tableEname}</Option>
  304. )
  305. })}
  306. </Select>
  307. </Form.Item>
  308. </div>
  309. <div className='item-box'>
  310. <Form.Item label="字段名称(中文)" rules={[{ required: true }]} style={{ width: '50%' }} name={['columnList', i, 'columnCname']} labelAlign="right">
  311. <Select allowClear showSearch onChange={(value, option) => colchange(i, value, option,1)} onSearch={colmeSearch} style={{ width: 160 }} placeholder="请选择" disabled={disable[i]}>
  312. {colList[i].map((item) => {
  313. return (
  314. <Option value={item.columnCname} val={item.columnEname} key={item.id} index={i}>{item.columnCname}</Option>
  315. )
  316. })}
  317. </Select>
  318. </Form.Item>
  319. <Form.Item label="字段名称(英文)" rules={[{ required: true }]} name={['columnList', i, 'columnEname']} labelAlign="right">
  320. <Select allowClear showSearch onChange={(value, option) => colchange(i, value, option,2)} onSearch={coleneSearch} style={{ width: 160 }} placeholder="请选择" disabled={disable[i]}>
  321. {colList[i].map((item) => {
  322. return (
  323. <Option value={item.columnEname} val={item.columnCname} key={item.id} index={i}>{item.columnEname}</Option>
  324. )
  325. })}
  326. </Select>
  327. </Form.Item>
  328. <Form.Item hidden={true} name={['columnList', i, 'id']} noStyle>
  329. <Input />
  330. </Form.Item>
  331. </div>
  332. </div>
  333. <img onClick={() => modifyData(i)} src={i == 0 ? add : del} />
  334. </div>
  335. )
  336. })
  337. }
  338. </Form.Item>
  339. <Form.Item label="是否必填数据" name="isRequired" rules={[{ required: true, message: '请选择是否必填' }]}>
  340. <Radio.Group>
  341. <Radio value={'1'}>是</Radio>
  342. <Radio value={'0'}>否</Radio>
  343. </Radio.Group>
  344. </Form.Item>
  345. <Form.Item label="标准值维护" >
  346. {
  347. standardValueList.map((it, i) => {
  348. return (
  349. <div className='box-2' key={i} >
  350. <Form.Item name={['standardValueList', i, 'tit']} style={{ width: 159 }} getValueFromEvent={getValueFromEvent} rules={[{ max: 30, message: '标准值不能超过30个字符' }]}>
  351. <Input autoComplete='off' placeholder="请输入" />
  352. </Form.Item>
  353. <img onClick={() => modifyStand(i)} src={i == 0 ? add : del} />
  354. </div>
  355. )
  356. })
  357. }
  358. </Form.Item>
  359. <Form.Item label="正则式名称" name="regularName">
  360. <Select showSearch allowClear onSearch={onSearch} onChange={onChange} style={{ width: 160 }} placeholder="请选择">
  361. {regularList.map((item) => {
  362. return (
  363. <Option value={item.name} key={item.id}>{item.name}</Option>
  364. )
  365. })}
  366. </Select>
  367. </Form.Item>
  368. <Form.Item hidden={true} name="regularId" noStyle>
  369. <Input />
  370. </Form.Item>
  371. <Form.Item label="说明" name="description" rules={[{ max: 200, message: '说明不能超过200个字符' }]} getValueFromEvent={getValueFromEvent}>
  372. <TextArea
  373. autoSize={{ minRows: 3, maxRows: 5 }}
  374. placeholder="请输入"
  375. />
  376. </Form.Item>
  377. </Form>
  378. <div className="button-box">
  379. <Button onClick={back}>取消</Button>
  380. <Button onClick={handleSave} type='primary'>保存</Button>
  381. </div>
  382. </>
  383. )
  384. }
  385. export default AddRules;