index.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import DatePicker from '@/components/DatePicker'
  2. import FileModal from '@/components/FileModal'
  3. import { contractReturnStore } from '@/store/mobx'
  4. import { iReceivableState, iEditableCellProps } from '@/types/contract'
  5. import { iFileModal } from '@/types/file'
  6. import consts from '@/utils/consts'
  7. import { dayjsFormat, formatMoney } from '@/utils/util'
  8. import { DisconnectOutlined } from '@ant-design/icons'
  9. import { Form, Input, Popconfirm, Table } from 'antd'
  10. import locale from 'antd/es/date-picker/locale/zh_CN'
  11. import dayjs from 'dayjs'
  12. import { observer } from 'mobx-react'
  13. import React, { useEffect, useMemo, useState } from 'react'
  14. import { apiDelReturn, apiGetReturns, apiUpdateReturn } from './api'
  15. const EditableCell: React.FC<iEditableCellProps> = ({
  16. editing,
  17. dataIndex,
  18. title,
  19. cellType,
  20. children,
  21. record,
  22. index,
  23. ...restProps
  24. }) => {
  25. const cellNode = cellType === 'text' ? <Input size="small" allowClear /> : <DatePicker size="small" allowClear locale={locale} />
  26. const isDate = useMemo(() => {
  27. return dataIndex === 'createTime' || dataIndex === 'time'
  28. }, [ dataIndex ])
  29. return (
  30. <td {...restProps}>
  31. {editing ? (
  32. <Form.Item name={dataIndex} initialValue={isDate ? dayjs(record[dataIndex]) : record[dataIndex]} style={{ margin: 0 }} rules={[ dataIndex === 'remarks' ? {} : { required: true, message: `请输入${title}!` } ]}>
  33. {cellNode}
  34. </Form.Item>
  35. ) : (
  36. children
  37. )}
  38. </td>
  39. )
  40. }
  41. interface ReceivableProps {
  42. updateTreeAndContract: () => Promise<void>
  43. }
  44. const Receivable: React.FC<ReceivableProps> = ({ updateTreeAndContract }) => {
  45. const [ form ] = Form.useForm()
  46. const [ data, setData ] = useState<Array<iReceivableState>>([])
  47. const [ id, setId ] = useState<string>('')
  48. const [ fileModal, setFileModal ] = useState<iFileModal>({
  49. visible: false,
  50. dataType: consts.DATA_TYPE.RETURN,
  51. dataId: ''
  52. })
  53. const [ editingKey, setEditingKey ] = useState<string>('')
  54. const delConfirm = async (id: string, contractsId: string, bidsectionId: string) => {
  55. const { code = -1 } = await apiDelReturn(id, contractsId, bidsectionId)
  56. if (code === consts.RET_CODE.SUCCESS) {
  57. const newData = data.filter(item => item.id !== id)
  58. setData(newData)
  59. updateTreeAndContract()
  60. }
  61. }
  62. useEffect(() => {
  63. if (contractReturnStore.contract.id) {
  64. if (contractReturnStore.contract.id !== id) {
  65. setId(contractReturnStore.contract.id)
  66. initData()
  67. } else if (contractReturnStore.shouldUpdate && contractReturnStore.shouldUpdate === '2') {
  68. initData()
  69. }
  70. contractReturnStore.shouldUpdate && (contractReturnStore.changeUpdate(''))
  71. }
  72. }, [ contractReturnStore.contract.id, contractReturnStore.shouldUpdate ])
  73. const initData = async () => {
  74. const { code = -1, data = [] } = await apiGetReturns(contractReturnStore.contract.id, contractReturnStore.contract.bidsectionId)
  75. if (code === consts.RET_CODE.SUCCESS) {
  76. setData(data)
  77. }
  78. }
  79. const save = async (key: React.Key) => {
  80. try {
  81. const row = (await form.validateFields()) as iReceivableState
  82. const newData = [ ...data ]
  83. const index = newData.findIndex(item => key === item.id)
  84. if (index > -1) {
  85. const item = newData[index]
  86. const payload = { ...row, time: dayjsFormat(row.time, 'YYYY-MM-DD'), createTime: dayjsFormat(row.createTime, 'YYYY-MM-DD'), id: item.id, bidsectionId: item.bidsectionId, contractsId: item.contractsId }
  87. const { code = -1 } = await apiUpdateReturn(payload)
  88. if (code === consts.RET_CODE.SUCCESS) {
  89. newData.splice(index, 1, {
  90. ...item,
  91. ...row
  92. })
  93. setData(newData)
  94. updateTreeAndContract()
  95. }
  96. }
  97. setEditingKey('')
  98. } catch (errInfo) {
  99. console.log('Validate Failed:', errInfo)
  100. }
  101. }
  102. const isEditing = (record: iReceivableState) => record.id === editingKey
  103. const columns = [
  104. {
  105. dataIndex: 'sort',
  106. width: '5%',
  107. // eslint-disable-next-line react/display-name
  108. render: (_: any, record: any, index: number) => {
  109. return <span>{index + 1}</span>
  110. }
  111. },
  112. {
  113. title: '回款日期',
  114. dataIndex: 'time',
  115. editable: true,
  116. width: '12%',
  117. // eslint-disable-next-line react/display-name
  118. render: (text: string) => <span>{dayjsFormat(text, 'YYYY-MM-DD')}</span>
  119. },
  120. {
  121. title: '回款金额',
  122. dataIndex: 'price',
  123. editable: true,
  124. width: '12%',
  125. // eslint-disable-next-line react/display-name
  126. render: (text: string) => <span className="pi-text-right pi-width-100P">{formatMoney(text)}</span>
  127. },
  128. {
  129. title: '回款方式',
  130. dataIndex: 'way',
  131. editable: true,
  132. width: '12%'
  133. },
  134. {
  135. title: '创建人',
  136. dataIndex: 'createUser',
  137. editable: false,
  138. width: '12%'
  139. },
  140. {
  141. title: '创建时间',
  142. dataIndex: 'createTime',
  143. editable: true,
  144. width: '12%',
  145. // eslint-disable-next-line react/display-name
  146. render: (text: any) => <span>{dayjsFormat(text, 'YYYY-MM-DD')}</span>
  147. },
  148. {
  149. title: '备注',
  150. dataIndex: 'remarks',
  151. editable: true,
  152. width: '12%'
  153. },
  154. {
  155. title: '附件',
  156. dataIndex: 'fileCounts',
  157. // eslint-disable-next-line react/display-name
  158. render: (text: number, record: iReceivableState) => <span className="pi-pointer" onClick={() => setFileModal({ ...fileModal, dataId: record.id, visible: true })}><DisconnectOutlined /> {text}</span>
  159. },
  160. {
  161. title: '操作',
  162. dataIndex: 'opreate',
  163. width: '10%',
  164. // eslint-disable-next-line react/display-name
  165. render: (text: any, record: iReceivableState) => {
  166. const editable = isEditing(record)
  167. return (
  168. <div>
  169. {
  170. editable ?
  171. (<><span className="pi-link-blue pi-mg-right-5" onClick={() => save(record.id)}>保存</span><span className="pi-link-blue" onClick={() => setEditingKey('')}>取消</span></>)
  172. :
  173. <span className="pi-link-blue" onClick={() => setEditingKey(record.id)}>编辑</span>
  174. }
  175. <Popconfirm title="确认删除?" cancelText="取消" okText="确认" okButtonProps={{ danger: true }} onConfirm={() => delConfirm(record.id, record.contractsId, record.bidsectionId)}>
  176. <span className="pi-link-red pi-mg-left-5">删除</span>
  177. </Popconfirm>
  178. </div>
  179. )
  180. }
  181. }
  182. ]
  183. const cancel = () => {
  184. setEditingKey('')
  185. }
  186. const mergedColumns = columns.map(col => {
  187. if (!col.editable) {
  188. return col
  189. }
  190. return {
  191. ...col,
  192. onCell: (record: iReceivableState) => ({
  193. record,
  194. cellType: col.dataIndex === 'createTime' || col.dataIndex === 'time' ? 'DatePicker' : 'text',
  195. dataIndex: col.dataIndex,
  196. title: col.title,
  197. editing: isEditing(record)
  198. })
  199. }
  200. })
  201. return (
  202. <>
  203. <Form form={form} component={false}>
  204. <Table
  205. components={{ body: { cell: EditableCell } }}
  206. dataSource={data}
  207. columns={mergedColumns}
  208. bordered
  209. rowClassName="editable-row"
  210. pagination={{ onChange: cancel, size: "small", pageSize: 7 }}
  211. rowKey={record => record.id}
  212. />
  213. </Form>
  214. <FileModal
  215. visible={fileModal.visible}
  216. dataType={fileModal.dataType}
  217. dataId={fileModal.dataId}
  218. onCancel={() => setFileModal({ ...fileModal, visible: false })}
  219. showUpload={true}
  220. uploadCallBack={() => initData()}
  221. />
  222. </>
  223. )
  224. }
  225. export default observer(Receivable)