package com.ghy.web.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.ghy.common.annotation.Log;
import com.ghy.common.constant.UserConstants;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.github.pagehelper.PageInfo;
import com.ghy.common.enums.*;
import com.ghy.common.utils.*;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.customer.domain.Customer;
import com.ghy.customer.domain.CustomerAddress;
import com.ghy.customer.service.CustomerAddressService;
import com.ghy.customer.service.CustomerService;
import com.ghy.goods.domain.*;
import com.ghy.goods.service.*;
import com.ghy.order.domain.*;
import com.ghy.order.request.OrderProcessRequest;
import com.ghy.order.service.*;
import com.ghy.payment.domain.FinancialChangeRecord;
import com.ghy.payment.domain.FinancialDetail;
import com.ghy.payment.domain.FinancialMaster;
import com.ghy.payment.service.AdapayService;
import com.ghy.payment.service.FinancialChangeRecordService;
import com.ghy.payment.service.FinancialDetailService;
import com.ghy.payment.service.FinancialMasterService;
import com.ghy.shop.domain.Shop;
import com.ghy.shop.service.ShopService;
import com.ghy.system.domain.SysArea;
import com.ghy.system.service.ISysAreaService;
import com.ghy.system.service.IWxMsgService;
import com.ghy.common.utils.BaiduMapUtils;
import com.ghy.web.pojo.vo .OrderChangePriceRequest;
import com.ghy.web.pojo.vo.OrderListResponse;
import com.ghy.web.pojo.vo.OrderStandard;
import com.ghy.web.service.InsuranceService;
import com.ghy.worker.domain.Worker;
import com.ghy.worker.domain.WorkerCertification;
import com.ghy.worker.service.IWorkerCertificationService;
import com.ghy.worker.service.WorkerService;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 详细订单API
 *
 * @author HH 2022/4/25
 */
@Controller
@RequestMapping("/order/detail")
public class OrderDetailController extends BaseController {

    private final String prefix = "order/detail";

    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private OrderMasterService orderMasterService;
    @Resource
    private CustomerService customerService;
    @Resource
    private WorkerService workerService;
    @Resource
    private OrderGoodsService orderGoodsService;
    @Resource
    private CustomerAddressService addressService;
    @Resource
    private ISysAreaService sysAreaService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsImgsService goodsImgsService;
    @Resource
    private GoodsStandardService goodsStandardService;
    @Resource
    private FinancialDetailService financialDetailService;
    @Resource
    private IAfterServiceRecordService afterServiceRecordService;
    @Resource
    private FinancialChangeRecordService financialChangeRecordService;
    @Resource
    private IWorkerCertificationService workerCertificationService;
    @Resource
    private IOrderAssessLabelService orderAssessLabelService;
    @Resource
    private FinancialMasterService financialMasterService;
    @Resource
    private OrderBehaviorService orderBehaviorService;
    @Resource
    private IWxMsgService wxMsgService;
    @Resource
    private ThreadPoolTaskExecutor executor;
    @Autowired
    private IOrderCallRecordService orderCallRecordService;
    @Resource
    private DeptGoodsCategoryService deptGoodsCategoryService;
    @Resource
    private GoodsCategoryService goodsCategoryService;

    @Resource
    private IInsuranceManagerService insuranceManagerService;
    @Autowired
    private InsuranceService insuranceService;
    @Autowired
    private ShopService shopService;
    @Resource
    private IOrderAttachmentRecordService orderAttachmentRecordService;

    @Resource
    private AdapayService adapayService;

    @Autowired
    private BaiduMapUtils baiduMapUtils;

    @RequiresPermissions("order:detail:view")
    @GetMapping()
    public String orderDetail() {
        return prefix;
    }

    //    @RequiresPermissions("order:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDetail orderDetail) {
        // 检查是否是售后纠纷查询
        if (orderDetail.getOrderStatusName() != null && "售后纠纷".equals(orderDetail.getOrderStatusName())) {
            // 直接使用afterServiceStatus字段查询售后纠纷订单
            orderDetail.setAfterServiceStatus(1);
            orderDetail.setOrderStatus(null); // 清除orderStatus条件，避免冲突
        } else if (orderDetail.getSearchAfterList() != null && orderDetail.getSearchAfterList()) {
            // 保持原有的售后查询逻辑
            List<?> rows = this.afterServiceList(orderDetail).getRows();
            StringBuilder detailIds = new StringBuilder();
            if (CollectionUtils.isNotEmpty(rows)) {
                rows.forEach(model -> {
                    OrderListResponse response = (OrderListResponse) model;
                    detailIds.append(response.getOrderDetailId()).append(",");
                });
            }
            String ids = detailIds.toString();
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(ids)) {
                orderDetail.setOrderDetailIds(ids.substring(0, ids.length() - 1));
            }
        }

        if (StringUtils.isNotEmpty(orderDetail.getWorkerName()) || StringUtils.isNotEmpty(orderDetail.getWorkerPhone())) {
            Worker param = new Worker();
            param.setName(orderDetail.getWorkerName());
            param.setPhone(orderDetail.getWorkerPhone());
            List<Worker> workList = workerService.getWorkList(param);
            List<Long> ids = workList.stream().map(Worker::getWorkerId).collect(Collectors.toList());
            orderDetail.setWorkerIds(ids);
            if (CollectionUtils.isEmpty(ids)) {
                return getDataTable(new ArrayList<>());
            }
        }
        if (orderDetail.getSysPayStatus() != null) {
            FinancialMaster param = new FinancialMaster();
            param.setPayStatus(orderDetail.getSysPayStatus());
            List<FinancialMaster> financialMasters = financialMasterService.selectFinancialMasterList(param);
            List<Long> masterIds = financialMasters.stream().map(FinancialMaster::getOrderMasterId).collect(Collectors.toList());
            orderDetail.setMasterIds(masterIds);
            if (CollectionUtils.isEmpty(masterIds)) {
                return getDataTable(new ArrayList<>());
            }
        }
        
        if (this.getSysUser().getDept().getParentId() != 101) {
            orderDetail.setDeptId(this.getSysUser().getDept().getParentId());
        }
        
        // 手动分页：先获取总数，再进行分页查询
        Long total = orderDetailService.selectOrderDetailCount(orderDetail);
        startPage();
        List<OrderDetail> orderDetails = orderDetailService.selectOrderDetailList(orderDetail);

        Set<Long> customerIds = orderDetails.stream().map(OrderDetail::getCustomerId).collect(Collectors.toSet());
        Map<Long, Customer> customerMap = customerService.selectByIds(customerIds)
                .stream().collect(Collectors.toMap(Customer::getCustomerId, x -> x, (x, y) -> y));

        Set<Long> orderMasterIds = orderDetails.stream().map(OrderDetail::getOrderMasterId).collect(Collectors.toSet());
        List<OrderMaster> orderMasters = orderMasterService.selectByIds(orderMasterIds);
        Map<Long, OrderMaster> orderMasterMap = orderMasters.stream().collect(Collectors.toMap(OrderMaster::getId, x -> x, (x, y) -> y));

        List<FinancialMaster> financialMasters = financialMasterService.selectByOrderMasterIds(orderMasterIds);
        Map<Long, FinancialMaster> financialMasterMap = financialMasters
                .stream().collect(Collectors.toMap(FinancialMaster::getOrderMasterId, x -> x, (x, y) -> y));

        Set<Long> orderDetailIds = orderDetails.stream().map(OrderDetail::getId).collect(Collectors.toSet());
        Map<Long, FinancialDetail> financialDetailMap = financialDetailService.selectByOrderDetailIds(orderDetailIds)
                .stream().collect(Collectors.toMap(FinancialDetail::getOrderDetailId, x -> x, (x, y) -> y));

        Map<Long, FinancialChangeRecord> fcMap = financialChangeRecordService.selectByOrderDetailIds(orderDetailIds)
                .stream().collect(Collectors.toMap(FinancialChangeRecord::getOrderDetailId, x -> x, (x, y) -> y));


        Set<Long> goodsIds = orderMasters.stream().map(OrderMaster::getGoodsId).collect(Collectors.toSet());
        List<Goods> goods = goodsService.selectByIds(goodsIds);
        Map<Long, Goods> goodsMap = goods.stream().collect(Collectors.toMap(Goods::getGoodsId, x -> x, (x, y) -> y));

        Set<Long> workerIds = orderDetails.stream().map(OrderDetail::getWorkerId).collect(Collectors.toSet());
        workerIds.addAll(goods.stream().map(Goods::getWorkerId).collect(Collectors.toSet()));
        Map<Long, Worker> workerMap = workerService.selectByIds(workerIds)
                .stream().collect(Collectors.toMap(Worker::getWorkerId, x -> x, (x, y) -> y));

        for (OrderDetail detail : orderDetails) {
            if (StringUtils.isNotEmpty(orderDetail.getOrderStatusName())) {
                detail.setOrderStatusName(orderDetail.getOrderStatusName());
            }
            // 确保afterServiceStatus字段不为null
            if (detail.getAfterServiceStatus() == null) {
                detail.setAfterServiceStatus(0);
            }
            Customer customer = customerMap.get(detail.getCustomerId());
            detail.setWorker(workerMap.get(detail.getWorkerId()));
            if (customer != null) {
                detail.setCustomerName(customer.getName());
                detail.setCustomerPhone(customer.getPhone());
            }
            OrderMaster orderMaster = orderMasterMap.get(detail.getOrderMasterId());
            if (orderMaster.getExpectTimeStart() != null && orderMaster.getExpectTimeEnd() != null) {
                detail.setMixExpectTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", orderMaster.getExpectTimeStart()) + "-" + DateUtils.parseDateToStr("HH:mm", orderMaster.getExpectTimeEnd()));
            }
            if (orderMaster != null) {
                detail.setPayStatus(orderMaster.getPayStatus());
                detail.setPayType(orderMaster.getPayType());
                detail.setPayTime(orderMaster.getPayTime());
                FinancialMaster fm = financialMasterMap.get(detail.getOrderMasterId());
                if (fm != null) {
                    detail.setFinancialMasterMoney(fm.getPayMoney());
                }
                Goods good = goodsMap.get(orderMaster.getGoodsId());
                if (good != null) {
                    detail.setGoods(good);
                    // 填充商品三级类目
                    if (good.getDeptGoodsCategoryId() != null) {
                        Long categoryId = null;
                        // 前端发单和后台派单
                        if (StringUtils.isEmpty(orderMaster.getOrderMode())) {
                            DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(good.getDeptGoodsCategoryId());
                            if (deptGoodsCategory != null) {
                                categoryId = deptGoodsCategory.getGoodsCategoryId();
                            }
                        } else {
                            DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(good.getDeptGoodsCategoryId());
                            if (deptGoodsCategory != null) {
                                categoryId = deptGoodsCategory.getGoodsCategoryId();
                            }
                        }
                        if (categoryId != null) {
                            GoodsCategory one = goodsCategoryService.selectById(categoryId);
                            if (one != null && one.getParentCategoryId() != null) {
                                GoodsCategory two = goodsCategoryService.selectById(one.getParentCategoryId());
                                if (two != null && two.getParentCategoryId() != null) {
                                    GoodsCategory three = goodsCategoryService.selectById(two.getParentCategoryId());
                                    if (three != null) {
                                        int totalNum = orderGoodsService.selectByOrderDetailId(detail.getId()).stream().mapToInt(OrderGoods::getGoodsNum).sum();
                                        detail.setConsoleGoodsName("【" + three.getGoodsCategoryName()
                                                + "-" + two.getGoodsCategoryName()
                                                + "-" + one.getGoodsCategoryName() + "】x" + totalNum);
                                    }
                                }
                            }
                        }
                    }
                    detail.setGoodsWorker(workerMap.get(orderMaster.getWorkerId()));
                }
                // 地址信息
                CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
                if (customerAddress != null) {
                    SysArea sysArea;
                    if (customerAddress.getStreetId() != null) {
                        sysArea = sysAreaService.selectById(customerAddress.getStreetId());
                    } else {
                        sysArea = sysAreaService.selectById(customerAddress.getCountryId());
                    }
                    String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress();
                    detail.setAddressName(customerAddress.getName());
                    detail.setAddressPhone(customerAddress.getPhone());
                    detail.setAddress(completeAddress);
                }
            }
            FinancialDetail fd = financialDetailMap.get(detail.getId());
            if (fd != null) {
                detail.setFinancialDetailMoney(fd.getPayMoney());
            }
            OrderCallRecord orderCallRecordParam = new OrderCallRecord();
            orderCallRecordParam.setOrderType("01");
            orderCallRecordParam.setOrderId(detail.getOrderMasterId());
            List<OrderCallRecord> orderCallRecords = orderCallRecordService.selectOrderCallRecordList(orderCallRecordParam);
            if (CollectionUtils.isNotEmpty(orderCallRecords)) {
                OrderCallRecord newOne = orderCallRecords.stream().sorted(Comparator.comparing(OrderCallRecord::getCallTime).reversed()).collect(Collectors.toList()).get(0);
                detail.setLastCallTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", newOne.getCallTime()) + " 拨号次数x" + orderCallRecords.size());
            }
            FinancialChangeRecord fc = fcMap.get(detail.getId());
            if (fc != null) {
                detail.setChangeMoney(fc.getChangeMoney());
            }
            // 完单图片
            GoodsImgs param = new GoodsImgs();
            param.setRemark(String.valueOf(detail.getId()));
            param.setImgType(ImgType.FINISH_IMG.getId());
            List<GoodsImgs> imgsList = goodsImgsService.qryGoodsImgs(param);
            StringBuilder stringBuilder = new StringBuilder();
            imgsList.forEach(model -> {
                stringBuilder.append("<a target='_blank' href='").append(model.getImgUrl()).append("' >查看图片<a/>");
            });
            detail.setOrderImgs(stringBuilder.toString());
        }

        // 使用手动获取的总数返回正确的分页信息
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(orderDetails);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 查询每种订单状态有多少数量
     */
    @GetMapping("/status/count")
    @ResponseBody
    public AjaxResult statusCount() {
        List<OrderStatusCount> statusCount = orderDetailService.statusCount();
        return AjaxResult.success(statusCount);
    }

    @PostMapping("/app/detail")
    @ResponseBody
    public AjaxResult appDetail(@RequestBody OrderDetail request) {
        try {
            // 子单信息
            OrderDetail detail = orderDetailService.selectById(request.getId());
            if (detail == null) {
                return AjaxResult.error("订单详情不存在");
            }
            // 主单信息
            OrderMaster orderMaster = orderMasterService.selectById(detail.getOrderMasterId());
            FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMaster.getId());
            // 初始化属性
            OrderListResponse orderListResponse = new OrderListResponse();
            List<OrderStandard> standardList = new ArrayList<>();

            // 师傅信息
            Worker worker = workerService.selectById(detail.getWorkerId());
            WorkerCertification workerRealInfo = null;
            String workerName = "";
            String masterWorkerName = "";
            String masterCompanyName = "";
            if (worker != null) {
                // 师傅实名信息
                workerRealInfo = workerCertificationService.selectByWorkerId(orderMaster.getWorkerId());
                workerName = workerRealInfo == null ? worker.getName() : workerRealInfo.getSurname() + workerRealInfo.getName();
            }
            workerRealInfo = null;
            // 大师傅信息
            Worker masterWorker = workerService.selectById(orderMaster.getWorkerId());
            if (masterWorker != null) {
                // 大师傅实名信息
                workerRealInfo = workerCertificationService.selectByWorkerId(orderMaster.getWorkerId());
                masterWorkerName = workerRealInfo == null ? masterWorker.getName() : workerRealInfo.getSurname() + workerRealInfo.getName();
                masterCompanyName = workerRealInfo == null ? null : workerRealInfo.getCompanyName();
            }

            // 消费者信息
//            Customer customer = customerService.selectByCustomerId(detail.getCustomerId());

            // 商品规格及信息
            List<OrderGoods> orderStandardList = orderGoodsService.selectByOrderDetailId(detail.getId());

            // 商品信息
//            GoodsStandard goodsStandard = goodsStandardService.selectById(orderStandardList.get(0).getGoodsStandardId());

            Goods goods = goodsService.selectById(orderMaster.getGoodsId());
            if (goods != null) {
                // 填充商品三级类目
                if (goods.getDeptGoodsCategoryId() != null) {
                    Long categoryId = null;
                    // 前端发单和后台派单
                    if (StringUtils.isEmpty(orderMaster.getOrderMode())) {
                        DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(goods.getDeptGoodsCategoryId());
                        if (deptGoodsCategory != null) {
                            categoryId = deptGoodsCategory.getGoodsCategoryId();
                        }
                    } else {
                        DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(goods.getDeptGoodsCategoryId());
                        if (deptGoodsCategory != null) {
                            categoryId = deptGoodsCategory.getGoodsCategoryId();
                        }
                    }
                    if (categoryId != null) {
                        GoodsCategory one = goodsCategoryService.selectById(categoryId);
                        if (one != null && one.getParentCategoryId() != null) {
                            GoodsCategory two = goodsCategoryService.selectById(one.getParentCategoryId());
                            if (two != null && two.getParentCategoryId() != null) {
                                GoodsCategory three = goodsCategoryService.selectById(two.getParentCategoryId());
                                if (three != null) {
                                    goods.setGoodsName(three.getGoodsCategoryName()
                                            + "-" + two.getGoodsCategoryName()
                                            + "-" + one.getGoodsCategoryName());
                                    orderListResponse.setTotalName(goods.getGoodsName());
                                }
                            }
                        }
                    }
                }
            }

            // 财务信息
            FinancialDetail financialDetail = financialDetailService.selectByOrderDetailId(detail.getId());
            BigDecimal detailPayMoney = financialDetail.getPayMoney();
            
            // 查询子单加价记录
//            FinancialChangeRecord changeRecordQry = new FinancialChangeRecord();
//            changeRecordQry.setOrderDetailId(detail.getId());
//            List<FinancialChangeRecord> financialChangeRecords = financialChangeRecordService.selectFinancialChangeRecordList(changeRecordQry);
//            if (CollectionUtils.isNotEmpty(financialChangeRecords)) {
//                detailPayMoney = detailPayMoney.add(financialChangeRecords.get(0).getChangeMoney());
//            }

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
            SysArea sysArea;
            if (customerAddress.getStreetId() != null) {
                sysArea = sysAreaService.selectById(customerAddress.getStreetId());
            } else {
                sysArea = sysAreaService.selectById(customerAddress.getCountryId());
            }
            String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress();

            // 完工图片
            List<String> finishImgList = null;
            if (detail.getOrderStatus() > 3) {
                GoodsImgs qryImgsObj = new GoodsImgs();
                qryImgsObj.setRemark(request.getId().toString());
                qryImgsObj.setImgType(ImgType.FINISH_IMG.getId());
                qryImgsObj.setGoodsId(0L);
                List<GoodsImgs> goodsImgs = goodsImgsService.qryGoodsImgs(qryImgsObj);
                finishImgList = goodsImgs.stream().map(GoodsImgs::getImgUrl).collect(Collectors.toList());
            }

            for (OrderGoods orderGoods : orderStandardList) {
                OrderStandard orderStandard = new OrderStandard();
                orderStandard.setStandardName(orderGoods.getGoodsName());
                orderStandard.setStandardNum(orderGoods.getGoodsNum());
                orderStandard.setServerNum(orderGoods.getServerGoodsNum() == null ? 0 : orderGoods.getServerGoodsNum());
                orderStandard.setWaitServerNum(orderStandard.getStandardNum() - orderStandard.getServerNum());
                standardList.add(orderStandard);
            }
             if (this.checkIsOnlyServer(orderMaster.getId(), orderMaster.getWorkerId())) {
                BigDecimal workerFee = financialMaster.getPayMoney();
                List<FinancialDetail> financialDetailList = financialDetailService.selectByFinancialMasterId(financialMaster.getId());
                for (FinancialDetail param : financialDetailList) {
                    if (!detail.getWorkerId().equals(param.getPayeeId())) {
                        workerFee = workerFee.subtract(param.getPayMoney());
                    }
                }
                orderListResponse.setPayMoney(workerFee);
                orderListResponse.setPayAddMoney(workerFee);
                orderListResponse.setIsOnlyServ(Boolean.TRUE);
            } else {
                orderListResponse.setPayMoney(detailPayMoney);
                orderListResponse.setPayAddMoney(detailPayMoney);
                orderListResponse.setIsOnlyServ(Boolean.FALSE);
            }

            // 售后记录
            AfterServiceRecord afterServiceRecord = new AfterServiceRecord();
            afterServiceRecord.setOrderDetailId(detail.getId());
            List<AfterServiceRecord> afterServiceRecordList = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
            if (CollectionUtils.isNotEmpty(afterServiceRecordList)) {
                // 计算平台退款金额总和
                BigDecimal totalPlatformRefund = BigDecimal.ZERO;
                for (AfterServiceRecord record : afterServiceRecordList) {
                    if (record.getPlatformRefund() != null) {
                        totalPlatformRefund = totalPlatformRefund.add(record.getPlatformRefund());
                    }
                }
                // 从detailPayMoney中减去平台退款金额
                // if (totalPlatformRefund.compareTo(BigDecimal.ZERO) > 0) {
                //     orderListResponse.setPayAddMoney(detailPayMoney.subtract(totalPlatformRefund));

                // }

             }
            
            
          



            // 查询加价记录
            FinancialChangeRecord changeRecordQry = new FinancialChangeRecord();
            changeRecordQry.setOrderDetailId(detail.getId());
            List<FinancialChangeRecord> financialChangeRecords = financialChangeRecordService.selectFinancialChangeRecordList(changeRecordQry);
            OrderAttachmentRecord param = new OrderAttachmentRecord();
            param.setOrderDetailId(detail.getId());
            List<OrderAttachmentRecord> orderAttachmentRecordList = orderAttachmentRecordService.selectOrderAttachmentRecordList(param);
            BigDecimal addMoneyTotal = BigDecimal.ZERO;
            String addMoneyRemark = null;

            for (FinancialChangeRecord financialChangeRecord : financialChangeRecords) {
                addMoneyTotal = addMoneyTotal.add(financialChangeRecord.getChangeMoney());
                if (StringUtils.isNotEmpty(financialChangeRecord.getRemark())) {
                    addMoneyRemark = financialChangeRecord.getRemark();
                }
                OrderAttachmentRecord param1 = new OrderAttachmentRecord();
                param1.setFinancialChangeRecordId(financialChangeRecord.getId());
                BigDecimal attachmentMoney = BigDecimal.ZERO;
                List<OrderAttachmentRecord> orderAttachmentRecords = orderAttachmentRecordService.selectOrderAttachmentRecordList(param1);
                logger.info("参数传递{}，配件的金额查询为{}", orderAttachmentRecords, param1);
                if (orderAttachmentRecords != null && orderAttachmentRecords.size() > 0) {
                    attachmentMoney = attachmentMoney.add(orderAttachmentRecords.get(0).getAttachMoney());
                    financialChangeRecord.setAttachmentMoney(attachmentMoney);
                    logger.info("配件的金为{}", attachmentMoney);
                }

            }
            for (OrderAttachmentRecord orderAttachmentRecord : orderAttachmentRecordList) {
                addMoneyTotal = addMoneyTotal.add(orderAttachmentRecord.getAttachMoney());
            }
            logger.info("所有的加价订单{}", financialChangeRecords);

            Shop shop = shopService.getShop(goods.getShopId());

            // 计算主单地址与店铺的距离
            shop = calculateShopDistance(orderMaster, shop);
            Shop serviceShop = shopService.getShop(orderMaster.getServiceShopId());
            serviceShop=calculateShopDistance(orderMaster, serviceShop);
            orderListResponse.setServiceShop(serviceShop);
            orderListResponse.setShop(shop);
            // 编辑返回属性
            orderListResponse.setTrackingNumber(detail.getTrackingNumber());
            orderListResponse.setDeliveryImages(detail.getDeliveryImages());
            orderListResponse.setDeliveryRemark(detail.getDeliveryRemark());
            orderListResponse.setDeliveryType(detail.getDeliveryType());
            orderListResponse.setShareAccountCountdownEndTime(detail.getShareAccountCountdownEndTime());
            orderListResponse.setShareAccountCountdownDuration(detail.getShareAccountCountdownDuration());
            orderListResponse.setHandoverImages(detail.getHandoverImages());
            orderListResponse.setHandoverRemark(detail.getHandoverRemark());
            orderListResponse.setGoods(goods);

            orderListResponse.setOrderType(detail.getOrderType());
            orderListResponse.setAfterPlatformServiceStatus(detail.getAfterServiceStatus());
            orderListResponse.setAddMoneyRemark(addMoneyRemark);
            orderListResponse.setAddMoney(addMoneyTotal);
            orderListResponse.setOrderDetailId(detail.getId());
            orderListResponse.setOrderDetailCode(detail.getCode());
            orderListResponse.setOrderMasterId(detail.getOrderMasterId());
            orderListResponse.setOrderMasterCode(detail.getOrderMasterCode());
            orderListResponse.setGoodsName(goods.getGoodsName());
            orderListResponse.setGoodsLogoUrl(goods.getGoodsImgUrl());
            orderListResponse.setDiscountMoney(financialDetail.getDiscountMoney());
            orderListResponse.setTotalMoney(financialDetail.getTotalMoney());
            orderListResponse.setWorkerName(workerName);
            orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
            orderListResponse.setMasterWorkerName(masterWorkerName);
            orderListResponse.setMasterCompanyName(masterCompanyName);
            orderListResponse.setMasterWorkerPhone(masterWorker == null ? "" : masterWorker.getPhone());
            orderListResponse.setCustomerName(org.apache.commons.lang3.StringUtils.isNotEmpty(orderMaster.getName()) ? orderMaster.getName() : customerAddress.getName());
            orderListResponse.setCustomerPhone(org.apache.commons.lang3.StringUtils.isNotEmpty(orderMaster.getPhone()) ? orderMaster.getPhone() : customerAddress.getPhone());
            orderListResponse.setOrderStatus(detail.getOrderStatus());
            orderListResponse.setPayStatus(orderMaster.getPayStatus());
            orderListResponse.setRefundPayStatus(orderMaster.getRefundPayStatus());
            orderListResponse.setPayType(orderMaster.getPayType());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(StringUtils.isNotEmpty(orderMaster.getAddress()) ? orderMaster.getAddress() : completeAddress);
            orderListResponse.setServerTime(detail.getRevTime());
            orderListResponse.setExpectTimeStart(detail.getExpectTimeStart());
            orderListResponse.setExpectTimeEnd(detail.getExpectTimeEnd());
            orderListResponse.setExpectTimeStartFront(detail.getExpectTimeStart());
            orderListResponse.setExpectTimeEndFront(detail.getExpectTimeEnd());
            orderListResponse.setCreateTime(detail.getCreateTime());
            orderListResponse.setWorkBeginTime(detail.getWorkBeginTime());
            orderListResponse.setWorkFinishTime(detail.getWorkFinishTime());
            orderListResponse.setRemark(goods.getRemark());
            orderListResponse.setCustomerRemark(orderMaster.getRemark());
            orderListResponse.setOrderImages(detail.getOrderImages());
            orderListResponse.setFinishImgList(finishImgList);
            orderListResponse.setWorkerRemark(detail.getRemark());
            orderListResponse.setAfterServiceRecordList(afterServiceRecordList);
            orderListResponse.setTimeout(detail.getTimeout());
            orderListResponse.setTimeoutFineTimes(detail.getTimeoutFineTimes());
            // TODO: 超时扣费
            orderListResponse.setFinancialChangeRecords(financialChangeRecords);
            orderListResponse.setInsuranceId(orderMaster.getInsuranceId());
            orderListResponse.setProvinceName(orderMaster.getProvinceName());
            orderListResponse.setCityName(orderMaster.getCityName());
            orderListResponse.setCountryName(orderMaster.getCountryName());
            orderListResponse.setStreetName(orderMaster.getStreetName());
            orderListResponse.setProvinceId(orderMaster.getProvinceId());
            orderListResponse.setCityId(orderMaster.getCityId());
            orderListResponse.setCountryId(orderMaster.getCountryId());
            orderListResponse.setStreetId(orderMaster.getStreetId());
            orderListResponse.setPhone(orderMaster.getPhone());
            orderListResponse.setName(orderMaster.getName());
            String addressSysArea = sysArea.getMergerName();
            String[] array = addressSysArea.split(",");
            if (org.apache.commons.lang3.StringUtils.isEmpty(orderMaster.getProvinceName())) {
                orderListResponse.setProvinceName(array[0]);
                orderListResponse.setCityName(array[1]);
                orderListResponse.setCountryName(array[2]);
                if (array.length > 3) {
                    orderListResponse.setStreetName(array[3]);
                }
                orderListResponse.setProvinceId(customerAddress.getProvinceId());
                orderListResponse.setCityId(customerAddress.getCityId());
                orderListResponse.setCountryId(customerAddress.getCountryId());
                orderListResponse.setStreetId(customerAddress.getStreetId());
                orderListResponse.setPhone(customerAddress.getPhone());
                orderListResponse.setName(customerAddress.getName());
            }
            if (orderMaster.getInsuranceId() != null) {
                InsuranceManager manager = insuranceManagerService.selectInsuranceManagerById(orderMaster.getInsuranceId());
                if (manager != null) {
                    orderListResponse.setInsuranceManager(manager);
                }
            }

            if (!CollectionUtils.isEmpty(financialChangeRecords)) {
                BigDecimal totalAdd = financialChangeRecords.stream()
                        .filter(record -> record.getPayStatus() == 1)  // 筛选 paystatus == 1 的记录
                        .map(FinancialChangeRecord::getChangeMoney)    // 提取 changeMoney
                        .reduce(BigDecimal.ZERO, BigDecimal::add);     // 计算总和
                Long goodsStandardId = orderStandardList.get(0).getGoodsStandardId();
                GoodsStandard standard = goodsStandardService.selectById(goodsStandardId);
                // 第四级规格
                DeptGoodsCategory childCategory = deptGoodsCategoryService.get(standard.getDeptGoodsCategoryId());
                // 需要找第三级规格
                DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(childCategory.getParentCategoryId());

                // 一级分销追加扣点
                BigDecimal saleRateOne = new BigDecimal(deptGoodsCategory.getOneRate());
                saleRateOne = MoneyUtil.lt0(saleRateOne) ? BigDecimal.ZERO : saleRateOne;

                // 二级分销追加扣点
                BigDecimal saleRateTwo = new BigDecimal(deptGoodsCategory.getTwoRate());
                saleRateTwo = MoneyUtil.lt0(saleRateTwo) ? BigDecimal.ZERO : saleRateTwo;

                // 三级分销追加扣点
                BigDecimal saleRateThree = new BigDecimal(deptGoodsCategory.getThreeRate());
                saleRateThree = MoneyUtil.lt0(saleRateThree) ? BigDecimal.ZERO : saleRateThree;

                // 分销扣点合计
                saleRateOne = saleRateOne.add(saleRateTwo).add(saleRateThree);

                // 大师傅提成
                BigDecimal teamRete = new BigDecimal(masterWorker.getLeaderTeamRate());
                teamRete = MoneyUtil.lt0(teamRete) ? BigDecimal.ZERO : teamRete;
                BigDecimal teamMoney = new BigDecimal(masterWorker.getLeaderTeamMoney());
                teamMoney = MoneyUtil.lt0(teamMoney) ? BigDecimal.ZERO : teamMoney;

                // 平台加价抽成
                BigDecimal deptRate = new BigDecimal(deptGoodsCategory.getDeptRate());
                deptRate = MoneyUtil.lt0(deptRate) ? BigDecimal.ZERO : deptRate;
                BigDecimal deptMoney = MoneyUtil.lt0(deptGoodsCategory.getDeptMoney()) ? BigDecimal.ZERO : deptGoodsCategory.getDeptMoney();

                // 分销抽成
                BigDecimal customerFee = totalAdd.multiply(saleRateOne).setScale(2, RoundingMode.UP);
                // 平台抽成
                BigDecimal platformFee = totalAdd.multiply(deptRate).add(deptMoney).setScale(2, RoundingMode.UP);
                // 大师傅抽成
                BigDecimal masterFee = totalAdd.multiply(teamRete).add(teamMoney).setScale(2, RoundingMode.UP);
                // 如果提成>追加金额 则去掉固定提成 只计算比例提成
                if (MoneyUtil.lt(masterFee.add(platformFee), totalAdd)) {
                    platformFee = totalAdd.multiply(deptRate).setScale(2, RoundingMode.UP);
                    masterFee = totalAdd.multiply(teamRete).setScale(2, RoundingMode.UP);
                }
                // 如果是大师傅自己接单,则不需要抽成
                if (detail.getWorkerId().equals(orderMaster.getWorkerId())) {
                    masterFee = BigDecimal.ZERO;
                }
                // 上门师傅应得加价的报酬
                BigDecimal workerFee = financialChangeRecords.stream()
                        .filter(record -> record != null && record.getPayStatus() == 1)
                        .map(record -> {
                            BigDecimal money = record.getWorkerMoney();
                            return money != null ? money : BigDecimal.ZERO;  // 处理 getWorkerMoney() 返回 null 的情况
                        }).reduce(BigDecimal.ZERO, BigDecimal::add);
                OrderAttachmentRecord orderAttachmentRecord = new OrderAttachmentRecord();
                orderAttachmentRecord.setOrderDetailId(detail.getId());
                List<OrderAttachmentRecord> orderAttachmentRecords = orderAttachmentRecordService.selectOrderAttachmentRecordList(orderAttachmentRecord);
                //配件费的实际获取金额计算费率
                BigDecimal rate = BigDecimal.valueOf(0.99);
                List<FinancialChangeRecord> changeRecords = financialChangeRecords.stream()
                        .filter(record -> record.getPayStatus() == 1).collect(Collectors.toList());
                BigDecimal attachmentMoney = BigDecimal.ZERO;
                if (!changeRecords.isEmpty()) {
                    attachmentMoney = orderAttachmentRecords.stream()
                            .filter(record -> StringUtils.isNotEmpty(record.getPaymentId()))
                            .map(OrderAttachmentRecord::getAttachMoney)
                            .map(attachMoney -> attachMoney.multiply(rate).setScale(2, RoundingMode.DOWN))
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                }
                BigDecimal platformRefundMoney = BigDecimal.ZERO;
                if (afterServiceRecord.getPlatformRefund()!=null) {
                    platformRefundMoney=afterServiceRecord.getPlatformRefund();
                }
                logger.info("是否支付{}加价的金额{}配件费{}总服务金额为{}", changeRecords, workerFee, attachmentMoney, orderListResponse.getPayMoney().add(workerFee).add(attachmentMoney));
             
                orderListResponse.setPayAddMoney(orderListResponse.getPayMoney().add(workerFee).add(attachmentMoney));
            }

            return AjaxResult.success(orderListResponse);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error();
        }
    }

    @PostMapping("/app/list")
    @ResponseBody
    public TableDataInfo appList(@RequestBody OrderDetail orderDetail) {

        startPage();
        List<OrderListResponse> orderListResponses = new ArrayList<>();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        list.forEach(detail -> {

            // 主单信息
            OrderMaster orderMaster = orderMasterService.selectById(detail.getOrderMasterId());
            FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMaster.getId());
            // 初始化属性
            OrderListResponse orderListResponse = new OrderListResponse();
            List<OrderStandard> standardList = new ArrayList<>();

            // 师傅信息
            Worker worker = workerService.selectById(detail.getWorkerId());
            Worker masterWorker = workerService.selectById(orderMaster.getWorkerId());
            WorkerCertification workerRealInfo = null;
            String workerName = "";
            if (worker != null) {
                // 师傅实名信息
                workerRealInfo = workerCertificationService.selectByWorkerId(orderMaster.getWorkerId());
                workerName = workerRealInfo == null ? worker.getName() : workerRealInfo.getSurname() + workerRealInfo.getName();
            }

            // 消费者信息
//            Customer customer = customerService.selectByCustomerId(detail.getCustomerId());

            // 商品规格及信息
            List<OrderGoods> orderStandardList = orderGoodsService.selectByOrderDetailId(detail.getId());

            Goods goods = goodsService.selectById(orderMaster.getGoodsId());
            // 填充商品三级类目
            if (goods.getDeptGoodsCategoryId() != null) {
                Long categoryId = null;
                // 前端发单和后台派单
                if (StringUtils.isEmpty(orderMaster.getOrderMode())) {
                    DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(goods.getDeptGoodsCategoryId());
                    if (deptGoodsCategory != null) {
                        categoryId = deptGoodsCategory.getGoodsCategoryId();
                    }
                } else {
                    DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(goods.getDeptGoodsCategoryId());
                    if (deptGoodsCategory != null) {
                        categoryId = deptGoodsCategory.getGoodsCategoryId();
                    }
                }
                if (categoryId != null) {
                    GoodsCategory one = goodsCategoryService.selectById(categoryId);
                    if (one != null && one.getParentCategoryId() != null) {
                        GoodsCategory two = goodsCategoryService.selectById(one.getParentCategoryId());
                        if (two != null && two.getParentCategoryId() != null) {
                            GoodsCategory three = goodsCategoryService.selectById(two.getParentCategoryId());
                            if (three != null) {
                                goods.setGoodsName(three.getGoodsCategoryName()
                                        + "-" + two.getGoodsCategoryName()
                                        + "-" + one.getGoodsCategoryName());
                            }
                        }
                    }
                }
            }
            // 财务信息
            FinancialDetail financialDetail = financialDetailService.selectByOrderDetailId(detail.getId());
            BigDecimal detailPayMoney = financialDetail.getPayMoney();

            // 查询子单加价记录
            FinancialChangeRecord financialChangeRecord = financialChangeRecordService.selectNotPayRecordByDetailId(detail.getId());
            BigDecimal changeMoney = financialChangeRecord == null ? BigDecimal.ZERO : financialChangeRecord.getChangeMoney();

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
            SysArea sysArea;
            if (customerAddress.getStreetId() != null) {
                sysArea = sysAreaService.selectById(customerAddress.getStreetId());
            } else {
                sysArea = sysAreaService.selectById(customerAddress.getCountryId());
            }
            String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress();

            for (OrderGoods orderGoods : orderStandardList) {
                OrderStandard orderStandard = new OrderStandard();
                orderStandard.setStandardName(orderGoods.getGoodsName());
                orderStandard.setStandardNum(orderGoods.getGoodsNum());
                orderStandard.setServerNum(orderGoods.getServerGoodsNum());
                orderStandard.setFinishTime(orderGoods.getFinishTime());
                orderStandard.setWaitServerNum(orderGoods.getGoodsNum() - orderGoods.getServerGoodsNum());
                standardList.add(orderStandard);
            }

            if (this.checkIsOnlyServer(orderMaster.getId(), detail.getWorkerId())) {
                BigDecimal workerFee = financialMaster.getPayMoney();
                List<FinancialDetail> financialDetailList = financialDetailService.selectByFinancialMasterId(financialMaster.getId());
                for (FinancialDetail param : financialDetailList) {
                    if (!detail.getWorkerId().equals(param.getPayeeId())) {
                        workerFee = workerFee.subtract(param.getPayMoney());
                    }
                }
                orderListResponse.setPayMoney(workerFee);
                orderListResponse.setIsOnlyServ(Boolean.TRUE);
            } else {
                orderListResponse.setPayMoney(detailPayMoney);
                orderListResponse.setIsOnlyServ(Boolean.FALSE);
            }
            AfterServiceRecord afterServiceRecord=new AfterServiceRecord();
            afterServiceRecord.setOrderDetailId(detail.getId());
            List<AfterServiceRecord> afterServiceRecordList=afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
           
            // 判断售后状态，如果售后状态不为1才显示售后记录按钮
            boolean hasNonDisputeStatus = afterServiceRecordList.stream()
                    .anyMatch(record -> record.getAfterServiceStatus() != null && (record.getAfterServiceStatus().equals(1)||record.getAfterServiceStatus().equals(2)));
            orderListResponse.setShowAfterServiceRecord(hasNonDisputeStatus ? 0 : 1);
            orderListResponse.setIsNeedBill(orderMaster.getIsNeedBill());
            orderListResponse.setIsMonitoredOrder(orderMaster.getIsMonitoredOrder());
            orderListResponse.setOrderImages(orderMaster.getOrderImages());
            orderListResponse.setIsInvoiced(orderMaster.getIsInvoiced());
            orderListResponse.setOriginalWorkerId(orderMaster.getOriginalWorkerId());
            orderListResponse.setConfirmStartTime(detail.getConfirmStartTime());
            orderListResponse.setDeliveryType(orderMaster.getDeliveryType());
            orderListResponse.setWorkerRemark(detail.getWorkerRemark());
            orderListResponse.setServiceCancelled(orderMaster.getServiceCancelled());
            orderListResponse.setOrderType(orderMaster.getOrderType());
            // 判断子单和主单的师傅id是否一致且主单的hasServiceOrder字段为1
            if (detail.getWorkerId() != null && orderMaster.getWorkerId() != null 
                && detail.getWorkerId().equals(orderMaster.getWorkerId()) 
                && orderMaster.getHasServiceOrder() != null && orderMaster.getHasServiceOrder() == 1) {
                orderListResponse.setHasServiceOrder(1);
            } else {
                orderListResponse.setHasServiceOrder(0);
            }


             Shop shop = shopService.getShop(goods.getShopId());

            // 计算主单地址与店铺的距离
           // shop = calculateShopDistance(orderMaster, shop);
            Shop serviceShop = shopService.getShop(orderMaster.getServiceShopId());
            // serviceShop=calculateShopDistance(orderMaster, serviceShop);
            orderListResponse.setServiceShop(serviceShop);
            orderListResponse.setShop(shop);
            orderListResponse.setServiceShopId(serviceShop != null ? serviceShop.getShopId() : null);
            if(orderMaster.getIsDeliveryToStore()!=null&&orderMaster.getIsDeliveryToStore()==1){
                orderListResponse.setIsDeliveryToStore(1);
                
            }
            // 编辑返回属性
            orderListResponse.setRefundPayStatus(orderMaster.getRefundPayStatus());
            orderListResponse.setAfterPlatformServiceStatus(detail.getAfterServiceStatus());
            orderListResponse.setShareAccountCountdownEndTime(detail.getShareAccountCountdownEndTime());
            orderListResponse.setShareAccountCountdownDuration(detail.getShareAccountCountdownDuration());
            orderListResponse.setOrderDetailId(detail.getId());
            orderListResponse.setOrderDetailCode(detail.getCode());
            orderListResponse.setOrderMasterId(detail.getOrderMasterId());
            orderListResponse.setOrderMasterCode(detail.getOrderMasterCode());
            orderListResponse.setGoods(goods);
            orderListResponse.setGoodsName(goods.getGoodsName());
            orderListResponse.setGoodsLogoUrl(goods.getGoodsImgUrl());
            orderListResponse.setDiscountMoney(financialDetail.getDiscountMoney());
            orderListResponse.setTotalMoney(financialDetail.getTotalMoney());
            orderListResponse.setWorkerName(workerName);
            orderListResponse.setWorkerId(worker.getWorkerId());
            orderListResponse.setMasterWorkerId(masterWorker.getWorkerId());
            orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
            orderListResponse.setMasterWorkerName(masterWorker.getName());
            orderListResponse.setCustomerName(org.apache.commons.lang3.StringUtils.isNotEmpty(orderMaster.getName()) ? orderMaster.getName() : customerAddress.getName());
            orderListResponse.setCustomerPhone(org.apache.commons.lang3.StringUtils.isNotEmpty(orderMaster.getPhone()) ? orderMaster.getPhone() : customerAddress.getPhone());
            orderListResponse.setOrderStatus(detail.getOrderStatus());
            orderListResponse.setPayStatus(orderMaster.getPayStatus());
            orderListResponse.setPayType(orderMaster.getPayType());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(StringUtils.isNotEmpty(orderMaster.getAddress()) ? orderMaster.getAddress() : completeAddress);
            orderListResponse.setServerTime(detail.getRevTime());
            orderListResponse.setExpectTimeStart(detail.getExpectTimeStart());
            orderListResponse.setExpectTimeEnd(detail.getExpectTimeEnd());
            orderListResponse.setExpectTimeStartFront(detail.getExpectTimeStart());
            orderListResponse.setExpectTimeEndFront(detail.getExpectTimeEnd());
            orderListResponse.setWorkBeginTime(detail.getWorkBeginTime());
            orderListResponse.setWorkFinishTime(detail.getWorkFinishTime());
            orderListResponse.setClockInLocation(detail.getClockInLocation());
            orderListResponse.setRemark(goods.getRemark());
            orderListResponse.setCustomerRemark(orderMaster.getRemark());
            orderListResponse.setOrderType(detail.getOrderType());
            orderListResponse.setTimeout(detail.getTimeout());
            orderListResponse.setChangeMoney(changeMoney);
            orderListResponse.setAfterServiceRecordList(afterServiceRecordList);
            orderListResponse.setUpdateTime(detail.getUpdateTime());
            orderListResponse.setShelveStatus(detail.getShelveStatus());
            orderListResponse.setLedgerAccountStatus(detail.getLedgerAccountStatus());
//            orderListResponse.setDrawCashId(detail.getDrawCashId());
            orderListResponse.setDrawCashTime(detail.getDrawCashTime());
            orderListResponse.setDrawCashStatus(detail.getDrawCashStatus());
            orderListResponse.setArrivalTime(detail.getArrivalTime());
            orderListResponse.setConsultMode(orderMaster.getConsultMode());
            orderListResponse.setInsuranceId(orderMaster.getInsuranceId());


            orderListResponse.setProvinceName(orderMaster.getProvinceName());
            orderListResponse.setCityName(orderMaster.getCityName());
            orderListResponse.setCountryName(orderMaster.getCountryName());
            orderListResponse.setStreetName(orderMaster.getStreetName());
            orderListResponse.setProvinceId(orderMaster.getProvinceId());
            orderListResponse.setCityId(orderMaster.getCityId());
            orderListResponse.setCountryId(orderMaster.getCountryId());
            orderListResponse.setStreetId(orderMaster.getStreetId());
            orderListResponse.setPhone(orderMaster.getPhone());
            orderListResponse.setName(orderMaster.getName());
            String addressSysArea = sysArea.getMergerName();
            String[] array = addressSysArea.split(",");
            if (org.apache.commons.lang3.StringUtils.isEmpty(orderMaster.getProvinceName())) {
                orderListResponse.setProvinceName(array[0]);
                orderListResponse.setCityName(array[1]);
                orderListResponse.setCountryName(array[2]);
                if (array.length > 3) {
                    orderListResponse.setStreetName(array[3]);
                }
                orderListResponse.setProvinceId(customerAddress.getProvinceId());
                orderListResponse.setCityId(customerAddress.getCityId());
                orderListResponse.setCountryId(customerAddress.getCountryId());
                orderListResponse.setStreetId(customerAddress.getStreetId());
                orderListResponse.setPhone(customerAddress.getPhone());
                orderListResponse.setName(customerAddress.getName());
            }
            if (orderMaster.getInsuranceId() != null) {
                InsuranceManager manager = insuranceManagerService.selectInsuranceManagerById(orderMaster.getInsuranceId());
                if (manager != null) {
                    orderListResponse.setInsuranceManager(manager);
                }
            }
            orderListResponses.add(orderListResponse);
        });
        return voDataTable(orderListResponses, list);
    }

    @PostMapping("/after/list")
    @ResponseBody
    public TableDataInfo afterServiceList(@RequestBody OrderDetail orderDetail) {
        AfterServiceRecord afterServiceRecord = new AfterServiceRecord();
        afterServiceRecord.setExcludeAfterServiceFinished(Boolean.TRUE);
        List<AfterServiceRecord> afterServiceRecordList = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
        Map<Long, List<AfterServiceRecord>> detailRecordMap = afterServiceRecordList.stream().collect(Collectors.groupingBy(record -> record.getOrderDetailId()));
        // 踢重后的子单ids
        List<Long> detailIds = afterServiceRecordList.stream().map(AfterServiceRecord::getOrderDetailId).distinct().collect(Collectors.toList());
        StringBuilder orderDetailIds = new StringBuilder();
        detailIds.forEach(id -> {
            orderDetailIds.append(id).append(",");
        });
        String ids = orderDetailIds.toString();
        if (StringUtils.isNotEmpty(ids)) {
            orderDetail.setOrderDetailIds(ids.substring(0, ids.length() - 1));
        } else {
            orderDetail.setOrderDetailIds("0");
        }
        startPage();
        List<OrderListResponse> orderListResponses = new ArrayList<>();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        list.forEach(detail -> {

            // 主单信息
            OrderMaster orderMaster = orderMasterService.selectById(detail.getOrderMasterId());
            // 初始化属性
            OrderListResponse orderListResponse = new OrderListResponse();
            List<OrderStandard> standardList = new ArrayList<>();

            // 师傅信息
            Worker worker = workerService.selectById(detail.getWorkerId());
            WorkerCertification workerRealInfo = null;
            String workerName = "";
            if (worker != null) {
                // 师傅实名信息
                workerRealInfo = workerCertificationService.selectByWorkerId(orderMaster.getWorkerId());
                workerName = workerRealInfo == null ? worker.getName() : workerRealInfo.getSurname() + workerRealInfo.getName();
            }

            // 消费者信息
//            Customer customer = customerService.selectByCustomerId(detail.getCustomerId());

            // 商品规格及信息
            List<OrderGoods> orderStandardList = orderGoodsService.selectByOrderDetailId(detail.getId());

            // 商品信息
            GoodsStandard goodsStandard = goodsStandardService.selectById(orderStandardList.get(0).getGoodsStandardId());

            Goods goods = goodsService.selectById(goodsStandard.getGoodsId());

            // 财务信息
            FinancialDetail financialDetail = financialDetailService.selectByOrderDetailId(detail.getId());
            BigDecimal detailPayMoney = financialDetail.getPayMoney();

            // 查询子单加价记录
            FinancialChangeRecord financialChangeRecord = financialChangeRecordService.selectNotPayRecordByDetailId(detail.getId());
            BigDecimal changeMoney = financialChangeRecord == null ? BigDecimal.ZERO : financialChangeRecord.getChangeMoney();
            // 查询子单加价记录
//            FinancialChangeRecord changeRecordQry = new FinancialChangeRecord();
//            changeRecordQry.setOrderDetailId(detail.getId());
//            List<FinancialChangeRecord> financialChangeRecords = financialChangeRecordService.selectFinancialChangeRecordList(changeRecordQry);
//            if (CollectionUtils.isNotEmpty(financialChangeRecords)) {
//                detailPayMoney = detailPayMoney.add(financialChangeRecords.get(0).getChangeMoney());
//            }

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
            SysArea sysArea;
            if (customerAddress.getStreetId() != null) {
                sysArea = sysAreaService.selectById(customerAddress.getStreetId());
            } else {
                sysArea = sysAreaService.selectById(customerAddress.getCountryId());
            }
            String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress();

            for (OrderGoods orderGoods : orderStandardList) {
                OrderStandard orderStandard = new OrderStandard();
                orderStandard.setStandardName(orderGoods.getGoodsName());
                orderStandard.setStandardNum(orderGoods.getGoodsNum());
                orderStandard.setServerNum(orderGoods.getServerGoodsNum());
                orderStandard.setFinishTime(orderGoods.getFinishTime());
                orderStandard.setWaitServerNum(orderGoods.getGoodsNum() - orderGoods.getServerGoodsNum());
                standardList.add(orderStandard);
            }

            if (this.checkIsOnlyServer(orderMaster.getId(), detail.getWorkerId())) {
                FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(detail.getOrderMasterId());
                BigDecimal workerFee = financialMaster.getPayMoney();
                List<FinancialDetail> financialDetailList = financialDetailService.selectByFinancialMasterId(financialMaster.getId());
                for (FinancialDetail param : financialDetailList) {
                    if (!detail.getWorkerId().equals(param.getPayeeId())) {
                        workerFee = workerFee.subtract(param.getPayMoney());
                    }
                }
                orderListResponse.setPayMoney(workerFee);
                orderListResponse.setIsOnlyServ(Boolean.TRUE);
            } else {
                orderListResponse.setPayMoney(detailPayMoney);
                orderListResponse.setIsOnlyServ(Boolean.FALSE);
            }

            orderListResponse.setIsNeedBill(orderMaster.getIsNeedBill());
            orderListResponse.setIsMonitoredOrder(orderMaster.getIsMonitoredOrder());
            orderListResponse.setOrderImages(orderMaster.getOrderImages());
            orderListResponse.setIsInvoiced(orderMaster.getIsInvoiced());
            orderListResponse.setOriginalWorkerId(orderMaster.getOriginalWorkerId());

            orderListResponse.setOrderType(orderMaster.getOrderType());
            orderListResponse.setHasServiceOrder(orderMaster.getHasServiceOrder());
            // 编辑返回属性
            orderListResponse.setOrderDetailId(detail.getId());
            orderListResponse.setOrderDetailCode(detail.getCode());
            orderListResponse.setOrderMasterId(detail.getOrderMasterId());
            orderListResponse.setOrderMasterCode(detail.getOrderMasterCode());
            orderListResponse.setGoods(goods);
            orderListResponse.setGoodsName(goods.getGoodsName());
            orderListResponse.setGoodsLogoUrl(goods.getGoodsImgUrl());
            orderListResponse.setDiscountMoney(financialDetail.getDiscountMoney());
            orderListResponse.setTotalMoney(financialDetail.getTotalMoney());
            orderListResponse.setWorkerName(workerName);
            orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
            orderListResponse.setCustomerName(org.apache.commons.lang3.StringUtils.isNotEmpty(orderMaster.getName()) ? orderMaster.getName() : customerAddress.getName());
            orderListResponse.setCustomerPhone(org.apache.commons.lang3.StringUtils.isNotEmpty(orderMaster.getPhone()) ? orderMaster.getPhone() : customerAddress.getPhone());
            orderListResponse.setOrderStatus(detail.getOrderStatus());
            orderListResponse.setPayStatus(orderMaster.getPayStatus());
            orderListResponse.setPayType(orderMaster.getPayType());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(StringUtils.isNotEmpty(orderMaster.getAddress()) ? orderMaster.getAddress() : completeAddress);
            orderListResponse.setServerTime(detail.getRevTime());
            orderListResponse.setExpectTimeStart(detail.getExpectTimeStart());
            orderListResponse.setExpectTimeEnd(detail.getExpectTimeEnd());
            orderListResponse.setExpectTimeStartFront(detail.getExpectTimeStart());
            orderListResponse.setExpectTimeEndFront(detail.getExpectTimeEnd());
            orderListResponse.setWorkBeginTime(detail.getWorkBeginTime());
            orderListResponse.setWorkFinishTime(detail.getWorkFinishTime());
            orderListResponse.setRemark(goods.getRemark());
            orderListResponse.setCustomerRemark(orderMaster.getRemark());
            orderListResponse.setOrderType(detail.getOrderType());
            orderListResponse.setTimeout(detail.getTimeout());
            orderListResponse.setAfterServiceRecordList(detailRecordMap.get(detail.getId()));
            orderListResponse.setChangeMoney(changeMoney);
            orderListResponse.setInsuranceId(orderMaster.getInsuranceId());
            orderListResponse.setProvinceName(orderMaster.getProvinceName());
            orderListResponse.setCityName(orderMaster.getCityName());
            orderListResponse.setCountryName(orderMaster.getCountryName());
            orderListResponse.setStreetName(orderMaster.getStreetName());
            orderListResponse.setProvinceId(orderMaster.getProvinceId());
            orderListResponse.setCityId(orderMaster.getCityId());
            orderListResponse.setCountryId(orderMaster.getCountryId());
            orderListResponse.setStreetId(orderMaster.getStreetId());
            orderListResponse.setPhone(orderMaster.getPhone());
            orderListResponse.setName(orderMaster.getName());
            String addressSysArea = sysArea.getMergerName();
            String[] array = addressSysArea.split(",");
            if (org.apache.commons.lang3.StringUtils.isEmpty(orderMaster.getProvinceName())) {
                orderListResponse.setProvinceName(array[0]);
                orderListResponse.setCityName(array[1]);
                orderListResponse.setCountryName(array[2]);
                if (array.length > 3) {
                    orderListResponse.setStreetName(array[3]);
                }
                orderListResponse.setProvinceId(customerAddress.getProvinceId());
                orderListResponse.setCityId(customerAddress.getCityId());
                orderListResponse.setCountryId(customerAddress.getCountryId());
                orderListResponse.setStreetId(customerAddress.getStreetId());
                orderListResponse.setPhone(customerAddress.getPhone());
                orderListResponse.setName(customerAddress.getName());
            }
            orderListResponses.add(orderListResponse);
        });
        return voDataTable(orderListResponses, list);
    }

    @Log(title = "详细订单管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("order:detail:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDetail orderDetail) {
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        ExcelUtil<OrderDetail> util = new ExcelUtil<>(OrderDetail.class);
        return util.exportExcel(list, "详细订单数据");
    }

    @RequiresPermissions("order:detail:remove")
    @Log(title = "详细订单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(orderDetailService.deleteOrderDetailByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改详细订单
     */
    @RequiresPermissions("order:detail:edit")
    @GetMapping("/edit/{orderDetailId}")
    public String edit(@PathVariable("orderDetailId") Long orderDetailId, ModelMap mmap) {
        mmap.put("orderDetail", orderDetailService.selectById(orderDetailId));
        return prefix + "/edit";
    }

    /**
     * 修改详细订单
     */
    @GetMapping("/payQrcode/{orderId}")
    public String payQrcode(@PathVariable("orderId") Long orderId, ModelMap mmap) {
        mmap.put("orderId", orderId);
        return "/order/pay-qrcode";
    }

    @GetMapping("/orderWorker/{orderId}")
    public String orderWorker(@PathVariable("orderId") Long orderId, ModelMap mmap) {
        List<Worker> workList = workerService.getWorkList(new Worker());
        mmap.put("orderId", orderId);
        mmap.put("workers", workList);
        return "order/orderWorker";
    }

    /**
     * 修改保存详细订单
     */
    @RequiresPermissions("order:detail:edit")
    @Log(title = "详细订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated OrderDetail orderDetail) {
        if (UserConstants.ORDER_CODE_NOT_UNIQUE.equals(orderDetailService.checkOrderDetailCodeUnique(orderDetail))) {
            return error("修改详细订单'" + orderDetail.getCode() + "'失败，详细订单编码已存在");
        }
        orderDetail.setUpdateBy(getLoginName());
        return toAjax(orderDetailService.updateOrderDetail(orderDetail));
    }

    /**
     * 修改保存详细订单
     */
    @PostMapping("/app/edit")
    @ResponseBody
    public AjaxResult appEdit(@RequestBody OrderDetail orderDetail) {
        if (UserConstants.ORDER_CODE_NOT_UNIQUE.equals(orderDetailService.checkOrderDetailCodeUnique(orderDetail))) {
            return error("修改详细订单'" + orderDetail.getCode() + "'失败，详细订单编码已存在");
        }
        if (StringUtils.isNotEmpty(orderDetail.getClockInLocation())) {
            OrderDetail model = orderDetailService.selectById(orderDetail.getId());
            Customer customer = customerService.selectByCustomerId(model.getCustomerId());
            // 更新订单状态为上门
            try {
                insuranceService.clockInsurance(model.getId());
            } catch (Exception e) {
                logger.error("订单上门返回异常:{}", e.getMessage());
                e.printStackTrace();
            }
            // 通知师傅新订单
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("thing1", "师傅打卡通知");
                params.put("thing5", "请确认师傅已到达!");
                wxMsgService.sendWxMsg(customer.getOpenId(), WxMsgEnum.CUSTOMER_ORDER, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      //  orderDetail=orderDetailService.selectById(orderDetail.getId());
        if (orderDetail.getIsQuicklyDelivery()!=null&&orderDetail.getIsQuicklyDelivery()==1) {
            // 立即发货时更新订单商品的已发货数量
            logger.info("立即发货订单[{}]开始更新商品已发货数量", orderDetail.getId());
            orderDetail.setTimeout(0);
            try {
                // 获取子订单商品列表
                List<OrderGoods> detailGoodsList = orderGoodsService.selectByOrderDetailId(orderDetail.getId());
                // 获取主订单商品列表
                OrderDetail orderDetail1 = orderDetailService.selectById(orderDetail.getId());
                List<OrderGoods> masterGoodsList = orderGoodsService.selectByOrderMasterId(orderDetail1.getOrderMasterId());
                
                logger.info("立即发货订单[{}]子订单商品数量：{}，主订单商品数量：{}", 
                    orderDetail.getId(), detailGoodsList.size(), masterGoodsList.size());
                
                // 对每个子订单商品进行处理
                for (OrderGoods detailGoods : detailGoodsList) {
                    // 找到对应的主订单商品
                    OrderGoods masterGoods = masterGoodsList.stream()
                        .filter(mg -> mg.getGoodsStandardId().equals(detailGoods.getGoodsStandardId()))
                        .findFirst()
                        .orElse(null);
                    
                    if (masterGoods != null) {
                        // 计算已分配给其他子订单的数量
                        // 查询该主单下该规格商品已分配数量（排除当前子单）
                        Integer allocatedNum = masterGoods.getServerGoodsNum() != null ? masterGoods.getServerGoodsNum() : 0;
                          
                        
                        // 计算剩余可发货数量 = 主单商品总数 - 已分配数量
                        int remainingNum = masterGoods.getGoodsNum() - allocatedNum;
                        
                        // 设置已发货数量 = Math.min(子单商品数量, Math.max(0, 剩余可发货数量))
                        int serverNum = Math.min(detailGoods.getGoodsNum(), Math.max(0, remainingNum));
                        
                        logger.info("立即发货订单[{}]商品[{}]数量计算：子单数量={}，主单总数={}，已分配={}，剩余={}，发货数量={}",
                            orderDetail.getId(), detailGoods.getGoodsStandardId(), 
                            detailGoods.getGoodsNum(), masterGoods.getGoodsNum(), allocatedNum, remainingNum, serverNum);
                        
                        // 更新子订单商品的serverGoodsNum字段
                        detailGoods.setServerGoodsNum(serverNum);
                        orderGoodsService.updateOrderGoods(detailGoods);
                        
                        // 更新主订单商品的serverGoodsNum字段
                        Integer currentMasterServerNum = masterGoods.getServerGoodsNum() != null ? masterGoods.getServerGoodsNum() : 0;
                        masterGoods.setServerGoodsNum(currentMasterServerNum + serverNum);
                        orderGoodsService.updateOrderGoods(masterGoods);
                        
                        logger.info("立即发货订单[{}]商品[{}]更新完成：子单发货数量={}，主单累计发货数量={}",
                            orderDetail.getId(), detailGoods.getGoodsStandardId(), serverNum, masterGoods.getServerGoodsNum());
                    } else {
                        logger.warn("立即发货订单[{}]未找到对应的主订单商品，商品规格ID：{}", 
                            orderDetail.getId(), detailGoods.getGoodsStandardId());
                    }
                }
                
                logger.info("立即发货订单[{}]商品已发货数量更新成功", orderDetail.getId());
            } catch (Exception e) {
                logger.error("立即发货订单[{}]更新商品已发货数量失败：{}", orderDetail.getId(), e.getMessage(), e);
                throw new RuntimeException("更新商品已发货数量失败：" + e.getMessage());
            }
        }
        return toAjax(orderDetailService.updateOrderDetail(orderDetail));
    }

    /**
     * 校验详细订单编码
     */
    @PostMapping("/checkOrderDetailCodeUnique")
    @ResponseBody
    public String checkOrderDetailCodeUnique(OrderDetail orderDetail) {
        return orderDetailService.checkOrderDetailCodeUnique(orderDetail);
    }

    /**
     * 消费者确认完成子订单
     *
     * @param orderDetailId 子订单ID
     */
    @PostMapping("/finish")
    @ResponseBody
    public AjaxResult finish(Long orderDetailId) {
        logger.info("消费者确认完成子订单,orderDetailId={}", orderDetailId);
        try {
            orderDetailService.finish(orderDetailId);
        } catch (BaseAdaPayException e) {
            logger.error(e.getMessage(), e);
            return AjaxResult.error("操作失败，请稍后重试");
        }
        return AjaxResult.success();
    }

    /**
     * 消费者申请取消子订单
     *
     * @param orderDetailId 子订单ID
     */
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult applyForCancel(Long orderDetailId) throws BaseAdaPayException {
        logger.info("消费者申请取消子订单,orderDetailId={}", orderDetailId);
        orderDetailService.applyForCancel(orderDetailId);
        return AjaxResult.success();
    }

    /**
     * 师傅审核取消子订单申请
     *
     * @param orderDetailId 子订单ID
     * @param agree         0=不同意 1=同意
     */
    @PostMapping("/cancel/agree")
    @ResponseBody
    public AjaxResult cancelAgree(Long orderDetailId, Integer agree) throws BaseAdaPayException {
        logger.info("师傅审核取消子订单申请,orderDetailId={},agree={}", orderDetailId, agree);
        orderDetailService.cancelAgree(orderDetailId, agree);
        return AjaxResult.success();
    }

    /**
     * 师傅发起完单
     *
     * @param request 完单发起请求对象
     */
    @PostMapping("/app/applyFinishOrder")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult applyFinishOrder(@RequestBody OrderProcessRequest request) {
        logger.info("师傅发起完单,orderDetailId={}", request.getOrderDetailId());
        // 判断是否有未完成的售后记录,如果有则不允许完单
        AfterServiceRecord unfinishedByOrderDetailId = afterServiceRecordService.getUnfinishedByOrderDetailId(request.getOrderDetailId());
        if (unfinishedByOrderDetailId != null) {
            return AjaxResult.error("存在未处理完的售后,不允许申请完单!");
        }
        // 判断工单状态是否为服务中以及是否已支付
        OrderDetail orderDetail = orderDetailService.selectById(request.getOrderDetailId());
        OrderMaster orderMaster = orderMasterService.selectById(orderDetail.getOrderMasterId());
        if (orderDetail.getOrderStatus().equals(OrderStatus.FINISH_CHECK.code())) {
            return AjaxResult.success("发起成功");
        }
        if (!"01".equals(orderMaster.getConsultMode()) && (orderDetail.getOrderStatus() != OrderStatus.SERVER.code()
                || !orderMaster.getPayStatus().equals(PayStatus.PAID.getCode()))) {
            return AjaxResult.error("未支付订单或非服务中订单，发起完单失败");
        }

        Worker worker = workerService.selectById(orderDetail.getWorkerId());
        Assert.notNull(worker, "师傅不存在 workerId=" + orderDetail.getWorkerId());
        WorkerCertification wcc = workerCertificationService.selectByWorkerId(orderDetail.getWorkerId());
        if (wcc == null || wcc.getStatus() == null || wcc.getStatus() != 1) {
            return AjaxResult.error("师傅未认证，请先去认证哦！");
        }

        // 存储完单图
        List<GoodsImgs> finishImgObjList = new ArrayList<>();
        for (String imgUrl : request.getFinishImgList()) {
            GoodsImgs finishImgObj = new GoodsImgs();
            finishImgObj.setGoodsId(0L);
            finishImgObj.setImgType(ImgType.FINISH_IMG.getId());
            finishImgObj.setImgUrl(imgUrl);
            finishImgObj.setRemark(String.valueOf(request.getOrderDetailId()));
            finishImgObjList.add(finishImgObj);
        }
        int insertedRows = goodsImgsService.batchInsert(finishImgObjList);
        Assert.isTrue(insertedRows > 0, "完单图入库失败，request=" + request);

        // 存储评价标签
        if (CollectionUtils.isNotEmpty(request.getAssessLabels())) {
            request.getAssessLabels().forEach(label -> {
                OrderAssessLabel orderAssessLabel = new OrderAssessLabel();
                orderAssessLabel.setLabelId(label);
                orderAssessLabel.setOrderId(request.getOrderDetailId());
                int insertLabelRow = orderAssessLabelService.insertOrderAssessLabel(orderAssessLabel);
                Assert.isTrue(insertLabelRow > 0, "，labelId=" + label);
            });
        }

        // 修改子单状态为完单待确认状态，更新完单时间
        Date workFinishTime = new Date();
        OrderDetail updateOrderDetail = new OrderDetail();
        updateOrderDetail.setId(request.getOrderDetailId());
        updateOrderDetail.setWorkFinishTime(workFinishTime);
        updateOrderDetail.setOrderStatus(OrderStatus.FINISH_CHECK.code());
        updateOrderDetail.setRemark(request.getRemark());

        // 查询订单商品对应的类目倒计时小时数，计算分账倒计时结束时间
        try {
            List<OrderGoods> orderGoodsList = orderGoodsService.selectByOrderDetailId(request.getOrderDetailId());
            if (CollectionUtils.isNotEmpty(orderGoodsList)) {
                // 取第一个商品的规格ID查询类目信息
                OrderGoods firstOrderGoods = orderGoodsList.get(0);
                GoodsStandard goodsStandard = goodsStandardService.selectById(firstOrderGoods.getGoodsStandardId());
                if (goodsStandard != null && goodsStandard.getDeptGoodsCategoryId() != null) {
                    // 递归查找类目的倒计时小时数
                    Integer countdownHours = deptGoodsCategoryService.findCountdownHoursRecursively(goodsStandard.getDeptGoodsCategoryId());
                    if (countdownHours != null) {
                        // 计算分账倒计时结束时间：workFinishTime + 倒计时小时数
                        long countdownMillis = countdownHours * 60 * 60 * 1000L;
                        Date shareAccountCountdownEndTime = new Date(workFinishTime.getTime() + countdownMillis);

                        updateOrderDetail.setShareAccountCountdownEndTime(shareAccountCountdownEndTime);
                        updateOrderDetail.setShareAccountCountdownDuration(countdownHours);

                        logger.info("订单[{}]设置分账倒计时：完单时间={}, 倒计时小时数={}, 分账倒计时结束时间={}",
                                request.getOrderDetailId(), workFinishTime, countdownHours, shareAccountCountdownEndTime);
                    } else {
                        logger.warn("订单[{}]未找到类目倒计时小时数配置，跳过分账倒计时设置", request.getOrderDetailId());
                    }
                }
            }
        } catch (Exception e) {
            logger.error("设置分账倒计时失败，订单ID：{}", request.getOrderDetailId(), e);
        }

        int affectedRow = orderDetailService.updateOrderDetail(updateOrderDetail);
        Assert.isTrue(affectedRow == 1, "子订单修改失败，orderDetail=" + orderDetail);

        OrderBehavior orderBehavior = new OrderBehavior()
                .setOrderMasterId(orderMaster.getId()).setOrderMasterCode(orderMaster.getCode())
                .setOrderDetailId(orderDetail.getId()).setOrderDetailCode(orderDetail.getCode())
                .setWorkerId(worker.getWorkerId()).setWorkerName(worker.getName())
                .setBehavior(OrderBehaviorEnum.FINISH_ORDER);
        orderBehaviorService.insert(orderBehavior);

        Customer customer = customerService.selectByCustomerId(orderDetail.getCustomerId());
        // 通知师傅新订单
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("thing1", "师傅完单通知");
            params.put("thing5", "请确认师傅工作已完成!");
            wxMsgService.sendWxMsg(customer.getOpenId(), WxMsgEnum.CUSTOMER_ORDER, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("发起成功");
    }

    /**
     * 拒绝订单
     * 后台管理系统使用，可控制权限
     */
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult reject(OrderDetail orderDetail) {
        return rejectDetailOrder(orderDetail);
    }

    @PostMapping("/callCustomer")
    @ResponseBody
    public AjaxResult callCustomer(@RequestBody OrderDetail orderDetail) {
        try {
            OrderDetail result = orderDetailService.selectById(orderDetail.getId());
            if (result.getTimeout() == 1 && OrderStatus.PLAIN.code() == result.getOrderStatus()) {
                orderDetail.setTimeout(0);
            }
            int i = orderDetailService.updateOrderDetail(orderDetail);
            if (i > 0) {
                OrderCallRecord param = new OrderCallRecord();
                param.setOrderId(orderDetail.getId());
                param.setCallTime(new Date());
                param.setOrderType("01");
                orderCallRecordService.insertOrderCallRecord(param);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 拒绝订单
     */
    @PostMapping("/app/order/reject")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult rejectDetailOrder(@RequestBody OrderDetail orderDetail) {
        // 查询订单信息
        OrderDetail od = this.orderDetailService.selectById(orderDetail.getId());
        OrderMaster om = orderMasterService.selectById(od.getOrderMasterId());
        Worker masterWorker = workerService.selectById(om.getWorkerId());
        Worker worker = workerService.selectById(od.getWorkerId());
        // 删除子单
        orderDetailService.deleteOrderDetailByIds(orderDetail.getId().toString());
        FinancialDetail financialDetail = financialDetailService.selectByOrderDetailId(od.getId());
        // 财务子单金额返还给主单
        FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(od.getOrderMasterId());
        financialMaster.setServerMoney(financialMaster.getServerMoney().add(financialDetail.getPayMoney()));
        financialMasterService.updateFinancialMaster(financialMaster);
        // 删除子财务单
        financialDetailService.deleteFinancialDetailByOrderDetailId(orderDetail.getId());
        // 退换加价费用
        List<FinancialChangeRecord> fcrList = financialChangeRecordService.selectByDetailIds(orderDetail.getId().toString());
        // 先把这个子单的加价单全退了
        // 主订单金额=订单原价+加价
        BigDecimal payMoney = financialMaster.getPayMoney();
        for (FinancialChangeRecord fcr : fcrList) {
            if (PayStatus.PAID.getCode().equals(fcr.getPayStatus()) || PayStatus.PAYED_ADD.getCode().equals(fcr.getPayStatus())) {
                executor.execute(() -> financialChangeRecordService.refund(om.getDeptId(), fcr.getId()));
                OrderAttachmentRecord orderAttachmentRecord = new OrderAttachmentRecord();
                orderAttachmentRecord.setFinancialChangeRecordId(fcr.getId());
                List<OrderAttachmentRecord> orderAttachmentRecords = orderAttachmentRecordService.selectOrderAttachmentRecordList(orderAttachmentRecord);

                // 创建一个定时任务，延迟1分钟后执行退款操作
                ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
                scheduledExecutor.schedule(() -> {
                    for (OrderAttachmentRecord orderAttachmentRecord1 : orderAttachmentRecords) {
                        executor.execute(() -> {
                            try {
                                JSONObject response = adapayService.payReverse(om.getDeptId(), orderAttachmentRecord1.getPaymentId(),
                                        String.valueOf(orderAttachmentRecord1.getAttachMoney()), RefundType.RCG);
                                logger.info("配件退款成功: paymentId={}, amount={}，response{}", orderAttachmentRecord1.getPaymentId(), orderAttachmentRecord1.getAttachMoney(), response);
                            } catch (BaseAdaPayException e) {
                                logger.error("配件退款失败: paymentId={}, amount={}, error={}",
                                        orderAttachmentRecord1.getPaymentId(), orderAttachmentRecord1.getAttachMoney(), e.getMessage(), e);
                            }
                        });
                    }
                    // 关闭定时器
                    scheduledExecutor.shutdown();
                }, 1, TimeUnit.MINUTES);
            }
        }
        orderAttachmentRecordService.deleteOrderAttachmentRecordByOrderDetailId(orderDetail.getId());
        // 退回旧商品
//        List<OrderGoods> goodsList = orderGoodsService.selectByOrderDetailId(orderDetail.getId());
//        goodsList.forEach(orderGoods -> {
//            List<OrderGoods> masterGoods = orderGoodsService.selectByOrderMasterId(od.getOrderMasterId());
//            masterGoods.forEach(master->{
//                if(master.getGoodsStandardId().equals(orderGoods.getGoodsStandardId())){
//                    master.setServerGoodsNum(master.getServerGoodsNum()-orderGoods.getGoodsNum());
//                }
//                orderGoodsService.updateOrderGoods(master);
//            });
//        });
        // 删除派单记录
        orderGoodsService.deleteByOrderDetailId(orderDetail.getId());
        OrderMaster orderMaster2Update = new OrderMaster();
        orderMaster2Update.setId(od.getOrderMasterId());
        orderMaster2Update.setOrderStatus(OrderStatus.PLAIN.code());
        // 查询是否还有派单记录，没有的话需要重置主单是否自己承接的字段为NULL
//        List<OrderGoods> existOrderGoods = orderGoodsService.selectByOrderMasterId(od.getOrderMasterId());
        List<OrderDetail> orderDetailList = orderDetailService.selectByOrderMasterId(od.getOrderMasterId());
        if (CollectionUtils.isEmpty(orderDetailList)) {
            orderMaster2Update.setResetAllSelfAssigned(true);
        }
        // 更新主单是否全部派完
        orderMaster2Update.setHasDispatchedAll(0);
        orderMasterService.updateOrderMaster(orderMaster2Update);

        OrderBehavior orderBehavior = new OrderBehavior()
                .setOrderMasterId(om.getId()).setOrderMasterCode(om.getCode())
                .setWorkerId(worker.getWorkerId()).setWorkerName(worker.getName())
                .setBehavior(OrderBehaviorEnum.REFUSE_ORDER);
        orderBehaviorService.insert(orderBehavior);

        // 拒单

       // logger.info("师傅资料{}", masterWorker);
        WorkerCertification workerRealInfo = workerCertificationService.selectByWorkerId(od.getWorkerId());
       // logger.info("认证师傅资料{}", workerRealInfo);
        try {
            // 推送公众号通知数据。
            // 消息组装。
            Map<String, Object> paramsNew = new HashMap<>();
            // 订单编号
            paramsNew.put("thing11", "您有派出的订单被拒接/退单，已退回");
            // 名称
            paramsNew.put("thing12", "请进入【我的订单--未约未排】查看");
            CustomerAddress address = addressService.selectByCustomerAddressId(om.getAddressId());

            //拒单人员
            paramsNew.put("thing13", workerRealInfo.getSurname() + workerRealInfo.getName());
            paramsNew.put("thing7", address.getName());
            // 预约时间
            paramsNew.put("time5", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", new Date()));
            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), masterWorker.getWxOpenId(), WxMsgEnum.REJECT_ORDER, paramsNew);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return AjaxResult.success();
    }

    @PostMapping("/app/change/price")
    @ResponseBody
    public AjaxResult changePrice(@Valid @RequestBody OrderChangePriceRequest request) {
        try {
            Assert.notNull(request.getChangeMoney(), "报价不能为空");
            return toAjax(orderDetailService.changePrice(request.getOrderDetailId(), request.getChangeMoney(), request.getType(), request.getRemark(), request.getUrls(), request.getFileNames(), request.getReason()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error("改价失败!");
        }
    }

    @PostMapping("/app/getChangePrice")
    @ResponseBody
    public AjaxResult getChangePrice(@Valid @RequestBody OrderChangePriceRequest request) {
        try {
            return AjaxResult.success(orderDetailService.getChangedPriceRecord(request.getOrderDetailId()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error("查询改价失败!");
        }
    }

    private boolean checkIsOnlyServer(Long orderMasterId, Long workId) {
        // 找出原单的数量
        List<OrderGoods> orderGoodsList = orderGoodsService.selectByOrderMasterId(orderMasterId);
        // 找出原单关联的细单的商品数量
        List<OrderDetail> orderDetailList = orderDetailService.selectByOrderMasterId(orderMasterId);
        for (OrderDetail detail : orderDetailList) {
            // 非一个师傅接单
            if (!workId.equals(detail.getWorkerId())) {
                return false;
            }
            // 子单数量
            List<OrderGoods> orderDetailGoodsList = orderGoodsService.selectByOrderDetailId(detail.getId());
            // 计算剩余未分配的商品数量
            for (OrderGoods detailGoods : orderDetailGoodsList) {
                for (OrderGoods masterGoods : orderGoodsList) {
                    if (Objects.equals(masterGoods.getGoodsStandardId(), detailGoods.getGoodsStandardId())) {
                        masterGoods.setGoodsNum(masterGoods.getGoodsNum() - detailGoods.getGoodsNum());
                    }
                }
            }
        }
        // 有单派完.
        for (OrderGoods orderGoods : orderGoodsList) {
            if (orderGoods.getGoodsNum() > 0) {
                return false;
            }
        }
        return true;
    }

    @PostMapping("/add-subtract/insert")
    @ResponseBody
    public AjaxResult addOrSubtract(@Valid @RequestBody OrderAddSubtract body) {
        body.setCreateBy(getLoginName());
        int i = orderDetailService.insertOrderAddSubtract(body);
        return AjaxResult.success(i);
    }

    @GetMapping("/differentStatus/count")
    @ResponseBody
    public AjaxResult differentStatusOrderCount(OrderDetail orderDetail) {
        return AjaxResult.success(orderDetailService.differentStatusOrderCount(orderDetail));
    }

    @GetMapping("/count")
    @ResponseBody
    public AjaxResult orderDetailCount(OrderDetail orderDetail) {
        if (StringUtils.isNotEmpty(orderDetail.getWorkerName()) || StringUtils.isNotEmpty(orderDetail.getWorkerPhone())) {
            Worker param = new Worker();
            param.setName(orderDetail.getWorkerName());
            param.setPhone(orderDetail.getWorkerPhone());
            List<Worker> workList = workerService.getWorkList(param);
            List<Long> ids = workList.stream().map(Worker::getWorkerId).collect(Collectors.toList());
            orderDetail.setWorkerIds(ids);
            if (CollectionUtils.isEmpty(ids)) {
                return AjaxResult.success(0);
            }
        }
        if (orderDetail.getSysPayStatus() != null) {
            FinancialMaster param = new FinancialMaster();
            param.setPayStatus(orderDetail.getSysPayStatus());
            List<FinancialMaster> financialMasters = financialMasterService.selectFinancialMasterList(param);
            List<Long> masterIds = financialMasters.stream().map(FinancialMaster::getOrderMasterId).collect(Collectors.toList());
            orderDetail.setMasterIds(masterIds);
            if (CollectionUtils.isEmpty(masterIds)) {
                return AjaxResult.success(0);
            }
        }
        if (this.getSysUser().getDept().getParentId() != 101) {
            orderDetail.setDeptId(this.getSysUser().getDept().getParentId());
        }
        return AjaxResult.success(orderDetailService.countOrderDetailList(orderDetail));
    }

    @GetMapping("/after/count")
    @ResponseBody
    public AjaxResult afterDetailOrderCount(OrderDetail orderDetail) {
        return AjaxResult.success(orderDetailService.afterDetailOrderCount(orderDetail));
    }

    @GetMapping("/after/dispute/count")
    @ResponseBody
    public AjaxResult countAfterDisputeList(OrderDetail orderDetail) {
        try {
            logger.info("查询子单售后纠纷数量");

            // 设置查询条件：售后纠纷状态
            orderDetail.setAfterServiceStatus(1);

            // 设置部门权限
//            if (this.getSysUser().getDept().getParentId() != 101) {
//                orderDetail.setDeptId(this.getSysUser().getDept().getParentId());
//            }

            return AjaxResult.success(orderDetailService.countOrderDetailList(orderDetail));
        } catch (Exception e) {
            logger.error("查询子单售后纠纷数量异常", e);
            return AjaxResult.error("查询售后纠纷数量失败：" + e.getMessage());
        }
    }

    @PostMapping("/after/records/byDetailId")
    @ResponseBody
    public AjaxResult getAfterServiceRecordsByDetailId(Long orderDetailId) {
        try {
            logger.info("查询子单[{}]的售后纠纷记录", orderDetailId);

            // 直接查询该子单的售后纠纷记录
            AfterServiceRecord queryRecord = new AfterServiceRecord();
            queryRecord.setOrderDetailId(orderDetailId);
            queryRecord.setCustomerFinalCheck(0L); // 客户不同意
            queryRecord.setExcludeAfterServiceFinished(Boolean.TRUE); // 排除已完成的售后

            List<AfterServiceRecord> disputeRecords = afterServiceRecordService.selectAfterServiceRecordList(queryRecord);

            logger.info("子单[{}]找到{}条售后纠纷记录", orderDetailId, disputeRecords.size());

            // 为每个售后记录添加子单信息
            OrderDetail detail = orderDetailService.selectById(orderDetailId);
            if (detail != null) {
                disputeRecords.forEach(record -> {
                    record.setOrderDetailCode(detail.getCode());
                });
            }

            return AjaxResult.success(disputeRecords);

        } catch (Exception e) {
            logger.error("查询子单[{}]售后纠纷记录异常", orderDetailId, e);
            return AjaxResult.error("查询售后纠纷记录失败：" + e.getMessage());
        }
    }

    @ResponseBody
    @Operation(summary = "获取当天订单统计数据", method = "GET")
    @GetMapping(path = "/statistics/now", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult statisticsNow() {
        return AjaxResult.success(orderDetailService.orderStatisticsReturnByNow());
    }

    @ResponseBody
    @Operation(summary = "获取全部订单统计数据", method = "GET")
    @GetMapping(path = "/statistics", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult statistics() {
        return AjaxResult.success(orderDetailService.orderStatisticsReturn());
    }

    /**
     * 延期到货接口
     * 如果是待确认状态则变为服务中，其他状态不变
     * 将confirm_start_time增加3天
     * 延期次数最多2次
     */
    @PostMapping("/app/delay")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delayOrder(@RequestBody OrderMaster orderMaster) {
        logger.info("延期到货,orderDetailId={}", orderMaster.getId());
        try {
            return toAjax(orderDetailService.delayOrder(orderMaster.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error("延期失败: " + e.getMessage());
        }
    }

    /**
     * 子订单退回接口
     * 如果订单为服务类型(orderType=0)则退回到服务中状态
     * 如果订单为商品类型(orderType=1)则退回到待排期状态
     */
    @PostMapping("/app/return")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult returnOrder(@RequestBody OrderDetail orderDetail) {
        logger.info("子订单退回,orderDetailId={}", orderDetail.getId());
        try {
            return toAjax(orderDetailService.returnOrder(orderDetail.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMessage(e));
            return AjaxResult.error("退回失败: " + e.getMessage());
        }
    }

    /**
     * 保存子单师傅备注
     */
    @PostMapping("/saveWorkerRemark")
    @ResponseBody
    public AjaxResult saveDetailWorkerRemark(@RequestBody OrderDetail orderDetail) {
        try {
            if (orderDetail.getWorkerRemark() == null || orderDetail.getWorkerRemark().trim().isEmpty()) {
                return AjaxResult.error("师傅备注不能为空");
            }

            if (orderDetail.getId() == null) {
                return AjaxResult.error("子单ID不能为空");
            }

            int result = orderDetailService.saveDetailWorkerRemark(orderDetail.getId(), orderDetail.getWorkerRemark());
            if (result > 0) {
                return AjaxResult.success("子单师傅备注保存成功");
            } else {
                return AjaxResult.error("子单师傅备注保存失败");
            }
        } catch (Exception e) {
            logger.error("保存子单师傅备注失败", e);
            return AjaxResult.error("保存子单师傅备注失败：" + e.getMessage());
        }
    }

    /**
     * 计算商家与主单地址之间的距离
     *
     * @param orderMaster 主单信息
     * @param shop        商家信息
     * @return 带有距离信息的商家对象
     */
    private Shop calculateShopDistance(OrderMaster orderMaster, Shop shop) {
        try {
            // 直接使用主单地址获取经纬度
            logger.info("使用主单地址获取经纬度");

            StringBuilder fullAddress = new StringBuilder();

            // 构建完整地址
            if (orderMaster.getProvinceId() != null) {
                SysArea province = sysAreaService.selectById(orderMaster.getProvinceId());
                if (province != null) {
                    fullAddress.append(province.getAreaName());
                }
            }

            if (orderMaster.getCityId() != null) {
                SysArea city = sysAreaService.selectById(orderMaster.getCityId());
                if (city != null) {
                    fullAddress.append(city.getAreaName());
                }
            }

            if (orderMaster.getCountryId() != null) {
                SysArea area = sysAreaService.selectById(orderMaster.getCountryId());
                if (area != null) {
                    fullAddress.append(area.getAreaName());
                }
            }

            if (orderMaster.getStreetId() != null) {
                SysArea street = sysAreaService.selectById(orderMaster.getStreetId());
                if (street != null) {
                    fullAddress.append(street.getAreaName());
                }
            }

            if (StringUtils.isNotEmpty(orderMaster.getAddress())) {
                fullAddress.append(orderMaster.getAddress());
            }

            String addressStr = fullAddress.toString();
            logger.info("构建的完整地址：{}", addressStr);

            if (StringUtils.isNotEmpty(addressStr)) {
                // 调用百度地图API获取经纬度
                Map<String, Double> coordinates = baiduMapUtils.getCoordinatesByAddress(shop.getProvinceName(), shop.getCityName(), shop.getCountryName(), shop.getStreetName(), addressStr);

                if (coordinates != null && coordinates.containsKey("longitude") && coordinates.containsKey("latitude")) {
                    BigDecimal lng = new BigDecimal(coordinates.get("longitude").toString());
                    BigDecimal lat = new BigDecimal(coordinates.get("latitude").toString());

                    logger.info("通过地址获取到经纬度：longitude={}, latitude={}", lng, lat);

                    // 计算距离
                    if (shop.getLongitude() != null && shop.getLatitude() != null) {

                        double distance = LocationUtils.getDistanceInMeters(
                                lat.doubleValue(),
                                lng.doubleValue(),
                                shop.getLatitude().doubleValue(),
                                shop.getLongitude().doubleValue()
                        );

                        String formattedDistance = LocationUtils.formatDistance(distance);
                        shop.setDistance(formattedDistance);
                        logger.info("通过地址获取经纬度后计算距离成功，商家[{}]与主单地址距离：{}", shop.getShopName(), formattedDistance);
                    }
                } else {
                    logger.warn("通过地址[{}]获取经纬度失败", addressStr);
                }
            } else {
                logger.warn("无法构建完整地址，跳过距离计算");
            }
        } catch (Exception e) {
            logger.error("计算商家与主单地址距离时发生异常", e);
        }

        return shop;
    }
}


    

