step5.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import React from 'react';
  2. import { Spin, Form, Button, message, Radio, Table, InputNumber } from 'antd';
  3. import ajax from 'jquery/src/ajax/xhr.js'
  4. import $ from 'jquery/src/ajax';
  5. import money from '@/money.js';
  6. const RadioButton = Radio.Button;
  7. const RadioGroup = Radio.Group;
  8. const FormItem = Form.Item;
  9. const formItemLayout = {
  10. labelCol: { span: 4 },
  11. wrapperCol: { span: 10 },
  12. };
  13. const NUMBERS = {
  14. 1: '第一年',
  15. 2: '第二年',
  16. 3: '第三年',
  17. 4: '第四年',
  18. 5: '第五年',
  19. }
  20. const EvaluateStep5 = Form.create({})(React.createClass({
  21. getInitialState() {
  22. return {
  23. loading: false,
  24. initialData: {},
  25. hasIncome: '1',
  26. type: '1',
  27. dataSource: []
  28. };
  29. },
  30. componentWillMount() {
  31. let initialData = this.props.data || {}, dataSource = initialData.forecastIncomes || [];
  32. let step4 = this.props.record && this.props.record[3] || {};
  33. let type = initialData.type || '1';
  34. if (step4.hasIncome == 2) {
  35. $.ajax({
  36. url: globalConfig.context + '/api/user/evaluate/step5/' + this.props.id
  37. }).done(function (res) {
  38. if (res.error && res.error.length) {
  39. message.error(res.error[0].message)
  40. } else if (res.data && res.data.length === 3) {
  41. for (let i = 0; i < 3; i++) {
  42. dataSource[i] = Object.assign({
  43. key: i,
  44. income: 0,
  45. unitPrice: 0,
  46. saleCount: 0,
  47. marketScale: 0,
  48. marketRate: 0,
  49. recommend: res.data[i] || 0
  50. }, dataSource[i]);
  51. }
  52. this.setState({
  53. loading: false,
  54. dataSource: dataSource
  55. })
  56. }
  57. }.bind(this)).fail(function () {
  58. this.setState({
  59. loading: false
  60. })
  61. }.bind(this));
  62. } else {
  63. for (let i = 0; i < 3; i++) {
  64. dataSource[i] = Object.assign({
  65. key: i,
  66. income: 0,
  67. unitPrice: 0,
  68. saleCount: 0,
  69. marketScale: 0,
  70. marketRate: 0
  71. }, dataSource[i] || {});
  72. }
  73. this.state.dataSource = dataSource;
  74. }
  75. this.state.initialData = initialData;
  76. this.state.type = type;
  77. this.state.hasIncome = String(step4.hasIncome) || '1'
  78. },
  79. next() {
  80. if (this.state.loading) {
  81. return;
  82. }
  83. this.props.form.validateFields((err, values) => {
  84. if (!err) {
  85. this.setState({
  86. loading: true
  87. })
  88. let incomes = [];
  89. for (let i = 0; i < 3; i++) {
  90. incomes.push({
  91. unitPrice: values['unitPrice' + i] || 0,
  92. saleCount: values['saleCount' + i] || 0,
  93. marketScale: values['marketScale' + i] || 0,
  94. marketRate: values['marketRate' + i] || 0,
  95. income: values['income' + i] || 0
  96. })
  97. }
  98. $.ajax({
  99. url: globalConfig.context + '/api/user/evaluate/step5',
  100. method: 'post',
  101. data: {
  102. id: this.props.id,
  103. type: values.type,
  104. forecastIncomes: JSON.stringify(incomes)
  105. }
  106. }).done(function (res) {
  107. if (res.error && res.error.length) {
  108. message.error(res.error[0].message)
  109. } else {
  110. if (this.props.next) {
  111. this.props.next({
  112. type: values.type,
  113. forecastIncomes: incomes
  114. });
  115. }
  116. }
  117. }.bind(this)).fail(function () {
  118. this.setState({
  119. loading: false
  120. })
  121. }.bind(this));
  122. } else {
  123. for (let field in err) {
  124. if (err.hasOwnProperty(field)) {
  125. message.error(err[field].errors[0].message)
  126. break;
  127. }
  128. }
  129. }
  130. });
  131. },
  132. prev() {
  133. if (this.props.prev) {
  134. this.props.prev();
  135. }
  136. },
  137. onRadioChange(e) {
  138. this.props.form.resetFields();
  139. this.setState({
  140. type: e.target.value,
  141. });
  142. },
  143. tableCols() {
  144. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  145. const { type } = this.state;
  146. let cols = [{
  147. title: '年份',
  148. dataIndex: 'key',
  149. key: 'key',
  150. render: (text, it, idx) => {
  151. return <div>{NUMBERS[idx + 1]}</div>
  152. }
  153. }];
  154. switch (type) {
  155. case "2":
  156. function calc(rate, scale, key) {
  157. let fv = {};
  158. fv['income' + key] = (scale * rate / 100) | 0
  159. setFieldsValue(fv);
  160. }
  161. cols.push({
  162. title: '市场规模(元)',
  163. dataIndex: 'marketScale',
  164. key: 'marketScale',
  165. render: (text, it, idx) => {
  166. return getFieldDecorator('marketScale' + it.key, {
  167. initialValue: text || 0,
  168. rules: [{
  169. validator: (rule, value, callback) => {
  170. if (!Number(value)) {
  171. callback('请输入' + NUMBERS[idx + 1] + '市场规模!');
  172. } else {
  173. callback();
  174. }
  175. },
  176. }]
  177. })(<InputNumber min={0} placeholder="请输入市场规模" onChange={value => {
  178. calc(Number(getFieldValue('marketRate' + it.key)) || 0, Number(value) || 0, it.key);
  179. }} />);
  180. }
  181. });
  182. cols.push({
  183. title: '技术覆盖率/市场份额(%)',
  184. dataIndex: 'marketRate',
  185. key: 'marketRate',
  186. render: (text, it, idx) => {
  187. return getFieldDecorator('marketRate' + it.key, {
  188. initialValue: text || 0,
  189. rules: [{
  190. validator: (rule, value, callback) => {
  191. if (!Number(value)) {
  192. callback('请输入' + NUMBERS[idx + 1] + '市场份额!');
  193. } else {
  194. callback();
  195. }
  196. },
  197. }]
  198. })(<InputNumber min={0} placeholder="请输入市场份额" onChange={value => {
  199. calc(Number(value) || 0, getFieldValue('marketScale' + it.key) || 0, it.key);
  200. }} />);
  201. }
  202. });
  203. cols.push({
  204. title: '营业收入(元)',
  205. dataIndex: 'income',
  206. key: 'income',
  207. render: (text, it, idx) => {
  208. return getFieldDecorator('income' + it.key, {
  209. initialValue: text || 0
  210. })(<InputNumber min={0} disabled={true} />);
  211. }
  212. });
  213. break;
  214. case "3":
  215. function calc(rate, scale, key) {
  216. let fv = {};
  217. fv['income' + key] = (scale * rate) | 0
  218. setFieldsValue(fv);
  219. }
  220. cols.push({
  221. title: '单价(元)',
  222. dataIndex: 'unitPrice',
  223. key: 'unitPrice',
  224. render: (text, it, idx) => {
  225. return getFieldDecorator('unitPrice' + it.key, {
  226. initialValue: text || 0,
  227. rules: [{
  228. validator: (rule, value, callback) => {
  229. if (!Number(value)) {
  230. callback('请输入' + NUMBERS[idx + 1] + '单价!');
  231. } else {
  232. callback();
  233. }
  234. },
  235. }]
  236. })(<InputNumber min={0} placeholder="请输入单价" onChange={value => {
  237. calc(Number(getFieldValue('saleCount' + it.key)) || 0, Number(value) || 0, it.key);
  238. }} />);
  239. }
  240. });
  241. cols.push({
  242. title: '销量',
  243. dataIndex: 'saleCount',
  244. key: 'saleCount',
  245. render: (text, it, idx) => {
  246. return getFieldDecorator('saleCount' + it.key, {
  247. initialValue: text || 0,
  248. rules: [{
  249. validator: (rule, value, callback) => {
  250. if (!Number(value)) {
  251. callback('请输入' + NUMBERS[idx + 1] + '销量!');
  252. } else {
  253. callback();
  254. }
  255. },
  256. }]
  257. })(<InputNumber min={0} placeholder="请输入销量" onChange={value => {
  258. calc(Number(value) || 0, getFieldValue('unitPrice' + it.key) || 0, it.key);
  259. }} />);
  260. }
  261. });
  262. cols.push({
  263. title: '营业收入(元)',
  264. dataIndex: 'income',
  265. key: 'income',
  266. render: (text, it, idx) => {
  267. return getFieldDecorator('income' + it.key, {
  268. initialValue: text || 0
  269. })(<InputNumber min={0} disabled={true} />);
  270. }
  271. });
  272. break;
  273. default:
  274. cols.push({
  275. title: '营业收入(元)',
  276. dataIndex: 'income',
  277. key: 'income',
  278. render: (text, it, idx) => {
  279. return getFieldDecorator('income' + it.key, {
  280. initialValue: text || 0,
  281. rules: [{
  282. validator: (rule, value, callback) => {
  283. if (!Number(value)) {
  284. callback('请输入' + NUMBERS[idx + 1] + '预估营业收入!');
  285. } else {
  286. callback();
  287. }
  288. },
  289. }]
  290. })(<InputNumber min={0} placeholder="请输入预估营业收入" />);
  291. }
  292. })
  293. break;
  294. }
  295. if (this.state.hasIncome == '2') {
  296. cols.push({
  297. title: '参考值(元)',
  298. dataIndex: 'recommend',
  299. key: 'recommend',
  300. render: (text) => {
  301. return money(text)
  302. }
  303. })
  304. }
  305. return cols
  306. },
  307. stringify(val) {
  308. return val && String(val)
  309. },
  310. render() {
  311. let { loading, dataSource, type } = this.state;
  312. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  313. return (
  314. <Spin spinning={loading}>
  315. <div style={{ marginBottom: 10 }}>根据市场等,预测该技术应用于生产活动并产生收入情况。(选择以下三种方式中的一种预估方法)</div>
  316. <Form className="steps-form">
  317. <FormItem label="请选择预测方法" {...formItemLayout}>
  318. {getFieldDecorator('type', {
  319. initialValue: this.stringify(type)
  320. })(
  321. <RadioGroup size="large" onChange={this.onRadioChange}>
  322. <RadioButton value="1">直接输入预测值法</RadioButton>
  323. <RadioButton value="2">市场份额法</RadioButton>
  324. <RadioButton value="3">数量单价法</RadioButton>
  325. </RadioGroup>
  326. )}
  327. </FormItem>
  328. <Table pagination={false} columns={this.tableCols()} dataSource={dataSource} />
  329. </Form>
  330. <div className="steps-action">
  331. <Button type="primary" onClick={() => this.next()}>保存,下一步</Button>
  332. <Button style={{ marginLeft: 8 }} onClick={() => this.prev()}>上一步</Button>
  333. </div>
  334. </Spin>
  335. )
  336. },
  337. }));
  338. export default EvaluateStep5;