StudentOrderController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package com.ym.mec.student.controller;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiImplicitParams;
  5. import io.swagger.annotations.ApiOperation;
  6. import java.io.IOException;
  7. import java.math.BigDecimal;
  8. import java.net.URLEncoder;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.Comparator;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.LinkedHashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.Objects;
  18. import java.util.stream.Collectors;
  19. import javax.servlet.http.HttpServletResponse;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.scheduling.annotation.EnableScheduling;
  25. import org.springframework.web.bind.annotation.GetMapping;
  26. import org.springframework.web.bind.annotation.ModelAttribute;
  27. import org.springframework.web.bind.annotation.PostMapping;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RequestParam;
  30. import org.springframework.web.bind.annotation.RestController;
  31. import com.alibaba.fastjson.JSON;
  32. import com.huifu.adapay.model.payment.PayChannelEnum;
  33. import com.huifu.adapay.model.payment.Payment;
  34. import com.ym.mec.biz.dal.dao.CourseScheduleEvaluateDao;
  35. import com.ym.mec.biz.dal.dao.OrganizationDao;
  36. import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
  37. import com.ym.mec.biz.dal.dao.SysConfigDao;
  38. import com.ym.mec.biz.dal.dao.TeacherCourseStatisticsDao;
  39. import com.ym.mec.biz.dal.dto.LuckStatisDto;
  40. import com.ym.mec.biz.dal.dto.OrderStatisDto;
  41. import com.ym.mec.biz.dal.dto.PracticeGroupsDto;
  42. import com.ym.mec.biz.dal.dto.VipBuyResultDto;
  43. import com.ym.mec.biz.dal.dto.practiceGroupStatisDto;
  44. import com.ym.mec.biz.dal.entity.Goods;
  45. import com.ym.mec.biz.dal.entity.MusicGroup;
  46. import com.ym.mec.biz.dal.entity.Organization;
  47. import com.ym.mec.biz.dal.entity.SporadicChargeInfo;
  48. import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
  49. import com.ym.mec.biz.dal.enums.DealStatusEnum;
  50. import com.ym.mec.biz.dal.enums.GroupType;
  51. import com.ym.mec.biz.service.MusicGroupService;
  52. import com.ym.mec.biz.service.SporadicChargeInfoService;
  53. import com.ym.mec.biz.service.StudentPaymentOrderDetailService;
  54. import com.ym.mec.biz.service.StudentPaymentOrderService;
  55. import com.ym.mec.biz.service.StudentRegistrationService;
  56. import com.ym.mec.biz.service.VipGroupService;
  57. import com.ym.mec.common.controller.BaseController;
  58. import com.ym.mec.common.entity.HttpResponseResult;
  59. import com.ym.mec.thirdparty.adapay.NotifyEvent;
  60. import com.ym.mec.thirdparty.adapay.Pay;
  61. import com.ym.mec.thirdparty.yqpay.Msg;
  62. import com.ym.mec.thirdparty.yqpay.RsqMsg;
  63. import com.ym.mec.thirdparty.yqpay.YqPayFeignService;
  64. import com.ym.mec.thirdparty.yqpay.YqPayUtil;
  65. import com.ym.mec.util.http.HttpUtil;
  66. @RequestMapping("studentOrder")
  67. @Api(tags = "订单回调")
  68. @RestController
  69. @EnableScheduling
  70. public class StudentOrderController extends BaseController {
  71. private static final Logger logger = LoggerFactory.getLogger(StudentOrderController.class);
  72. @Autowired
  73. private StudentPaymentOrderService studentPaymentOrderService;
  74. @Autowired
  75. private StudentRegistrationService studentRegistrationService;
  76. @Autowired
  77. private VipGroupService vipGroupService;
  78. @Autowired
  79. private MusicGroupService musicGroupService;
  80. @Autowired
  81. private StudentPaymentOrderDetailService studentPaymentOrderDetailService;
  82. @Autowired
  83. private YqPayFeignService yqPayFeignService;
  84. @Autowired
  85. private StudentPaymentOrderDao studentPaymentOrderDao;
  86. @Autowired
  87. private SysConfigDao sysConfigDao;
  88. @Autowired
  89. private SporadicChargeInfoService sporadicChargeInfoService;
  90. @Autowired
  91. private OrganizationDao organizationDao;
  92. @Autowired
  93. private TeacherCourseStatisticsDao teacherCourseStatisticsDao;
  94. @Autowired
  95. private CourseScheduleEvaluateDao courseScheduleEvaluateDao;
  96. @PostMapping("/notify")
  97. public Msg notify(@ModelAttribute Msg msg) throws Exception {
  98. logger.info(msg.toString());
  99. Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
  100. rqMap.put("code", msg.getCode());
  101. rqMap.put("msg", msg.getMsg());
  102. rqMap.put("responseType", msg.getResponseType());
  103. rqMap.put("responseParameters", msg.getResponseParameters());
  104. rqMap.put("sign", msg.getSign());
  105. //boolean rs = YqPayUtil.verify(rqMap);
  106. msg.setMsg("fail");
  107. Map<String, String> notifyMap = new HashMap<>();
  108. //if (rs) {
  109. notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
  110. //}
  111. //支付中订单存在,更新状态
  112. if (msg.getResponseType().equals("1") && notifyMap.size() > 0) {
  113. String tradeState = msg.getCode().equals("88") ? "1" : "0";
  114. String channelType = notifyMap.get("channelType").equals("1") ? "WXPay" : (notifyMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
  115. notifyMap.put("tradeState", tradeState);
  116. notifyMap.put("totalMoney", notifyMap.get("payAmount"));
  117. notifyMap.put("merOrderNo", notifyMap.get("merMerOrderNo"));
  118. notifyMap.put("channelType", channelType);
  119. studentPaymentOrderService.updateOrder(notifyMap);
  120. msg.setCode("000000");
  121. msg.setMsg("success");
  122. }
  123. return msg;
  124. }
  125. @ApiOperation(value = "查询订单状态")
  126. @PostMapping("/checkOrderStatus")
  127. public Object checkOrderStatus(String orderNo) {
  128. if (StringUtils.isBlank(orderNo)) {
  129. return failed("请指定订单");
  130. }
  131. StudentPaymentOrder orderByOrderNo = studentPaymentOrderService.findOrderByOrderNo(orderNo);
  132. if (Objects.isNull(orderByOrderNo)) {
  133. return failed("未找到指定订单");
  134. }
  135. HashMap<String, Object> orderDetail = new HashMap<>();
  136. orderDetail.put("order", orderByOrderNo);
  137. orderDetail.put("groupType", orderByOrderNo.getGroupType());
  138. if (orderByOrderNo.getGroupType().equals(GroupType.MUSIC)) {
  139. MusicGroup musicGroup = musicGroupService.get(orderByOrderNo.getMusicGroupId());
  140. List<Goods> goodsList = studentPaymentOrderDetailService.findApplyOrderGoods(orderByOrderNo.getId());
  141. orderDetail.put("goods", goodsList);
  142. orderDetail.put("course", musicGroup.getCourseForm());
  143. orderDetail.put("ownershipType", musicGroup.getOwnershipType());
  144. } else if (orderByOrderNo.getGroupType().equals(GroupType.VIP)) {
  145. VipBuyResultDto vipBuyResultInfo = vipGroupService.findVipBuyResultInfo(Integer.valueOf(orderByOrderNo.getMusicGroupId()));
  146. orderDetail.put("detail", vipBuyResultInfo);
  147. } else if (orderByOrderNo.getGroupType().equals(GroupType.SPORADIC)) {
  148. SporadicChargeInfo info = sporadicChargeInfoService.get(Integer.valueOf(orderByOrderNo.getMusicGroupId()));
  149. orderDetail.put("detail", info);
  150. }
  151. return succeed(orderDetail);
  152. }
  153. @ApiOperation(value = "台牌支付")
  154. @PostMapping("/executePayment")
  155. @ApiImplicitParams({
  156. @ApiImplicitParam(name = "amount", value = "支付金额", required = true, dataType = "BigDecimal"),
  157. @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String"),
  158. @ApiImplicitParam(name = "payChannel", value = "支付方式(alipay-支付宝app支付)", required = true, dataType = "String"),
  159. })
  160. public Object executePayment(BigDecimal amount, String orderNo, String payChannel, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sign, String code) throws Exception {
  161. String appId = "wxcf8e8b33a9477845";
  162. String appSecret = "1286452b9c68b13325dece7cdf892645";
  163. String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
  164. payChannel = PayChannelEnum.ALIPAY_QR.getCode();
  165. // if (!new Pay().verifySign(amount, orderNo, notifyUrl, orderSubject, orderBody,sign)) {
  166. // return failed("签名验证失败");
  167. // }
  168. String openid = "";
  169. if (payChannel == "wx_pub") {
  170. if (code == null || code.isEmpty()) {
  171. return failed("微信支付请先授权");
  172. }
  173. wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
  174. Map<String, String> weChatRes = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
  175. if (!weChatRes.containsKey("openid")) {
  176. return failed("授权失败,请重新授权");
  177. }
  178. openid = weChatRes.get("openid");
  179. }
  180. StudentPaymentOrder order = studentPaymentOrderService.findOrderByOrderNo(orderNo);
  181. if (order == null) {
  182. return failed("订单不存在");
  183. }
  184. Payment payment = new Pay().executePayment(amount, orderNo, payChannel, orderSubject, orderBody, code);
  185. order.setTransNo(payment.getId());
  186. studentPaymentOrderService.update(order);
  187. return succeed(payment);
  188. }
  189. @PostMapping("/adaNotify")
  190. public void adaNotify(@ModelAttribute NotifyEvent notifyEvent) throws Exception {
  191. logger.info(notifyEvent.toString());
  192. if (notifyEvent.getType().equals("payment.success") && notifyEvent.getType().equals("payment.failed")) {
  193. return;
  194. }
  195. Map<String, String> notifyMap = JSON.parseObject(notifyEvent.getData(), Map.class);
  196. //支付中订单存在,更新状态
  197. if (notifyMap.size() > 0) {
  198. String tradeState = notifyEvent.getType().equals("payment.success") ? "1" : "0";
  199. notifyMap.put("tradeState", tradeState);
  200. notifyMap.put("totalMoney", notifyMap.get("pay_amt"));
  201. notifyMap.put("merOrderNo", notifyMap.get("order_no"));
  202. notifyMap.put("merOrderNo", notifyMap.get("order_no"));
  203. notifyMap.put("remarks", notifyMap.get("description"));
  204. studentPaymentOrderService.updateOrder(notifyMap);
  205. }
  206. }
  207. @GetMapping("/authorize")
  208. public String authorize(@RequestParam("returnUrl") String returnUrl) {
  209. String appId = "wxcf8e8b33a9477845";
  210. String url = URLEncoder.encode("http://wxwechat.utools.club/studentOrder/userInfo");
  211. 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",
  212. appId, url);
  213. return "redirect:" + redirectURL;
  214. }
  215. @GetMapping("/userInfo")
  216. public Object userInfo(@RequestParam("code") String code,
  217. @RequestParam("state") String returnUrl) throws Exception {
  218. String appId = "wxcf8e8b33a9477845";
  219. String appSecret = "1286452b9c68b13325dece7cdf892645";
  220. String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
  221. wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
  222. Map<String, String> map = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
  223. if (!map.containsKey("openid")) {
  224. return failed("授权失败,请重新授权");
  225. }
  226. return map.get("openid");
  227. }
  228. // @Scheduled(cron = "0/30 * * * * ?")
  229. @GetMapping("/setSuccessStatus")
  230. public void setSuccessStatus() throws Exception {
  231. List<StudentPaymentOrder> payingOrders = studentPaymentOrderService.findOrdersByStatus(DealStatusEnum.ING, "YQPAY");
  232. String[] statusArr = {"0", "1", "7"};
  233. for (StudentPaymentOrder payingOrder : payingOrders) {
  234. Map<String, String> rpMap = new HashMap<>();
  235. rpMap.put("tradeState", "1");
  236. rpMap.put("remarks", "模拟支付成功");
  237. rpMap.put("merOrderNo", payingOrder.getOrderNo());
  238. rpMap.put("orderNo", payingOrder.getOrderNo());
  239. rpMap.put("channelType", "1");
  240. String channelType = rpMap.get("channelType").equals("1") ? "WXPay" : (rpMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
  241. rpMap.put("channelType", channelType);
  242. if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
  243. try {
  244. studentPaymentOrderService.updateOrder(rpMap); //更新订单
  245. } catch (Exception e) {
  246. e.printStackTrace();
  247. continue;
  248. }
  249. }
  250. }
  251. }
  252. @GetMapping("/getOrderStatus")
  253. private HttpResponseResult getOrderStatus(String orderNo) throws Exception {
  254. if (orderNo == null || orderNo.isEmpty()) {
  255. return failed("订单号必须填");
  256. }
  257. String notifyUrl = ""; //回调地址
  258. Map<String, Object> resultMap = new LinkedHashMap<>();
  259. resultMap.put("merOrderNoList", orderNo);
  260. Map<String, Object> requestMap = YqPayUtil.getRequestMap(notifyUrl, resultMap);
  261. RsqMsg rsqMsg = new RsqMsg(requestMap);
  262. Msg queryRs = yqPayFeignService.orderQuery(rsqMsg);
  263. if (queryRs.getCode().equals("88")) {
  264. String responseParameters = queryRs.getResponseParameters();
  265. List<Map<String, Object>> responseList = JSON.parseObject(responseParameters, List.class);
  266. return succeed(responseList);
  267. }
  268. return succeed("订单不存在");
  269. }
  270. @RequestMapping("paymentResult")
  271. public void paymentResult(HttpServletResponse response, String orderNo) {
  272. try {
  273. String baseApiUrl = sysConfigDao.findConfigValue("base_api_url");
  274. response.sendRedirect(baseApiUrl + "/#/paymentresult?orderNo=" + orderNo);
  275. } catch (IOException e) {
  276. e.printStackTrace();
  277. }
  278. }
  279. @GetMapping("/getLuckStatis")
  280. public HttpResponseResult getLuckStatis() {
  281. List<Organization> organs = organizationDao.findAllOrgans();
  282. List<OrderStatisDto> orders = studentPaymentOrderDao.getLuckStatis();
  283. for (Organization organ : organs) {
  284. boolean flag = false;
  285. for (OrderStatisDto order : orders) {
  286. if (organ.getName().equals(order.getOrganName())) {
  287. flag = true;
  288. break;
  289. }
  290. }
  291. if (!flag) {
  292. OrderStatisDto orderStatisDto = new OrderStatisDto();
  293. orderStatisDto.setOrganName(organ.getName());
  294. orderStatisDto.setMoney(BigDecimal.ZERO);
  295. orderStatisDto.setNums(0);
  296. orders.add(orderStatisDto);
  297. }
  298. }
  299. BigDecimal totalMoney = BigDecimal.ZERO;
  300. Integer totalNum = 0;
  301. Iterator<OrderStatisDto> iterator = orders.iterator();
  302. while (iterator.hasNext()) {
  303. OrderStatisDto order = iterator.next();
  304. if (order.getOrganName().equals("总部国际") || order.getOrganName().equals("阳光总部国际") || order.getOrganName().equals("武汉小学")) {
  305. iterator.remove();
  306. continue;
  307. }
  308. totalMoney = totalMoney.add(order.getMoney());
  309. totalNum += order.getNums();
  310. }
  311. orders = orders.stream().sorted(Comparator.comparing(OrderStatisDto::getNums).reversed()).collect(Collectors.toList());
  312. LuckStatisDto luckStatisDto = new LuckStatisDto();
  313. luckStatisDto.setOrderStatisDtoList(orders);
  314. luckStatisDto.setTotalNum(totalNum);
  315. luckStatisDto.setTotalMoney(totalMoney);
  316. return succeed(luckStatisDto);
  317. }
  318. @GetMapping("/getPracticeGroupStatis")
  319. public HttpResponseResult getPracticeGroupStatis() {
  320. List<Organization> organs = organizationDao.findAllOrgans();
  321. List<PracticeGroupsDto> hasReportNums = courseScheduleEvaluateDao.getHasReportNums();
  322. List<PracticeGroupsDto> organMoneys = courseScheduleEvaluateDao.getOrganMoney();
  323. List<PracticeGroupsDto> organPracticeGroups = courseScheduleEvaluateDao.getOrganPracticeGroups();
  324. List<PracticeGroupsDto> practiceGroupsFrees = courseScheduleEvaluateDao.getPracticeGroupsFree();
  325. List<PracticeGroupsDto> practiceGroupsBuys = courseScheduleEvaluateDao.getPracticeGroupsBuy();
  326. List<PracticeGroupsDto> practiceGroups = new ArrayList<>();
  327. for (Organization organ : organs) {
  328. PracticeGroupsDto practiceGroupsDto = new PracticeGroupsDto();
  329. practiceGroupsDto.setOrganName(organ.getName());
  330. practiceGroupsDto.setOrganId(organ.getId());
  331. //体验人数
  332. for (PracticeGroupsDto practiceGroupsFree : practiceGroupsFrees) {
  333. if (organ.getId().equals(practiceGroupsFree.getOrganId())) {
  334. practiceGroupsDto.setTotalNums(practiceGroupsFree.getTotalNums());
  335. break;
  336. }
  337. }
  338. //购买人数
  339. for (PracticeGroupsDto practiceGroupsBuy : practiceGroupsBuys) {
  340. if (organ.getId().equals(practiceGroupsBuy.getOrganId())) {
  341. practiceGroupsDto.setBuyNums(practiceGroupsBuy.getBuyNums());
  342. break;
  343. }
  344. }
  345. //已完成人数
  346. for (PracticeGroupsDto organPracticeGroup : organPracticeGroups) {
  347. if (organ.getId().equals(organPracticeGroup.getOrganId()) && organPracticeGroup.getBuyNums() >= organPracticeGroup.getTotalNums()) {
  348. practiceGroupsDto.setOverNums(practiceGroupsDto.getOverNums() + 1);
  349. }
  350. }
  351. //已提交报告人数
  352. for (PracticeGroupsDto hasReportNum : hasReportNums) {
  353. if (organ.getId().equals(hasReportNum.getOrganId())) {
  354. practiceGroupsDto.setReportNums(hasReportNum.getReportNums());
  355. break;
  356. }
  357. }
  358. //成交金额
  359. for (PracticeGroupsDto organMoney : organMoneys) {
  360. if (organ.getId().equals(organMoney.getOrganId())) {
  361. practiceGroupsDto.setTotalMoney(organMoney.getTotalMoney());
  362. break;
  363. }
  364. }
  365. if(practiceGroupsDto.getTotalNums() >0 ){
  366. double scale = new BigDecimal(practiceGroupsDto.getBuyNums()).multiply(new BigDecimal(100)).divide(new BigDecimal(practiceGroupsDto.getTotalNums()),2, BigDecimal.ROUND_HALF_UP).doubleValue();
  367. practiceGroupsDto.setBuyScale(scale);
  368. }
  369. practiceGroups.add(practiceGroupsDto);
  370. }
  371. BigDecimal totalMoney = BigDecimal.ZERO;
  372. Integer totalNum = 0;
  373. Integer buyNum = 0;
  374. Integer overNum = 0;
  375. Integer reportNum = 0;
  376. Iterator<PracticeGroupsDto> iterator = practiceGroups.iterator();
  377. while (iterator.hasNext()) {
  378. PracticeGroupsDto next = iterator.next();
  379. if (next.getOrganName().equals("总部国际") || next.getOrganName().equals("阳光总部国际") || next.getOrganName().equals("武汉小学")) {
  380. iterator.remove();
  381. continue;
  382. }
  383. totalMoney = totalMoney.add(next.getTotalMoney());
  384. totalNum += next.getTotalNums();
  385. overNum += next.getOverNums();
  386. buyNum += next.getBuyNums();
  387. reportNum += next.getReportNums();
  388. }
  389. practiceGroups = practiceGroups.stream().sorted(Comparator.comparingDouble(PracticeGroupsDto::getBuyScale).reversed()).collect(Collectors.toList());
  390. practiceGroupStatisDto practiceGroupStatisDto = new practiceGroupStatisDto();
  391. practiceGroupStatisDto.setPracticeGroupsDtoList(practiceGroups);
  392. practiceGroupStatisDto.setTotalMoney(totalMoney);
  393. practiceGroupStatisDto.setTotalNum(totalNum);
  394. practiceGroupStatisDto.setBuyNum(buyNum);
  395. practiceGroupStatisDto.setOverNum(overNum);
  396. practiceGroupStatisDto.setReportNum(reportNum);
  397. if(totalNum >0 ){
  398. BigDecimal scale = new BigDecimal(buyNum).multiply(new BigDecimal(100)).divide(new BigDecimal(totalNum), 2, BigDecimal.ROUND_HALF_UP);
  399. practiceGroupStatisDto.setBuyScale(scale);
  400. }
  401. return succeed(practiceGroupStatisDto);
  402. }
  403. }