UserEvaluationApiController.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. package com.goafanti.evaluation.controller;
  2. import java.math.BigDecimal;
  3. import java.math.MathContext;
  4. import java.math.RoundingMode;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.Stream;
  11. import javax.validation.Valid;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.util.Assert;
  17. import org.springframework.validation.BindingResult;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import com.alibaba.fastjson.JSON;
  23. import com.alibaba.fastjson.JSONObject;
  24. import com.goafanti.common.bo.Result;
  25. import com.goafanti.common.constant.ErrorConstants;
  26. import com.goafanti.common.controller.BaseApiController;
  27. import com.goafanti.common.model.DistrictGlossory;
  28. import com.goafanti.common.model.IndustryCategory;
  29. import com.goafanti.common.model.ValueEvaluation;
  30. import com.goafanti.common.service.DistrictGlossoryService;
  31. import com.goafanti.common.service.IndustryCategoryService;
  32. import com.goafanti.common.utils.LoggerUtils;
  33. import com.goafanti.core.shiro.token.TokenManager;
  34. import com.goafanti.evaluation.bo.ForecastIncome;
  35. import com.goafanti.evaluation.bo.Step1;
  36. import com.goafanti.evaluation.bo.Step2;
  37. import com.goafanti.evaluation.bo.Step3;
  38. import com.goafanti.evaluation.bo.Step4;
  39. import com.goafanti.evaluation.bo.Step5;
  40. import com.goafanti.evaluation.bo.Step6;
  41. import com.goafanti.evaluation.bo.Step7;
  42. import com.goafanti.evaluation.bo.YearIncome;
  43. import com.goafanti.evaluation.enums.ProfitRate;
  44. import com.goafanti.evaluation.service.ValueEvaluationService;
  45. @RestController
  46. @RequestMapping(value = "/api/user/evaluate")
  47. public class UserEvaluationApiController extends BaseApiController {
  48. @SuppressWarnings("unused")
  49. private static final String GOVERNMENT_LOAN_ROR = "government_loan_ror";
  50. private static final Logger logger = LoggerFactory.getLogger(UserEvaluationApiController.class);
  51. private static final MathContext DEFAULT_PRECISION = new MathContext(4, RoundingMode.HALF_UP);
  52. private static final String STEP1 = "0";
  53. private static final String STEP2 = "1";
  54. private static final String STEP3 = "2";
  55. private static final String STEP4 = "3";
  56. private static final String STEP5 = "4";
  57. private static final String STEP6 = "5";
  58. private static final String STEP7 = "6";
  59. private static final Integer[] SCORES = new Integer[] { 100, 80, 60, 40, 20, 0 };
  60. private static final Integer[] DISCOUNT_SCORES = new Integer[] { 1, 2, 3, 4, 5 };
  61. private static final BigDecimal MIN_RATE = new BigDecimal(2);
  62. private static final BigDecimal MAX_RATE = new BigDecimal(6);
  63. private static final BigDecimal HUNDRED = new BigDecimal(100);
  64. private static final BigDecimal PERCENTAGE = new BigDecimal(0.01, DEFAULT_PRECISION);
  65. @Autowired
  66. ValueEvaluationService valueEvaluationService;
  67. @Autowired
  68. private DistrictGlossoryService districtGlossoryService;
  69. @Autowired
  70. private IndustryCategoryService industryCategoryService;
  71. @RequestMapping(value = "/create", method = RequestMethod.GET)
  72. public Result create() {
  73. ValueEvaluation ve = new ValueEvaluation();
  74. ve.setUid(TokenManager.getUserId());
  75. ve.setStep(0);
  76. ve.setValue(0l);
  77. ve.setLog("{}");
  78. valueEvaluationService.insert(ve);
  79. return new Result(ve.getId().toString());
  80. }
  81. @RequestMapping(value = "/list", method = RequestMethod.GET)
  82. public Result list(String pageNo, String pageSize) {
  83. return new Result().data(valueEvaluationService.list(handlePageNo(pageNo), handlePageSize(pageSize)));
  84. }
  85. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  86. public Result info(@PathVariable String id) {
  87. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  88. ValueEvaluation ve = valueEvaluationService.getMyEvaluation(Long.valueOf(id));
  89. Assert.notNull(ve, ErrorConstants.EVALUATE_ID);
  90. Map<String, Object> res = new HashMap<>();
  91. res.put("name", ve.getName());
  92. res.put("step", ve.getStep());
  93. res.put("value", ve.getValue());
  94. res.put("steps", JSON.parse(ve.getLog()));
  95. return new Result().data(res);
  96. }
  97. @RequestMapping(value = "/remove", method = RequestMethod.POST)
  98. public Result remove(String ids) {
  99. Assert.isTrue(StringUtils.isNotBlank(ids), ErrorConstants.EVALUATE_PARAM);
  100. Result res = new Result();
  101. List<Long> idList = new ArrayList<>();
  102. String[] idArr = ids.split(",");
  103. for (String id : idArr) {
  104. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_PARAM);
  105. idList.add(Long.valueOf(id));
  106. }
  107. res.data(valueEvaluationService.deleteMySteps(idList));
  108. return res;
  109. }
  110. @RequestMapping(value = "/step1", method = RequestMethod.POST)
  111. public Result step1(String id, @Valid Step1 data, BindingResult bindingResult) {
  112. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  113. Result res = new Result();
  114. if (handleBindingError(res, bindingResult)) {
  115. return res;
  116. }
  117. updateSteps(data, 1, "0", res, id);
  118. return res;
  119. }
  120. @RequestMapping(value = "/step2", method = RequestMethod.POST)
  121. public Result step2(String id, @Valid Step2 data, BindingResult bindingResult) {
  122. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  123. Result res = new Result();
  124. if (handleBindingError(res, bindingResult)) {
  125. return res;
  126. }
  127. updateSteps(data, 2, STEP2, res, id);
  128. return res;
  129. }
  130. @RequestMapping(value = "/step3", method = RequestMethod.POST)
  131. public Result step3(String id, @Valid Step3 data, BindingResult bindingResult) {
  132. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  133. Result res = new Result();
  134. if (handleBindingError(res, bindingResult)) {
  135. return res;
  136. }
  137. updateSteps(data, 3, STEP3, res, id);
  138. return res;
  139. }
  140. @RequestMapping(value = "/step4", method = RequestMethod.POST)
  141. public Result step4(String id, String hasIncome, String incomes) {
  142. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  143. Result res = new Result();
  144. Step4 step = new Step4();
  145. try {
  146. step.setHasIncome(Integer.valueOf(hasIncome));
  147. if (step.getHasIncome().equals(2)) {
  148. List<YearIncome> ja = JSON.parseArray(incomes, YearIncome.class);
  149. if (ja.size() != 3) {
  150. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  151. } else {
  152. step.setIncomes(ja);
  153. }
  154. }
  155. } catch (Exception e) {
  156. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  157. }
  158. if (res.getError().isEmpty()) {
  159. updateSteps(step, 4, STEP4, res, id);
  160. }
  161. return res;
  162. }
  163. @RequestMapping(value = "/step5", method = RequestMethod.POST)
  164. public Result step5(String id, String type, String forecastIncomes) {
  165. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  166. Result res = new Result();
  167. Step5 step = new Step5();
  168. try {
  169. step.setType(Integer.valueOf(type));
  170. List<ForecastIncome> ja = JSON.parseArray(forecastIncomes, ForecastIncome.class);
  171. if (ja.size() != 3) {
  172. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  173. } else {
  174. step.setForecastIncomes(ja);
  175. }
  176. } catch (Exception e) {
  177. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  178. }
  179. if (res.getError().isEmpty()) {
  180. updateSteps(step, 5, STEP5, res, id);
  181. }
  182. return res;
  183. }
  184. @RequestMapping(value = "/step5/{id}", method = RequestMethod.GET)
  185. public Result step5Info(@PathVariable String id) {
  186. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  187. Result res = new Result();
  188. ValueEvaluation ve = valueEvaluationService.getMyEvaluation(Long.valueOf(id));
  189. try {
  190. JSONObject jo = JSON.parseObject(ve.getLog());
  191. Step4 step4 = ((JSON) jo.get(STEP4)).toJavaObject(Step4.class);
  192. if (step4.getHasIncome().equals(2)) {
  193. Step1 step1 = ((JSON) jo.get(STEP1)).toJavaObject(Step1.class);
  194. res.data(calcForecastIncome(step4.getIncomes(), getIndustryCategoryValue(step1)));
  195. }
  196. } catch (Exception e) {
  197. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  198. }
  199. return res;
  200. }
  201. @RequestMapping(value = "/step6", method = RequestMethod.POST)
  202. public Result step6(String id, @Valid Step6 data, BindingResult bindingResult) {
  203. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  204. Result res = new Result();
  205. if (handleBindingError(res, bindingResult)) {
  206. return res;
  207. }
  208. updateSteps(data, 6, STEP6, res, id);
  209. return res;
  210. }
  211. @RequestMapping(value = "/step7", method = RequestMethod.POST)
  212. public Result step7(String id, @Valid Step7 data, BindingResult bindingResult) {
  213. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  214. Result res = new Result();
  215. if (handleBindingError(res, bindingResult)) {
  216. return res;
  217. }
  218. updateSteps(data, 7, STEP7, res, id);
  219. return res;
  220. }
  221. @RequestMapping(value = "/calc/{id}", method = RequestMethod.GET)
  222. public Result calc(@PathVariable String id) {
  223. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  224. Result res = new Result();
  225. ValueEvaluation ve = valueEvaluationService.getMyEvaluation(Long.valueOf(id));
  226. if (ve == null || ve.getStep() < 7) {
  227. res.error(buildError(ErrorConstants.EVALUATE_ID));
  228. } else {
  229. Long value = calcValue(ve, new HashMap<>());
  230. res.data(value);
  231. ValueEvaluation upt = new ValueEvaluation();
  232. upt.setId(ve.getId());
  233. upt.setValue(value);
  234. valueEvaluationService.update(upt);
  235. }
  236. return res;
  237. }
  238. @RequestMapping(value = "/report/{id}", method = RequestMethod.GET)
  239. public Result report(@PathVariable String id, String noCache) {
  240. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  241. Result res = new Result();
  242. ValueEvaluation ve = valueEvaluationService.getMyEvaluation(Long.valueOf(id));
  243. if (ve == null || ve.getStep() < 7) {
  244. res.error(buildError(ErrorConstants.EVALUATE_ID));
  245. } else {
  246. if (noCache != null) {
  247. /*sysDictService.clear(GOVERNMENT_LOAN_ROR);*/
  248. }
  249. Map<String, Object> map = new HashMap<>();
  250. map.put("customer", TokenManager.getUserToken().getNickname());
  251. calcValue(ve, map);
  252. res.data(map);
  253. }
  254. return res;
  255. }
  256. private Long calcValue(ValueEvaluation ve, Map<String, Object> result) {
  257. JSONObject log = JSON.parseObject(ve.getLog());
  258. Step1 step1 = ((JSON) log.get(STEP1)).toJavaObject(Step1.class);
  259. Step2 step2 = ((JSON) log.get(STEP2)).toJavaObject(Step2.class);
  260. Step3 step3 = ((JSON) log.get(STEP3)).toJavaObject(Step3.class);
  261. Step5 step5 = ((JSON) log.get(STEP5)).toJavaObject(Step5.class);
  262. Step6 step6 = ((JSON) log.get(STEP6)).toJavaObject(Step6.class);
  263. Step7 step7 = ((JSON) log.get(STEP7)).toJavaObject(Step7.class);
  264. Integer timeLeft = step1.getTimeLeft();
  265. timeLeft = timeLeft < 3 ? 3 : timeLeft > 10 ? 10 : timeLeft; // 剩余经济寿命最低3,最大10年
  266. Map<Integer, IndustryCategory> subs = getIndustryCates(step1.getIndustry());
  267. Map<Integer, DistrictGlossory> provinces = getProvinces();
  268. result.put("subIndustries", Stream.of(step1.getSubIndustry().split(",")).map(it -> {
  269. return subs.get(Integer.valueOf(it)).getName();
  270. }).collect(Collectors.joining(",")));
  271. result.put("transferArea", Stream.of(step1.getTransferArea().split(",")).map(it -> {
  272. return it.equals("0") ? "中国全境" : provinces.get(Integer.valueOf(it)).getName();
  273. }).collect(Collectors.joining(",")));
  274. result.put("industry", industryCategoryService.list(0).stream().filter(it -> {
  275. return it.getId().equals(step1.getIndustry());
  276. }).findFirst().get().getName());
  277. BigDecimal industryAverageRate = getIndustryCategoryValue(step1, subs);
  278. List<Long> userForecast = step5.getForecastIncomes().stream().map(fc -> {
  279. return fc.getIncome();
  280. }).collect(Collectors.toList());
  281. List<Long> sysForecast = calcSysForecast(userForecast, industryAverageRate, timeLeft);
  282. calcUserForecast(userForecast, timeLeft);
  283. List<Long> measuredForecast = calcMeasuredForecast(userForecast, sysForecast);
  284. Map<String, Integer> profitScores = getProfitScores(step2, step3);
  285. Map<String, Integer> discountRates = getDiscountRates(step7);
  286. BigDecimal profitRate = calcProfitRate(profitScores, step1.getIndustry(), result);
  287. /*BigDecimal governmentLoanRoR = new BigDecimal(sysDictService.getValue(GOVERNMENT_LOAN_ROR),
  288. MathContext.DECIMAL32);*/
  289. BigDecimal governmentLoanRoR = new BigDecimal(0);
  290. BigDecimal taxRate = new BigDecimal(step6.getTaxRate());
  291. BigDecimal discountRate = calcDiscountRate(discountRates, taxRate, governmentLoanRoR);
  292. result.put("userForecast", userForecast);
  293. result.put("sysForecast", sysForecast);
  294. result.put("measuredForecast", measuredForecast);
  295. result.put("profitRate", profitRate);
  296. result.put("governmentLoanRoR", governmentLoanRoR);
  297. result.put("industryAverageRate", industryAverageRate);
  298. result.put("discountRate", discountRate);
  299. result.put("benchmarkDate", step1.getBenchmarkDate());
  300. result.put("timeLeft", timeLeft);
  301. result.put("name", step1.getName());
  302. Long value = calcTotal(measuredForecast, profitRate, discountRate, result);
  303. result.put("value", value);
  304. result.put("log", log);
  305. return value;
  306. }
  307. private Long calcTotal(List<Long> measuredForecast, BigDecimal profitRate, BigDecimal discountRate,
  308. Map<String, Object> result) {
  309. List<Long> profitList = new ArrayList<>();
  310. BigDecimal total = BigDecimal.ZERO;
  311. BigDecimal incomeDiscount = BigDecimal.ZERO;
  312. double discountTerm = 0.5;
  313. int index = 0;
  314. BigDecimal discountFactor = BigDecimal.ZERO;
  315. for (Long val : measuredForecast) {
  316. incomeDiscount = new BigDecimal(val).multiply(profitRate);
  317. profitList.add(incomeDiscount.longValue());
  318. discountFactor = BigDecimal.ONE.divide(BigDecimal.ONE.add(discountRate), 4, RoundingMode.FLOOR);
  319. discountTerm += index;
  320. double pow = Math.pow(discountFactor.doubleValue(), discountTerm);
  321. total = total.add(incomeDiscount.multiply(new BigDecimal(pow)));
  322. index++;
  323. }
  324. result.put("profitList", profitList); // 收入分成额
  325. return total.longValue();
  326. }
  327. private BigDecimal calcDiscountRate(Map<String, Integer> discountRates, BigDecimal taxRate,
  328. BigDecimal governmentLoanRoR) {
  329. BigDecimal discountRate = new BigDecimal(discountRates.get("capital"))
  330. .add(new BigDecimal(discountRates.get("management"))).add(new BigDecimal(discountRates.get("market")))
  331. .add(new BigDecimal(discountRates.get("political")))
  332. .add(new BigDecimal(discountRates.get("technical")));
  333. discountRate = discountRate.add(governmentLoanRoR).divide(HUNDRED.subtract(taxRate), 4, RoundingMode.FLOOR);
  334. double rate = discountRate.doubleValue();
  335. rate = rate > 0.3 ? 0.3 : rate < 0.17 ? 0.17 : rate;
  336. discountRate = new BigDecimal(rate, DEFAULT_PRECISION);
  337. return discountRate;
  338. }
  339. private Map<String, Integer> getDiscountRates(Step7 step7) {
  340. Map<String, Integer> profitScores = new HashMap<>();
  341. profitScores.put("capital", getDiscountScore(step7.getCapital()));// 资金风险
  342. profitScores.put("management", getDiscountScore(step7.getManagement()));// 管理风险
  343. profitScores.put("market", getDiscountScore(step7.getMarket()));// 市场风险
  344. profitScores.put("political", getDiscountScore(step7.getPolitical()));// 政策风险
  345. profitScores.put("technical", getDiscountScore(step7.getTechnical()));// 技术风险
  346. return profitScores;
  347. }
  348. private BigDecimal calcProfitRate(Map<String, Integer> profitScores, Integer industry, Map<String, Object> map) {
  349. BigDecimal legalFactor = new BigDecimal(0.2, DEFAULT_PRECISION);
  350. BigDecimal techFactor = new BigDecimal(0.6, DEFAULT_PRECISION);
  351. BigDecimal fundFactor = legalFactor;
  352. BigDecimal profitRate = new BigDecimal(profitScores.get("confidentiality")).multiply(legalFactor)
  353. .multiply(new BigDecimal(0.4, DEFAULT_PRECISION));
  354. profitRate = profitRate.add(new BigDecimal(profitScores.get("legalStatus")).multiply(legalFactor)
  355. .multiply(new BigDecimal(0.3, DEFAULT_PRECISION)));
  356. profitRate = profitRate.add(new BigDecimal(profitScores.get("decidability")).multiply(legalFactor)
  357. .multiply(new BigDecimal(0.3, DEFAULT_PRECISION)));
  358. profitRate = profitRate.add(new BigDecimal(profitScores.get("prospect")).multiply(techFactor)
  359. .multiply(new BigDecimal(0.1, DEFAULT_PRECISION)));
  360. profitRate = profitRate.add(new BigDecimal(profitScores.get("alternatives")).multiply(techFactor)
  361. .multiply(new BigDecimal(0.2, DEFAULT_PRECISION)));
  362. profitRate = profitRate.add(new BigDecimal(profitScores.get("progressiveness")).multiply(techFactor)
  363. .multiply(new BigDecimal(0.2, DEFAULT_PRECISION)));
  364. profitRate = profitRate.add(new BigDecimal(profitScores.get("innovativeness")).multiply(techFactor)
  365. .multiply(new BigDecimal(0.1, DEFAULT_PRECISION)));
  366. profitRate = profitRate.add(new BigDecimal(profitScores.get("ripeness")).multiply(techFactor)
  367. .multiply(new BigDecimal(0.2, DEFAULT_PRECISION)));
  368. profitRate = profitRate.add(new BigDecimal(profitScores.get("rangeOfApplication")).multiply(techFactor)
  369. .multiply(new BigDecimal(0.1, DEFAULT_PRECISION)));
  370. profitRate = profitRate.add(new BigDecimal(profitScores.get("defensive")).multiply(techFactor)
  371. .multiply(new BigDecimal(0.1, DEFAULT_PRECISION)));
  372. profitRate = profitRate.add(new BigDecimal(profitScores.get("supplyAndDemand")).multiply(fundFactor)
  373. .multiply(new BigDecimal(0.6, DEFAULT_PRECISION)));
  374. profitRate = profitRate.add(new BigDecimal(profitScores.get("profitability")).multiply(fundFactor)
  375. .multiply(new BigDecimal(0.4, DEFAULT_PRECISION)));
  376. BigDecimal minRate = MIN_RATE;
  377. BigDecimal maxRate = MAX_RATE;
  378. if (ProfitRate.containsType(industry)) {
  379. ProfitRate pr = ProfitRate.getProfitRate(industry);
  380. minRate = new BigDecimal(pr.getMinRate());
  381. maxRate = new BigDecimal(pr.getMaxRate());
  382. }
  383. map.put("minRate", minRate);
  384. map.put("maxRate", maxRate);
  385. profitRate = maxRate.subtract(minRate).multiply(profitRate.multiply(PERCENTAGE)).add(minRate)
  386. .multiply(PERCENTAGE).setScale(4, RoundingMode.FLOOR);
  387. return profitRate;
  388. }
  389. private Map<String, Integer> getProfitScores(Step2 step2, Step3 step3) {
  390. // 法律因素权重 20%
  391. Map<String, Integer> profitScores = new HashMap<>();
  392. profitScores.put("legalStatus", getScore(step2.getLegalStatus()));// 法律状态
  393. profitScores.put("confidentiality", getScore(step2.getConfidentiality()));// 保密性
  394. profitScores.put("decidability", getScore(step2.getDecidability()));// 侵权可判定性
  395. // 技术因素权重 60%
  396. profitScores.put("prospect", getScore(step3.getProspect()));// 领域前景
  397. profitScores.put("alternatives", getScore(step3.getAlternatives()));// 可替代性
  398. profitScores.put("progressiveness", getScore(step3.getProgressiveness()));// 先进性
  399. profitScores.put("innovativeness", getScore(step3.getInnovativeness()));// 创新性
  400. profitScores.put("ripeness", getScore(step3.getRipeness()));// 成熟度
  401. profitScores.put("rangeOfApplication", getScore(step3.getRangeOfApplication()));// 应用范围
  402. profitScores.put("defensive", getScore(step3.getDefensive()));// 应用范围
  403. // 经济因素 20%
  404. profitScores.put("supplyAndDemand", getScore(step3.getSupplyAndDemand()));// 供求关系
  405. profitScores.put("profitability", getScore(step3.getProfitability()));// 独立获利能力
  406. return profitScores;
  407. }
  408. private Integer getScore(int option) {
  409. int idx = option - 1;
  410. idx = idx < 0 ? 0 : idx >= SCORES.length ? SCORES.length - 1 : idx;
  411. return SCORES[idx];
  412. }
  413. private Integer getDiscountScore(int option) {
  414. int idx = option - 1;
  415. idx = idx < 0 ? 0 : idx >= DISCOUNT_SCORES.length ? DISCOUNT_SCORES.length - 1 : idx;
  416. return DISCOUNT_SCORES[idx];
  417. }
  418. private void calcUserForecast(List<Long> userForecast, Integer timeLeft) {
  419. Assert.isTrue(userForecast.get(0) > 0 && userForecast.get(1) > 0, ErrorConstants.EVALUATE_PARAM);
  420. BigDecimal second = new BigDecimal(userForecast.get(1));
  421. BigDecimal lastYear = new BigDecimal(userForecast.get(2));
  422. BigDecimal rate = lastYear.divide(second, 2, RoundingMode.HALF_UP)
  423. .subtract(BigDecimal.ONE).add(second
  424. .divide(new BigDecimal(userForecast.get(0)), 2, RoundingMode.HALF_UP).subtract(BigDecimal.ONE))
  425. .divide(new BigDecimal(2));
  426. BigDecimal step = new BigDecimal(0.1, DEFAULT_PRECISION);
  427. int size = userForecast.size();
  428. for (int i = size; i < timeLeft; i++) {
  429. if (i < 6) {
  430. lastYear = lastYear.multiply(
  431. BigDecimal.ONE.add(rate.multiply(rate.subtract(step.multiply(new BigDecimal(i - size))))));
  432. }
  433. userForecast.add(lastYear.longValue());
  434. }
  435. }
  436. private List<Long> calcSysForecast(List<Long> userForecast, BigDecimal industryAverageRate, Integer timeLeft) {
  437. List<Long> sysForecast = new ArrayList<>();
  438. BigDecimal lastYear = BigDecimal.ZERO;
  439. BigDecimal step = new BigDecimal(0.1, DEFAULT_PRECISION);
  440. industryAverageRate = industryAverageRate.multiply(PERCENTAGE);
  441. for (int i = 0; i < timeLeft; i++) {
  442. if (i == 0) {
  443. lastYear = new BigDecimal(userForecast.get(0)).multiply(new BigDecimal(0.9, DEFAULT_PRECISION));
  444. } else if (i < 6) {
  445. lastYear = lastYear.multiply(BigDecimal.ONE.add(
  446. industryAverageRate.multiply(BigDecimal.ONE.subtract(step.multiply(new BigDecimal(i - 1))))));
  447. }
  448. sysForecast.add(lastYear.longValue());
  449. }
  450. return sysForecast;
  451. }
  452. private List<Long> calcMeasuredForecast(List<Long> userForecast, List<Long> sysForecast) {
  453. List<Long> measuredForecast = new ArrayList<>();
  454. for (int i = 0; i < sysForecast.size(); i++) {
  455. measuredForecast.add((userForecast.get(i) + sysForecast.get(i)) >> 1);
  456. }
  457. return measuredForecast;
  458. }
  459. private void updateSteps(Object step, Integer nextStep, String key, Result res, String id) {
  460. ValueEvaluation ve = valueEvaluationService.getMyEvaluation(Long.valueOf(id));
  461. Assert.notNull(ve, ErrorConstants.EVALUATE_ID);
  462. ve.setStep(Math.max(nextStep, ve.getStep()));
  463. if (nextStep == 1) {
  464. ve.setName(((Step1) step).getName());
  465. }
  466. ve.setValue(0l);
  467. JSONObject jo = JSON.parseObject(ve.getLog());
  468. jo.put(key, step);
  469. ve.setLog(jo.toJSONString());
  470. res.data(valueEvaluationService.update(ve));
  471. }
  472. private boolean handleBindingError(Result res, BindingResult bindingResult) {
  473. if (bindingResult.hasErrors()) {
  474. LoggerUtils.debug(logger, "参数错误:[%s], [%s], [%s]", bindingResult.getFieldError().getDefaultMessage(),
  475. bindingResult.getFieldError().getField(), bindingResult.getFieldError().getRejectedValue());
  476. res.getError().add(buildError(ErrorConstants.EVALUATE_PARAM));
  477. return true;
  478. }
  479. return false;
  480. }
  481. private BigDecimal getIndustryCategoryValue(Step1 step1) {
  482. return getIndustryCategoryValue(step1, getIndustryCates(step1.getIndustry()));
  483. }
  484. private Map<Integer, IndustryCategory> getIndustryCates(Integer pid) {
  485. return industryCategoryService.list(pid).stream()
  486. .collect(Collectors.toMap(IndustryCategory::getId, (it) -> it));
  487. }
  488. private Map<Integer, DistrictGlossory> getProvinces() {
  489. return districtGlossoryService.list(0).stream().collect(Collectors.toMap(DistrictGlossory::getId, (it) -> it));
  490. }
  491. private BigDecimal getIndustryCategoryValue(Step1 step1, Map<Integer, IndustryCategory> cates) {
  492. List<Integer> subIds = Stream.of(step1.getSubIndustry().split(",")).map(it -> {
  493. return Integer.valueOf(it);
  494. }).collect(Collectors.toList());
  495. BigDecimal average = BigDecimal.ZERO;
  496. int count = 0;
  497. for (Integer id : subIds) {
  498. if (cates.containsKey(id)) {
  499. average = average.add(cates.get(id).getValue());
  500. count++;
  501. }
  502. }
  503. return average.divide(new BigDecimal(count == 0 ? 1 : count), 2, RoundingMode.HALF_UP);
  504. }
  505. @SuppressWarnings("unlikely-arg-type")
  506. private Long[] calcForecastIncome(List<YearIncome> incomes, BigDecimal growth) {
  507. BigDecimal useGrowth = growth.multiply(PERCENTAGE);
  508. BigDecimal base = new BigDecimal(incomes.get(0).getIncome());
  509. if (!incomes.get(1).getIncome().equals(0)) {
  510. useGrowth = base.divide(new BigDecimal(incomes.get(1).getIncome()), 2, RoundingMode.CEILING)
  511. .subtract(BigDecimal.ONE);
  512. }
  513. Long[] res = new Long[3];
  514. BigDecimal one = base.multiply(useGrowth.add(BigDecimal.ONE));
  515. useGrowth = useGrowth.multiply(new BigDecimal(0.9, DEFAULT_PRECISION));
  516. BigDecimal two = one.multiply(useGrowth.add(BigDecimal.ONE));
  517. useGrowth = useGrowth.multiply(new BigDecimal(0.8, DEFAULT_PRECISION));
  518. res[2] = two.multiply(useGrowth.add(BigDecimal.ONE)).longValue();
  519. res[0] = one.longValue();
  520. res[1] = two.longValue();
  521. return res;
  522. }
  523. }