package cc.mrbird.febs.mall.service.impl; import cc.mrbird.febs.common.entity.FebsResponse; import cc.mrbird.febs.common.enumerates.*; import cc.mrbird.febs.common.exception.FebsException; import cc.mrbird.febs.common.utils.AppContants; import cc.mrbird.febs.common.utils.LoginUserUtil; import cc.mrbird.febs.common.utils.MallUtils; import cc.mrbird.febs.common.utils.ShareCodeUtil; import cc.mrbird.febs.mall.dto.AddOrderItemDto; import cc.mrbird.febs.mall.dto.clothes.*; import cc.mrbird.febs.mall.entity.*; import cc.mrbird.febs.mall.mapper.*; import cc.mrbird.febs.mall.service.ApiClothesOrderService; import cc.mrbird.febs.mall.service.IApiMallMemberWalletService; import cc.mrbird.febs.mall.service.IMallMoneyFlowService; import cc.mrbird.febs.mall.vo.clothes.*; import cc.mrbird.febs.pay.model.BrandWCPayRequestData; import cc.mrbird.febs.pay.service.IPayService; import cc.mrbird.febs.pay.service.IXcxPayService; import cc.mrbird.febs.rabbit.producter.AgentProducer; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; import java.util.stream.Collectors; @Slf4j @Service @RequiredArgsConstructor public class ApiClothesOrderServiceImpl extends ServiceImpl implements ApiClothesOrderService { private final ClothesOrderMapper clothesOrderMapper; private final ClothesOrderItemMapper clothesOrderItemMapper; private final ClothesOrderDraftMapper clothesOrderDraftMapper; private final ClothesTypeMapper clothesTypeMapper; private final ClothesTypeClothMapper clothesTypeClothMapper; private final ClothesTypePatternMapper clothesTypePatternMapper; private final ClothesTypeSizeMapper clothesTypeSizeMapper; private final ClothesSizeMapper clothesSizeMapper; private final ClothesTypeLocationMapper clothesTypeLocationMapper; private final ClothesTypeArtMapper clothesTypeArtMapper; private final ClothesMemberStatureMapper clothesMemberStatureMapper; private final ClothesPatternMapper clothesPatternMapper; private final ClothesLocationMapper clothesLocationMapper; private final ClothesClothMapper clothesClothMapper; private final ClothesArtMapper clothesArtMapper; private final MallAddressInfoMapper mallAddressInfoMapper; private final MallMemberCouponMapper mallMemberCouponMapper; private final MallGoodsCouponMapper mallGoodsCouponMapper; private final MallExpressInfoMapper mallExpressInfoMapper; private final AgentProducer agentProducer; private final IXcxPayService iXcxPayService; private final IPayService payService; private final MallMemberMapper mallMemberMapper; private final IApiMallMemberWalletService apiMallMemberWalletService; private final IMallMoneyFlowService mallMoneyFlowService; private final ClothesPatternRemarkMapper clothesPatternRemarkMapper; private final ClothesLocationRemarkMapper clothesLocationRemarkMapper; @Override public FebsResponse myDraft(ApiMyDraftPageDto dto) { Long memberId = LoginUserUtil.getLoginUser().getId(); dto.setMemberId(memberId); // 创建分页对象,传入当前页和每页大小 Page page = new Page<>(dto.getPageNow(), dto.getPageSize()); // 调用Mapper方法获取活动分页数据 Page voPage = clothesOrderDraftMapper.selectPageInDraft(page, dto); if (CollUtil.isNotEmpty(voPage.getRecords())){ List records = voPage.getRecords(); for (ApiMyDraftVo record : records){ Long typeId = record.getTypeId(); ClothesType clothesType = clothesTypeMapper.selectById(typeId); record.setTypeName(clothesType.getName()); record.setTypeImage(clothesType.getImage()); Long sizeId = ObjectUtil.defaultIfNull(record.getSizeId(),0L); List clothesTypeSizes = clothesTypeSizeMapper.selectList(Wrappers.lambdaQuery().eq(ClothesTypeSize::getTypeId, typeId)); if (CollUtil.isNotEmpty(clothesTypeSizes)){ Set collect = clothesTypeSizes.stream().map(ClothesTypeSize::getSizeId).collect(Collectors.toSet()); if(CollUtil.isNotEmpty( collect) && collect.contains(sizeId)){ ClothesSize clothesSize = clothesSizeMapper.selectById(sizeId); if (ObjectUtil.isNotNull(clothesSize)){ record.setSizeName(clothesSize.getName()); record.setSizePrice(clothesSize.getPrice()); } } } Long statureId = ObjectUtil.defaultIfNull(record.getStatureId(),0L); ClothesMemberStature clothesMemberStature = clothesMemberStatureMapper.selectById(statureId); if (ObjectUtil.isNotEmpty(clothesMemberStature)){ record.setStatureName(clothesMemberStature.getName()); } BigDecimal totalPatternPrice = BigDecimal.ZERO; List clothesPatternRemarks = clothesPatternRemarkMapper.selectList( Wrappers.lambdaQuery(ClothesPatternRemark.class) .eq(ClothesPatternRemark::getSourceId, record.getId()) .eq(ClothesPatternRemark::getType, SocialPatternLocationTypeEnum.DRAFT.getValue()) ); if(CollUtil.isNotEmpty(clothesPatternRemarks)){ List vos = new ArrayList<>(); for (ClothesPatternRemark entity : clothesPatternRemarks){ Long patternId = entity.getPatternId(); ApiClothesPatternInfoVo vo = new ApiClothesPatternInfoVo(); vo.setPatternId(patternId); ClothesPattern clothesPattern = clothesPatternMapper.selectById(patternId); vo.setPatternName(clothesPattern.getName()); vo.setPatternPrice(clothesPattern.getPrice()); vo.setPatternRemark(entity.getRemark()); vos.add(vo); totalPatternPrice = totalPatternPrice.add(clothesPattern.getPrice()); } record.setPatternList(vos); } BigDecimal totalLocationPrice = BigDecimal.ZERO; List clothesLocationRemarks = clothesLocationRemarkMapper.selectList( Wrappers.lambdaQuery(ClothesLocationRemark.class) .eq(ClothesLocationRemark::getSourceId, record.getId()) .eq(ClothesLocationRemark::getType, SocialPatternLocationTypeEnum.DRAFT.getValue()) ); if(CollUtil.isNotEmpty(clothesLocationRemarks)){ List vos = new ArrayList<>(); for (ClothesLocationRemark entity : clothesLocationRemarks){ Long locationId = entity.getLocationId(); ApiClothesLocationInfoVo vo = new ApiClothesLocationInfoVo(); vo.setLocationId(locationId); ClothesLocation location = clothesLocationMapper.selectById(locationId); vo.setLocationName(location.getName()); vo.setLocationPrice(location.getPrice()); vo.setLocationRemark(entity.getRemark()); vos.add(vo); totalLocationPrice = totalLocationPrice.add(location.getPrice()); } record.setLocationList(vos); } Long artId = ObjectUtil.defaultIfNull(record.getArtId(),0L); List clothesTypeArts = clothesTypeArtMapper.selectList(Wrappers.lambdaQuery(ClothesTypeArt.class).eq(ClothesTypeArt::getTypeId, typeId)); if(CollUtil.isNotEmpty(clothesTypeArts)){ Set artIds = clothesTypeArts.stream().map(ClothesTypeArt::getArtId).collect(Collectors.toSet()); if (CollUtil.isNotEmpty(artIds) && artIds.contains(artId)){ ClothesArt clothesArt = clothesArtMapper.selectById(artId); if (ObjectUtil.isNotNull(clothesArt)){ record.setArtName(clothesArt.getName()); record.setArtPrice(clothesArt.getPrice()); } } } Long clothId = ObjectUtil.defaultIfNull(record.getClothId(),0L); List clothesTypeCloths = clothesTypeClothMapper.selectList(Wrappers.lambdaQuery(ClothesTypeCloth.class).eq(ClothesTypeCloth::getTypeId, typeId)); if(CollUtil.isNotEmpty(clothesTypeCloths)){ Set clothIds = clothesTypeCloths.stream().map(ClothesTypeCloth::getClothId).collect(Collectors.toSet()); if (CollUtil.isNotEmpty(clothIds) && clothIds.contains(clothId)){ ClothesCloth clothesCloth = clothesClothMapper.selectById(clothId); if (ObjectUtil.isNotNull(clothesCloth)){ record.setClothName(clothesCloth.getName()); record.setClothPrice(clothesCloth.getPrice()); } } } BigDecimal amount = record.getClothPrice() .add(totalPatternPrice) .add(record.getArtPrice()) .add(record.getSizePrice()) .add(totalLocationPrice).setScale(2, RoundingMode.DOWN); record.setAmount(amount); } } return new FebsResponse().success().data(voPage); } @Override public FebsResponse draftToDesign(ApiClothesOrderDraftDto dto) { Long memberId = LoginUserUtil.getLoginUser().getId(); ApiMyDraftVo record = new ApiMyDraftVo(); Long id = dto.getId(); ClothesOrderDraft clothesOrderDraft = clothesOrderDraftMapper.selectById(id); if (ObjectUtil.isNotNull(clothesOrderDraft)){ Long typeId = clothesOrderDraft.getTypeId(); ClothesType clothesType = clothesTypeMapper.selectById(typeId); record.setTypeId(typeId); record.setTypeName(clothesType.getName()); record.setTypeImage(clothesType.getImage()); Long sizeId = ObjectUtil.defaultIfNull(clothesOrderDraft.getSizeId(),0L); List clothesTypeSizes = clothesTypeSizeMapper.selectList(Wrappers.lambdaQuery().eq(ClothesTypeSize::getTypeId, typeId)); if (CollUtil.isNotEmpty(clothesTypeSizes)){ Set collect = clothesTypeSizes.stream().map(ClothesTypeSize::getSizeId).collect(Collectors.toSet()); if(CollUtil.isNotEmpty( collect) && collect.contains(sizeId)){ ClothesSize clothesSize = clothesSizeMapper.selectById(sizeId); if(ObjectUtil.isNotNull(clothesSize)){ record.setSizeId(sizeId); record.setSizeName(clothesSize.getName()); record.setSizePrice(clothesSize.getPrice()); } } } Long statureId = ObjectUtil.defaultIfNull(clothesOrderDraft.getStatureId(),0L); ClothesMemberStature clothesMemberStature = clothesMemberStatureMapper.selectById(statureId); if (ObjectUtil.isNotNull(clothesMemberStature)){ record.setStatureId(clothesMemberStature.getId()); record.setStatureName(clothesMemberStature.getName()); } BigDecimal totalPatternPrice = BigDecimal.ZERO; List clothesPatternRemarks = clothesPatternRemarkMapper.selectList( Wrappers.lambdaQuery(ClothesPatternRemark.class) .eq(ClothesPatternRemark::getSourceId, record.getId()) .eq(ClothesPatternRemark::getType, SocialPatternLocationTypeEnum.DRAFT.getValue()) ); if(CollUtil.isNotEmpty(clothesPatternRemarks)){ List vos = new ArrayList<>(); for (ClothesPatternRemark entity : clothesPatternRemarks){ Long patternId = entity.getPatternId(); ApiClothesPatternInfoVo vo = new ApiClothesPatternInfoVo(); vo.setPatternId(patternId); ClothesPattern clothesPattern = clothesPatternMapper.selectById(patternId); vo.setPatternName(clothesPattern.getName()); vo.setPatternPrice(clothesPattern.getPrice()); vo.setPatternRemark(entity.getRemark()); vos.add(vo); totalPatternPrice = totalPatternPrice.add(clothesPattern.getPrice()); } record.setPatternList(vos); } BigDecimal totalLocationPrice = BigDecimal.ZERO; List clothesLocationRemarks = clothesLocationRemarkMapper.selectList( Wrappers.lambdaQuery(ClothesLocationRemark.class) .eq(ClothesLocationRemark::getSourceId, record.getId()) .eq(ClothesLocationRemark::getType, SocialPatternLocationTypeEnum.DRAFT.getValue()) ); if(CollUtil.isNotEmpty(clothesLocationRemarks)){ List vos = new ArrayList<>(); for (ClothesLocationRemark entity : clothesLocationRemarks){ Long locationId = entity.getLocationId(); ApiClothesLocationInfoVo vo = new ApiClothesLocationInfoVo(); vo.setLocationId(locationId); ClothesLocation location = clothesLocationMapper.selectById(locationId); vo.setLocationName(location.getName()); vo.setLocationPrice(location.getPrice()); vo.setLocationRemark(entity.getRemark()); vos.add(vo); totalLocationPrice = totalLocationPrice.add(location.getPrice()); } record.setLocationList(vos); } Long artId = ObjectUtil.defaultIfNull(clothesOrderDraft.getArtId(),0L); List clothesTypeArts = clothesTypeArtMapper.selectList(Wrappers.lambdaQuery(ClothesTypeArt.class).eq(ClothesTypeArt::getTypeId, typeId)); if(CollUtil.isNotEmpty(clothesTypeArts)){ Set artIds = clothesTypeArts.stream().map(ClothesTypeArt::getArtId).collect(Collectors.toSet()); if (CollUtil.isNotEmpty(artIds) && artIds.contains(artId)){ ClothesArt clothesArt = clothesArtMapper.selectById(artId); if (ObjectUtil.isNotNull(clothesArt)){ record.setArtId(artId); record.setArtName(clothesArt.getName()); record.setArtPrice(clothesArt.getPrice()); } } } Long clothId = ObjectUtil.defaultIfNull(clothesOrderDraft.getClothId(),0L); List clothesTypeCloths = clothesTypeClothMapper.selectList(Wrappers.lambdaQuery(ClothesTypeCloth.class).eq(ClothesTypeCloth::getTypeId, typeId)); if(CollUtil.isNotEmpty(clothesTypeCloths)){ Set clothIds = clothesTypeCloths.stream().map(ClothesTypeCloth::getClothId).collect(Collectors.toSet()); if (CollUtil.isNotEmpty(clothIds) && clothIds.contains(clothId)){ ClothesCloth clothesCloth = clothesClothMapper.selectById(clothId); if (ObjectUtil.isNotNull(clothesCloth)){ record.setClothId(clothId); record.setClothName(clothesCloth.getName()); record.setClothPrice(clothesCloth.getPrice()); } } } BigDecimal amount = record.getClothPrice() .add(totalPatternPrice) .add(record.getArtPrice()) .add(record.getSizePrice()) .add(totalLocationPrice).setScale(2, RoundingMode.DOWN); record.setAmount(amount); } return new FebsResponse().success().data(record); } @Override public FebsResponse draftDel(ApiClothesOrderDraftDto dto) { Long memberId = LoginUserUtil.getLoginUser().getId(); clothesOrderDraftMapper.update(null, Wrappers.lambdaUpdate(ClothesOrderDraft.class) .set(ClothesOrderDraft::getDelFlag, ClothesEnum.UP.getCode()) .eq(ClothesOrderDraft::getId, dto.getId()) .eq(ClothesOrderDraft::getMemberId, memberId) ); return new FebsResponse().success().message("操作成功"); } private BigDecimal getCouponAmountReduce(BigDecimal totalAmount, Long memberCouponId) { BigDecimal reduceAmount = BigDecimal.ZERO; HashMap couponAmountMap = new HashMap<>(); // 获取优惠券信息 Optional optionalMallMemberCoupon = Optional.ofNullable(mallMemberCouponMapper.selectById(memberCouponId)); MallMemberCoupon mallMemberCoupon = optionalMallMemberCoupon.orElse(null); if (ObjectUtil.isNull(mallMemberCoupon) || mallMemberCoupon.getState() != 1) { return reduceAmount; } Optional optionalMallGoodsCoupon = Optional.ofNullable(mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId())); MallGoodsCoupon mallGoodsCoupon = optionalMallGoodsCoupon.orElse(null); if (ObjectUtil.isNull(mallGoodsCoupon)) { return reduceAmount; } BigDecimal costAmount = mallGoodsCoupon.getCostAmount(); BigDecimal realAmount = mallGoodsCoupon.getRealAmount(); Integer type = mallGoodsCoupon.getType(); if (MallGoodsCoupon.TYPE_ONE == type) { reduceAmount = mallGoodsCoupon.getRealAmount(); } else if (MallGoodsCoupon.TYPE_TWO == type) { BigDecimal divideTime = totalAmount.divide(costAmount, 0, RoundingMode.DOWN); // 累计减免次数 reduceAmount = divideTime.multiply(realAmount); // 实际减免金额 } return reduceAmount; } @Override @Transactional(rollbackFor = Exception.class) public FebsResponse createOrder(ApiClothesOrderInfoDto dto) { Long memberId = LoginUserUtil.getLoginUser().getId(); if (CollUtil.isEmpty(dto.getItems())) { throw new FebsException("参数错误"); } //选择了地址才允许下单 Long addressId = dto.getAddressId(); MallAddressInfo address = mallAddressInfoMapper.selectAddressInfoByMemberIdAndId(memberId, addressId); if(ObjectUtil.isNull( address)){ throw new FebsException("请选择收货地址"); } if (StrUtil.isBlank(address.getLatitude())||StrUtil.isBlank(address.getLongitude())) { throw new FebsException("请添加地址"); } Long typeId = dto.getTypeId(); ClothesType clothesType = clothesTypeMapper.selectById(typeId); if(ObjectUtil.isNull( clothesType)){ throw new FebsException("请选择商品"); } if(ClothesEnum.UP.getCode() != clothesType.getState()){ throw new FebsException("商品已下架"); } String orderNo = MallUtils.getOrderNum("CL"); ClothesOrder orderInfo = new ClothesOrder(); orderInfo.setMemberId(memberId); orderInfo.setTypeId(typeId); orderInfo.setOrderNo(orderNo); orderInfo.setOrderTime(new Date()); orderInfo.setGoodsCnt(dto.getCnt()); orderInfo.setStatus(ClothesOrderStatusEnum.WAIT_PAY.getValue()); orderInfo.setRemark(dto.getRemark()); orderInfo.setAddressId(addressId); orderInfo.setName(address.getName()); orderInfo.setPhone(address.getPhone()); orderInfo.setAddress(address.getProvince() + address.getCity() + address.getArea() + address.getAddress()); orderInfo.setLatitude(address.getLatitude()); orderInfo.setLongitude(address.getLongitude()); clothesOrderMapper.insert(orderInfo); Long orderId = orderInfo.getId(); /** * 创建订单子表 */ BigDecimal total = BigDecimal.ZERO; for (ApiClothesOrderItemInfoDto item : dto.getItems()) { ClothesOrderItem orderItem = new ClothesOrderItem(); orderItem.setMemberId(memberId); orderItem.setOrderId(orderId); orderItem.setType(item.getType()); orderItem.setItemCnt(dto.getCnt()); if (ClothesOrderItemEnum.CLOTH.getCode() == item.getType()) { ClothesCloth cloth = clothesClothMapper.selectById(item.getSkuId()); if (ObjectUtil.isNull(cloth)) { throw new FebsException("请选择布料"); } orderItem.setItemId(cloth.getId()); orderItem.setName(cloth.getName()); orderItem.setPrice(cloth.getPrice()); orderItem.setItemCnt(item.getCnt()); orderItem.setAmount(cloth.getPrice().multiply(new BigDecimal(orderItem.getItemCnt())).setScale(2, RoundingMode.DOWN)); clothesOrderItemMapper.insert(orderItem); total = total.add(orderItem.getAmount()); continue; } else if (ClothesOrderItemEnum.SIZE.getCode() == item.getType()) { ClothesSize size = clothesSizeMapper.selectById(item.getSkuId()); if (ObjectUtil.isNull(size)) { throw new FebsException("请选择尺寸"); } orderItem.setItemId(size.getId()); orderItem.setName(size.getName()); orderItem.setPrice(size.getPrice()); orderItem.setItemCnt(item.getCnt()); orderItem.setAmount(orderItem.getPrice().multiply(new BigDecimal(orderItem.getItemCnt())).setScale(2, RoundingMode.DOWN)); clothesOrderItemMapper.insert(orderItem); total = total.add(orderItem.getAmount()); continue; } else if (ClothesOrderItemEnum.LOCATION.getCode() == item.getType()) { ClothesLocation location = clothesLocationMapper.selectById(item.getSkuId()); if (ObjectUtil.isNull(location)) { throw new FebsException("请选择图案位置"); } orderItem.setItemId(location.getId()); orderItem.setName(location.getName()); orderItem.setPrice(location.getPrice()); orderItem.setItemCnt(item.getCnt()); orderItem.setAmount(orderItem.getPrice().multiply(new BigDecimal(orderItem.getItemCnt())).setScale(2, RoundingMode.DOWN)); orderItem.setRemark(item.getPatternRemark()); clothesOrderItemMapper.insert(orderItem); total = total.add(orderItem.getAmount()); continue; } else if (ClothesOrderItemEnum.PATTERN.getCode() == item.getType()) { ClothesPattern pattern = clothesPatternMapper.selectById(item.getSkuId()); if (ObjectUtil.isNull(pattern)) { throw new FebsException("请选择图案"); } orderItem.setItemId(pattern.getId()); orderItem.setRemark(item.getPatternRemark()); orderItem.setName(pattern.getName()); orderItem.setPrice(pattern.getPrice()); orderItem.setItemCnt(item.getCnt()); orderItem.setRemark(item.getPatternRemark()); orderItem.setAmount(orderItem.getPrice().multiply(new BigDecimal(orderItem.getItemCnt())).setScale(2, RoundingMode.DOWN)); clothesOrderItemMapper.insert(orderItem); total = total.add(orderItem.getAmount()); continue; } else if (ClothesOrderItemEnum.ART.getCode() == item.getType()) { ClothesArt art = clothesArtMapper.selectById(item.getSkuId()); if (ObjectUtil.isNull(art)) { throw new FebsException("请选择工艺"); } orderItem.setItemId(art.getId()); orderItem.setName(art.getName()); orderItem.setPrice(art.getPrice()); orderItem.setItemCnt(item.getCnt()); orderItem.setAmount(orderItem.getPrice().multiply(new BigDecimal(orderItem.getItemCnt())).setScale(2, RoundingMode.DOWN)); clothesOrderItemMapper.insert(orderItem); total = total.add(orderItem.getAmount()); continue; } else if (ClothesOrderItemEnum.CUSTOMIZE.getCode() == item.getType()) { ClothesMemberStature clothesMemberStature = clothesMemberStatureMapper.selectById(item.getSkuId()); orderItem.setItemId(clothesMemberStature.getId()); orderItem.setName(clothesMemberStature.getName()); orderItem.setPrice(BigDecimal.ZERO); orderItem.setItemCnt(item.getCnt()); orderItem.setAmount(orderItem.getPrice().multiply(new BigDecimal(orderItem.getItemCnt())).setScale(2, RoundingMode.DOWN)); clothesOrderItemMapper.insert(orderItem); total = total.add(orderItem.getAmount()); continue; } else { throw new FebsException("详情参数错误"); } } total = total.multiply(new BigDecimal(orderInfo.getGoodsCnt())).setScale(2, RoundingMode.DOWN); orderInfo.setAmount(total); /** * 计算优惠金额、运费和实际支付金额 */ BigDecimal discountAmount = BigDecimal.ZERO; MallMemberCoupon mallMemberCoupon = new MallMemberCoupon(); if(ObjectUtil.isNotEmpty(dto.getMemberCouponId())){ Long memberCouponId = dto.getMemberCouponId(); mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId); if(ObjectUtil.isEmpty(mallMemberCoupon)){ throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用"); } Integer state = mallMemberCoupon.getState(); if(1 != state){ throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用"); } List items = dto.getItems(); discountAmount = getCouponAmountReduce(total, memberCouponId); // 优惠金额大于0,则更新优惠卷 if(discountAmount.compareTo(BigDecimal.ZERO) > 0){ mallMemberCoupon.setState(2); mallMemberCouponMapper.updateById(mallMemberCoupon); orderInfo.setMemberCouponId(memberCouponId); } } orderInfo.setDiscountAmount(discountAmount); BigDecimal deliveryAmount = dto.getDeliveryAmount() == null ? BigDecimal.ZERO : dto.getDeliveryAmount(); orderInfo.setCarriage(deliveryAmount); BigDecimal realAmount = total.subtract(discountAmount).add(deliveryAmount).setScale(2, RoundingMode.DOWN); orderInfo.setRealAmount(realAmount); clothesOrderMapper.updateById(orderInfo); //过期时间修改成24小时 agentProducer.sendClothesOrderCancelDelayMsg(orderInfo.getId(), 24 * 60 * 60 * 1000L); ApiClothesOrderInfoVo apiClothesOrderInfoVo = new ApiClothesOrderInfoVo(); apiClothesOrderInfoVo.setOrderId(orderInfo.getId()); return new FebsResponse().success().data(apiClothesOrderInfoVo); } @Override public void orderCancelDelayClothes(long id) { ClothesOrder orderInfo = clothesOrderMapper.selectById(id); if (ObjectUtil.isNull(orderInfo)) { log.error("自动取消订单参数错误:{}", id); return; } if (orderInfo.getStatus() == ClothesOrderStatusEnum.WAIT_PAY.getValue()) { log.info("自动取消订单:{},{}", orderInfo.getMemberId(), id); clothesOrderMapper.update(null, Wrappers.lambdaUpdate(ClothesOrder.class) .set(ClothesOrder::getStatus, ClothesOrderStatusEnum.CANCEL.getValue()) .set(ClothesOrder::getUpdatedTime, new Date()) .eq(ClothesOrder::getId, id) ); } } @Override public FebsResponse cancelOrder(Long id) { ClothesOrder clothesOrder = clothesOrderMapper.selectById(id); if( ObjectUtil.isNotNull(clothesOrder) && ClothesEnum.DOWN.getCode() == clothesOrder.getDelFlag() && ClothesOrderStatusEnum.WAIT_PAY.getValue() == clothesOrder.getStatus() ){ clothesOrderMapper.update(null, Wrappers.lambdaUpdate(ClothesOrder.class) .set(ClothesOrder::getStatus, ClothesOrderStatusEnum.CANCEL.getValue()) .set(ClothesOrder::getUpdatedTime, new Date()) .eq(ClothesOrder::getId, id) ); Long memberCouponId = ObjectUtil.defaultIfNull(clothesOrder.getMemberCouponId(), 0L); MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId); if(ObjectUtil.isNotNull(mallMemberCoupon)){ mallMemberCoupon.setState(1); mallMemberCouponMapper.updateById(mallMemberCoupon); } } return new FebsResponse().success().message("操作成功"); } @Override public FebsResponse delOrder(Long id) { ClothesOrder clothesOrder = clothesOrderMapper.selectById(id); if( ObjectUtil.isNotNull(clothesOrder) && ClothesEnum.DOWN.getCode() == clothesOrder.getDelFlag() && ClothesOrderStatusEnum.CANCEL.getValue() == clothesOrder.getStatus() ){ clothesOrderMapper.update(null, Wrappers.lambdaUpdate(ClothesOrder.class) .set(ClothesOrder::getDelFlag, ClothesEnum.UP.getCode()) .set(ClothesOrder::getUpdatedTime, new Date()) .eq(ClothesOrder::getId, id) ); } return new FebsResponse().success().message("操作成功"); } private String balancePay(ClothesOrder orderInfo, String tradePwd, String field) { if (StrUtil.isBlank(tradePwd)) { throw new FebsException("支付密码错误"); } MallMember mallMember = mallMemberMapper.selectById(orderInfo.getMemberId()); if (StrUtil.isBlank(mallMember.getTradePassword())) { throw new FebsException("未设置支付密码"); } if (!SecureUtil.md5(tradePwd).equals(mallMember.getTradePassword())) { throw new FebsException("支付密码错误"); } int reduce = apiMallMemberWalletService.reduce(orderInfo.getAmount(), mallMember.getId(), field); if (reduce == 2) { throw new FebsException("余额不足"); } return orderInfo.getOrderNo(); } @Override @Transactional(rollbackFor = Exception.class) public FebsResponse payOrder(ApiClothesOrderPayDto dto) { MallMember member = LoginUserUtil.getLoginUser(); ClothesOrder clothesOrder = clothesOrderMapper.selectById(dto.getId()); if (ObjectUtil.isNull(clothesOrder)){ throw new FebsException("订单不存在"); } if (ClothesEnum.DOWN.getCode() != clothesOrder.getDelFlag()) { throw new FebsException("订单不存在"); } if (ClothesOrderStatusEnum.WAIT_PAY.getValue() != clothesOrder.getStatus()) { throw new FebsException("订单不是待支付状态"); } if( ClothesOrderPayTypeEnum.WECHAT.getCode() != dto.getType() && ClothesOrderPayTypeEnum.ALIPAY.getCode() != dto.getType() && ClothesOrderPayTypeEnum.BALANCE.getCode() != dto.getType() ){ throw new FebsException("支付方式不支持"); } String payResultStr = ""; String wxResultStr = ""; switch (dto.getType()) { case 1: BrandWCPayRequestData brandWCPayRequestData = null; try { brandWCPayRequestData = iXcxPayService.startPayment(clothesOrder); } catch (Exception e) { throw new FebsException("支付失败"); } wxResultStr = JSONUtil.toJsonStr(brandWCPayRequestData); payResultStr = brandWCPayRequestData.getPrepay_id(); clothesOrder.setPayMethod(ClothesOrderPayTypeEnum.WECHAT.getName()); break; case 2: if (StrUtil.isNotBlank(clothesOrder.getPayOrderNo())) { payResultStr = clothesOrder.getPayOrderNo(); } else { payResultStr = payService.aliPay(clothesOrder); } clothesOrder.setPayOrderNo(payResultStr); clothesOrder.setPayMethod(ClothesOrderPayTypeEnum.ALIPAY.getName()); break; case 3: Date date = new Date(); payResultStr = balancePay(clothesOrder, dto.getTradePwd(), "balance"); clothesOrder.setPayOrderNo(clothesOrder.getOrderNo()); clothesOrder.setPayMethod(ClothesOrderPayTypeEnum.BALANCE.getName()); clothesOrder.setStatus(ClothesOrderStatusEnum.WAIT_SHIPPING.getValue()); clothesOrder.setPayTime(date); clothesOrder.setPayResult(1); clothesOrder.setDeliveryState(OrderDeliveryStateEnum.DELIVERY_WAIT.getValue()); mallMoneyFlowService.addMoneyFlow( member.getId(), clothesOrder.getRealAmount().negate(), ScoreFlowTypeEnum.PAY_BALANCE.getValue(), clothesOrder.getOrderNo(), FlowTypeEnum.BALANCE.getValue(), StrUtil.format(ScoreFlowTypeEnum.PAY_BALANCE.getDesc(),clothesOrder.getRealAmount()), 2 ); Long memberCouponId = ObjectUtil.defaultIfNull(clothesOrder.getMemberCouponId(), 0L); MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId); if(ObjectUtil.isNotNull(mallMemberCoupon)){ mallMemberCoupon.setState(2); mallMemberCouponMapper.updateById(mallMemberCoupon); } break; default: } clothesOrderMapper.updateById(clothesOrder); Map map = new HashMap<>(); map.put("orderInfo", payResultStr); map.put("orderNo", clothesOrder.getOrderNo()); map.put("orderId", clothesOrder.getId()); map.put("wxResultStr", wxResultStr); ApiClothesOrderPayVo apiClothesOrderPayVo = new ApiClothesOrderPayVo(); apiClothesOrderPayVo.setOrder(map); apiClothesOrderPayVo.setType(dto.getType()); return new FebsResponse().success().data(apiClothesOrderPayVo).message("支付成功"); } @Override public FebsResponse findOrderList(ApiClothesOrderListVoDto dto) { Long memberId = LoginUserUtil.getLoginUser().getId(); dto.setMemberId(memberId); // 创建分页对象,传入当前页和每页大小 Page page = new Page<>(dto.getPageNow(), dto.getPageSize()); // 调用Mapper方法获取活动分页数据 Page voPage = clothesOrderMapper.selectPageInOrder(page, dto); return new FebsResponse().success().data(voPage); } private List buildOrderItemInfo(Long id) { List vos = new ArrayList<>(); List clothesOrderItems = clothesOrderItemMapper.selectList( Wrappers.lambdaQuery(ClothesOrderItem.class) .eq(ClothesOrderItem::getOrderId, id) ); if(CollUtil.isNotEmpty(clothesOrderItems)){ for (ClothesOrderItem item : clothesOrderItems) { ApiClothesOrderItemInfoVo vo = new ApiClothesOrderItemInfoVo(); vo.setName(item.getName()); vo.setPrice(item.getPrice()); vo.setType(item.getType()); vo.setRemark(item.getRemark()); vos.add( vo); } } return vos; } @Override public FebsResponse orderDetails(Long id) { Long memberId = LoginUserUtil.getLoginUser().getId(); ApiClothesOrderListInfoVo apiClothesOrderListInfoVo = new ApiClothesOrderListInfoVo(); ClothesOrder clothesOrder = clothesOrderMapper.selectById(id); if(ObjectUtil.isNull(clothesOrder)){ throw new FebsException("订单不存在"); } apiClothesOrderListInfoVo.setId(clothesOrder.getId()); apiClothesOrderListInfoVo.setOrderNo(clothesOrder.getOrderNo()); apiClothesOrderListInfoVo.setOrderTime(clothesOrder.getOrderTime()); apiClothesOrderListInfoVo.setAmount(clothesOrder.getAmount()); apiClothesOrderListInfoVo.setRealAmount(clothesOrder.getRealAmount()); ClothesType clothesType = clothesTypeMapper.selectById(clothesOrder.getTypeId()); if(ObjectUtil.isNotNull(clothesType)){ apiClothesOrderListInfoVo.setTypeName(clothesType.getName()); apiClothesOrderListInfoVo.setTypeImage(clothesType.getImage()); } apiClothesOrderListInfoVo.setGoodsCnt(clothesOrder.getGoodsCnt()); apiClothesOrderListInfoVo.setPayMethod(clothesOrder.getPayMethod()); apiClothesOrderListInfoVo.setStatus(clothesOrder.getStatus()); apiClothesOrderListInfoVo.setLongitude(clothesOrder.getLongitude()); apiClothesOrderListInfoVo.setLatitude(clothesOrder.getLatitude()); apiClothesOrderListInfoVo.setName(clothesOrder.getName()); apiClothesOrderListInfoVo.setPhone(clothesOrder.getPhone()); apiClothesOrderListInfoVo.setAddress(clothesOrder.getAddress()); MallExpressInfo mallExpressInfo = mallExpressInfoMapper.selectOne( Wrappers.lambdaQuery(MallExpressInfo.class) .eq(MallExpressInfo::getOrderId, clothesOrder.getId()) .eq(MallExpressInfo::getOrderNo, clothesOrder.getOrderNo()) .last("limit 1") ); if(ObjectUtil.isNotNull(mallExpressInfo)){ apiClothesOrderListInfoVo.setExpressNo(mallExpressInfo.getExpressNo()); apiClothesOrderListInfoVo.setExpressCom(mallExpressInfo.getExpressCom()); } List items = buildOrderItemInfo(clothesOrder.getId()); apiClothesOrderListInfoVo.setItems(items); if (OrderStatusEnum.WAIT_PAY.getValue() == clothesOrder.getStatus()) { Date endTime = DateUtil.offsetMinute(clothesOrder.getOrderTime(), 1440); long remainTime = DateUtil.between(new Date(), endTime, DateUnit.SECOND, false); apiClothesOrderListInfoVo.setRemainTime(remainTime); } apiClothesOrderListInfoVo.setCarriage(clothesOrder.getCarriage()); apiClothesOrderListInfoVo.setRemark(clothesOrder.getRemark()); apiClothesOrderListInfoVo.setCommentState(clothesOrder.getCommentState()); return new FebsResponse().success().data(apiClothesOrderListInfoVo); } @Override public FebsResponse confirmOrder(Long id) { MallMember member = LoginUserUtil.getLoginUser(); ClothesOrder orderInfo =clothesOrderMapper.selectById(id); if(ObjectUtil.isNull(orderInfo)){ throw new FebsException("订单不存在"); } if (member.getId() != orderInfo.getMemberId()) { throw new FebsException("无权限操作"); } if (ClothesEnum.UP.getCode() == orderInfo.getDelFlag()) { throw new FebsException("订单不存在"); } if (orderInfo.getStatus() != ClothesOrderStatusEnum.WAIT_FINISH.getValue()) { throw new FebsException("订单不是待收货状态"); } clothesOrderMapper.update(null, Wrappers.lambdaUpdate(ClothesOrder.class) .set(ClothesOrder::getStatus, OrderStatusEnum.FINISH.getValue()) .set(ClothesOrder::getDeliveryState, OrderDeliveryStateEnum.DELIVERY_FINISH.getValue()) .set(ClothesOrder::getUpdatedTime, new Date()) .eq(ClothesOrder::getId, id) .eq(ClothesOrder::getMemberId, member.getId()) ); return new FebsResponse().success().message("操作成功"); } }