EvaluationController.java 24 KB

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