YqPayController.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. package com.ym.mec.collectfee.controller;
  2. import com.alibaba.druid.sql.visitor.functions.Now;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.ym.mec.collectfee.common.sms.SmsExample;
  7. import com.ym.mec.collectfee.common.web.BaseController;
  8. import com.ym.mec.collectfee.entity.*;
  9. import com.ym.mec.collectfee.service.*;
  10. import com.ym.mec.collectfee.utils.Constants;
  11. import com.ym.mec.collectfee.utils.GenerateNum;
  12. import com.ym.mec.collectfee.utils.ShortUrlUtil;
  13. import com.ym.mec.collectfee.utils.yqpay.*;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.io.IOUtils;
  16. import org.apache.http.protocol.HTTP;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.core.io.ClassPathResource;
  21. import org.springframework.http.HttpStatus;
  22. import org.springframework.scheduling.annotation.EnableScheduling;
  23. import org.springframework.scheduling.annotation.Scheduled;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import org.springframework.validation.annotation.Validated;
  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.RestController;
  30. import java.io.InputStreamReader;
  31. import java.lang.reflect.Array;
  32. import java.math.BigDecimal;
  33. import java.text.SimpleDateFormat;
  34. import java.time.LocalDateTime;
  35. import java.util.*;
  36. //@Api("支付")
  37. @Slf4j
  38. @RestController
  39. @RequestMapping("yqpay")
  40. @EnableScheduling // 2.开启定时任务
  41. public class YqPayController extends BaseController {
  42. protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  43. @Autowired
  44. private YqPayService yqPayService;
  45. @Autowired
  46. private YqQueryService yqQueryService;
  47. @Autowired
  48. private OrderService orderService;
  49. @Autowired
  50. private AccountService accountService;
  51. @Autowired
  52. private CourseGroupInfoService CourseGroupInfoService;
  53. @Autowired
  54. private ApplyInfoService applyInfoService;
  55. @Autowired
  56. private RenewalsService renewalsService;
  57. @Autowired
  58. private SchoolService schoolService;
  59. /**
  60. * 统一下单(乐团缴费)
  61. *
  62. * @return String
  63. * @throws Exception
  64. */
  65. // @ApiOperation(value = "提交支付", notes = "易乾支付统一下单")
  66. @PostMapping("/toPay")
  67. @Transactional(rollbackFor = Exception.class)
  68. public Object toPay(@ModelAttribute @Validated Order order) throws Exception {
  69. BigDecimal amount = new BigDecimal("0");
  70. School school = schoolService.get(order.getClassId());
  71. //计划招生人数有更新,更新
  72. orderService.getSchoolDetail(school.getSchoolId());
  73. Order userOrder = orderService.findRegOrderByStatus(order.getUserId(), 2);
  74. if (userOrder != null) {
  75. return failed("您已支付成功,请勿重复支付");
  76. }
  77. //判断用户是否已存在订单
  78. userOrder = orderService.findRegOrderByStatus(order.getUserId(), 1);
  79. if (userOrder != null) {
  80. return failed(HttpStatus.CONTINUE, "您有待支付的订单");
  81. }
  82. if (!school.getStatus().equals(2)) {
  83. return failed("乐团不在缴费状态!");
  84. }
  85. //1、判断已报名人数
  86. CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getCourseId());
  87. if (courseGroupInfo.getRegNum().compareTo(courseGroupInfo.getPlanNum()) >= 0) {
  88. Integer nums = orderService.getPayingOrderNums(order.getCourseId());
  89. if (nums != null && nums > 0) {
  90. return failed(HttpStatus.FORBIDDEN, "当前排队人数" + nums + "人,请您耐心等待");
  91. } else {
  92. return succeed("该声部已满,请咨询教务老师选择其他声部!");
  93. }
  94. }
  95. //课程组价格
  96. BigDecimal courseFee = courseGroupInfo.getFeeAmount();
  97. amount = amount.add(courseFee);
  98. //获取乐器的价格
  99. ClassPathResource classPathResource = new ClassPathResource("instruments.json"); //解析乐器数据
  100. BigDecimal instrumentPrice = new BigDecimal("0");//乐器价格
  101. String instrumentName = "";//乐器名称
  102. String instrumentId = order.getInstrument();
  103. String jsonString = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(), "UTF-8"));
  104. Instrument instrument = JSONObject.parseObject(jsonString, Instrument.class);
  105. if (order.getInstrument() != null && !order.getInstrument().isEmpty()) {
  106. instrumentPrice = new BigDecimal(instrument.getInstruments().get(instrumentId).get("referencePrice"));
  107. instrumentName = (String) instrument.getInstruments().get(instrumentId).get("index") + "-" +
  108. (String) instrument.getInstruments().get(instrumentId).get("name");
  109. }
  110. //2 版本为3.0( 26),不收乐器费用,收押金800放乐器费用
  111. if (courseGroupInfo.getFeeType().equals(26) && order.getInstrument() != null && !order.getInstrument().isEmpty()) {
  112. instrumentPrice = new BigDecimal("800");
  113. }
  114. amount = amount.add(instrumentPrice);
  115. //辅件价格
  116. String adjunctIds = order.getAdjunct();
  117. BigDecimal adjunctPrice = new BigDecimal("0");//辅件价格
  118. String adjunctName = "";//辅件名称
  119. if (adjunctIds != null && !adjunctIds.isEmpty()) {
  120. String[] adjunctIdArr = adjunctIds.split(",");
  121. for (String adjunctId : adjunctIdArr) {
  122. adjunctPrice = adjunctPrice.add(new BigDecimal(instrument.getAuxiliaries().get(adjunctId).get("referencePrice")));
  123. adjunctName += (String) instrument.getAuxiliaries().get(adjunctId).get("name") + "|";
  124. }
  125. }
  126. amount = amount.add(adjunctPrice);
  127. ApplyInfo applyInfo = applyInfoService.get(order.getUserId());
  128. order.setPoName(school.getName());
  129. order.setVoicyPart(courseGroupInfo.getSubName());
  130. order.setGroupId(courseGroupInfo.getId());
  131. order.setAmount(amount);
  132. order.setRemark(instrumentName);
  133. order.setTuiFee(courseFee);
  134. order.setGoodsFee(instrumentPrice);
  135. order.setSdName(adjunctName + "教材|琴谱");
  136. order.setSdFee(adjunctPrice);
  137. order.setType(1);
  138. order.setUserName(applyInfo.getName());
  139. String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
  140. order.setOrderNo(orderNo);
  141. order.setCreateTime(new Date()); //订单提交时间
  142. order.setStatus(1); //订单状态
  143. //获取分佣账户
  144. Integer branchId = 1002;//默认分佣账
  145. Account routingAccount = accountService.getRoutingAccount(branchId, amount);
  146. order.setAccount(routingAccount.getSellerNo());
  147. order.setUAccount(routingAccount.getId().toString());
  148. //1、插入订单
  149. orderService.insert(order);
  150. //2、修改已报名人数
  151. courseGroupInfo.setRegNum(courseGroupInfo.getRegNum() + 1);
  152. CourseGroupInfoService.upByIdAndVersion(courseGroupInfo);
  153. //3、修改分佣账户已收金额
  154. BigDecimal HasRouting = routingAccount.getHasRouting().add(order.getAmount());
  155. routingAccount.setHasRouting(HasRouting);
  156. accountService.upByIdAndVersion(routingAccount);
  157. Map rqMap = orderService.getPayMap(routingAccount, order, school); //获取支付map
  158. //订单金额为0,直接成功
  159. if (order.getAmount().compareTo(new BigDecimal("0")) <= 0) {
  160. HashMap<String, String> notifyMap = new HashMap<>();
  161. notifyMap.put("tradeState", "1");
  162. notifyMap.put("merOrderNo", order.getOrderNo());
  163. notifyMap.put("channelType", "10");
  164. notifyMap.put("orderNo", "");
  165. notifyMap.put("totalMoney", "0");
  166. this.updateOrder(notifyMap);
  167. return failed(HttpStatus.CREATED, "恭喜您,报名成功!");
  168. }
  169. return succeed(rqMap);
  170. }
  171. /**
  172. * 重新支付订单
  173. *
  174. * @param order
  175. * @return
  176. * @throws Exception
  177. */
  178. @PostMapping("/rePay")
  179. @Transactional(rollbackFor = Exception.class)
  180. public Object rePay(@ModelAttribute @Validated Order order) throws Exception {
  181. BigDecimal amount = new BigDecimal("0");
  182. School school = schoolService.get(order.getClassId());
  183. Order userOrder = orderService.findRegOrderByStatus(order.getUserId(), 2);
  184. if (userOrder != null) {
  185. return failed("您已支付成功,请勿重复支付");
  186. }
  187. //判断用户是否已存在订单
  188. userOrder = orderService.findRegOrderByStatus(order.getUserId(), 1);
  189. if (userOrder == null) {
  190. return failed("您没有支付中的订单,请勿非法请求");
  191. }
  192. CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getCourseId());
  193. //课程组价格
  194. BigDecimal courseFee = courseGroupInfo.getFeeAmount();
  195. amount = amount.add(courseFee);
  196. //获取乐器的价格
  197. ClassPathResource classPathResource = new ClassPathResource("instruments.json"); //解析乐器数据
  198. BigDecimal instrumentPrice = new BigDecimal("0");//乐器价格
  199. String instrumentName = "";//乐器名称
  200. String instrumentId = order.getInstrument();
  201. String jsonString = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(), "UTF-8"));
  202. Instrument instrument = JSONObject.parseObject(jsonString, Instrument.class);
  203. if (order.getInstrument() != null && !order.getInstrument().isEmpty()) {
  204. instrumentPrice = new BigDecimal(instrument.getInstruments().get(instrumentId).get("referencePrice"));
  205. instrumentName = (String) instrument.getInstruments().get(instrumentId).get("index") + "-" +
  206. (String) instrument.getInstruments().get(instrumentId).get("name");
  207. }
  208. //2 版本为3.0( 26),不收乐器费用,收押金800放乐器费用
  209. if (courseGroupInfo.getFeeType().equals(26) && order.getInstrument() != null && !order.getInstrument().isEmpty()) {
  210. instrumentPrice = new BigDecimal("800");
  211. }
  212. amount = amount.add(instrumentPrice);
  213. //辅件价格
  214. String adjunctIds = order.getAdjunct();
  215. BigDecimal adjunctPrice = new BigDecimal("0");//辅件价格
  216. String adjunctName = "";//辅件名称
  217. if (adjunctIds != null && !adjunctIds.isEmpty()) {
  218. String[] adjunctIdArr = adjunctIds.split(",");
  219. for (String adjunctId : adjunctIdArr) {
  220. adjunctPrice = adjunctPrice.add(new BigDecimal(instrument.getAuxiliaries().get(adjunctId).get("referencePrice")));
  221. adjunctName += (String) instrument.getAuxiliaries().get(adjunctId).get("name") + "|";
  222. }
  223. }
  224. amount = amount.add(adjunctPrice);
  225. ApplyInfo applyInfo = applyInfoService.get(order.getUserId());
  226. order.setPoName(school.getName());
  227. order.setVoicyPart(courseGroupInfo.getSubName());
  228. order.setGroupId(courseGroupInfo.getId());
  229. order.setAmount(amount);
  230. order.setRemark(instrumentName);
  231. order.setTuiFee(courseFee);
  232. order.setGoodsFee(instrumentPrice);
  233. order.setSdName(adjunctName + "教材|琴谱");
  234. order.setSdFee(adjunctPrice);
  235. order.setType(1);
  236. order.setUserName(applyInfo.getName());
  237. String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
  238. order.setOrderNo(orderNo);
  239. order.setCreateTime(new Date()); //订单提交时间
  240. order.setStatus(1); //订单状态
  241. //获取分佣账户
  242. int accountId = Integer.parseInt(userOrder.getUAccount());
  243. Account routingAccount = accountService.get(accountId);
  244. order.setAccount(userOrder.getAccount());
  245. order.setUAccount(userOrder.getUAccount());
  246. //1.关闭订单
  247. HashMap<String, Object> upMap = new HashMap<>();
  248. upMap.put("status", 0);
  249. upMap.put("oldStatus", 1);
  250. upMap.put("id", userOrder.getId());
  251. orderService.updateByIdAndStatus(upMap);
  252. //2、插入订单
  253. orderService.insert(order);
  254. //3、修改分佣账户已收金额
  255. BigDecimal HasRouting = routingAccount.getHasRouting().add(order.getAmount()).subtract(userOrder.getAmount());
  256. routingAccount.setHasRouting(HasRouting);
  257. accountService.upByIdAndVersion(routingAccount);
  258. Map rqMap = orderService.getPayMap(routingAccount, order, school); //获取支付map
  259. return succeed(rqMap);
  260. }
  261. /**
  262. * 续费支付
  263. *
  264. * @return String
  265. * @throws Exception
  266. */
  267. // @ApiOperation(value = "续费支付", notes = "续费支付")
  268. @PostMapping("/renewalsPay")
  269. public Object renewalsPay(@ModelAttribute @Validated Renewals renewals) throws Exception {
  270. MecUser mecUser = applyInfoService.findMecUser(renewals.getUserId());
  271. if (mecUser == null) {
  272. return failed("续费用户不存在");
  273. }
  274. renewals.setBranchId(mecUser.getBranchId());
  275. //课程组价格
  276. List<MecCourse> courses = applyInfoService.queryUserCourse(renewals.getUserId());//获取续费课程
  277. if (courses == null) {
  278. return failed("您没有续费的课程");
  279. }
  280. MecCourse mecCourse4json = JSON.parseObject(renewals.getCourses(), MecCourse.class);
  281. if (mecCourse4json == null) {
  282. return failed("请选择续费课程");
  283. }
  284. //classType 小课1 大课2
  285. Integer buyCount = mecCourse4json.getBuyCount();
  286. if (buyCount <= 0 || buyCount > 20) {
  287. return failed("购买课程次数不能小于1,大于20");
  288. }
  289. BigDecimal amount = new BigDecimal("0"); //课程总价
  290. String remark = "";
  291. List<MecCourse> pickCourses = new ArrayList<>();
  292. for (int i = 0; i < courses.size(); i++) {
  293. MecCourse course = courses.get(i);
  294. if (mecCourse4json.getCourseId().equals(course.getCourseId().intValue())) {
  295. BigDecimal price = course.getClassType().equals(1) ? course.getPrice().multiply(BigDecimal.valueOf(buyCount)) : course.getPrice().multiply(BigDecimal.valueOf(course.getBuyCount()));
  296. amount = amount.add(price);
  297. remark += course.getClassName();
  298. if (course.getClassType().equals(1)) {
  299. course.setBuyCount(buyCount);
  300. }
  301. pickCourses.add(course);
  302. }
  303. }
  304. if (pickCourses.size() == 0) {
  305. return failed("请选择续费课程");
  306. }
  307. String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
  308. //获取分佣账户
  309. Integer branchId = 1002;//默认分佣账户;
  310. Account routingAccount = accountService.getRoutingAccount(branchId, amount);
  311. Order order = renewalsService.addRenewalsOrder(renewals, amount, orderNo, pickCourses, routingAccount, remark);
  312. Map rqMap = orderService.getPayMap(routingAccount, order, null); //获取支付map
  313. return succeed(rqMap);
  314. }
  315. /**
  316. * 交易查询
  317. *
  318. * @param merOrderNoList 用户订单号
  319. * @return
  320. * @throws Exception
  321. */
  322. //@PostMapping("/query")
  323. //@Scheduled(cron = "0/3 40 11 * * ?")
  324. public Object query(String merOrderNoList) throws Exception {
  325. String notifyUrl = ""; //回调地址
  326. Map<String, Object> resultMap = new LinkedHashMap<>();
  327. resultMap.put("merOrderNoList", merOrderNoList);
  328. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  329. return yqQueryService.orderQuery(requestMap);
  330. }
  331. /**
  332. * 用户信息(商户)查询
  333. *
  334. * @param sonMerNo 子商户号
  335. * @return
  336. * @throws Exception
  337. */
  338. @PostMapping("/queryaccount")
  339. public String queryAccount(String sonMerNo) throws Exception {
  340. String notifyUrl = ""; //回调地址
  341. Map<String, Object> resultMap = new LinkedHashMap<>();
  342. resultMap.put("merOrderNoList", sonMerNo);
  343. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  344. return yqQueryService.queryAccount(requestMap);
  345. }
  346. /**
  347. * 对账查询(定时任务每天对账)
  348. *
  349. * @return
  350. */
  351. public String queryBill() throws Exception {
  352. String notifyUrl = ""; //回调地址
  353. Map<String, Object> resultMap = new LinkedHashMap<>();
  354. resultMap.put("tradeDate", ""); //交易日期
  355. resultMap.put("payState", ""); //订单状态
  356. resultMap.put("tradeType", ""); //交易类型,不填为全部
  357. resultMap.put("channelType", ""); //通道类型,不填为全部
  358. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  359. return yqQueryService.billQuery(requestMap);
  360. }
  361. /**
  362. * 平台转账
  363. *
  364. * @return
  365. * @throws Exception
  366. */
  367. @PostMapping("/platformtransferacc")
  368. public String platformTransferAcc(String payeeNo, String payeeName, String amount, String remarks) throws Exception {
  369. String notifyUrl = ""; //回调地址
  370. String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
  371. Map<String, Object> resultMap = new LinkedHashMap<>();
  372. resultMap.put("payeeNo", payeeNo); //收款方商户号
  373. resultMap.put("payeeName", payeeName); //收款方姓名
  374. resultMap.put("amount", amount); //金额
  375. resultMap.put("merOrderNo", orderNo); //商户订单号
  376. resultMap.put("remarks", remarks); //备注
  377. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  378. return yqPayService.platformTransferAcc(requestMap);
  379. }
  380. /**
  381. * 提现短信
  382. *
  383. * @return
  384. * @throws Exception
  385. */
  386. @PostMapping("/sendsms")
  387. public Msg sendSms(@ModelAttribute Intfc intfc) throws Exception {
  388. String notifyUrl = ""; //回调地址
  389. String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo();
  390. Map<String, Object> resultMap = new LinkedHashMap<>();
  391. resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号
  392. resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号
  393. resultMap.put("amount", intfc.getAmount()); //提现金额
  394. resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号
  395. // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填)
  396. // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填)
  397. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  398. return yqPayService.intfc(requestMap);
  399. }
  400. /**
  401. * 提现
  402. *
  403. * @return
  404. * @throws Exception
  405. */
  406. @PostMapping("/intfc")
  407. public Msg intfc(@ModelAttribute Intfc intfc) throws Exception {
  408. String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //回调地址
  409. String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo();
  410. Map<String, Object> resultMap = new LinkedHashMap<>();
  411. resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号
  412. resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号
  413. resultMap.put("amount", intfc.getAmount()); //提现金额
  414. resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号
  415. // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填)
  416. // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填)
  417. resultMap.put("seqNo", ""); //流水号(选填)
  418. resultMap.put("smsCode", ""); //验证码(选填)
  419. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  420. return yqPayService.intfc(requestMap);
  421. }
  422. /**
  423. * 易乾异步通知接口
  424. *
  425. * @param msg
  426. * @return String
  427. * @throws Exception
  428. */
  429. @PostMapping("/notify")
  430. public Msg notify(@ModelAttribute Msg msg) throws Exception {
  431. logger.info(msg.toString());
  432. Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
  433. rqMap.put("code", msg.getCode());
  434. rqMap.put("msg", msg.getMsg());
  435. rqMap.put("responseType", msg.getResponseType());
  436. rqMap.put("responseParameters", msg.getResponseParameters());
  437. rqMap.put("sign", msg.getSign());
  438. boolean rs = YqPayUtil.verify(rqMap);
  439. msg.setMsg("fail");
  440. Map<String, String> notifyMap = new HashMap<>();
  441. if (rs) {
  442. notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
  443. }
  444. //支付中订单存在,更新状态
  445. if (msg.getResponseType().equals("1") && notifyMap.size() > 0) {
  446. String tradeState = msg.getCode().equals("88") ? "1" : "0";
  447. notifyMap.put("tradeState", tradeState);
  448. notifyMap.put("totalMoney", notifyMap.get("payAmount"));
  449. notifyMap.put("merOrderNo", notifyMap.get("merMerOrderNo"));
  450. this.updateOrder(notifyMap);
  451. msg.setCode("000000");
  452. msg.setMsg("success");
  453. }
  454. return msg;
  455. }
  456. @Scheduled(cron = "0/5 * * * * ?")
  457. //@RequestMapping("/getOrderStatus")
  458. public void getOrderStatus() throws Exception {
  459. List<Order> payingOrders = orderService.findPayingOrders();
  460. String merOrderNos = ""; //
  461. ArrayList<String> orderNoList = new ArrayList<String>();
  462. for (Order payingOrder : payingOrders) {
  463. String orderNo = payingOrder.getOrderNo();
  464. orderNoList.add(orderNo);
  465. merOrderNos += orderNo + ",";
  466. }
  467. if (merOrderNos.isEmpty()) {
  468. return;
  469. }
  470. merOrderNos = merOrderNos.substring(0, merOrderNos.length() - 1);
  471. String notifyUrl = ""; //回调地址
  472. Map<String, Object> resultMap = new LinkedHashMap<>();
  473. resultMap.put("merOrderNoList", merOrderNos);
  474. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  475. Msg queryRs = yqQueryService.orderQuery(requestMap);
  476. logger.info("查询易乾结果" + queryRs.toString());
  477. if (queryRs.getCode().equals("88")) {
  478. //更新订单状态
  479. String[] statusArr = {"0", "1", "7"};
  480. String responseParameters = queryRs.getResponseParameters();
  481. List<Map<String, String>> responseList = JSON.parseObject(responseParameters, List.class);
  482. for (Map<String, String> response : responseList) {
  483. Map<String, String> rpMap = response;
  484. if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
  485. this.updateOrder(rpMap);
  486. }
  487. if (orderNoList.contains(rpMap.get("merOrderNo"))) {
  488. orderNoList.remove(rpMap.get("merOrderNo"));
  489. }
  490. }
  491. this.failOrders(orderNoList);
  492. }
  493. }
  494. @Transactional(rollbackFor = Exception.class)
  495. public void updateOrder(Map<String, String> rpMap) throws Exception {
  496. int status = rpMap.get("tradeState").equals("1") ? 2 : 0;
  497. Order order = orderService.getOrderByOrderNo(rpMap.get("merOrderNo"));
  498. if (order == null) {
  499. return;
  500. }
  501. HashMap<String, Object> upMap = new HashMap<>();
  502. upMap.put("id", order.getId());
  503. upMap.put("oldStatus", 1);
  504. upMap.put("status", status);
  505. upMap.put("bank", rpMap.get("channelType"));
  506. if (order.getPayId() == null) {
  507. upMap.put("payId", rpMap.get("orderNo"));
  508. }
  509. ApplyInfo applyInfo = applyInfoService.get(order.getUserId());
  510. if (status == 2) {
  511. upMap.put("pay", rpMap.get("totalMoney"));
  512. upMap.put("payTime", new Date());
  513. if (order.getTuiFee() != null) { //乐团报名
  514. applyInfo.setStatus(1);
  515. applyInfoService.update(applyInfo);
  516. }
  517. }
  518. if (status == 0) {
  519. //失败减去已收款金额
  520. Account account = accountService.get(Integer.parseInt(order.getUAccount()));
  521. BigDecimal HasRouting = account.getHasRouting().subtract(order.getAmount());
  522. account.setHasRouting(HasRouting);
  523. accountService.upByIdAndVersion(account);
  524. //减去报名人数
  525. if (order.getTuiFee() != null) {
  526. CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getGroupId());
  527. courseGroupInfo.setRegNum(courseGroupInfo.getRegNum() - 1);
  528. CourseGroupInfoService.upByIdAndVersion(courseGroupInfo);
  529. }
  530. }
  531. orderService.updateByIdAndStatus(upMap);
  532. //推送mec
  533. if (status == 2) {
  534. if (order.getTuiFee() != null) { //乐团报名
  535. applyInfoService.userRegister(applyInfo.getPatriarchPhone(), order.getId()); //推送mec
  536. schoolService.sendPayMsg(applyInfo.getPatriarchPhone(), order.getAmount().toString());
  537. return;
  538. }
  539. if (order.getPromotionType().equals(1)) {
  540. RenewBean renewBean = new RenewBean();
  541. renewBean.setUserId(order.getUserId());
  542. renewBean.setType(101);
  543. renewBean.setIn(order.getAmount());
  544. renewBean.setOut(new BigDecimal(0));
  545. renewBean.setWay(30);
  546. applyInfoService.pushRenew(renewBean);
  547. return;
  548. }
  549. if (order.getType() != null && order.getType() == 3) {
  550. Renewals renewals = renewalsService.getRenewalsByOrderId(order.getId());
  551. RenewBean renewBean = new RenewBean();
  552. // renewBean.setUserId(renewals.getUserId());
  553. // renewBean.setClassId(renewals.getClassId());
  554. // renewBean.setWay(renewals.getWay());
  555. // renewBean.setPay(renewals.getPay());
  556. // renewBean.setChargeMode(renewals.getChangeMode());
  557. // renewBean.setBuy(new BigDecimal(renewals.getBuy()));
  558. // renewBean.setPrice(renewals.getPrice());
  559. renewBean.setUserId(renewals.getUserId());
  560. renewBean.setType(101);
  561. renewBean.setIn(renewals.getPay());
  562. renewBean.setOut(new BigDecimal(0));
  563. renewBean.setWay(renewals.getWay());
  564. applyInfoService.pushRenew(renewBean);
  565. }
  566. }
  567. }
  568. @Transactional(rollbackFor = Exception.class)
  569. public void failOrders(ArrayList<String> orderNoList) throws Exception {
  570. if (orderNoList.size() == 0) {
  571. return;
  572. }
  573. Calendar beforeTime = Calendar.getInstance();
  574. beforeTime.add(Calendar.MINUTE, -15);// 5分钟之前的时间
  575. Date beforeDate = beforeTime.getTime();
  576. Map<String, Object> rqMap = new HashMap<>();
  577. rqMap.put("orderNoList", orderNoList);
  578. rqMap.put("beforeTime", beforeDate);
  579. List<Order> payingOrders = orderService.findPayingOrdersOver(rqMap);
  580. for (Order order : payingOrders) {
  581. HashMap<String, Object> upMap = new HashMap<>();
  582. upMap.put("id", order.getId());
  583. upMap.put("oldStatus", 1);
  584. upMap.put("status", 0);
  585. //失败减去已收款金额
  586. Account account = accountService.get(Integer.parseInt(order.getUAccount()));
  587. BigDecimal HasRouting = account.getHasRouting().subtract(order.getAmount());
  588. account.setHasRouting(HasRouting);
  589. accountService.upByIdAndVersion(account);
  590. //减去报名人数
  591. if (order.getTuiFee() != null) {
  592. CourseGroupInfo courseGroupInfo = CourseGroupInfoService.get(order.getGroupId());
  593. courseGroupInfo.setRegNum(courseGroupInfo.getRegNum() - 1);
  594. CourseGroupInfoService.upByIdAndVersion(courseGroupInfo);
  595. }
  596. orderService.updateByIdAndStatus(upMap);
  597. }
  598. }
  599. /**
  600. * 双11活动小课续费支付
  601. *
  602. * @return String
  603. * @throws Exception
  604. */
  605. // @ApiOperation(value = "续费支付", notes = "续费支付")
  606. @PostMapping("/promotionPay")
  607. public Object promotionPay(@ModelAttribute @Validated Renewals renewals) throws Exception {
  608. MecUser mecUser = applyInfoService.findMecUser(renewals.getUserId());
  609. if (mecUser == null) {
  610. return failed("续费用户不存在");
  611. }
  612. ClassPathResource classPathResource = new ClassPathResource("branchRule.json"); //规则json
  613. String branchRuleJson = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(), "UTF-8"));
  614. List<BranchRule> branchRules = JSONArray.parseObject(branchRuleJson, List.class);
  615. BigDecimal amount = new BigDecimal("0");
  616. for (BranchRule branchRule : branchRules) {
  617. if (!branchRule.getBranchId().equals(mecUser.getBranchId())) continue;
  618. amount = branchRule.getBranchPrice();
  619. }
  620. if (!amount.equals(renewals.getPay())) {
  621. return failed("金额有误!请勿非法请求");
  622. }
  623. if (amount.equals(new BigDecimal(0))) {
  624. return failed("分部没有相应的活动");
  625. }
  626. String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
  627. //获取分佣账户
  628. Integer branchId = 1002;//默认分佣账户;
  629. Account routingAccount = accountService.getRoutingAccount(branchId, amount);
  630. Order order = renewalsService.promotionPay(mecUser, amount, orderNo, routingAccount);
  631. Map rqMap = orderService.getPayMap(routingAccount, order, null); //获取支付map
  632. return succeed(rqMap);
  633. }
  634. @RequestMapping("/getOrderStatus")
  635. public Object getOrderStatus(String orderNo) throws Exception {
  636. Order order = orderService.getOrderByOrderNo(orderNo);
  637. if(order == null){
  638. return failed("订单信息不存在");
  639. }
  640. if(order.getStatus().equals(2)){
  641. return succeed("SUCCESS");
  642. }
  643. if(order.getStatus().equals(1)){
  644. return succeed("ING");
  645. }
  646. return succeed("FAIL");
  647. }
  648. }