package com.ruoyi.web.H5Api.api; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.commons.collections.map.HashedMap; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.RepeatSubmit; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.enums.OperatorType; import com.ruoyi.web.line.LineCallBankParam; import com.ruoyi.web.line.LinePay; import com.ruoyi.web.line.LinePayAPOD; import com.ruoyi.web.line.LinePayConfirmAPOD; import com.ruoyi.web.line.PayApi; import com.ruoyi.web.pcApi.domain.TCommodityManage; import com.ruoyi.web.pcApi.domain.TCommoditySchedul; import com.ruoyi.web.pcApi.domain.TCustomerCollectionOrFootprint; import com.ruoyi.web.pcApi.domain.TCustomerCoupon; import com.ruoyi.web.pcApi.domain.TCustomerShoppingCart; import com.ruoyi.web.pcApi.domain.TOrderEvaluate; import com.ruoyi.web.pcApi.domain.TOrderManage; import com.ruoyi.web.pcApi.domain.TOrderManageTotal; import com.ruoyi.web.pcApi.service.ITCommodityManageService; import com.ruoyi.web.pcApi.service.ITCommoditySchedulService; import com.ruoyi.web.pcApi.service.ITCustomerCollectionOrFootprintService; import com.ruoyi.web.pcApi.service.ITCustomerCouponService; import com.ruoyi.web.pcApi.service.ITCustomerShoppingCartService; import com.ruoyi.web.pcApi.service.ITOrderEvaluateService; import com.ruoyi.web.pcApi.service.ITOrderManageService; import com.ruoyi.web.pcApi.service.ITOrderManageTotalService; /** * h5端-订单信息api * * @author lzp * @date 2022-08-26 */ @RestController @RequestMapping("/h5/order") @CrossOrigin public class H5OrderController extends BaseController { @Autowired private ITCommodityManageService commodityManageService; @Autowired private ITCustomerCouponService customerCouponService; @Autowired private ITCustomerCollectionOrFootprintService customerCollectionOrFootprintService; @Autowired private ITCustomerShoppingCartService shoppingCartService; @Autowired private ITOrderManageService orderManageService; @Autowired private RedissonClient redissonClient; @Autowired private ITOrderEvaluateService orderEvaluateService; @Autowired private ITOrderManageTotalService orderManageTotalService; @Autowired private LinePay linePay; @Autowired private ITCommoditySchedulService commoditySchedulService; @Autowired private PayApi payApi; /** * 增加收藏 * * @param customerCollectionOrFootprint * @return */ @Log(title = "用户增加收藏", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/addCollection") public AjaxResult selectPreferredList(@RequestBody TCustomerCollectionOrFootprint customerCollectionOrFootprint) { customerCollectionOrFootprint.setType("2"); customerCollectionOrFootprint.setCustomerId(getLoginUser().getLoginCustomer().getId()); customerCollectionOrFootprint.setDeptId(getLoginUser().getLoginCustomer().getDeptId()); customerCollectionOrFootprint.setCreateBy(getLoginUser().getLoginCustomer().getUsername()); return AjaxResult.success(customerCollectionOrFootprintService.insertTCustomerCollectionOrFootprint(customerCollectionOrFootprint)); } /** * 商品详情-移除收藏 * * @param commodityId 商品id * @return */ @Log(title = "用户移除收藏", operatorType = OperatorType.MOBILE, businessType = BusinessType.DELETE) @RepeatSubmit @DeleteMapping("/delCollection/{commodityId}") public AjaxResult delCollection(@PathVariable("commodityId") Long commodityId) { int i = customerCollectionOrFootprintService.deleteByCustomerIdAndCommodityId(TCustomerCollectionOrFootprint.builder().commodityId(commodityId).customerId(getLoginUser().getLoginCustomer().getId()).type("2").build()); return AjaxResult.success(i); } /** * 用户领取优惠券 * * @param customerCoupon * @return */ @Log(title = "用户领取优惠券", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/receiveCoupon") public AjaxResult receiveCoupon(@RequestBody TCustomerCoupon customerCoupon) { customerCoupon.setCustomerId(getLoginUser().getLoginCustomer().getId()); List tCustomerCoupons = customerCouponService.selectTCustomerCouponList(customerCoupon); if (tCustomerCoupons.size() > 0) { return AjaxResult.error("该用户已有该商品优惠券,无法重复领取"); } customerCoupon.setStatus("0"); customerCoupon.setDeptId(getLoginUser().getLoginCustomer().getDeptId()); customerCoupon.setCreateBy(getLoginUser().getLoginCustomer().getUsername()); return AjaxResult.success(customerCouponService.insertTCustomerCoupon(customerCoupon)); } /** * 增加购物车 * * @param shoppingCart * @return */ @Log(title = "用户添加购物车", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/addShoppingCar") public AjaxResult addShoppingCar(@RequestBody TCustomerShoppingCart shoppingCart) { shoppingCart.setCustomerId(getLoginUser().getLoginCustomer().getId()); shoppingCart.setDeptId(getLoginUser().getLoginCustomer().getDeptId()); shoppingCart.setCreateBy(getLoginUser().getLoginCustomer().getUsername()); // 商品信息/商品旅程信息 TCommodityManage commodityManage = commodityManageService.selectTCommodityManageById(shoppingCart.getCommodityId()); // 判断是否有未使用的优惠券 shoppingCart.setIsCoupon("0"); if (commodityManage.getCommodityCouponList() != null && commodityManage.getCommodityCouponList().size() > 0) { List tCustomerCoupons = customerCouponService.selectTCustomerCouponList(TCustomerCoupon.builder().customerId(getLoginUser().getLoginCustomer().getId()).couponId(commodityManage.getCommodityCouponList().get(0).getId()).status("0").build()); if (tCustomerCoupons.size() > 0) { shoppingCart.setIsCoupon("1"); } } return AjaxResult.success(shoppingCartService.insertTCustomerShoppingCart(shoppingCart)); } /** * 提交订单(分布式) * * @param orderManageTotal * @return */ @Log(title = "用户提交订单", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/submitOrder") public AjaxResult submitOrder(@RequestBody TOrderManageTotal orderManageTotal) { orderManageTotal.setStatus("0");// 已支付 orderManageTotal.setPayDate(new Date()); orderManageTotal.setCreateBy(getLoginUser().getLoginCustomer().getUsername()); orderManageTotal.setDeptId(getLoginUser().getLoginCustomer().getDeptId()); orderManageTotal.setAipayNo(String.valueOf(new Date().getTime())); orderManageTotalService.insertTOrderManageTotal(orderManageTotal); for (TOrderManage order : orderManageTotal.getOrderList()) { order.setCreateBy(getLoginUser().getLoginCustomer().getUsername()); order.setOrderNo(String.valueOf(new Date().getTime())); order.setOrderTotalId(orderManageTotal.getId()); order.setCustomerId(getLoginUser().getLoginCustomer().getId()); order.setStatus("0");// 已支付 order.setPayDate(new Date()); order.setDeptId(getLoginUser().getLoginCustomer().getDeptId()); order.setOrderMoney(order.getMoney()); // 分布式redis锁 // 商品信息/商品旅程信息 TCommodityManage commodityManage = commodityManageService.selectTCommodityManageById(order.getCommodityId()); RLock lock = redissonClient.getLock("submitOrder@" + commodityManage.getId()); lock.lock(); try { // 判断是否有未使用的优惠券 TCustomerCoupon customerCoupon = null; if (commodityManage.getCommodityCouponList() != null && commodityManage.getCommodityCouponList().size() > 0) { List tCustomerCoupons = customerCouponService.selectTCustomerCouponList(TCustomerCoupon.builder().customerId(getLoginUser().getLoginCustomer().getId()).couponId(commodityManage.getCommodityCouponList().get(0).getId()).status("0").build()); if (tCustomerCoupons.size() > 0) { order.setIsCoupon("1"); customerCoupon = tCustomerCoupons.get(0); } } // 减掉库存 Integer orderNum = order.getAdultNum() + order.getChildNum(); //座位信息取得。 TCommoditySchedul schedulSeat = new TCommoditySchedul(); schedulSeat.setCommodityId(commodityManage.getId()); schedulSeat.setDepartureDate(order.getDepartureDate()); List seatList = commoditySchedulService.selectTCommoditySchedulList(schedulSeat); long stock = 0; for (TCommoditySchedul seatRcd : seatList) { stock = seatRcd.getSeatNum(); if (stock - orderNum >= 0) { seatRcd.setSeatNum(stock - orderNum); commoditySchedulService.updateTCommoditySchedul(seatRcd); } else { return AjaxResult.error(commodityManage.getCommodityName() + "在庫がございません" + orderNum); } } /** 优惠券失效*/ if (customerCoupon != null) { customerCoupon.setStatus("1"); customerCoupon.setUpdateBy(order.getCreateBy()); customerCouponService.updateTCustomerCoupon(customerCoupon); } orderManageService.insertTOrderManage(order); // 删除购物车信息 if (order.getShopCarId() != null) { shoppingCartService.deleteTCustomerShoppingCartById(order.getShopCarId()); } } catch (RuntimeException e) { e.printStackTrace(); return AjaxResult.error("系统异常,请稍后再试"); } finally { // 释放锁 lock.unlock(); } } Map map = new HashedMap(); map.put("totalId",orderManageTotal.getId()); map.put("orderList",orderManageTotal.getOrderList()); return AjaxResult.success(map); } /** * 增加订单信息评价 * * @param orderEvaluate * @return */ @Log(title = "增加订单信息评价", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/addOrderEvalute") public AjaxResult addOrderEvalute(@RequestBody TOrderEvaluate orderEvaluate) { // 更新订单状态 orderManageService.updateTOrderManage(TOrderManage.builder().id(orderEvaluate.getOrderId()).status("9").build()); orderEvaluate.setCustomerId(getLoginUser().getLoginCustomer().getId()); orderEvaluate.setDeptId(getLoginUser().getLoginCustomer().getDeptId()); return AjaxResult.success(orderEvaluateService.insertTOrderEvaluate(orderEvaluate)); } /** * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付 * * @param orderManageTotal * @return */ @Log(title = "用户支付", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/aiPay") public AjaxResult aiPay(@RequestBody TOrderManageTotal orderManageTotal) { //按分 int len = orderManageTotal.getOrderList().size(); //total BigDecimal total = orderManageTotal.getOrderMoney(); BigDecimal sendAmout = BigDecimal.ZERO; //提交多条数据的时候要进行按分。 if (len > 1) { int cnt = 0; BigDecimal anbun = BigDecimal.ZERO; // BigDecimal preTotal = BigDecimal.ZERO; for (TOrderManage orderManageTemp : orderManageTotal.getOrderList()) { preTotal=preTotal.add(orderManageTemp.getOrderMoney()); } for (TOrderManage orderManage : orderManageTotal.getOrderList()) { cnt++; //amount BigDecimal amount = BigDecimal.ZERO; if (cnt == len) { amount = total.subtract(anbun); } else { amount = orderManage.getOrderMoney().multiply(total).divide(preTotal, BigDecimal.ROUND_UP); anbun = anbun.add(amount); } orderManage.setOrderMoney(amount); sendAmout = sendAmout.add(amount); } } else { orderManageTotal.getOrderList().get(0).setOrderMoney(total); sendAmout = total; } //0元LINE支付不要。 if (sendAmout.intValue() == 0) { // payApi.pay(orderManageTotal, getLoginUser()); return AjaxResult.success("Line支払不要"); } // 接入支付接口 LinePayAPOD resp = linePay.pay(orderManageTotal, orderManageTotalService.getDeptName(orderManageTotal.getId())); //ok if (resp != null && "0000".equals(resp.returnCode)) { //set the transactionId to the db. for (TOrderManage order : orderManageTotal.getOrderList()) { order.setTransactionId(resp.info.transactionId); orderManageService.updateTOrderManage(order); } //update ordermoney orderManageTotalService.updateTOrderManageTotal(orderManageTotal); return AjaxResult.success(resp.info); } //line支付error return AjaxResult.error("LINE PAY失敗しました。"); } /** * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付 * * @param orderManageTotal * @return */ @Log(title = "キャンセルCallBack", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/cancel") public AjaxResult apayCancelCallBack(@RequestBody LineCallBankParam param) { String[] orderIds = param.getOrderId().split("_"); for (int i = 1; i < orderIds.length; i++) { // TOrderManage select = new TOrderManage(); select.setTransactionId(param.getTransactionId()); select.setId(Long.valueOf(orderIds[i])); select.setDelFlag("1"); orderManageService.updateTOrderManage(select); } return AjaxResult.success("支払キャンセルしました。"); } /** * 支付回调接口,需接入支付接口后,填写该回调URL,进行订单支付 * * @param orderManageTotal * @return */ @Log(title = "用户支付CallBack", operatorType = OperatorType.MOBILE, businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/confirm") public AjaxResult aiPayCallBack(@RequestBody LineCallBankParam param) { String[] orderIds = param.getOrderId().split("_"); List orderList = new ArrayList(); for (int i = 1; i < orderIds.length; i++) { // TOrderManage order = orderManageService.selectTOrderManageById(Long.valueOf(orderIds[i])); orderList.add(order); } TOrderManageTotal orderManageTotal = orderManageTotalService.selectTOrderManageTotalById(Long.valueOf(orderIds[0])); orderManageTotal.setOrderList(orderList); // 接入支付接口 LinePayConfirmAPOD resp = null; //0元支付不要。 if (orderManageTotal.getOrderMoney().longValue() > 0) { resp = linePay.payConfirm(param.getTransactionId(), orderManageTotal.getOrderMoney().longValue()); } if (resp == null || "0000".equals(resp.returnCode)) { payApi.pay(orderManageTotal, getLoginUser()); return AjaxResult.success("支払成功"); } else { return AjaxResult.success("支払失敗した"); } } }