batchImport.jsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import React from 'react';
  2. import { Input, Modal, Button, message } from 'antd';
  3. import { achievementCategoryList } from '../../dataDic.js';
  4. import ajax from 'jquery/src/ajax/xhr.js';
  5. import $ from 'jquery/src/ajax';
  6. const BatchImport = React.createClass({
  7. getInitialState: function () {
  8. return {
  9. loading: false,
  10. error: false
  11. };
  12. },
  13. showModal () {
  14. this.setState({
  15. visible: true,
  16. });
  17. },
  18. handleOk (e) {
  19. this.setState({
  20. visible: false,
  21. });
  22. },
  23. handleCancel (e) {
  24. this.setState({
  25. visible: false,
  26. });
  27. },
  28. //数据反向转化
  29. getDataCategory (e, row) {
  30. switch (e) {
  31. case "成果":
  32. return 0;
  33. case "技术":
  34. return 1;
  35. case "项目":
  36. return 2;
  37. default:
  38. message.warning("第 " + row + " 行中没有对应的数据类型!");
  39. this.state.error = true;
  40. break;
  41. }
  42. },
  43. getOwnerType (e, row) {
  44. switch (e) {
  45. case "个人":
  46. return 0;
  47. case "组织":
  48. return 1;
  49. default:
  50. message.warning("第 " + row + " 行中没有对应的所有人类型!");
  51. this.state.error = true;
  52. break;
  53. }
  54. },
  55. getCategory (e, row) {
  56. let theType = null, _me = this;
  57. if (e) {
  58. achievementCategoryList.map(function (item) {
  59. if (item.key == e) {
  60. theType = Number(item.value);
  61. };
  62. });
  63. if (theType == null) {
  64. message.warning("第 " + row + " 行中没有对应的成果类型!");
  65. _me.state.error = true;
  66. }
  67. return theType;
  68. };
  69. },
  70. getCooperationMode (e, row) {
  71. switch (e) {
  72. case "技术转让":
  73. return 0;
  74. case "授权生产":
  75. return 1;
  76. default:
  77. message.warning("第 " + row + " 行中没有对应的合作方式!");
  78. this.state.error = true;
  79. break;
  80. }
  81. },
  82. //提交
  83. handleSubmit () {
  84. let rowsArr = this.state.inputValue.split(/[\r\n]/g);
  85. let columnArr = [], _me = this;
  86. if (!rowsArr.length) {
  87. return;
  88. };
  89. let columnLength = 12;//定义数据正常长度
  90. for (let i = 0; i < rowsArr.length; i++) {
  91. if (_me.state.error) {
  92. this.state.error = false;
  93. return;
  94. };
  95. let theArr = rowsArr[i].split(/\t/);
  96. if (theArr.length == columnLength) {
  97. columnArr.push({
  98. dataCategory: _me.getDataCategory(theArr[0], i + 1),
  99. name: theArr[1],
  100. keyword: theArr[2],
  101. category: _me.getCategory(theArr[3], i + 1),
  102. ownerName: theArr[4],
  103. ownerType: _me.getOwnerType(theArr[5], i + 1),
  104. ownerIdNumber: theArr[6],
  105. ownerMobile: theArr[7],
  106. ownerEmail: theArr[8],
  107. ownerPostalAddress: theArr[9],
  108. cooperationMode: _me.getCooperationMode(theArr[10], i + 1),
  109. introduction: theArr[11],
  110. });
  111. } else if (rowsArr[i].replace(/(^\s+)|(\s+$)/g, "").length) {
  112. message.warning("第" + (i + 1) + "行数据长度错误!");
  113. return;
  114. };
  115. };
  116. // for (let i = 0; i < columnArr.length; i++) {
  117. // if (columnArr[i].dataCategory.toString().length > 1) { console.log((i + 1) + "行:" + "dataCategory") }
  118. // if (columnArr[i].name.toString().length > 31) { console.log((i + 1) + "行:" + "name") }
  119. // if (columnArr[i].category.toString().length > 1) { console.log((i + 1) + "行:" + "category") }
  120. // if (columnArr[i].ownerName.toString().length > 31) { console.log((i + 1) + "行:" + "ownerName") }
  121. // if (columnArr[i].ownerIdNumber.toString().length > 35) { console.log((i + 1) + "行:" + "ownerIdNumber") }
  122. // if (columnArr[i].ownerMobile.toString().length > 11) { console.log((i + 1) + "行:" + "ownerMobile") }
  123. // if (columnArr[i].ownerEmail.toString().length > 254) { console.log((i + 1) + "行:" + "ownerEmail") }
  124. // if (columnArr[i].ownerPostalAddress.toString().length > 254) { console.log((i + 1) + "行:" + "ownerPostalAddress") }
  125. // if (columnArr[i].cooperationMode.toString().length > 1) { console.log((i + 1) + "行:" + "cooperationMode") }
  126. // if (columnArr[i].introduction.toString().length > 298) { console.log((i + 1) + "行:" + "introduction") }
  127. // if (columnArr[i].keyword.toString().length > 44) { console.log((i + 1) + "行:" + "ownerType") }
  128. // if (columnArr[i].ownerType.toString().length > 1) { console.log((i + 1) + "行:" + "ownerType") }
  129. // }
  130. if (columnArr.length > 1000) {
  131. message.warning("数据条数不能多于1000条!");
  132. return;
  133. };
  134. this.setState({
  135. loading: true
  136. });
  137. $.ajax({
  138. method: "POST",
  139. dataType: "json",
  140. crossDomain: false,
  141. url: globalConfig.context + '/api/admin/achievement/importAchievement',
  142. data: {
  143. data: JSON.stringify(columnArr)
  144. }
  145. }).done(function (data) {
  146. this.setState({
  147. loading: false
  148. });
  149. if (!data.error.length) {
  150. message.success('导入成功!');
  151. this.props.closeDesc(false, true);
  152. this.handleOk();
  153. } else {
  154. message.warning(data.error[0].message);
  155. };
  156. }.bind(this));
  157. },
  158. render () {
  159. return (
  160. <div>
  161. <Button type="primary" onClick={this.showModal}>批量导入</Button>
  162. <Modal
  163. title="批量导入"
  164. visible={this.state.visible}
  165. onCancel={this.handleCancel}
  166. width='1000px'
  167. footer={[
  168. <Button key="ok" className="set-submit" type="primary" loading={this.state.loading} onClick={this.handleSubmit}>保存</Button>,
  169. <Button key="clancel" className="set-submit" type="ghost" onClick={this.handleCancel}>取消</Button>
  170. ]} >
  171. <Input type="textarea" rows={12} onChange={(e) => { this.state.inputValue = e.target.value; }} />
  172. </Modal>
  173. </div>
  174. );
  175. }
  176. });
  177. export default BatchImport;