EvaluationController.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.List;
  7. import javax.validation.Valid;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.util.Assert;
  13. import org.springframework.validation.BindingResult;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.alibaba.fastjson.JSON;
  19. import com.alibaba.fastjson.JSONObject;
  20. import com.goafanti.common.bo.Result;
  21. import com.goafanti.common.constant.ErrorConstants;
  22. import com.goafanti.common.controller.BaseApiController;
  23. import com.goafanti.common.model.IndustryCategory;
  24. import com.goafanti.common.model.ValueEvaluation;
  25. import com.goafanti.common.service.IndustryCategoryService;
  26. import com.goafanti.common.utils.LoggerUtils;
  27. import com.goafanti.core.shiro.token.TokenManager;
  28. import com.goafanti.evaluation.bo.ForecastIncome;
  29. import com.goafanti.evaluation.bo.Step1;
  30. import com.goafanti.evaluation.bo.Step2;
  31. import com.goafanti.evaluation.bo.Step3;
  32. import com.goafanti.evaluation.bo.Step4;
  33. import com.goafanti.evaluation.bo.Step5;
  34. import com.goafanti.evaluation.bo.Step6;
  35. import com.goafanti.evaluation.bo.Step7;
  36. import com.goafanti.evaluation.bo.YearIncome;
  37. import com.goafanti.evaluation.service.ValueEvaluationService;
  38. @RestController
  39. @RequestMapping(value = "/api/user/evaluate")
  40. public class EvaluationController extends BaseApiController {
  41. private static final Logger logger = LoggerFactory.getLogger(EvaluationController.class);
  42. private static final MathContext DEFAULT_PRECISION = new MathContext(3, RoundingMode.HALF_UP);
  43. @Autowired
  44. ValueEvaluationService valueEvaluationService;
  45. @Autowired
  46. private IndustryCategoryService industryCategoryService;
  47. @RequestMapping(value = "/create", method = RequestMethod.GET)
  48. public Result create() {
  49. ValueEvaluation ve = new ValueEvaluation();
  50. ve.setUid(TokenManager.getUserId());
  51. ve.setStep(0);
  52. ve.setLog("{}");
  53. valueEvaluationService.insert(ve);
  54. return new Result(ve.getId().toString());
  55. }
  56. @RequestMapping(value = "/list", method = RequestMethod.GET)
  57. public Result list(String pageNo, String pageSize) {
  58. return new Result().data(valueEvaluationService.list(handlePageNo(pageNo), handlePageSize(pageSize)));
  59. }
  60. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
  61. public String info(@PathVariable String id) {
  62. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  63. return valueEvaluationService.getMyEvaluationSteps(Long.valueOf(id));
  64. }
  65. @RequestMapping(value = "/remove", method = RequestMethod.POST)
  66. public Result remove(String ids) {
  67. Assert.isTrue(StringUtils.isNotBlank(ids), ErrorConstants.EVALUATE_PARAM);
  68. Result res = new Result();
  69. List<Long> idList = new ArrayList<>();
  70. String[] idArr = ids.split(",");
  71. for (String id : idArr) {
  72. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_PARAM);
  73. idList.add(Long.valueOf(id));
  74. }
  75. res.data(valueEvaluationService.deleteMySteps(idList));
  76. return res;
  77. }
  78. @RequestMapping(value = "/step1", method = RequestMethod.POST)
  79. public Result step1(String id, @Valid Step1 data, BindingResult bindingResult) {
  80. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  81. Result res = new Result();
  82. if (handleBindingError(res, bindingResult)) {
  83. return res;
  84. }
  85. ValueEvaluation ve = valueEvaluationService.getEvaluation(Long.valueOf(id));
  86. Assert.notNull(ve, ErrorConstants.EVALUATE_ID);
  87. ve.setName(data.getName());
  88. ve.setStep(1);
  89. JSONObject jo = JSON.parseObject(ve.getLog());
  90. jo.put("0", data);
  91. ve.setLog(jo.toJSONString());
  92. res.data(valueEvaluationService.update(ve));
  93. return res;
  94. }
  95. @RequestMapping(value = "/step2", method = RequestMethod.POST)
  96. public Result step2(String id, @Valid Step2 data, BindingResult bindingResult) {
  97. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  98. Result res = new Result();
  99. if (handleBindingError(res, bindingResult)) {
  100. return res;
  101. }
  102. updateSteps(data, 2, "1", res, id);
  103. return res;
  104. }
  105. @RequestMapping(value = "/step3", method = RequestMethod.POST)
  106. public Result step3(String id, @Valid Step3 data, BindingResult bindingResult) {
  107. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  108. Result res = new Result();
  109. if (handleBindingError(res, bindingResult)) {
  110. return res;
  111. }
  112. updateSteps(data, 3, "2", res, id);
  113. return res;
  114. }
  115. @RequestMapping(value = "/step4", method = RequestMethod.POST)
  116. public Result step4(String id, String hasIncome, String incomes) {
  117. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  118. Result res = new Result();
  119. Step4 step = new Step4();
  120. try {
  121. step.setHasIncome(Integer.valueOf(hasIncome));
  122. if (step.getHasIncome().equals(2)) {
  123. List<YearIncome> ja = JSON.parseArray(incomes, YearIncome.class);
  124. if (ja.size() != 3) {
  125. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  126. } else {
  127. step.setIncomes(ja);
  128. }
  129. }
  130. } catch (Exception e) {
  131. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  132. }
  133. if (res.getError().isEmpty()) {
  134. updateSteps(step, 4, "3", res, id);
  135. }
  136. return res;
  137. }
  138. @RequestMapping(value = "/step5", method = RequestMethod.POST)
  139. public Result step5(String id, String type, String forecastIncomes) {
  140. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  141. Result res = new Result();
  142. Step5 step = new Step5();
  143. try {
  144. step.setType(Integer.valueOf(type));
  145. List<ForecastIncome> ja = JSON.parseArray(forecastIncomes, ForecastIncome.class);
  146. if (ja.size() != 3) {
  147. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  148. } else {
  149. step.setForecastIncomes(ja);
  150. }
  151. } catch (Exception e) {
  152. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  153. }
  154. if (res.getError().isEmpty()) {
  155. updateSteps(step, 5, "4", res, id);
  156. }
  157. return res;
  158. }
  159. @RequestMapping(value = "/step5/{id}", method = RequestMethod.GET)
  160. public Result step5Info(@PathVariable String id) {
  161. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  162. Result res = new Result();
  163. ValueEvaluation ve = valueEvaluationService.getEvaluation(Long.valueOf(id));
  164. try {
  165. JSONObject jo = JSON.parseObject(ve.getLog());
  166. Step4 step4 = ((JSON) jo.get("3")).toJavaObject(Step4.class);
  167. if (step4.getHasIncome().equals(2)) {
  168. Step1 step1 = ((JSON) jo.get("0")).toJavaObject(Step1.class);
  169. res.data(getPredicateIncome(step4.getIncomes(), getIndustryCategoryValue(step1)));
  170. }
  171. } catch (Exception e) {
  172. res.error(buildError(ErrorConstants.EVALUATE_PARAM));
  173. }
  174. return res;
  175. }
  176. @RequestMapping(value = "/step6", method = RequestMethod.POST)
  177. public Result step6(String id, @Valid Step6 data, BindingResult bindingResult) {
  178. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  179. Result res = new Result();
  180. if (handleBindingError(res, bindingResult)) {
  181. return res;
  182. }
  183. updateSteps(data, 6, "5", res, id);
  184. return res;
  185. }
  186. @RequestMapping(value = "/step7", method = RequestMethod.POST)
  187. public Result step7(String id, @Valid Step7 data, BindingResult bindingResult) {
  188. Assert.isTrue(StringUtils.isNumeric(id), ErrorConstants.EVALUATE_ID);
  189. Result res = new Result();
  190. if (handleBindingError(res, bindingResult)) {
  191. return res;
  192. }
  193. updateSteps(data, 7, "6", res, id);
  194. return res;
  195. }
  196. private void updateSteps(Object step, Integer nextStep, String key, Result res, String id) {
  197. ValueEvaluation ve = valueEvaluationService.getEvaluation(Long.valueOf(id));
  198. Assert.notNull(ve, ErrorConstants.EVALUATE_ID);
  199. ve.setStep(Math.max(nextStep, ve.getStep()));
  200. JSONObject jo = JSON.parseObject(ve.getLog());
  201. jo.put(key, step);
  202. ve.setLog(jo.toJSONString());
  203. res.data(valueEvaluationService.update(ve));
  204. }
  205. private boolean handleBindingError(Result res, BindingResult bindingResult) {
  206. if (bindingResult.hasErrors()) {
  207. LoggerUtils.debug(logger, "参数错误:[%s], [%s], [%s]", bindingResult.getFieldError().getDefaultMessage(),
  208. bindingResult.getFieldError().getField(), bindingResult.getFieldError().getRejectedValue());
  209. res.getError().add(buildError(ErrorConstants.EVALUATE_PARAM));
  210. return true;
  211. }
  212. return false;
  213. }
  214. private BigDecimal getIndustryCategoryValue(Step1 step1) {
  215. List<IndustryCategory> cates = industryCategoryService.list(step1.getIndustry());
  216. String[] subs = step1.getSubIndustry().split(",");
  217. Integer[] subIds = new Integer[subs.length];
  218. for (int i = 0; i < subs.length; i++) {
  219. subIds[i] = Integer.valueOf(subs[i]);
  220. }
  221. BigDecimal average = BigDecimal.ZERO;
  222. for (IndustryCategory ic : cates) {
  223. for (int i = 0; i < subIds.length; i++) {
  224. if (ic.getId().equals(subIds[i])) {
  225. average = average.add(ic.getValue());
  226. }
  227. }
  228. }
  229. return average.divide(new BigDecimal(3), 2, RoundingMode.HALF_UP);
  230. }
  231. private Long[] getPredicateIncome(List<YearIncome> incomes, BigDecimal growth) {
  232. BigDecimal useGrowth = growth.divide(new BigDecimal(100), 2, RoundingMode.CEILING);
  233. BigDecimal base = new BigDecimal(incomes.get(0).getIncome());
  234. if (!incomes.get(1).getIncome().equals(0)) {
  235. useGrowth = base.divide(new BigDecimal(incomes.get(1).getIncome()), 2, RoundingMode.CEILING)
  236. .subtract(BigDecimal.ONE);
  237. }
  238. Long[] res = new Long[3];
  239. BigDecimal one = base.multiply(useGrowth.add(BigDecimal.ONE));
  240. useGrowth = useGrowth.multiply(new BigDecimal(0.9, DEFAULT_PRECISION));
  241. BigDecimal two = one.multiply(useGrowth.add(BigDecimal.ONE));
  242. useGrowth = useGrowth.multiply(new BigDecimal(0.8, DEFAULT_PRECISION));
  243. res[2] = two.multiply(useGrowth.add(BigDecimal.ONE)).longValue();
  244. res[0] = one.longValue();
  245. res[1] = two.longValue();
  246. return res;
  247. }
  248. }