|
|
@@ -0,0 +1,347 @@
|
|
|
+package com.ruoyi.web.H5Api.api;
|
|
|
+
|
|
|
+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.common.utils.DateUtils;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.web.pcApi.domain.*;
|
|
|
+import com.ruoyi.web.pcApi.mapper.TCommodityManageMapper;
|
|
|
+import com.ruoyi.web.pcApi.service.*;
|
|
|
+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.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpRequest;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.Reader;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 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;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加收藏
|
|
|
+ *
|
|
|
+ * @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<TCustomerCoupon> 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<TCustomerCoupon> 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<TCustomerCoupon> 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<String,Object> 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) {
|
|
|
+ //TODO 接入支付接口
|
|
|
+
|
|
|
+ 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<TCustomerIntegralSet> 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<TCustomerDistributionSet> 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<TOrderManage> orderManageList = orderManageService.selectTOrderManageList(TOrderManage.builder().
|
|
|
+ orderTotalId(orderManageTotal.getId()).status("0").build());
|
|
|
+ if(orderManageList.size() <= 0){
|
|
|
+ orderManageTotal.setStatus("1");
|
|
|
+ orderManageTotalService.updateTOrderManageTotal(orderManageTotal);
|
|
|
+ }
|
|
|
+
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|