| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.IOException; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | @RestController |
| | | @Slf4j |
| | |
| | | |
| | | if (!party.isEnabled()) { |
| | | |
| | | return Result.succeed("用户已锁定"); |
| | | return Result.succeed("User is locked"); |
| | | } |
| | | |
| | | // C2C用户未结束订单最大数量 |
| | |
| | | this.c2cOrderService.saveOpen(c2cOrder,remarks); |
| | | data.put("order_no", c2cOrder.getOrderNo()); |
| | | resultObject.setData(data); |
| | | resultObject.setCode(0); |
| | | } catch (BusinessException e) { |
| | | log.error("用户"+direction+"执行异常1当前用户uid:"+party.getUserCode()+"当前用户名:"+party.getUserName()+"生成的订单号:"+orderNo); |
| | | resultObject.setCode(1); |
| | |
| | | } catch (Throwable t) { |
| | | log.error("用户"+direction+"执行异常2当前用户uid:"+party.getUserCode()+"当前用户名:"+party.getUserName()+"生成的订单号:"+orderNo); |
| | | resultObject.setCode(1); |
| | | resultObject.setMsg("程序错误"); |
| | | //resultObject.setMsg("程序错误"); |
| | | resultObject.setMsg(t.getMessage()); |
| | | log.error("error:", t); |
| | | } finally { |
| | | if (lock) { |
| | |
| | | return resultObject; |
| | | } |
| | | |
| | | @RequestMapping(action + "pay_finish.action") |
| | | public Object pay_finish(HttpServletRequest request) { |
| | | String order_no = request.getParameter("order_no"); |
| | | String safe_password = request.getParameter("safe_password"); |
| | | |
| | | Result resultObject = new Result(); |
| | | |
| | | boolean lock = false; |
| | | |
| | | try { |
| | | |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | if (null == partyId) { |
| | | throw new BusinessException("请重新登录"); |
| | | } |
| | | |
| | | User party = userService.getById(partyId); |
| | | if (Constants.SECURITY_ROLE_TEST.equals(party.getRoleName())) { |
| | | throw new BusinessException("无权限"); |
| | | } |
| | | |
| | | if (!userService.checkLoginSafeword(partyId, safe_password)) { |
| | | throw new BusinessException("资金密码错误"); |
| | | } |
| | | |
| | | if (!party.isEnabled()) { |
| | | return Result.succeed("User is locked"); |
| | | } |
| | | |
| | | C2cOrder order = this.c2cOrderService.get(order_no); |
| | | if (null == order) { |
| | | throw new BusinessException("订单不存在"); |
| | | } |
| | | |
| | | if (partyId.equals(order.getPartyId())) { |
| | | // 用户操作 |
| | | if (!"buy".equals(order.getDirection())) { |
| | | throw new BusinessException("用户不能支付卖单"); |
| | | } |
| | | } else if (partyId.equals(order.getC2cUserPartyId())) { |
| | | // 承兑商操作 |
| | | if (!"sell".equals(order.getDirection())) { |
| | | throw new BusinessException("承兑商不能支付买单"); |
| | | } |
| | | } else { |
| | | throw new BusinessException("无权限"); |
| | | } |
| | | |
| | | if (!"0".equals(order.getState())) { |
| | | throw new BusinessException("订单不是未付款状态"); |
| | | } |
| | | |
| | | if (!C2cOrderLock.add(order_no)) { |
| | | throw new BusinessException(1, "请稍后再试"); |
| | | } |
| | | |
| | | lock = true; |
| | | |
| | | order.setState("1"); |
| | | order.setPayTime(new Date()); |
| | | this.c2cOrderService.updateById(order); |
| | | |
| | | if (Arrays.asList("0", "1").contains(order.getState())) { |
| | | // 只有未付款和已付款的订单存入redis |
| | | redisTemplate.opsForValue().set(RedisKeys.C2C_ORDER_NO + order.getOrderNo(), order); |
| | | } else { |
| | | redisTemplate.delete(RedisKeys.C2C_ORDER_NO + order.getOrderNo()); |
| | | } |
| | | |
| | | // 订单自动取消时间和确认收款超时时间(0未付款/1已付款) |
| | | Map<String, Long> map = (Map<String, Long>) redisTemplate.opsForValue().get(RedisKeys.C2C_ORDER_NO_EXPIRE_TIME); |
| | | if (null == map) { |
| | | map = new ConcurrentHashMap<String, Long>(); |
| | | } |
| | | if (!Arrays.asList("0", "1").contains(order.getState())) { |
| | | map.remove(order.getOrderNo() + "_auto_cancel"); |
| | | map.remove(order.getOrderNo() + "_expire"); |
| | | } else { |
| | | if (Arrays.asList("0").contains(order.getState())) { |
| | | map.put(order.getOrderNo() + "_auto_cancel", order.getCreateTime().getTime() + (long) order.getExpireTime() * 60 * 1000); |
| | | map.put(order.getOrderNo() + "_expire", order.getCreateTime().getTime() + (long) order.getExpireTime() * 60 * 1000 * 2); |
| | | } else if (Arrays.asList("1").contains(order.getState())) { |
| | | map.put(order.getOrderNo() + "_auto_cancel", order.getCreateTime().getTime() + (long) order.getExpireTime() * 60 * 1000); |
| | | map.put(order.getOrderNo() + "_expire", order.getPayTime().getTime() + (long) order.getExpireTime() * 60 * 1000); |
| | | } |
| | | } |
| | | this.redisTemplate.opsForValue().set(RedisKeys.C2C_ORDER_NO_EXPIRE_TIME, map); |
| | | |
| | | resultObject.setCode(0); |
| | | } catch (BusinessException e) { |
| | | resultObject.setCode(1); |
| | | resultObject.setMsg(e.getMessage()); |
| | | log.error("error: {}", e.getMessage()); |
| | | } catch (Throwable t) { |
| | | resultObject.setCode(1); |
| | | //resultObject.setMsg("程序错误"); |
| | | resultObject.setMsg(t.getMessage()); |
| | | log.error("error:", t); |
| | | } finally { |
| | | if (lock) { |
| | | ThreadUtils.sleep(100); |
| | | C2cOrderLock.remove(order_no); |
| | | } |
| | | } |
| | | |
| | | return resultObject; |
| | | } |
| | | |
| | | /** |
| | | * 订单放行 |
| | | */ |
| | | @RequestMapping(action + "order_pass.action") |
| | | public Object order_pass(HttpServletRequest request) { |
| | | String order_no = request.getParameter("order_no"); |
| | | String safe_password = request.getParameter("safe_password"); |
| | | |
| | | Result resultObject = new Result(); |
| | | boolean lock = false; |
| | | try { |
| | | |
| | | String partyId = SecurityUtils.getCurrentUserId(); |
| | | if (null == partyId) { |
| | | throw new BusinessException("请重新登录"); |
| | | } |
| | | |
| | | User party = userService.getById(partyId); |
| | | if (Constants.SECURITY_ROLE_TEST.equals(party.getRoleName())) { |
| | | throw new BusinessException("无权限"); |
| | | } |
| | | |
| | | if (!C2cOrderLock.add(order_no)) { |
| | | throw new BusinessException(1, "请稍后再试"); |
| | | } |
| | | |
| | | lock = true; |
| | | |
| | | if (!userService.checkLoginSafeword(partyId, safe_password)) { |
| | | throw new BusinessException("资金密码错误"); |
| | | } |
| | | if (!party.isEnabled()) { |
| | | return Result.succeed("User is locked"); |
| | | } |
| | | |
| | | C2cOrder order = this.c2cOrderService.get(order_no); |
| | | if (null == order || !order.getPartyId().equals(partyId)) { |
| | | throw new BusinessException("订单不存在"); |
| | | } |
| | | if ("0".equals(order.getState())) { |
| | | throw new BusinessException("待付款的订单无法放行"); |
| | | } |
| | | if ("3".equals(order.getState())) { |
| | | resultObject.setCode(0); |
| | | return resultObject; |
| | | //throw new BusinessException("订单已完成,无法放行"); |
| | | } |
| | | if ("4".equals(order.getState())) { |
| | | throw new BusinessException("订单已取消,无法放行"); |
| | | } |
| | | |
| | | this.c2cOrderService.saveOrderPass(order); |
| | | resultObject.setCode(0); |
| | | } catch (BusinessException e) { |
| | | resultObject.setCode(1); |
| | | resultObject.setMsg(e.getMessage()); |
| | | log.error("error:{}", e.getMessage()); |
| | | } catch (Throwable t) { |
| | | resultObject.setCode(1); |
| | | //resultObject.setMsg("程序错误"); |
| | | resultObject.setMsg(t.getMessage()); |
| | | log.error("error:", t); |
| | | } finally { |
| | | if (lock) { |
| | | ThreadUtils.sleep(100); |
| | | C2cOrderLock.remove(order_no); |
| | | } |
| | | } |
| | | |
| | | return resultObject; |
| | | } |
| | | |
| | | |
| | | } |