package cn.ysk.cashier.service.impl.order;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.cons.rabbit.RabbitConstants;
import cn.ysk.cashier.dto.order.TbOrderInfoDto;
import cn.ysk.cashier.dto.order.TbOrderInfoQueryCriteria;
import cn.ysk.cashier.dto.order.TbPayCountQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mapper.order.TbOrderInfoMapper;
import cn.ysk.cashier.mapper.product.TbProductMapper;
import cn.ysk.cashier.mapper.product.TbProductSkuMapper;
import cn.ysk.cashier.mybatis.entity.TbActivateInRecord;
import cn.ysk.cashier.mybatis.entity.TbActivateOutRecord;
import cn.ysk.cashier.mybatis.entity.TbActivateProduct;
import cn.ysk.cashier.mybatis.entity.TbOrderPayment;
import cn.ysk.cashier.mybatis.service.TbActivateInRecordService;
import cn.ysk.cashier.mybatis.service.TbActivateOutRecordService;
import cn.ysk.cashier.mybatis.service.TbOrderPaymentService;
import cn.ysk.cashier.pojo.TbShopPayType;
import cn.ysk.cashier.pojo.order.TbFullOrderDetail;
import cn.ysk.cashier.pojo.order.TbOrderDetail;
import cn.ysk.cashier.pojo.order.TbOrderInfo;
import cn.ysk.cashier.pojo.product.TbProduct;
import cn.ysk.cashier.pojo.shop.TbMerchantThirdApply;
import cn.ysk.cashier.repository.TbShopPayTypeRepository;
import cn.ysk.cashier.repository.order.TbCashierCartRepository;
import cn.ysk.cashier.repository.order.TbOrderDetailRepository;
import cn.ysk.cashier.repository.order.TbOrderInfoRepository;
import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.repository.shop.TbMerchantThirdApplyRepository;
import cn.ysk.cashier.service.order.TbOrderInfoService;
import cn.ysk.cashier.service.product.TbProductService;
import cn.ysk.cashier.thirdpay.resp.OrderStatusQueryResp;
import cn.ysk.cashier.thirdpay.resp.PublicResp;
import cn.ysk.cashier.thirdpay.service.ThirdPayService;
import cn.ysk.cashier.utils.*;
import cn.ysk.cashier.vo.TbOrderInfoVo;
import cn.ysk.cashier.vo.TbOrderPayCountVo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lyf
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-03-02
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class TbOrderInfoServiceImpl implements TbOrderInfoService {

    private final TbOrderInfoRepository tbOrderInfoRepository;
    private final TbOrderInfoMapper tbOrderInfoMapper;

    private final TbOrderDetailRepository tbOrderDetailRepository;
    private final TbShopPayTypeRepository payTypeRepository;
    private final TbProductSkuRepository skuRepository;
    private final TbProductMapper productMapper;
    private final TbProductSkuMapper productSkuMapper;
    private final TbProductService productService;
    private final TbOrderPaymentService paymentService;
    private final TbMerchantThirdApplyRepository thirdApplyRepository;
    private final RedisUtils redisUtils;
    private final ThirdPayService thirdPayService;
    private final TbCashierCartRepository cartRepository;
    private final TbActivateInRecordService inRecordService;
    private final TbActivateOutRecordService outRecordService;
    private final RabbitTemplate rabbitTemplate;

    @Value("${thirdPay.url}")
    private String url;

    @Override
    public Map<String, Object> queryAllPage(TbOrderInfoQueryCriteria criteria) {
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getPageSize(), Sort.by(Sort.Direction.DESC, "createdAt"));
        if (criteria != null) {
            if (StringUtils.isNotBlank(criteria.getOrderType()) && criteria.getOrderType().equals("0")) {
                criteria.setOrderType(null);
            }
        }
        Page<TbOrderInfo> page = tbOrderInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = QueryHelp.getPredicate(root, criteria, criteriaBuilder);
            // 追加校验参数 status不为空 且 source不为空 不查询状态为 "refund" 的
            if (StringUtils.isBlank(criteria.getStatus()) && StringUtils.isBlank(criteria.getSource())) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.notEqual(root.get("orderType"), "return"));
            }

            if (criteria.getUserId() != null && criteria.getUserId() != 0) {
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("userId"), criteria.getUserId()));
            }

            if (StringUtils.isNotBlank(criteria.getProductName())) {
                Date startTime, endTime;
                DateTime offsetMonth = cn.hutool.core.date.DateUtil.offsetMonth(new Date(), 3);
                if (criteria.getCreatedAt() != null && criteria.getCreatedAt().size() == 2) {
                    if (criteria.getCreatedAt().get(0) < System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 90L) {
                        throw new BadRequestException("查询时间范围不能超过三个月");
                    }
                    Long startLong = criteria.getCreatedAt().get(0);
                    Long endLong = criteria.getCreatedAt().get(1);

                    startTime = new Date(startLong);
                    endTime = new Date(endLong);
                } else {
                    startTime = offsetMonth;
                    endTime = new Date();
                }

                List<Integer> productIds = tbOrderDetailRepository.findOrderIdsByProductNameLike(criteria.getProductName(),
                        criteria.getShopId(), startTime, endTime);

                if (productIds.isEmpty()) {
                    predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("id"), 0));
                } else {
                    predicate = criteriaBuilder.and(predicate, root.get("id").in(productIds));
                }
            }

            return predicate;
        }, pageable);

        List<TbOrderInfoVo> orderInfoVoList = new ArrayList<>();
        for (TbOrderInfo tbOrderInfo : page.getContent()) {
            TbOrderInfoVo orderInfoVo = new TbOrderInfoVo();
            BigDecimal refundAmount = BigDecimal.ZERO;
            List<TbOrderDetail> details = tbOrderDetailRepository.searchDetailByOrderId(tbOrderInfo.getId());
            if (!tbOrderInfo.getOrderType().equals("return")) {
                List<TbOrderInfo> tbOrderInfos = tbOrderInfoRepository.selTbOrdersBysource(tbOrderInfo.getId(), tbOrderInfo.getShopId());
                Map<Integer, Integer> map;
                if (!CollectionUtils.isEmpty(tbOrderInfos)) {
                    List<Integer> ids = tbOrderInfos.stream().map(TbOrderInfo::getId).collect(Collectors.toList());
                    List<TbOrderDetail> refundOrderDetail = tbOrderDetailRepository.searchDetailByOrderIds(ids);
                    map = refundOrderDetail.stream().collect(Collectors.groupingBy(TbOrderDetail::getProductSkuId, Collectors.summingInt(TbOrderDetail::getNum)));
                    refundAmount = tbOrderInfos.stream().map(TbOrderInfo::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                } else {
                    map = new HashMap<>();
                }
                details.parallelStream().forEach(detail -> {
                    Integer refundNumber = map.get(detail.getProductSkuId());
                    if (refundNumber != null) {
                        detail.setRefundNumber(refundNumber);
                    }
                });
            } else {
                details.parallelStream().forEach(detail -> {
                    detail.setRefundNumber(detail.getNum());
                });
            }
            orderInfoVo.setTableName(tbOrderInfo.getTableName());
            orderInfoVo.setDetailList(details);
            BeanUtils.copyProperties(tbOrderInfo, orderInfoVo);
            orderInfoVo.setRefundAmount(refundAmount);
            orderInfoVoList.add(orderInfoVo);
        }
        return PageUtil.toPage(orderInfoVoList, page.getTotalElements());
    }

    @Override
    public List<TbOrderPayCountVo> queryTbOrderPayCount(TbPayCountQueryCriteria criteria) {
        List<TbOrderPayCountVo> result = new ArrayList<>();
        List<Long> createdAt = criteria.getCreatedAt();
        // 指定时间，例如2024年1月1日 00:00:00
        Long start = 1704038400000L;
        Long end = Instant.now().toEpochMilli();
        if (!CollectionUtils.isEmpty(createdAt)) {
            if (createdAt.get(0) > createdAt.get(1)) {
                start = createdAt.get(1);
                end = createdAt.get(0);
            } else {
                start = createdAt.get(0);
                end = createdAt.get(1);
            }
        }
        if (ObjectUtil.isEmpty(criteria.getTableName())) {
            criteria.setTableName(null);
        }
        List<TbOrderPayCountVo> payCountVoList = tbOrderInfoRepository.queryTbOrderPayCount(criteria.getShopId(), criteria.getTableName(), start, end);
        BigDecimal totalPayAmount = BigDecimal.ZERO;
        for (TbOrderPayCountVo payCount : payCountVoList) {
            totalPayAmount = totalPayAmount.add(new BigDecimal(payCount.getPayAmount().toString()));
            if (StringUtils.isNotBlank(payCount.getPayType())) {
                TbShopPayType byPayType = payTypeRepository.findByPayType(payCount.getPayType(), criteria.getShopId());
                if (byPayType != null) {
                    payCount.setPayType(byPayType.getPayName());
                    payCount.setIcon(byPayType.getIcon());
                }
                if (payCount.getPayType().equals("wx_lite")) {
                    payCount.setPayType("微信小程序支付");
                    payCount.setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/20240318/ecf9e234aad8426ab0b1c4d5b8e50c9d.jpg");
                }
                result.add(payCount);
            }
        }
        TbOrderPayCountVo payRufund = tbOrderInfoRepository.queryTbOrderRefund(criteria.getShopId(), criteria.getTableName(), start, end);
        if (payRufund != null) {
            totalPayAmount = totalPayAmount.subtract(new BigDecimal(payRufund.getPayAmount().toString()));
            payRufund.setPayType("退单");
            payRufund.setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/20240319/5741608662244b878762c61bb37c95c9.png");
            result.add(payRufund);
        }
        result.add(0, new TbOrderPayCountVo("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/20240625/fa0e5b24884d4ac2a18973b3855e5309.png", "总金额", null, totalPayAmount));
        return result;
    }

    @Override
    public List<TbOrderInfoDto> queryAll(TbOrderInfoQueryCriteria criteria) {
        if (criteria != null) {
            if (StringUtils.isNotBlank(criteria.getOrderType()) && criteria.getOrderType().equals("0")) {
                criteria.setOrderType(null);
            }
        }

        List<TbOrderInfoDto> dto = tbOrderInfoMapper.toDto(tbOrderInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
        for (TbOrderInfoDto tbOrderInfo : dto) {
            List<TbOrderDetail> details = tbOrderDetailRepository.searchDetailByOrderId(tbOrderInfo.getId());
            tbOrderInfo.setDetailList(details);
        }
        return dto;
    }

    @Override
    @Transactional
    public TbOrderInfoDto findById(Integer id) {
        TbOrderInfo tbOrderInfo = tbOrderInfoRepository.findById(id).orElseGet(TbOrderInfo::new);
        ValidationUtil.isNull(tbOrderInfo.getId(), "TbOrderInfo", "id", id);
        TbOrderInfoDto dto = tbOrderInfoMapper.toDto(tbOrderInfo);
        if (StringUtils.isNotBlank(dto.getPayType())) {
            switch (dto.getPayType()) {
                case "scanCode":
                    dto.setPayType("扫码支付");
                    break;
                case "cash":
                    dto.setPayType("现金");
                    break;
                case "wx_lite":
                    dto.setPayType("微信小程序支付");
                    break;
                case "deposit":
                    dto.setPayType("储值卡支付");
                    break;
                default:
                    dto.setPayType(tbOrderInfo.getSendType());
                    break;
            }
        } else {
            if (dto.getStatus().equals("refund")) {
                dto.setPayType("退单");
            }
        }
        List<TbOrderDetail> details = tbOrderDetailRepository.searchDetailByOrderId(tbOrderInfo.getId());
        dto.setIsRefund(0);
        dto.setRefundAmount(BigDecimal.ZERO);
        List<TbOrderInfo> tbOrderInfos = tbOrderInfoRepository.selTbOrdersBysource(tbOrderInfo.getId(), tbOrderInfo.getShopId());
        if (!CollectionUtils.isEmpty(tbOrderInfos)) {
            dto.setIsRefund(1);
            dto.setRefundAmount(tbOrderInfos.stream().map(TbOrderInfo::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        }
        dto.setDetailList(details);
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbOrderInfoDto create(TbOrderInfo resources) {
        return tbOrderInfoMapper.toDto(tbOrderInfoRepository.save(resources));
    }

    @Async
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void expired(String orderId) {
        //修改耗材数据
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("type", "delete");

        Optional<TbOrderInfo> byId = tbOrderInfoRepository.findById(Integer.valueOf(orderId));
        if (byId != null && byId.isPresent()) {
            TbOrderInfo tbOrderInfo = byId.get();
            if (tbOrderInfo.getStatus().equals("unpaid")) {
                upOrderStatus(tbOrderInfo);
                jsonObject1.put("orderId", tbOrderInfo.getId());
                // 发送取消订单mq消息
                rabbitTemplate.convertAndSend(RabbitConstants.CONS_COLLECT_PUT, RabbitConstants.CONS_COLLECT_ROUTINGKEY_PUT,
                        jsonObject1.toJSONString(), new CorrelationData(UUID.randomUUID().toString()));
            } else if (tbOrderInfo.getStatus().equals("paying") &&
                    (StringUtils.isBlank(tbOrderInfo.getPayType()) ||
                            !tbOrderInfo.getPayType().equals("deposit"))) {
                QueryWrapper<TbOrderPayment> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("order_id", tbOrderInfo.getId());
                TbOrderPayment payment = paymentService.getOne(queryWrapper);
                if (payment != null && StringUtils.isNotBlank(payment.getTradeNumber())) {
                    if (System.currentTimeMillis() - payment.getCreatedAt() < 60 * 2 * 1000L) {
                        modfiyOrderInfo(tbOrderInfo);
                        redisUtils.set(CacheKey.ORDER_EXPIRED + tbOrderInfo.getId(), tbOrderInfo.getId(), 60 * 2);
                    } else {
                        upOrderStatus(tbOrderInfo);
                        jsonObject1.put("orderId", tbOrderInfo.getId());

                        // 发送取消订单mq消息
                        rabbitTemplate.convertAndSend(RabbitConstants.CONS_COLLECT_PUT, RabbitConstants.CONS_COLLECT_ROUTINGKEY_PUT,
                                jsonObject1.toJSONString(), new CorrelationData(UUID.randomUUID().toString()));
                    }
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void modfiyOrderInfo(TbOrderInfo tbOrderInfo) {
        QueryWrapper<TbOrderPayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", tbOrderInfo.getId());
        TbOrderPayment payment = paymentService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(payment) && ObjectUtil.isNotEmpty(payment.getTradeNumber())) {
            TbMerchantThirdApply thirdApply = thirdApplyRepository.getById(Integer.valueOf(tbOrderInfo.getMerchantId()));
            if (ObjectUtil.isEmpty(thirdApply) || ObjectUtil.isNull(thirdApply)) {
                //支付通道不存在
            }
            PublicResp<OrderStatusQueryResp> publicResp = thirdPayService.queryOrder(url, thirdApply.getAppId(), payment.getTradeNumber(), null, thirdApply.getAppToken());
            if (ObjectUtil.isNotNull(publicResp) && ObjectUtil.isNotEmpty(publicResp)) {
                if ("000000".equals(publicResp.getCode())) {
                    String cartStatus = "";
                    switch (publicResp.getObjData().getState()) {
                        case "TRADE_SUCCESS":
                            //修改数据库中购物车数据
                            cartRepository.updateToFinal(tbOrderInfo.getId());
                            //更新子单状态
                            tbOrderDetailRepository.updateToClosed(tbOrderInfo.getId());
                            //修改主单状态
                            tbOrderInfo.setStatus("closed");
                            tbOrderInfo.setPayType("wx_lite");
                            tbOrderInfo.setPayOrderNo(payment.getTradeNumber());
                            tbOrderInfo.setPayAmount(tbOrderInfo.getOrderAmount());
                            tbOrderInfoRepository.save(tbOrderInfo);
//                            JSONObject jsonObject = new JSONObject();
//                            jsonObject.put("token", 0);
//                            jsonObject.put("type", "wxcreate");
//                            jsonObject.put("orderId", orderInfo.getId().toString());
//                            producer.putOrderCollect(jsonObject.toJSONString());
//                            log.info("发送打印数据");
//                            producer.printMechine(orderInfo.getId() + "");
                    }
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void upOrderStatus(TbOrderInfo tbOrderInfo) {
        tbOrderInfo.setStatus("cancelled");
        //订单取消 赠送商品数量返回
        QueryWrapper<TbActivateOutRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",tbOrderInfo.getId());
        queryWrapper.eq("status","create");
        List<TbActivateOutRecord> outRecords = outRecordService.list(queryWrapper);
        for (TbActivateOutRecord outRecord : outRecords) {
            TbActivateInRecord inRecord = inRecordService.getById(outRecord.getGiveId());
            inRecord.setOverNum(inRecord.getOverNum() + outRecord.getUseNum());
            inRecordService.updateById(inRecord);
        }
        LambdaUpdateWrapper<TbActivateOutRecord> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TbActivateOutRecord::getOrderId, tbOrderInfo.getId())
                .eq(TbActivateOutRecord::getStatus, "create")
                .set(TbActivateOutRecord::getStatus, "cancel");
        outRecordService.update(lambdaUpdateWrapper);

        List<TbOrderDetail> details = tbOrderDetailRepository.searchDetailByOrderId(tbOrderInfo.getId());
        Set<String> keys = new HashSet<>();
        for (TbOrderDetail detail : details) {
            detail.setStatus("cancelled");
            // 返还库存
            productService.returnStockByPro(detail.getProductId(), detail.getProductSkuId(), detail.getNum());
            tbOrderDetailRepository.save(detail);
            keys.add(CacheKey.PRODUCT_SKU + detail.getShopId() + ":" + detail.getId());
        }
        String[] keysArray = keys.toArray(new String[keys.size()]);
        redisUtils.del(keysArray);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbOrderInfo resources) {
        TbOrderInfo tbOrderInfo = tbOrderInfoRepository.findById(resources.getId()).orElseGet(TbOrderInfo::new);
        ValidationUtil.isNull(tbOrderInfo.getId(), "TbOrderInfo", "id", resources.getId());
        tbOrderInfo.copy(resources);
        tbOrderInfoRepository.save(tbOrderInfo);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbOrderInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TbOrderInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();

        all = all.stream().sorted(Comparator.comparing(TbOrderInfoDto::getId).reversed()).collect(Collectors.toList());
        for (TbOrderInfoDto tbOrderInfo : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("订单编号", tbOrderInfo.getOrderNo());
            switch (tbOrderInfo.getOrderType()) {
                case "cash":
                    tbOrderInfo.setOrderType("收银");
                    break;
                case "miniapp":
                    tbOrderInfo.setOrderType("小程序");
                    break;
                case "offline":
                    tbOrderInfo.setOrderType("线下");
                    break;
                case "return":
                    tbOrderInfo.setOrderType("退单");
                    break;
                default:
                    tbOrderInfo.setOrderType(tbOrderInfo.getOrderType() + "未知订单类型");
                    break;
            }
            map.put("订单类型", tbOrderInfo.getOrderType());
            switch (tbOrderInfo.getSendType()) {
                case "post":
                    tbOrderInfo.setSendType("快递");
                    break;
                case "takeaway":
                    tbOrderInfo.setSendType("外卖");
                    break;
                case "takeself":
                    tbOrderInfo.setSendType("自提");
                    break;
                case "table":
                    tbOrderInfo.setSendType("堂食");
                    break;
                default:
                    tbOrderInfo.setSendType(tbOrderInfo.getSendType() + "未知送货类型");
                    break;
            }
            map.put("发货类型", tbOrderInfo.getSendType());
            // 商品信息
            String productNames = tbOrderInfo.getDetailList().stream()
                    .map(TbOrderDetail::getProductName)
                    .distinct()
                    .collect(Collectors.joining("/"));
            map.put("商品信息", productNames);
            //

            if (ObjectUtil.isNotEmpty(tbOrderInfo.getPayType()) && ObjectUtil.isNotNull(tbOrderInfo.getPayType())) {
                switch (tbOrderInfo.getPayType()) {
                    case "scanCode":
                        tbOrderInfo.setPayType("收款码支付");
                        break;
                    case "deposit":
                        tbOrderInfo.setPayType("会员卡支付");
                        break;
                    case "cash":
                        tbOrderInfo.setPayType("现金支付");
                        break;
                    case "wx_lite":
                        tbOrderInfo.setPayType("微信支付");
                        break;

                }
            } else {
                tbOrderInfo.setPayType("");
            }


            map.put("支付类型", tbOrderInfo.getPayType());
            map.put("订单金额", tbOrderInfo.getOrderAmount());
            map.put("支付金额", tbOrderInfo.getPayAmount());
            switch (tbOrderInfo.getStatus()) {
                case "unpaid":
                    tbOrderInfo.setStatus("待支付");
                    break;
                case "unsend":
                    tbOrderInfo.setStatus("待发货");
                    break;
                case "closed":
                    tbOrderInfo.setStatus("订单完成");
                    break;
                case "send":
                    tbOrderInfo.setStatus("已发货");
                    break;
                case "refunding":
                    tbOrderInfo.setStatus("申请退单中");
                    break;
                case "refund":
                    map.put("支付金额", "-"+tbOrderInfo.getPayAmount());
                    tbOrderInfo.setStatus("退单完成");
                    break;
                case "cancelled":
                    tbOrderInfo.setStatus("取消订单");
                    break;
                case "merge":
                    tbOrderInfo.setStatus("合台");
                    break;
                case "pending":
                    tbOrderInfo.setStatus("挂单");
                    break;
                case "activate":
                    tbOrderInfo.setStatus("激活订单");
                    break;
                default:
                    tbOrderInfo.setStatus(tbOrderInfo.getStatus() + "未知订单状态");
                    break;
            }
            map.put("状态", tbOrderInfo.getStatus());
            map.put("创建日期", DateUtil.timeStampFormatyMdHms(tbOrderInfo.getCreatedAt()));
            map.put("备注", tbOrderInfo.getRemark());
            map.put("台桌", tbOrderInfo.getTableName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
