Administrator
2025-07-04 d448834784e996a3c35c90097c4e7b110899bed2
src/main/java/cc/mrbird/febs/mall/service/impl/ClothesTypeServiceImpl.java
@@ -2,12 +2,27 @@
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.enumerates.StateUpDownEnum;
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.properties.XcxProperties;
import cc.mrbird.febs.common.utils.SpringContextHolder;
import cc.mrbird.febs.common.utils.excl.ExcelSheetPO;
import cc.mrbird.febs.common.utils.excl.ExcelUtil;
import cc.mrbird.febs.common.utils.excl.ExcelVersion;
import cc.mrbird.febs.common.utils.excl.ResponseHeadUtil;
import cc.mrbird.febs.mall.dto.clothes.AdminClothesDeliverGoodsDto;
import cc.mrbird.febs.mall.dto.clothes.AdminClothesOrderListDto;
import cc.mrbird.febs.mall.dto.clothes.AdminClothesRefundOrderDto;
import cc.mrbird.febs.mall.dto.clothes.AdminClothesTypeInfoDto;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.ClothesTypeService;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import cc.mrbird.febs.mall.vo.AdminMallOrderInfoVo;
import cc.mrbird.febs.mall.vo.clothes.AdminClothesOrderListVo;
import cc.mrbird.febs.pay.util.WeixinServiceUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -17,12 +32,17 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@@ -42,6 +62,13 @@
    private final ClothesTypeClothMapper clothesTypeClothMapper;
    private final ClothesTypePatternMapper clothesTypePatternMapper;
    private final ClothesTypeLocationMapper clothesTypeLocationMapper;
    private final ClothesOrderMapper clothesOrderMapper;
    private final MallExpressInfoMapper mallExpressInfoMapper;
    private final IApiMallMemberWalletService memberWalletService;
    private final IMallMoneyFlowService mallMoneyFlowService;
    private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
    @Autowired
    private WeixinServiceUtil weixinServiceUtil;
    @Override
    public IPage<ClothesType> adminTypeList(ClothesType dto, QueryRequest request) {
@@ -421,4 +448,288 @@
        }
        return new FebsResponse().success().message("操作成功");
    }
    @Override
    public IPage<AdminClothesOrderListVo> getOrderListInPage(AdminClothesOrderListDto dto, QueryRequest request) {
        Page<AdminClothesOrderListVo> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<AdminClothesOrderListVo> vos = clothesOrderMapper.selectOrderListInPage(page, dto);
        return vos;
    }
    @Override
    public FebsResponse deliverGoods(AdminClothesDeliverGoodsDto dto) {
        ClothesOrder clothesOrder = clothesOrderMapper.selectById(dto.getId());
        if(ObjectUtil.isNull(clothesOrder)){
            return new FebsResponse().fail().message("订单不存在,刷新后重试");
        }
        Integer status = clothesOrder.getStatus();
        if(ClothesOrderStatusEnum.WAIT_SHIPPING.getValue() != status){
            return new FebsResponse().fail().message("订单不是待发货状态");
        }
        String expressNo = dto.getExpressNo();
        if(StrUtil.isEmpty(expressNo)){
            return new FebsResponse().fail().message("请输入物流单号");
        }
        String expressCom = dto.getExpressCom();
        if(StrUtil.isEmpty(expressCom)){
            return new FebsResponse().fail().message("请输入物流公司");
        }
        MallExpressInfo mallExpressInfo = new MallExpressInfo();
        mallExpressInfo.setMemberId(clothesOrder.getMemberId());
        mallExpressInfo.setOrderId(clothesOrder.getId());
        mallExpressInfo.setOrderNo(clothesOrder.getOrderNo());
        mallExpressInfo.setExpressNo(expressNo);
        mallExpressInfo.setExpressCom(expressCom);
        mallExpressInfoMapper.insert(mallExpressInfo);
        clothesOrderMapper.update(
                null,
                Wrappers.lambdaUpdate(ClothesOrder.class)
                .set(ClothesOrder::getStatus, ClothesOrderStatusEnum.WAIT_FINISH.getValue())
                .set(ClothesOrder::getUpdatedTime, new Date())
                .set(ClothesOrder::getDeliveryState, OrderDeliveryStateEnum.DELIVERY_FINISH.getValue())
                        .eq(ClothesOrder::getId, clothesOrder.getId())
        );
        return new FebsResponse().success().message("操作成功");
    }
    @Override
    public FebsResponse deliverGoodsUpdate(AdminClothesDeliverGoodsDto dto) {
        ClothesOrder clothesOrder = clothesOrderMapper.selectById(dto.getId());
        if(ObjectUtil.isNull(clothesOrder)){
            return new FebsResponse().fail().message("订单不存在,刷新后重试");
        }
        Integer status = clothesOrder.getStatus();
        if(ClothesOrderStatusEnum.WAIT_FINISH.getValue() != status){
            return new FebsResponse().fail().message("订单不是待发货状态");
        }
        String expressNo = dto.getExpressNo();
        if(StrUtil.isEmpty(expressNo)){
            return new FebsResponse().fail().message("请输入物流单号");
        }
        String expressCom = dto.getExpressCom();
        if(StrUtil.isEmpty(expressCom)){
            return new FebsResponse().fail().message("请输入物流公司");
        }
        MallExpressInfo mallExpressInfo = mallExpressInfoMapper.selectOne(
                Wrappers.lambdaQuery(MallExpressInfo.class)
                        .eq(MallExpressInfo::getOrderId, clothesOrder.getId())
                        .eq(MallExpressInfo::getOrderNo, clothesOrder.getOrderNo())
                        .last("limit 1")
        );
        if(ObjectUtil.isNull(mallExpressInfo)){
            return new FebsResponse().fail().message("请先发货");
        }
        mallExpressInfoMapper.update(
                null,
                Wrappers.lambdaUpdate(MallExpressInfo.class)
                        .set(MallExpressInfo::getExpressNo, expressNo)
                        .set(MallExpressInfo::getExpressCom, expressCom)
                        .set(MallExpressInfo::getUpdatedTime, new Date())
                        .eq(MallExpressInfo::getId, mallExpressInfo.getId())
        );
        return new FebsResponse().success().message("操作成功");
    }
    private void clothesUpdateStatusAndUpdateTimeById(Long id, Date date, int value) {
        clothesOrderMapper.update(
                null,
                Wrappers.lambdaUpdate(ClothesOrder.class)
                        .set(ClothesOrder::getStatus, ClothesOrderStatusEnum.CANCEL.getValue())
                        .set(ClothesOrder::getUpdatedTime, new Date())
                        .eq(ClothesOrder::getId, id)
        );
    }
    private Boolean weChatRefundOrderEvent(int refundMoney, BigDecimal orderAmount, String orderNo, String refundNo) {
        Boolean flag;
        Boolean debug = xcxProperties.getDebug();
        if (debug) {
            if(orderAmount.compareTo(BigDecimal.ZERO) > 0){
                boolean b = weixinServiceUtil.comRefund(orderNo, refundNo, 1, 1, null);
                flag = b;
            }else{
                flag = true;
            }
        } else {
            if(orderAmount.compareTo(BigDecimal.ZERO) > 0){
                log.info("开始调用退款接口。。。退款编号为{}", refundNo);
                boolean b = weixinServiceUtil.comRefund(orderNo, refundNo, refundMoney, refundMoney, null);
                flag = b;
            }else{
                flag = true;
            }
        }
        return flag;
    }
    @Override
    public FebsResponse refundOrder(AdminClothesRefundOrderDto dto) {
        ClothesOrder clothesOrder = clothesOrderMapper.selectById(dto.getId());
        if(ObjectUtil.isNull(clothesOrder)){
            return new FebsResponse().fail().message("订单不存在,刷新后重试");
        }
        Integer status = clothesOrder.getStatus();
        if(ClothesOrderStatusEnum.WAIT_SHIPPING.getValue() != status){
            return new FebsResponse().fail().message("订单不是待发货状态");
        }
        //退款订单编号
        String orderNo = clothesOrder.getOrderNo();
        //退款退款编号
        String refundNo = clothesOrder.getOrderNo()+"_REFUND_"+clothesOrder.getId();
        //退款订单金额
        BigDecimal orderAmount = clothesOrder.getRealAmount();
        //余额支付退款
        if(ClothesOrderPayTypeEnum.BALANCE.getName().equals(clothesOrder.getPayMethod())){
            log.info("余额支付退款");
            if(orderAmount.compareTo(BigDecimal.ZERO) > 0){
                //更新订单详情
                clothesUpdateStatusAndUpdateTimeById(clothesOrder.getId(),new Date(),ClothesOrderStatusEnum.CANCEL.getValue());
                memberWalletService.add(orderAmount, clothesOrder.getMemberId(), "balance");
                mallMoneyFlowService.addMoneyFlow(
                        clothesOrder.getMemberId(),
                        orderAmount,
                        ScoreFlowTypeEnum.REFUND.getValue(),
                        clothesOrder.getOrderNo(),
                        FlowTypeEnum.BALANCE.getValue(),
                        StrUtil.format(ScoreFlowTypeEnum.REFUND.getDesc(),orderAmount),
                        2
                );
            }
            return new FebsResponse().success().message("退款成功");
        }else if(ClothesOrderPayTypeEnum.WECHAT.getName().equals(clothesOrder.getPayMethod())){
            int refundMoney = orderAmount.multiply(new BigDecimal(100)).intValue();
            Boolean flag = weChatRefundOrderEvent(refundMoney, orderAmount, orderNo, refundNo);
            if(flag){
                if(orderAmount.compareTo(BigDecimal.ZERO) > 0){
                    //更新订单详情
                    clothesUpdateStatusAndUpdateTimeById(clothesOrder.getId(),new Date(),ClothesOrderStatusEnum.CANCEL.getValue());
                    mallMoneyFlowService.addMoneyFlow(
                            clothesOrder.getMemberId(),
                            orderAmount,
                            ScoreFlowTypeEnum.WECHAT_REFUND.getValue(),
                            clothesOrder.getOrderNo(),
                            FlowTypeEnum.BALANCE.getValue(),
                            StrUtil.format(ScoreFlowTypeEnum.WECHAT_REFUND.getDesc(),orderAmount),
                            2
                    );
                }
            }else{
                return new FebsResponse().fail().message("退款失败,请联系客服人员");
            }
        }else{
            return new FebsResponse().fail().message("退款失败,请联系客服人员");
        }
        return new FebsResponse().success().message("退款成功");
    }
    @Override
    public void confirmOrder(long orderId) {
        ClothesOrder clothesOrder = clothesOrderMapper.selectById(orderId);
        if(ObjectUtil.isNotNull(clothesOrder) && ClothesOrderStatusEnum.WAIT_FINISH.getValue() == clothesOrder.getStatus()){
            clothesUpdateStatusAndUpdateTimeById(clothesOrder.getId(),new Date(),ClothesOrderStatusEnum.FINISH.getValue());
        }
    }
    @Override
    public void exportOrderList(List<Long> orderIds, HttpServletResponse response) throws IOException {
        List<ExcelSheetPO> res = new ArrayList<>();
        ExcelSheetPO orderSheet = new ExcelSheetPO();
        String title = "订单列表";
        orderSheet.setSheetName(title);
        orderSheet.setTitle(title);
        String[] header = {"订单ID", "订单编号", "收货姓名", "收货电话", "收货地址", "商品详情", "备注", "物流单号", "物流公司", "物流公司码"};
        orderSheet.setHeaders(header);
        QueryRequest request = new QueryRequest();
        request.setPageNum(1);
        request.setPageSize(9999);
        List<ClothesOrder> dataList = clothesOrderMapper.selectList(
                Wrappers.lambdaQuery(ClothesOrder.class)
                        .in(ClothesOrder::getId, orderIds)
                        .eq(ClothesOrder::getDelFlag, 0)
                        .eq(ClothesOrder::getStatus, ClothesOrderStatusEnum.WAIT_SHIPPING.getValue()));
        Map<Long, ClothesType> longClothesTypeHashMap = new HashMap<>();
        if(CollUtil.isNotEmpty(dataList)){
            Set<Long> typeIds = dataList.stream().map(ClothesOrder::getTypeId).collect(Collectors.toSet());
            List<ClothesType> clothesTypes = clothesTypeMapper.selectList(
                    Wrappers.lambdaQuery(ClothesType.class)
                            .in(ClothesType::getId, typeIds)
            );
            if(CollUtil.isNotEmpty(clothesTypes)){
                //stream操作clothesTypes,返回一个HashMap<Long, ClothesType>, key为clothesType.id, value为clothesType
                longClothesTypeHashMap = clothesTypes.stream().collect(Collectors.toMap(ClothesType::getId, clothesType -> clothesType));
            }
        }
        List<List<Object>> list = new ArrayList<>();
        if (dataList.size() > 0) {
            for (ClothesOrder item : dataList) {
                List<Object> temp = new ArrayList<>();
                temp.add(item.getId());
                temp.add(item.getOrderNo());
                temp.add(item.getName());
                temp.add(item.getPhone());
                temp.add(item.getAddress());
                temp.add(longClothesTypeHashMap.get(item.getTypeId()).getName());
                temp.add(item.getRemark());
                list.add(temp);
            }
        }
        orderSheet.setDataList(list);
        res.add(orderSheet);
        response = ResponseHeadUtil.setExcelHead(response);
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(title + DateUtil.format(new Date(), "yyyyMMDDHHmmss") + ".xlsx".trim(), "UTF-8"));
        OutputStream os = response.getOutputStream();
        ExcelUtil.createWorkbookAtOutStream(ExcelVersion.V2007, res, os, true);
    }
    @Override
    public void deliverGoodsImport(AdminClothesDeliverGoodsDto dto) {
        ClothesOrder clothesOrder = clothesOrderMapper.selectById(dto.getId());
        if(ObjectUtil.isNull(clothesOrder)){
            return;
        }
        Integer status = clothesOrder.getStatus();
        if(ClothesOrderStatusEnum.WAIT_SHIPPING.getValue() != status){
            return;
        }
        String expressNo = dto.getExpressNo();
        if(StrUtil.isEmpty(expressNo)){
            return;
        }
        String expressCom = dto.getExpressCom();
        if(StrUtil.isEmpty(expressCom)){
            return;
        }
        mallExpressInfoMapper.update(
                null,
                Wrappers.lambdaUpdate(MallExpressInfo.class)
                        .set(MallExpressInfo::getExpressNo, expressNo)
                        .set(MallExpressInfo::getExpressCom, expressCom)
                        .eq(MallExpressInfo::getOrderId, clothesOrder.getId())
                        .eq(MallExpressInfo::getOrderNo, clothesOrder.getOrderNo())
        );
        clothesOrderMapper.update(
                null,
                Wrappers.lambdaUpdate(ClothesOrder.class)
                        .set(ClothesOrder::getStatus, ClothesOrderStatusEnum.WAIT_FINISH.getValue())
                        .set(ClothesOrder::getUpdatedTime, new Date())
                        .set(ClothesOrder::getDeliveryState, OrderDeliveryStateEnum.DELIVERY_ING.getValue())
                        .eq(ClothesOrder::getId, clothesOrder.getId())
        );
    }
}