package com.ruoyi.web.H5Api.api; import java.math.BigDecimal; 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.pcApi.domain.TCommodityManage; import com.ruoyi.web.pcApi.domain.TCustomerCollectionOrFootprint; import com.ruoyi.web.pcApi.domain.TCustomerCoupon; import com.ruoyi.web.pcApi.domain.TCustomerDistributionRecord; import com.ruoyi.web.pcApi.domain.TCustomerDistributionSet; import com.ruoyi.web.pcApi.domain.TCustomerIntegralRecord; import com.ruoyi.web.pcApi.domain.TCustomerIntegralSet; import com.ruoyi.web.pcApi.domain.TCustomerManage; import com.ruoyi.web.pcApi.domain.TCustomerNotice; 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.mapper.TCommodityManageMapper; import com.ruoyi.web.pcApi.service.ITCommodityManageService; import com.ruoyi.web.pcApi.service.ITCustomerCollectionOrFootprintService; import com.ruoyi.web.pcApi.service.ITCustomerCouponService; import com.ruoyi.web.pcApi.service.ITCustomerDistributionRecordService; import com.ruoyi.web.pcApi.service.ITCustomerDistributionSetService; import com.ruoyi.web.pcApi.service.ITCustomerIntegralRecordService; import com.ruoyi.web.pcApi.service.ITCustomerIntegralSetService; import com.ruoyi.web.pcApi.service.ITCustomerManageService; import com.ruoyi.web.pcApi.service.ITCustomerNoticeService; 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 TCommodityManageMapper commodityManageMapper; @Autowired private ITOrderEvaluateService orderEvaluateService; @Autowired private ITCustomerDistributionRecordService customerDistributionRecordService; @Autowired private ITCustomerDistributionSetService customerDistributionSetService; @Autowired private ITCustomerManageService customerManageService; @Autowired private ITCustomerIntegralRecordService customerIntegralRecordService; @Autowired private ITCustomerNoticeService customerNoticeService; @Autowired private ITOrderManageTotalService orderManageTotalService; @Autowired private ITCustomerIntegralSetService customerIntegralSetService; @Autowired private LinePay linePay; /** * 增加收藏 * * @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(); if (commodityManage.getStock() - orderNum >= 0) { commodityManage.setStock(commodityManage.getStock() - orderNum); // 增加销售数量 commodityManage.setSaleNum(commodityManage.getSaleNum() == null ? 0 + orderNum : commodityManage.getSaleNum() + orderNum); commodityManageMapper.updateTCommodityManage(commodityManage); } 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) { // 接入支付接口 LinePayAPOD resp = linePay.pay(orderManageTotal, orderManageTotalService.getDeptName(orderManageTotal.getId())); //ok if (resp != null && "0000".equals(resp.returnCode)) { //set the transactionId to the db. orderManageTotal.setTransactionId(resp.info.transactionId); 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) { // TOrderManageTotal select = new TOrderManageTotal(); select.setTransactionId(param.getTransactionId()); select.setId(param.getOrderId()); select.setDelFlag("1"); orderManageTotalService.updateTOrderManageTotal(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) { // TOrderManageTotal select = new TOrderManageTotal(); select.setTransactionId(param.getTransactionId()); select.setId(param.getOrderId()); List lst = orderManageTotalService.selectTOrderManageTotalList(select); TOrderManageTotal orderManageTotal = lst.get(0); // 接入支付接口 LinePayConfirmAPOD resp = linePay.payConfirm(orderManageTotal.getTransactionId(), orderManageTotal.getOrderMoney().longValue()); if ("0000".equals(resp.returnCode)) { //set order list TOrderManage orderPram = new TOrderManage(); orderPram.setOrderTotalId(param.getOrderId()); List orderList = orderManageService.selectTOrderManageList(orderPram); orderManageTotal.setOrderList(orderList); TCustomerManage customer = customerManageService.selectTCustomerManageById(getLoginUser().getLoginCustomer().getId()); // 多/单订单支付 for (TOrderManage order : orderManageTotal.getOrderList()) { TOrderManage orderManage = orderManageService.selectTOrderManageById(order.getId()); /** 通知待使用*/ customerNoticeService.insertTCustomerNotice(TCustomerNotice.builder().customerId(customer.getId()).commodityId(order.getId()) .content("您有一张" + orderManage.getCommodityName() + "商品待使用。").title("订单信息") .createBy(orderManage.getCreateBy()).status("0") .build()); // 若使用积分 则订单金额减去积分金额 if(orderManageTotal.getIntegralDeduction() != BigDecimal.ZERO && orderManageTotal.getIntegralDeduction() != null){ order.setOrderMoney(orderManage.getOrderMoney().subtract(orderManageTotal.getIntegralDeduction())); order.setIntegralDeduction(orderManageTotal.getIntegralDeduction()); } order.setStatus("1");// 已支付 支付状态,若接入支付回调,则变更为支付中状态-9,支付回调成功后变更为1-已支付 order.setPayDate(new Date()); orderManageService.updateTOrderManage(order); } // 获取场馆积分获取比例 List tCustomerIntegralSets = customerIntegralSetService.selectTCustomerIntegralSetList(TCustomerIntegralSet.builder().deptId(getLoginUser().getLoginCustomer().getDeptId()).build()); BigDecimal orderIntegral = orderManageTotal.getOrderMoney(); if (tCustomerIntegralSets.size() > 0) { // 计算分销人员奖励 BigDecimal proportion = new BigDecimal(tCustomerIntegralSets.get(0).getProportion()).divide(new BigDecimal("100")); orderIntegral= (orderManageTotal.getOrderMoney().multiply(proportion)).setScale(0, BigDecimal.ROUND_DOWN); } // 记录用户积分 customerIntegralRecordService.insertTCustomerIntegralRecord(TCustomerIntegralRecord.builder(). supIntegral(customer.getIntegral()==null?"0":customer.getIntegral().toString()).integralNum(String.valueOf(orderIntegral.longValue())).integralType("1") .createBy(getLoginUser().getLoginCustomer().getUsername()).orderId(orderManageTotal.getId()).customerId(customer.getId()).deptId(getLoginUser().getDeptId()) .build()); /** 增加用户积分*/ customer.setIntegral(customer.getIntegral()==null?0:customer.getIntegral() + orderIntegral.longValue()); customerManageService.updateTCustomerManage(customer); // 会员减去积分 if(orderManageTotal.getIntegralDeduction() != null && orderManageTotal.getIntegralDeduction() != BigDecimal.ZERO){ customer.setIntegral(customer.getIntegral() - orderManageTotal.getIntegralDeduction().longValue()); customerManageService.updateTCustomerManage(customer); // 记录减去用户积分 customerIntegralRecordService.insertTCustomerIntegralRecord(TCustomerIntegralRecord.builder(). supIntegral(customer.getIntegral().toString()).integralNum(orderManageTotal.getIntegralDeduction().toString()).integralType("4") .createBy(getLoginUser().getLoginCustomer().getUsername()).orderId(orderManageTotal.getOrderList().get(0).getId()).customerId(customer.getId()).deptId(getLoginUser().getDeptId()) .build()); } // 是否有分销 if (orderManageTotal.getDistributionId() != null) { // 获取场馆分销比例 List tCustomerDistributionSets = customerDistributionSetService.selectTCustomerDistributionSetList(TCustomerDistributionSet.builder().deptId(getLoginUser().getLoginCustomer().getDeptId()).build()); if (tCustomerDistributionSets.size() > 0) { // 计算分销人员奖励 BigDecimal proportion = new BigDecimal(tCustomerDistributionSets.get(0).getProportion()).divide(new BigDecimal("100")); BigDecimal integral = (orderManageTotal.getOrderMoney().multiply(proportion)).setScale(0, BigDecimal.ROUND_DOWN); // 增加用户积分 TCustomerManage customerManage = customerManageService.selectTCustomerManageById(orderManageTotal.getDistributionId()); customerManage.setIntegral(customerManage.getIntegral() + integral.longValue()); customerManageService.updateTCustomerManage(customerManage); // 记录分销 customerDistributionRecordService.insertTCustomerDistributionRecord( TCustomerDistributionRecord.builder().customerId(orderManageTotal.getDistributionId()).deptId(getLoginUser().getLoginCustomer().getDeptId()) .distributionIntegral(integral.toString()).createBy(getLoginUser().getLoginCustomer().getUsername()).orderId(orderManageTotal.getId()).build()); } } // 查询总订单下-是否还有未支付的信息 List orderManageList = orderManageService.selectTOrderManageList(TOrderManage.builder(). orderTotalId(orderManageTotal.getId()).status("0").build()); if(orderManageList.size() <= 0){ orderManageTotal.setStatus("1"); orderManageTotalService.updateTOrderManageTotal(orderManageTotal); } return AjaxResult.success("支払成功"); } else { return AjaxResult.success("支払失敗した"); } } }