addRules.js 14 KB

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