StudentOrderController.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. package com.ym.mec.student.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ym.mec.biz.dal.dao.*;
  5. import com.ym.mec.biz.dal.dto.*;
  6. import com.ym.mec.biz.dal.entity.*;
  7. import com.ym.mec.biz.dal.enums.DealStatusEnum;
  8. import com.ym.mec.biz.dal.enums.GroupType;
  9. import com.ym.mec.biz.dal.enums.OrderTypeEnum;
  10. import com.ym.mec.biz.service.*;
  11. import com.ym.mec.common.controller.BaseController;
  12. import com.ym.mec.common.entity.HttpResponseResult;
  13. import com.ym.mec.thirdparty.adapay.ConfigInit;
  14. import com.ym.mec.thirdparty.adapay.Payment;
  15. import com.ym.mec.thirdparty.yqpay.Msg;
  16. import com.ym.mec.thirdparty.yqpay.RsqMsg;
  17. import com.ym.mec.thirdparty.yqpay.YqPayFeignService;
  18. import com.ym.mec.thirdparty.yqpay.YqPayUtil;
  19. import com.ym.mec.util.date.DateUtil;
  20. import com.ym.mec.util.http.HttpUtil;
  21. import io.swagger.annotations.Api;
  22. import io.swagger.annotations.ApiImplicitParam;
  23. import io.swagger.annotations.ApiImplicitParams;
  24. import io.swagger.annotations.ApiOperation;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import org.springframework.scheduling.annotation.EnableScheduling;
  31. import org.springframework.util.DigestUtils;
  32. import org.springframework.web.bind.annotation.*;
  33. import javax.servlet.http.HttpServletResponse;
  34. import java.io.IOException;
  35. import java.math.BigDecimal;
  36. import java.net.URLEncoder;
  37. import java.text.SimpleDateFormat;
  38. import java.util.*;
  39. import java.util.stream.Collectors;
  40. @RequestMapping("studentOrder")
  41. @Api(tags = "订单回调")
  42. @RestController
  43. @EnableScheduling
  44. public class StudentOrderController extends BaseController {
  45. private static final Logger logger = LoggerFactory.getLogger(StudentOrderController.class);
  46. @Autowired
  47. private StudentPaymentOrderService studentPaymentOrderService;
  48. @Autowired
  49. private VipGroupService vipGroupService;
  50. @Autowired
  51. private MusicGroupService musicGroupService;
  52. @Autowired
  53. private StudentPaymentOrderDetailService studentPaymentOrderDetailService;
  54. @Autowired
  55. private YqPayFeignService yqPayFeignService;
  56. @Autowired
  57. private StudentPaymentOrderDao studentPaymentOrderDao;
  58. @Autowired
  59. private SysConfigDao sysConfigDao;
  60. @Autowired
  61. private SporadicChargeInfoService sporadicChargeInfoService;
  62. @Autowired
  63. private OrganizationDao organizationDao;
  64. @Autowired
  65. private CourseScheduleEvaluateDao courseScheduleEvaluateDao;
  66. @Autowired
  67. private TenantPaymentOrderService tenantPaymentOrderService;
  68. @Autowired
  69. private StudentPaymentRouteOrderDao studentPaymentRouteOrderDao;
  70. @Value("${spring.profiles.active:dev}")
  71. private String profiles;
  72. @PostMapping("/notify")
  73. public Msg notify(@ModelAttribute Msg msg) throws Exception {
  74. logger.info(msg.toString());
  75. Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
  76. rqMap.put("code", msg.getCode());
  77. rqMap.put("msg", msg.getMsg());
  78. rqMap.put("responseType", msg.getResponseType());
  79. rqMap.put("responseParameters", msg.getResponseParameters());
  80. rqMap.put("sign", msg.getSign());
  81. //boolean rs = YqPayUtil.verify(rqMap);
  82. msg.setMsg("fail");
  83. Map<String, String> notifyMap = new HashMap<>();
  84. //if (rs) {
  85. notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
  86. //}
  87. //支付中订单存在,更新状态
  88. if (msg.getResponseType().equals("1") && notifyMap.size() > 0) {
  89. String tradeState = msg.getCode().equals("88") ? "1" : "0";
  90. String channelType = notifyMap.get("channelType").equals("1") ? "WXPay" : (notifyMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
  91. notifyMap.put("tradeState", tradeState);
  92. notifyMap.put("totalMoney", notifyMap.get("payAmount"));
  93. notifyMap.put("merOrderNo", notifyMap.get("merMerOrderNo"));
  94. notifyMap.put("channelType", channelType);
  95. studentPaymentOrderService.updateOrder(notifyMap);
  96. msg.setCode("000000");
  97. msg.setMsg("success");
  98. }
  99. return msg;
  100. }
  101. @ApiOperation(value = "查询订单状态")
  102. @PostMapping("/checkOrderStatus")
  103. public Object checkOrderStatus(String orderNo) {
  104. if (StringUtils.isBlank(orderNo)) {
  105. return failed("请指定订单");
  106. }
  107. StudentPaymentOrder orderByOrderNo = studentPaymentOrderService.findOrderByOrderNo(orderNo);
  108. if (Objects.isNull(orderByOrderNo)) {
  109. return failed("未找到指定订单");
  110. }
  111. HashMap<String, Object> orderDetail = new HashMap<>();
  112. orderDetail.put("order", orderByOrderNo);
  113. orderDetail.put("groupType", orderByOrderNo.getGroupType());
  114. if (orderByOrderNo.getGroupType().equals(GroupType.MUSIC)) {
  115. MusicGroup musicGroup = musicGroupService.get(orderByOrderNo.getMusicGroupId());
  116. List<Goods> goodsList = studentPaymentOrderDetailService.findApplyOrderGoods(orderByOrderNo.getId());
  117. List<String> orderDetailType = studentPaymentOrderDetailService.getOrderDetailType(orderByOrderNo.getId());
  118. orderDetail.put("goods", goodsList);
  119. orderDetail.put("course", musicGroup.getCourseForm());
  120. orderDetail.put("ownershipType", musicGroup.getOwnershipType());
  121. orderDetail.put("detailType",orderDetailType);
  122. } else if (orderByOrderNo.getGroupType().equals(GroupType.VIP)) {
  123. VipBuyResultDto vipBuyResultInfo = vipGroupService.findVipBuyResultInfo(Integer.valueOf(orderByOrderNo.getMusicGroupId()));
  124. orderDetail.put("detail", vipBuyResultInfo);
  125. } else if (orderByOrderNo.getGroupType().equals(GroupType.SPORADIC)) {
  126. SporadicChargeInfo info = sporadicChargeInfoService.get(Integer.valueOf(orderByOrderNo.getMusicGroupId()));
  127. orderDetail.put("detail", info);
  128. }
  129. return succeed(orderDetail);
  130. }
  131. @ApiOperation(value = "台牌支付")
  132. @PostMapping("/executePayment")
  133. @ApiImplicitParams({
  134. @ApiImplicitParam(name = "amount", value = "支付金额", required = true, dataType = "BigDecimal"),
  135. @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String"),
  136. @ApiImplicitParam(name = "payChannel", value = "支付方式(alipay-支付宝app支付)", required = true, dataType = "String"),
  137. })
  138. public Object executePayment(BigDecimal amount, String orderNo, String payChannel, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sign, String code, String platform) throws Exception {
  139. Map<String, Object> signParams = new LinkedHashMap<>();
  140. signParams.put("appId", ConfigInit.appId);
  141. signParams.put("amount", amount.setScale(2,BigDecimal.ROUND_HALF_UP));
  142. signParams.put("orderNo", orderNo);
  143. // signParams.put("notifyUrl", notifyUrl);
  144. // signParams.put("returnUrl", returnUrl);
  145. signParams.put("orderSubject", orderSubject);
  146. signParams.put("orderBody", orderBody);
  147. signParams.put("wxAppId", ConfigInit.wxAppId);
  148. String originalStr = JSONObject.toJSONString(signParams);
  149. String mewSign = DigestUtils.md5DigestAsHex(originalStr.getBytes());
  150. if (!mewSign.equals(sign)) {
  151. return failed("请勿非法请求");
  152. }
  153. String openId = "";
  154. if (payChannel.equals("wx_pub")) {
  155. if (code == null || code.isEmpty()) {
  156. return failed("微信支付请先授权");
  157. }
  158. String wxMpOAuth2AccessTokenUrl = String.format(ConfigInit.wxMpOAuth2AccessTokenUrl, ConfigInit.wxAppId, ConfigInit.wxAppSecret, code);
  159. Map<String, String> weChatRes = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
  160. if (!weChatRes.containsKey("openid")) {
  161. return failed("授权失败,请重新授权");
  162. }
  163. openId = weChatRes.get("openid");
  164. }
  165. Date createTime = null;
  166. TenantPaymentOrder tenantPaymentOrder = null;
  167. StudentPaymentOrder studentPaymentOrder = null;
  168. if (platform != null && platform.equals("teacher")) {
  169. tenantPaymentOrder = tenantPaymentOrderService.queryByOrderNo(orderNo);
  170. } else {
  171. studentPaymentOrder = studentPaymentOrderService.findOrderByOrderNo(orderNo);
  172. }
  173. if (studentPaymentOrder == null && tenantPaymentOrder == null) {
  174. return failed("订单不存在");
  175. }
  176. if (studentPaymentOrder != null) {
  177. createTime = studentPaymentOrder.getCreateTime();
  178. } else {
  179. createTime = tenantPaymentOrder.getCreateTime();
  180. }
  181. Calendar beforeTime = Calendar.getInstance();
  182. beforeTime.add(Calendar.MINUTE, -28);// 28 分钟之前的时间
  183. Date beforeDate = beforeTime.getTime();
  184. if (createTime.before(beforeDate)) {
  185. return failed("订单已超时,请重新下单");
  186. }
  187. Date expireDate = DateUtil.addMinutes(createTime, 30);
  188. String timeExpire = new SimpleDateFormat("yyyyMMddHHmmss").format(expireDate);
  189. Map<String, Object> paymentParams = new HashMap<>();
  190. paymentParams.put("app_id", ConfigInit.appId);
  191. paymentParams.put("order_no", orderNo);
  192. paymentParams.put("pay_channel", payChannel);
  193. paymentParams.put("pay_amt", amount.setScale(2,BigDecimal.ROUND_HALF_UP));
  194. paymentParams.put("goods_title", orderSubject);
  195. paymentParams.put("goods_desc", orderBody);
  196. paymentParams.put("time_expire", timeExpire);
  197. List<StudentPaymentRouteOrder> routeOrders = studentPaymentRouteOrderDao.getRouteOrders(orderNo);
  198. List<Map<String, Object>> divMembers = new ArrayList<>();
  199. for (StudentPaymentRouteOrder routeOrder : routeOrders) {
  200. Map<String, Object> divMember = new HashMap<>();
  201. divMember.put("member_id", routeOrder.getMerNo());//分佣账户
  202. divMember.put("amount", routeOrder.getRouteAmount().setScale(2, BigDecimal.ROUND_HALF_UP));//分佣金额
  203. divMember.put("fee_flag", routeOrder.getFeeFlag());
  204. if (routeOrder.getMerNo().equals(ConfigInit.merNo)) {
  205. divMember.put("member_id", 0);
  206. }
  207. divMembers.add(divMember);
  208. }
  209. if (divMembers.size() > 0) {
  210. paymentParams.put("div_members", JSON.toJSONString(divMembers));
  211. }
  212. Map<String, Object> expendParams = new HashMap<>(5);
  213. expendParams.put("open_id", openId);
  214. expendParams.put("is_raw", "1");
  215. expendParams.put("callback_url", returnUrl);
  216. expendParams.put("limit_pay", "1");
  217. paymentParams.put("expend", expendParams);
  218. Map<String, Object> payment = Payment.executePayment(paymentParams);
  219. if (studentPaymentOrder != null) {
  220. studentPaymentOrder.setTransNo((String) payment.get("id"));
  221. studentPaymentOrderService.update(studentPaymentOrder);
  222. } else {
  223. tenantPaymentOrder.setTransNo((String) payment.get("id"));
  224. tenantPaymentOrderService.update(tenantPaymentOrder);
  225. }
  226. return succeed(payment);
  227. }
  228. @GetMapping("/authorize")
  229. public String authorize(@RequestParam("returnUrl") String returnUrl) {
  230. String appId = "wxcf8e8b33a9477845";
  231. String url = URLEncoder.encode("http://wxwechat.utools.club/studentOrder/userInfo");
  232. String redirectURL = String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect",
  233. appId, url);
  234. return "redirect:" + redirectURL;
  235. }
  236. @GetMapping("/userInfo")
  237. public Object userInfo(@RequestParam("code") String code,
  238. @RequestParam("state") String returnUrl) throws Exception {
  239. String appId = "wxcf8e8b33a9477845";
  240. String appSecret = "1286452b9c68b13325dece7cdf892645";
  241. String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
  242. wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
  243. Map<String, String> map = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
  244. if (!map.containsKey("openid")) {
  245. return failed("授权失败,请重新授权");
  246. }
  247. return map.get("openid");
  248. }
  249. // @Scheduled(cron = "0/30 * * * * ?")
  250. @GetMapping("/setSuccessStatus")
  251. public HttpResponseResult setSuccessStatus() throws Exception {
  252. if (!profiles.equals("dev")) {
  253. return failed("非法操作");
  254. }
  255. List<StudentPaymentOrder> payingOrders = studentPaymentOrderService.findOrdersByStatus(DealStatusEnum.ING, "ADAPAY");
  256. String[] statusArr = {"0", "1", "7"};
  257. for (StudentPaymentOrder payingOrder : payingOrders) {
  258. Map<String, String> rpMap = new HashMap<>();
  259. rpMap.put("tradeState", "1");
  260. rpMap.put("remarks", "模拟支付成功");
  261. rpMap.put("merOrderNo", payingOrder.getOrderNo());
  262. rpMap.put("orderNo", payingOrder.getOrderNo());
  263. rpMap.put("channelType", "1");
  264. String channelType = rpMap.get("channelType").equals("1") ? "WXPay" : (rpMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
  265. rpMap.put("channelType", channelType);
  266. if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
  267. try {
  268. studentPaymentOrderService.updateOrder(rpMap); //更新订单
  269. } catch (Exception e) {
  270. e.printStackTrace();
  271. continue;
  272. }
  273. }
  274. }
  275. return succeed();
  276. }
  277. @GetMapping("/getOrderStatus")
  278. private HttpResponseResult getOrderStatus(String orderNo) throws Exception {
  279. if (orderNo == null || orderNo.isEmpty()) {
  280. return failed("订单号必须填");
  281. }
  282. String notifyUrl = ""; //回调地址
  283. Map<String, Object> resultMap = new LinkedHashMap<>();
  284. resultMap.put("merOrderNoList", orderNo);
  285. Map<String, Object> requestMap = YqPayUtil.getRequestMap(notifyUrl, resultMap);
  286. RsqMsg rsqMsg = new RsqMsg(requestMap);
  287. Msg queryRs = yqPayFeignService.orderQuery(rsqMsg);
  288. if (queryRs.getCode().equals("88")) {
  289. String responseParameters = queryRs.getResponseParameters();
  290. List<Map<String, Object>> responseList = JSON.parseObject(responseParameters, List.class);
  291. return succeed(responseList);
  292. }
  293. return succeed("订单不存在");
  294. }
  295. @RequestMapping("paymentResult")
  296. public void paymentResult(HttpServletResponse response, String orderNo, String type) {
  297. try {
  298. String baseApiUrl = sysConfigDao.findConfigValue(SysConfigService.BASE_API_URL);
  299. if (type != null && type.equals("edu")) {
  300. baseApiUrl = sysConfigDao.findConfigValue(SysConfigService.EDU_TEACHER_BASE_URL);
  301. }
  302. response.sendRedirect(baseApiUrl + "/#/paymentresult?orderNo=" + orderNo);
  303. } catch (IOException e) {
  304. e.printStackTrace();
  305. }
  306. }
  307. @GetMapping("/getLuckStatis")
  308. public HttpResponseResult getLuckStatis() {
  309. List<Organization> organs = organizationDao.findAllOrgans();
  310. List<OrderStatisDto> orders = studentPaymentOrderDao.getLuckStatis();
  311. for (Organization organ : organs) {
  312. boolean flag = false;
  313. for (OrderStatisDto order : orders) {
  314. if (organ.getName().equals(order.getOrganName())) {
  315. flag = true;
  316. break;
  317. }
  318. }
  319. if (!flag) {
  320. OrderStatisDto orderStatisDto = new OrderStatisDto();
  321. orderStatisDto.setOrganName(organ.getName());
  322. orderStatisDto.setMoney(BigDecimal.ZERO);
  323. orderStatisDto.setNums(0);
  324. orders.add(orderStatisDto);
  325. }
  326. }
  327. BigDecimal totalMoney = BigDecimal.ZERO;
  328. Integer totalNum = 0;
  329. Iterator<OrderStatisDto> iterator = orders.iterator();
  330. while (iterator.hasNext()) {
  331. OrderStatisDto order = iterator.next();
  332. if (order.getOrganName().equals("总部国际") || order.getOrganName().equals("阳光总部国际") || order.getOrganName().equals("武汉小学")) {
  333. iterator.remove();
  334. continue;
  335. }
  336. totalMoney = totalMoney.add(order.getMoney());
  337. totalNum += order.getNums();
  338. }
  339. orders = orders.stream().sorted(Comparator.comparing(OrderStatisDto::getNums).reversed()).collect(Collectors.toList());
  340. LuckStatisDto luckStatisDto = new LuckStatisDto();
  341. luckStatisDto.setOrderStatisDtoList(orders);
  342. luckStatisDto.setTotalNum(totalNum);
  343. luckStatisDto.setTotalMoney(totalMoney);
  344. return succeed(luckStatisDto);
  345. }
  346. @GetMapping("/getPracticeGroupStatis")
  347. public HttpResponseResult getPracticeGroupStatis() {
  348. List<Organization> organs = organizationDao.findAllOrgans();
  349. List<PracticeGroupsDto> hasReportNums = courseScheduleEvaluateDao.getHasReportNums();
  350. List<PracticeGroupsDto> organMoneys = courseScheduleEvaluateDao.getOrganMoney();
  351. List<PracticeGroupsDto> organPracticeGroups = courseScheduleEvaluateDao.getOrganPracticeGroups();
  352. List<PracticeGroupsDto> practiceGroupsFrees = courseScheduleEvaluateDao.getPracticeGroupsFree();
  353. List<PracticeGroupsDto> practiceGroupsBuys = courseScheduleEvaluateDao.getPracticeGroupsBuy();
  354. List<PracticeGroupsDto> practiceGroupsRenews = courseScheduleEvaluateDao.getPracticeGroupsRenew();
  355. Date startTime = DateUtil.getFirstDayOfMonth(new Date());
  356. List<PracticeGroupsDto> monthOrganBuyMoneys = courseScheduleEvaluateDao.getMonthOrganMoney(OrderTypeEnum.PRACTICE_GROUP_BUY, startTime);
  357. List<PracticeGroupsDto> monthOrganRenewMoneys = courseScheduleEvaluateDao.getMonthOrganMoney(OrderTypeEnum.PRACTICE_GROUP_RENEW, startTime);
  358. //List<PracticeGroupsDto> monthOrganBuyMoneys = courseScheduleEvaluateDao.getOrganNewPracticeGroupsOfMonth(OrderTypeEnum.PRACTICE_GROUP_BUY,startTime);
  359. //List<PracticeGroupsDto> monthOrganRenewMoneys = courseScheduleEvaluateDao.getOrganNewPracticeGroupsOfMonth(OrderTypeEnum.PRACTICE_GROUP_RENEW,startTime);
  360. List<PracticeGroupsDto> practiceGroups = new ArrayList<>();
  361. for (Organization organ : organs) {
  362. PracticeGroupsDto practiceGroupsDto = new PracticeGroupsDto();
  363. practiceGroupsDto.setOrganName(organ.getName());
  364. practiceGroupsDto.setOrganId(organ.getId());
  365. //体验人数
  366. for (PracticeGroupsDto practiceGroupsFree : practiceGroupsFrees) {
  367. if (organ.getId().equals(practiceGroupsFree.getOrganId())) {
  368. practiceGroupsDto.setTotalNums(practiceGroupsFree.getTotalNums());
  369. break;
  370. }
  371. }
  372. //购买人数
  373. for (PracticeGroupsDto practiceGroupsBuy : practiceGroupsBuys) {
  374. if (organ.getId().equals(practiceGroupsBuy.getOrganId())) {
  375. practiceGroupsDto.setBuyNums(practiceGroupsBuy.getBuyNums());
  376. break;
  377. }
  378. }
  379. //续费人数
  380. for (PracticeGroupsDto practiceGroupsRenew : practiceGroupsRenews) {
  381. if (organ.getId().equals(practiceGroupsRenew.getOrganId())) {
  382. practiceGroupsDto.setRenewNums(practiceGroupsRenew.getRenewNums());
  383. break;
  384. }
  385. }
  386. //已完成人数
  387. for (PracticeGroupsDto organPracticeGroup : organPracticeGroups) {
  388. if (organ.getId().equals(organPracticeGroup.getOrganId()) && organPracticeGroup.getBuyNums() >= organPracticeGroup.getTotalNums()) {
  389. practiceGroupsDto.setOverNums(practiceGroupsDto.getOverNums() + 1);
  390. }
  391. }
  392. //已提交报告人数
  393. for (PracticeGroupsDto hasReportNum : hasReportNums) {
  394. if (organ.getId().equals(hasReportNum.getOrganId())) {
  395. practiceGroupsDto.setReportNums(hasReportNum.getReportNums());
  396. break;
  397. }
  398. }
  399. //成交金额
  400. for (PracticeGroupsDto organMoney : organMoneys) {
  401. if (organ.getId().equals(organMoney.getOrganId())) {
  402. practiceGroupsDto.setTotalMoney(organMoney.getTotalMoney());
  403. break;
  404. }
  405. }
  406. //当月新增金额和人数
  407. for (PracticeGroupsDto monthOrganBuyMoney : monthOrganBuyMoneys) {
  408. if (organ.getId().equals(monthOrganBuyMoney.getOrganId())) {
  409. practiceGroupsDto.setMonthBuyMoney(monthOrganBuyMoney.getTotalMoney());
  410. practiceGroupsDto.setMonthBuyNums(monthOrganBuyMoney.getBuyNums());
  411. break;
  412. }
  413. }
  414. //当月续费金额和人数
  415. for (PracticeGroupsDto monthOrganRenewMoney : monthOrganRenewMoneys) {
  416. if (organ.getId().equals(monthOrganRenewMoney.getOrganId())) {
  417. practiceGroupsDto.setMonthRenewNums(monthOrganRenewMoney.getBuyNums());
  418. practiceGroupsDto.setMonthRenewMoney(monthOrganRenewMoney.getTotalMoney());
  419. break;
  420. }
  421. }
  422. if (practiceGroupsDto.getTotalNums() > 0) {
  423. double scale = new BigDecimal(practiceGroupsDto.getBuyNums()).multiply(new BigDecimal(100)).divide(new BigDecimal(practiceGroupsDto.getTotalNums()), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
  424. practiceGroupsDto.setBuyScale(scale);
  425. }
  426. practiceGroups.add(practiceGroupsDto);
  427. }
  428. BigDecimal totalMoney = BigDecimal.ZERO;
  429. Integer totalNum = 0;
  430. Integer buyNum = 0;
  431. Integer renewNum = 0;
  432. Integer overNum = 0;
  433. Integer reportNum = 0;
  434. Integer monthBuyNums = 0;
  435. Integer monthRenewNums = 0;
  436. BigDecimal monthBuyMoney = BigDecimal.ZERO;
  437. BigDecimal monthRenewMoney = BigDecimal.ZERO;
  438. Iterator<PracticeGroupsDto> iterator = practiceGroups.iterator();
  439. while (iterator.hasNext()) {
  440. PracticeGroupsDto next = iterator.next();
  441. if (next.getOrganName().equals("总部国际") || next.getOrganName().equals("阳光总部国际") || next.getOrganName().equals("武汉小学")) {
  442. iterator.remove();
  443. continue;
  444. }
  445. totalMoney = totalMoney.add(next.getTotalMoney());
  446. totalNum += next.getTotalNums();
  447. overNum += next.getOverNums();
  448. buyNum += next.getBuyNums();
  449. reportNum += next.getReportNums();
  450. renewNum += next.getRenewNums();
  451. monthBuyNums += next.getMonthBuyNums();
  452. monthRenewNums += next.getMonthRenewNums();
  453. monthBuyMoney = monthBuyMoney.add(next.getMonthBuyMoney());
  454. monthRenewMoney = monthRenewMoney.add(next.getMonthRenewMoney());
  455. }
  456. practiceGroups = practiceGroups.stream().sorted(Comparator.comparingDouble(PracticeGroupsDto::getBuyScale).reversed()).collect(Collectors.toList());
  457. PracticeGroupStatisDto practiceGroupStatisDto = new PracticeGroupStatisDto();
  458. practiceGroupStatisDto.setPracticeGroupsDtoList(practiceGroups);
  459. practiceGroupStatisDto.setTotalMoney(totalMoney);
  460. practiceGroupStatisDto.setTotalNum(totalNum);
  461. practiceGroupStatisDto.setBuyNum(buyNum);
  462. practiceGroupStatisDto.setRenewNum(renewNum);
  463. practiceGroupStatisDto.setOverNum(overNum);
  464. practiceGroupStatisDto.setReportNum(reportNum);
  465. practiceGroupStatisDto.setMonthBuyNums(monthBuyNums);
  466. practiceGroupStatisDto.setMonthBuyMoney(monthBuyMoney);
  467. practiceGroupStatisDto.setMonthRenewNums(monthRenewNums);
  468. practiceGroupStatisDto.setMonthRenewMoney(monthRenewMoney);
  469. if (totalNum > 0) {
  470. BigDecimal scale = new BigDecimal(buyNum).multiply(new BigDecimal(100)).divide(new BigDecimal(totalNum), 2, BigDecimal.ROUND_HALF_UP);
  471. practiceGroupStatisDto.setBuyScale(scale);
  472. }
  473. return succeed(practiceGroupStatisDto);
  474. }
  475. @GetMapping("/getPracticeStatis")
  476. public HttpResponseResult getPracticeStatis() {
  477. Date nowDate = new Date();
  478. List<Organization> organs = organizationDao.findAllOrgans();
  479. //除去禁止的体验人数
  480. // List<Practice4OrganDto> organTryNums = courseScheduleEvaluateDao.getTryNums();
  481. // List<Practice4OrganDto> organPracticeTryNums = courseScheduleEvaluateDao.getPracticeTryNums();
  482. // List<Practice4OrganDto> organVipTryNums = courseScheduleEvaluateDao.getVipTryNums();
  483. // List<Practice4OrganDto> organPracticeAndVipTryNums = courseScheduleEvaluateDao.getPracticeAndVipTryNums();
  484. List<Practice4OrganDto> allTryNums = courseScheduleEvaluateDao.getAllTryNums();
  485. List<Practice4OrganDto> practiceBuyNums = courseScheduleEvaluateDao.getPracticeBuyNums(nowDate, null);
  486. List<Practice4OrganDto> vipBuyNums = courseScheduleEvaluateDao.getVipBuyNums(nowDate, null);
  487. List<Practice4OrganDto> practiceAndVipBuyNums = courseScheduleEvaluateDao.getPracticeAndVipNums(nowDate, null);
  488. Date startTime = DateUtil.getFirstDayOfMonth(nowDate);
  489. Date endTime = DateUtil.addMonths(startTime, 1);
  490. //当月新增网管课人数
  491. List<PracticeGroupsDto> monthOrganPracticeBuyMoneys = courseScheduleEvaluateDao.getMonthOrganMoney(OrderTypeEnum.PRACTICE_GROUP_BUY, startTime);
  492. //当月新增VIP人数
  493. List<PracticeGroupsDto> monthOrganVipBuyMoneys = courseScheduleEvaluateDao.getMonthOrganMoney(OrderTypeEnum.SMALL_CLASS_TO_BUY, startTime);
  494. //当月续费人数
  495. List<PracticeGroupsDto> monthOrganRenewMoneys = courseScheduleEvaluateDao.getMonthOrganMoney(OrderTypeEnum.PRACTICE_GROUP_RENEW, startTime);
  496. //本月总人数
  497. List<Practice4OrganDto> nowMonthPracticeBuyNums = courseScheduleEvaluateDao.getPracticeBuyNums(startTime, endTime);
  498. List<Practice4OrganDto> nowMonthVipBuyNums = courseScheduleEvaluateDao.getVipBuyNums(startTime, endTime);
  499. List<Practice4OrganDto> nowMonthPracticeAndVipBuyNums = courseScheduleEvaluateDao.getPracticeAndVipNums(startTime, endTime);
  500. //上月总人数
  501. Date lastMonthStartTime = DateUtil.addMonths(startTime, -1);
  502. List<Practice4OrganDto> lastMonthPracticeBuyNums = courseScheduleEvaluateDao.getPracticeBuyNums(lastMonthStartTime, startTime);
  503. List<Practice4OrganDto> lastMonthVipBuyNums = courseScheduleEvaluateDao.getVipBuyNums(lastMonthStartTime, startTime);
  504. List<Practice4OrganDto> lastMonthPracticeAndVipBuyNums = courseScheduleEvaluateDao.getPracticeAndVipNums(lastMonthStartTime, startTime);
  505. List<Practice4OrganDto> practice4Organs = new ArrayList<>();
  506. for (Organization organ : organs) {
  507. Practice4OrganDto practice4OrganDto = new Practice4OrganDto();
  508. practice4OrganDto.setOrganName(organ.getName());
  509. practice4OrganDto.setOrganId(organ.getId());
  510. //体验人数(除去禁止)
  511. // for (Practice4OrganDto organTryNum : organTryNums) {
  512. // if (organ.getId().equals(organTryNum.getOrganId())) {
  513. // practice4OrganDto.setTryNum(practice4OrganDto.getTryNum() + organTryNum.getTryNum());
  514. // break;
  515. // }
  516. // }
  517. // //网管课体验人数(禁止)
  518. // for (Practice4OrganDto organPracticeTryNum : organPracticeTryNums) {
  519. // if (organ.getId().equals(organPracticeTryNum.getOrganId())) {
  520. // practice4OrganDto.setTryNum(practice4OrganDto.getTryNum() + organPracticeTryNum.getTryNum());
  521. // break;
  522. // }
  523. // }
  524. // //vip课体验人数(禁止)
  525. // for (Practice4OrganDto organVipTryNum : organVipTryNums) {
  526. // if (organ.getId().equals(organVipTryNum.getOrganId())) {
  527. // practice4OrganDto.setTryNum(practice4OrganDto.getTryNum() + organVipTryNum.getTryNum());
  528. // break;
  529. // }
  530. // }
  531. // //减去vip中在网管课的人数
  532. // for (Practice4OrganDto organPracticeAndVipTryNum : organPracticeAndVipTryNums) {
  533. // if (organ.getId().equals(organPracticeAndVipTryNum.getOrganId())) {
  534. // practice4OrganDto.setTryNum(practice4OrganDto.getTryNum() - organPracticeAndVipTryNum.getTryNum());
  535. // break;
  536. // }
  537. // }
  538. for (Practice4OrganDto allTryNum : allTryNums) {
  539. if (organ.getId().equals(allTryNum.getOrganId())) {
  540. practice4OrganDto.setTryNum(allTryNum.getTryNum());
  541. }
  542. }
  543. //网管课转化人数
  544. for (Practice4OrganDto practiceBuyNum : practiceBuyNums) {
  545. if (organ.getId().equals(practiceBuyNum.getOrganId())) {
  546. practice4OrganDto.setPracticeNum(practiceBuyNum.getPracticeNum());
  547. break;
  548. }
  549. }
  550. //VIP课转化人数
  551. for (Practice4OrganDto vipBuyNum : vipBuyNums) {
  552. if (organ.getId().equals(vipBuyNum.getOrganId())) {
  553. practice4OrganDto.setVipNum(vipBuyNum.getVipNum());
  554. break;
  555. }
  556. }
  557. for (Practice4OrganDto practiceAndVipBuyNum : practiceAndVipBuyNums) {
  558. if (organ.getId().equals(practiceAndVipBuyNum.getOrganId())) {
  559. practice4OrganDto.setVipNum(practice4OrganDto.getVipNum() - practiceAndVipBuyNum.getPracticeNum());
  560. break;
  561. }
  562. }
  563. practice4OrganDto.setTotalNum(practice4OrganDto.getPracticeNum() + practice4OrganDto.getVipNum());
  564. //当月新增网管课人数
  565. for (PracticeGroupsDto monthOrganPracticeBuyMoney : monthOrganPracticeBuyMoneys) {
  566. if (organ.getId().equals(monthOrganPracticeBuyMoney.getOrganId())) {
  567. practice4OrganDto.setPractice4MonthNum(monthOrganPracticeBuyMoney.getBuyNums());
  568. practice4OrganDto.setTotalMoney(practice4OrganDto.getTotalMoney().add(monthOrganPracticeBuyMoney.getTotalMoney()));
  569. break;
  570. }
  571. }
  572. //当月新增VIP课人数
  573. for (PracticeGroupsDto monthOrganVipBuyMoney : monthOrganVipBuyMoneys) {
  574. if (organ.getId().equals(monthOrganVipBuyMoney.getOrganId())) {
  575. practice4OrganDto.setVip4MonthNum(monthOrganVipBuyMoney.getBuyNums());
  576. practice4OrganDto.setTotalMoney(practice4OrganDto.getTotalMoney().add(monthOrganVipBuyMoney.getTotalMoney()));
  577. break;
  578. }
  579. }
  580. //当月续费人数
  581. for (PracticeGroupsDto monthOrganRenewMoney : monthOrganRenewMoneys) {
  582. if (organ.getId().equals(monthOrganRenewMoney.getOrganId())) {
  583. practice4OrganDto.setRenew4MonthNum(monthOrganRenewMoney.getBuyNums());
  584. practice4OrganDto.setTotalMoney(practice4OrganDto.getTotalMoney().add(monthOrganRenewMoney.getTotalMoney()));
  585. break;
  586. }
  587. }
  588. //同比上月增减人数
  589. for (Practice4OrganDto nowMonthPracticeBuyNum : nowMonthPracticeBuyNums) {
  590. if (organ.getId().equals(nowMonthPracticeBuyNum.getOrganId())) {
  591. practice4OrganDto.setChange4MonthNum(nowMonthPracticeBuyNum.getPracticeNum());
  592. break;
  593. }
  594. }
  595. for (Practice4OrganDto nowMonthVipBuyNum : nowMonthVipBuyNums) {
  596. if (organ.getId().equals(nowMonthVipBuyNum.getOrganId())) {
  597. practice4OrganDto.setChange4MonthNum(practice4OrganDto.getChange4MonthNum() + nowMonthVipBuyNum.getVipNum());
  598. break;
  599. }
  600. }
  601. for (Practice4OrganDto nowMonthPracticeAndVipBuyNum : nowMonthPracticeAndVipBuyNums) {
  602. if (organ.getId().equals(nowMonthPracticeAndVipBuyNum.getOrganId())) {
  603. practice4OrganDto.setChange4MonthNum(practice4OrganDto.getChange4MonthNum() - nowMonthPracticeAndVipBuyNum.getPracticeNum());
  604. break;
  605. }
  606. }
  607. //上月人数
  608. for (Practice4OrganDto lastMonthPracticeBuyNum : lastMonthPracticeBuyNums) {
  609. if (organ.getId().equals(lastMonthPracticeBuyNum.getOrganId())) {
  610. practice4OrganDto.setChange4MonthNum(practice4OrganDto.getChange4MonthNum() - lastMonthPracticeBuyNum.getPracticeNum());
  611. break;
  612. }
  613. }
  614. for (Practice4OrganDto lastMonthVipBuyNum : lastMonthVipBuyNums) {
  615. if (organ.getId().equals(lastMonthVipBuyNum.getOrganId())) {
  616. practice4OrganDto.setChange4MonthNum(practice4OrganDto.getChange4MonthNum() - lastMonthVipBuyNum.getVipNum());
  617. break;
  618. }
  619. }
  620. for (Practice4OrganDto lastMonthPracticeAndVipBuyNum : lastMonthPracticeAndVipBuyNums) {
  621. if (organ.getId().equals(lastMonthPracticeAndVipBuyNum.getOrganId())) {
  622. practice4OrganDto.setChange4MonthNum(practice4OrganDto.getChange4MonthNum() + lastMonthPracticeAndVipBuyNum.getPracticeNum());
  623. break;
  624. }
  625. }
  626. if (practice4OrganDto.getTryNum() > 0) {
  627. double scale = new BigDecimal(practice4OrganDto.getTotalNum()).multiply(new BigDecimal(100)).divide(new BigDecimal(practice4OrganDto.getTryNum()), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
  628. practice4OrganDto.setScale(scale);
  629. }
  630. practice4Organs.add(practice4OrganDto);
  631. }
  632. practice4Organs = practice4Organs.stream().sorted(Comparator.comparingDouble(Practice4OrganDto::getScale).reversed()).collect(Collectors.toList());
  633. return succeed(practice4Organs);
  634. }
  635. }