package com.ghy.web.controller.order;

import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.domain.entity.SysUser;
import com.ghy.common.enums.*;
import com.ghy.common.utils.WechatMsgUtils;
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.DeptGoodsCategory;
import com.ghy.goods.domain.Goods;
import com.ghy.goods.domain.GoodsStandard;
import com.ghy.goods.request.AppGoodsRequest;
import com.ghy.goods.service.DeptGoodsCategoryService;
import com.ghy.goods.service.GoodsService;
import com.ghy.goods.service.GoodsStandardService;
import com.ghy.order.domain.*;
import com.ghy.order.request.AppOrderAssignRequest;
import com.ghy.order.request.AppOrderRequest;
import com.ghy.order.request.SysOrderAssignRequest;
import com.ghy.order.request.SysOrderGoodsStandards;
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.FinancialChangeRecordService;
import com.ghy.payment.service.FinancialDetailService;
import com.ghy.payment.service.FinancialMasterService;
import com.ghy.system.domain.SysArea;
import com.ghy.system.service.ISysAreaService;
import com.ghy.system.service.IWxMsgService;
import com.ghy.web.pojo.vo.*;
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 org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author clunt
 * 下单接口
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

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

    /**
     * 可派单商品数量
     */
    @PostMapping("/can/assign")
    @ResponseBody
    public AjaxResult canAssign(@RequestBody AppOrderAssignRequest request) {
        try {
            // 找出原单的数量
            List<OrderGoods> orderGoodsList = orderGoodsService.selectByOrderMasterId(request.getOrderMasterId());
            // 找出原单关联的细单的商品数量
            List<OrderDetail> orderDetailList = orderDetailService.selectByOrderMasterId(request.getOrderMasterId());
            for (OrderDetail orderDetail : orderDetailList) {
                // 子单数量
                List<OrderGoods> orderDetailGoodsList = orderGoodsService.selectByOrderDetailId(orderDetail.getId());

                // 计算剩余未分配的商品数量
                for (OrderGoods detailGoods : orderDetailGoodsList) {
                    for (OrderGoods masterGoods : orderGoodsList) {
                        if (Objects.equals(masterGoods.getGoodsStandardId(), detailGoods.getGoodsStandardId())) {
                            masterGoods.setGoodsNum(masterGoods.getGoodsNum() - detailGoods.getGoodsNum());
                        }
                    }
                }
            }
            //计算新单的数量
            return AjaxResult.success(orderGoodsList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 后台批量下单
     */
    @PostMapping("/sys/order")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult sysOrder(@RequestBody SysOrderAssignRequest request) {

        // 判断传递价格是否有小数
        if (new BigDecimal(request.getPrice().intValue()).compareTo(request.getPrice())!=0){
            return AjaxResult.error("后台发单金额不能含有小数!");
        }

        // 判断是否有客户
        SysUser sysUser = getSysUser();
        Long deptId = sysUser.getDept().getDeptId();
        String loginName = sysUser.getLoginName();
        Customer customer = customerService.selectByAccount("sys_" + loginName);
        if (customer == null) {
            customer = new Customer();
            customer.setAccount("sys_" + loginName);
            customer.setName("平台用户_" + loginName);
            customerService.insertCustomer(customer);
        }

        // 判断是否有客户地址
        if (request.getProvinceId() == null || request.getCityId() == null || request.getDistrictId() == null || StringUtils.isBlank(request.getFullAddress())) {
            return AjaxResult.error("请填写地址");
        }
        CustomerAddress customerAddress = customerAddressService.selectByCustomerAndAddress(customer.getCustomerId(),
                request.getProvinceId(), request.getCityId(), request.getDistrictId(), request.getFullAddress());
        if (customerAddress == null) {
            customerAddress = new CustomerAddress();
            customerAddress.setCustomerId(customer.getCustomerId());
            customerAddress.setProvinceId(request.getProvinceId());
            customerAddress.setCityId(request.getCityId());
            customerAddress.setCountryId(request.getDistrictId());
            customerAddress.setAddress(request.getFullAddress());
            customerAddress.setIsDefault(0);
            customerAddress.setPhone(request.getCustomerPhone());
            customerAddress.setName(request.getCustomerName());
            customerAddressService.insertCustomerAddress(customerAddress);
        }

        // 订单总价 不能小于0
        BigDecimal orderPrice = request.getPrice().max(BigDecimal.ZERO);
        Integer totalNum = 0;
        for (SysOrderGoodsStandards model : request.getOrderGoodsStandards()) {
            totalNum = totalNum + model.getGoodsStandardNum();
        }
        // 商品单价
        BigDecimal goodsPrice = orderPrice.divide(BigDecimal.valueOf(totalNum), RoundingMode.HALF_UP);

        // 增加商品
        Goods goods = new Goods();
        goods.setDeptId(deptId);
        // 后续改成 三级类目+品牌
        goods.setGoodsName(request.getGoodsBrand());
        goods.setGoodsImgUrl(request.getImageUrl());
        goods.setGoodsVideoUrl(request.getVideoUrl());
        goods.setDeptGoodsCategoryId(request.getCategoryId3());
        goods.setStatus(1);
        goods.setRemark(request.getRemark());
        goodsService.insertGoods(goods);

        List<GoodsStandard> goodsStandards = new ArrayList<>();
        // TODO 生成规格 -- orderGoods完成后未会写规格id
        request.getOrderGoodsStandards().forEach(list -> {
            DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(list.getDeptCategoryId());
            GoodsStandard standard = new GoodsStandard();
            standard.setGoodsStandardName(deptGoodsCategory.getGoodsCategoryName());
            standard.setDeptGoodsCategoryId(deptGoodsCategory.getDeptGoodsCategoryId());
            standard.setGoodsCategoryName(deptGoodsCategory.getGoodsCategoryName());
            standard.setGoodsPrice(goodsPrice);
            standard.setGoodsId(goods.getGoodsId());
            standard.setExtMoney(BigDecimal.ZERO);
            standard.setGoodsNum(list.getGoodsStandardNum());
            standard.setSaleNum(0);
            goodsStandardService.insertGoodsStandard(standard);
            goodsStandards.add(standard);
        });

        // 生成主单
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setDeptId(deptId);
        orderMaster.setCode(orderMasterService.createOrderCode());
        orderMaster.setOrderType(0);
        orderMaster.setOrderStatus(OrderStatus.RECEIVE.code());
        orderMaster.setAddressId(customerAddress.getCustomerAddressId());
        orderMaster.setPayStatus(PayStatus.WAIT_PAY.getCode());
        orderMaster.setCreateTime(new Date());
        orderMaster.setCustomerId(customer.getCustomerId());
        orderMaster.setGoodsId(goods.getGoodsId());
        orderMaster.setPayType(0);
        orderMaster.setOrderMode(request.getOrderMode());
        orderMaster.setPayMode(request.getPayMode());
        // 存在登陆用户的情况下
        if(getSysUser() != null){
            orderMaster.setCreateBy(getSysUser().getUserId().toString());
        }
        // 服务时间
        String[] split = request.getServTime().split("-");
        try {
            orderMaster.setExpectTimeStart(DateUtils.parseDate(request.getServDate() + " " + split[0], "yyyy-MM-dd hh:mm"));
            orderMaster.setExpectTimeEnd(DateUtils.parseDate(request.getServDate() + " " + split[1], "yyyy-MM-dd hh:mm"));
        } catch (Exception e) {

        }
        orderMasterService.insertOrderMaster(orderMaster);
        Assert.notNull(orderMaster.getId(), "OrderMaster.id is null!");

        // 生成财务主单
        FinancialMaster financialMaster = new FinancialMaster(financialMasterService.createCode(), deptId,
                orderMaster.getId(), orderMaster.getCode(), request.getPrice(), BigDecimal.ZERO, request.getPrice());
        financialMaster.setServerMoney(financialMaster.getTotalMoney());
        financialMasterService.insertFinancialMaster(financialMaster);
        Assert.notNull(financialMaster.getId(), "FinancialMaster.id is null!");

        // 生成提成类子财务单
        createDeptFinancialDetail(goodsStandards.get(0), deptId, financialMaster);

        // 生成商品订单
        goodsStandards.forEach(goodsStandard -> {
            OrderGoods orderGoods = new OrderGoods(orderMaster.getId(), goodsStandard.getGoodsStandardId(),
                    goodsStandard.getGoodsStandardName(), goodsStandard.getGoodsNum(), 0);
            orderGoodsService.insertOrderGoods(orderGoods);
        });

        // 推送公众号通知数据。
        try {
            Worker assignWorker = workerService.selectById(orderMaster.getWorkerId());
            // 消息组装。
            Map<String, Object> params = new HashMap<>();
            // 订单编号
            params.put("character_string6", orderMaster.getCode());
            // 名称
            params.put("thing14", customer.getName());
            // 预约时间
            params.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", orderMaster.getExpectTimeStart()));
            // 消息推送
            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getWxOpenId(), WxMsgEnum.TEXT, params);
        } catch (Exception e) {
            // 暂时不做任何操作。
            logger.error(e.getMessage(), e);
        }
        return AjaxResult.success("下单成功");
    }

    /**
     * 平台下单时生成子财务单的方法
     * 只有平台扣点和截流扣点
     *
     * @param goodsStandard   商品 -- 第一个商品的规格来设置扣点
     * @param deptId          企业ID
     * @param financialMaster 主财务单
     */
    private void createDeptFinancialDetail(GoodsStandard goodsStandard, Long deptId, FinancialMaster financialMaster) {
        // 平台
        BigDecimal deptMoney = BigDecimal.ZERO;
        // 截流扣点
        BigDecimal specialMoney = BigDecimal.ZERO;
        // 服务金额
        BigDecimal serverMoney = financialMaster.getServerMoney();
        // 扣点设置类目
        DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(goodsStandard.getDeptGoodsCategoryId());
        // 平台扣点 = 平台金额 + 商品单价 * 扣点比例
        deptMoney = deptMoney.add(deptGoodsCategory.getPcDeptMoney())
                .add(financialMaster.getPayMoney().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getPcDeptRate()))));
        // 截流扣点 = 截流金额 + 商品单价 * 扣点比例
        specialMoney = specialMoney.add(deptGoodsCategory.getPcRetainMoney())
                .add(financialMaster.getPayMoney().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getPcRetainRate()))));
        // 平台扣点记录
        FinancialDetail deptDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), deptMoney, FinancialDetailType.PLATFORM_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(deptDetail);
        // 剩余服务金额
        serverMoney = serverMoney.subtract(deptMoney);

        // 截流扣点记录
        FinancialDetail retainDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), specialMoney, FinancialDetailType.PLATFORM_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(retainDetail);
        // 剩余服务金额
        serverMoney = serverMoney.subtract(specialMoney);

        FinancialMaster fmUpdate = new FinancialMaster();
        fmUpdate.setId(financialMaster.getId());
        fmUpdate.setServerMoney(serverMoney);
        financialMasterService.updateFinancialMaster(fmUpdate);
    }

    /**
     * 分配订单
     */
    @PostMapping("/assign")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult assign(@RequestBody AppOrderAssignRequest request) throws BaseAdaPayException {
        logger.info("分配订单请求参数:{}", request);
        OrderMaster om = orderMasterService.selectById(request.getOrderMasterId());
        Assert.notNull(om, "订单不存在");
        FinancialMaster fm = financialMasterService.selectByOrderMasterId(om.getId());
        Assert.notNull(fm, "找不到对应的订单");
        // 派单师傅
        Worker assignWorker = workerService.selectById(om.getWorkerId());
        Assert.notNull(assignWorker, "找不到师傅信息");
        // 接单师傅
        Worker acceptWorker = workerService.selectById(request.getWorkerId());
        Assert.notNull(acceptWorker, "查询接单师傅信息失败");

        // 校验接单师傅和派单师傅是不是同一个Team
        if (om.getWorkerId().equals(request.getWorkerId())) {
            // 自己接单 子单金额=0
            request.setTotalPay(BigDecimal.ZERO);
        } else {
            if (fm.getServerMoney().compareTo(request.getTotalPay()) < 0) {
                logger.warn("派单金额[{}]大于订单总金额[{}]", request.getTotalPay(), fm.getServerMoney());
                return AjaxResult.error("派单金额不能大于订单总金额");
            }
            boolean checkInTeam = workerService.checkInTeam(assignWorker.getWorkerId(), acceptWorker.getWorkerId());
            Assert.isTrue(checkInTeam, "接单师傅不在本团队");
        }

        // 创建子订单
        OrderDetail od = new OrderDetail();
        od.setDeptId(om.getDeptId());
        od.setCode(orderDetailService.createCode());
        od.setOrderMasterId(om.getId());
        od.setOrderMasterCode(om.getCode());
        od.setCustomerId(om.getCustomerId());
        od.setOrderType(om.getOrderType());
        od.setOrderStatus(request.getWorkerId().equals(om.getWorkerId()) ? OrderStatus.GOING.code() : OrderStatus.RECEIVE.code());
        od.setWorkerId(request.getWorkerId());
        od.setRevTime(om.getRevTime());
        od.setExpectTimeStart(om.getExpectTimeStart());
        od.setExpectTimeEnd(om.getExpectTimeEnd());
        orderDetailService.insertOrderDetail(od);

        // 批量生成订单商品
        request.getGoodsList().forEach(goods -> {
            // 查询规格信息
            GoodsStandard goodsStandard = goodsStandardService.selectById(goods.getGoodsStandardId());
            OrderGoods orderGoods = new OrderGoods();
            orderGoods.setGoodsStandardId(goods.getGoodsStandardId());
            orderGoods.setGoodsNum(goods.getNum());
            orderGoods.setGoodsName(goodsStandard.getGoodsStandardName());
            orderGoods.setOrderDetailId(od.getId());
            orderGoods.setServerGoodsNum(0);
            orderGoodsService.insertOrderGoods(orderGoods);
        });


        //为接单师傅生成子财务单
        FinancialDetail fd = new FinancialDetail(fm.getDeptId(), financialDetailService.createCode(), fm.getId(),
                fm.getCode(), od.getId(), od.getCode(), request.getTotalPay(), FinancialDetailType.ORDER_FEE.getCode(), acceptWorker.getWorkerId(),
                fm.getPayType(), fm.getPayStatus(), fm.getPayTime());
        financialDetailService.insertFinancialDetail(fd);

        if (!BigDecimal.ZERO.equals(request.getTotalPay())) {
            // 更新财务主单服务金额
            FinancialMaster fm2up = new FinancialMaster();
            fm2up.setId(fm.getId());
            fm2up.setServerMoney(fm.getServerMoney().subtract(request.getTotalPay()));
            financialMasterService.updateFinancialMaster(fm2up);
        }

        // 保存订单操作记录
        OrderBehavior orderBehavior = new OrderBehavior().setOrderMasterId(om.getId()).setOrderMasterCode(om.getCode())
                .setWorkerId(assignWorker.getWorkerId()).setWorkerName(assignWorker.getName()).setBehavior(OrderBehaviorEnum.ASSIGN_ORDER);
        orderBehaviorService.insert(orderBehavior);

        // 更新主订单
        OrderMaster om2up = new OrderMaster();
        om2up.setId(om.getId());
        // 判断是否全部派完
        if (orderMasterService.isAllAssign(om.getId())) {
            Set<Long> workIds = orderDetailService.selectByOrderMasterId(om.getId()).stream().map(OrderDetail::getWorkerId).collect(Collectors.toSet());
            if (workIds.size() == 1 && workIds.contains(om.getWorkerId())) {
                // 派完单 并且只有自己接单
                om2up.setHasDispatchedAll(1);
                om2up.setAllSelfAssigned(1);
                om2up.setOrderStatus(OrderStatus.GOING.code());
            } else {
                // 派完单 但是有其他师傅接单
                om2up.setHasDispatchedAll(1);
                om2up.setAllSelfAssigned(0);
                om2up.setOrderStatus(OrderStatus.PLAIN.code());
            }
        } else {
            // 未派完单
            om2up.setAllSelfAssigned(0);
            om2up.setHasDispatchedAll(0);
            om2up.setOrderStatus(OrderStatus.PLAIN.code());
        }
        orderMasterService.updateOrderMaster(om2up);

        // 通知师傅新订单
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("thing1", "新订单通知");
            params.put("thing5", "收到师傅派单,请及时处理!");
            wxMsgService.sendWxMsg(acceptWorker.getOpenId(), WxMsgEnum.ORDER_PLAN, params);

            // 推送公众号通知数据。
            // 消息组装。
            Map<String, Object> paramsNew = new HashMap<>();
            // 订单编号
            paramsNew.put("character_string6", om.getCode());
            // 名称
            paramsNew.put("thing14", acceptWorker.getName());
            // 预约时间
            paramsNew.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", om.getExpectTimeStart()));
            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), acceptWorker.getWxOpenId(), WxMsgEnum.TEXT, paramsNew);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return AjaxResult.success();
    }

    @PostMapping("/server/app")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult appOrder(@RequestBody AppOrderRequest appOrderRequest) throws ParseException {
        // 校验用户信息
        Customer customer = customerService.selectByCustomerId(appOrderRequest.getCustomerId());
        if (customer == null) {
            return AjaxResult.error("用户不存在!");
        }
        List<AppGoodsRequest> appGoodsList = appOrderRequest.getGoodsList();
        // 校验商品信息(库存)
        boolean flag = goodsService.checkStore(appGoodsList);
        if (!flag) {
            return AjaxResult.error("库存不足!");
        }
        // 计算商品费用
        BigDecimal totalPay = goodsService.calculate(appGoodsList);
        // 所有商品的ID
        Set<Long> goodsStandardIds = appGoodsList.stream().map(AppGoodsRequest::getGoodsStandardId).collect(Collectors.toSet());
        // 所有商品
        List<GoodsStandard> goodsList = goodsStandardService.selectByIds(goodsStandardIds);
        // 商品所属师傅
        Goods goods = goodsService.selectById(goodsList.get(0).getGoodsId());
        // 师傅信息
        Worker worker = workerService.selectById(goods.getWorkerId());
        // 商户ID
        Long deptId = appOrderRequest.getDeptId();
        Assert.notNull(deptId, "deptId is null!");

        // 生成主单
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setDeptId(deptId);
        orderMaster.setCode(orderMasterService.createOrderCode());
        orderMaster.setOrderType(0);
        orderMaster.setPayType(appOrderRequest.getPayType());
        orderMaster.setOrderStatus(OrderStatus.RECEIVE.code());
        orderMaster.setCustomerId(appOrderRequest.getCustomerId());
        orderMaster.setAddressId(appOrderRequest.getAddressId());
        orderMaster.setPayStatus(PayStatus.WAIT_PAY.getCode());
        orderMaster.setCreateTime(new Date());
        orderMaster.setWorkerId(goods.getWorkerId());
        orderMaster.setGoodsId(appOrderRequest.getGoodsId());
//        orderMaster.setRevTime(DateUtils.parseDate(appOrderRequest.getServerTime(), com.ghy.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS));
        orderMaster.setRemark(appOrderRequest.getRemark());
        orderMaster.setExpectTimeStart(DateUtils.parseDate(appOrderRequest.getExpectTimeStart(), com.ghy.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS));
        orderMaster.setExpectTimeEnd(DateUtils.parseDate(appOrderRequest.getExpectTimeEnd(), com.ghy.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS));
        orderMasterService.insertOrderMaster(orderMaster);
        Assert.notNull(orderMaster.getId(), "OrderMaster.id is null!");

        // TODO 优惠金额不知道咋算 暂时先给0
        BigDecimal discountMoney = BigDecimal.ZERO;
        BigDecimal payMoney = totalPay.subtract(discountMoney);

        // 当实付金额payType<=0时 使payType=BigDecimal.ZERO
        payMoney = BigDecimal.ZERO.max(payMoney);

        // 生成财务主单
        FinancialMaster financialMaster = new FinancialMaster(financialMasterService.createCode(), deptId,
                orderMaster.getId(), orderMaster.getCode(), totalPay, discountMoney, payMoney);
        financialMaster.setServerMoney(financialMaster.getTotalMoney());
        financialMasterService.insertFinancialMaster(financialMaster);
        Assert.notNull(financialMaster.getId(), "FinancialMaster.id is null!");


//      createFinancialDetail(goodsList.get(0).getDeptGoodsCategoryId(), deptId, customer, payMoney, financialMaster);

        //生成财务子单
        createFinancialDetail(appGoodsList, deptId, customer, payMoney, financialMaster);

        // 生成商品订单
        Map<Long, GoodsStandard> goodsMap = goodsList.stream().filter(Objects::nonNull)
                .collect(Collectors.toMap(GoodsStandard::getGoodsStandardId, x -> x));
        for (AppGoodsRequest appGoods : appGoodsList) {
            GoodsStandard goodsStandard = goodsMap.get(appGoods.getGoodsStandardId());
            OrderGoods orderGoods = new OrderGoods(orderMaster.getId(), goodsStandard.getGoodsStandardId(),
                    goodsStandard.getGoodsStandardName(), appGoods.getNum(), 0);
            orderGoodsService.insertOrderGoods(orderGoods);
        }
        // 通知师傅新订单
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("character_string22", orderMaster.getCode());
            params.put("thing19", customer.getName());
            params.put("time1", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", new Date()));
            params.put("thing5", "有新订单,请及时处理!");
            wxMsgService.sendWxMsg(worker.getOpenId(), WxMsgEnum.NEW_ORDER, params);

            Worker assignWorker = workerService.selectById(orderMaster.getWorkerId());
            // 消息组装。
            Map<String, Object> paramsNew = new HashMap<>();
            // 订单编号
            paramsNew.put("character_string6", orderMaster.getCode());
            // 名称
            paramsNew.put("thing14", customer.getName());
            // 预约时间
            paramsNew.put("time4", com.ghy.common.utils.DateUtils.parseDateToStr("yyyy年MM月dd日 HH:mm", orderMaster.getExpectTimeStart()));
            // 消息推送
            WechatMsgUtils.sendWeChatMsg(WechatMsgUtils.getToken(), assignWorker.getWxOpenId(), WxMsgEnum.TEXT, paramsNew);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success(orderMaster);
    }

    private void createFinancialDetail(List<AppGoodsRequest> appGoodsList, Long deptId, Customer customer, BigDecimal serverMoney, FinancialMaster financialMaster) {
        // 平台
        BigDecimal deptMoney = BigDecimal.ZERO;
        // 一级分销
        BigDecimal oneMoney = BigDecimal.ZERO;
        // 二级分销
        BigDecimal twoMoney = BigDecimal.ZERO;
        // 三级分销
        BigDecimal threeMoney = BigDecimal.ZERO;
        // 截流扣点
        BigDecimal specialMoney = BigDecimal.ZERO;
        for (AppGoodsRequest goodsRequest : appGoodsList) {
            // 商品规格-提成额
            GoodsStandard goodsStandard = goodsStandardService.selectById(goodsRequest.getGoodsStandardId());
            // 扣点设置类目
            DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(goodsStandard.getDeptGoodsCategoryId());
            for (int index = 0; index < goodsRequest.getNum(); index++) {
                // 一级分销 = 商品单价 * 扣点比例
                oneMoney = oneMoney
                        .add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getOneRate()))));
                // 二级分销 = 商品单价 * 扣点比例
                twoMoney = twoMoney.add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getTwoRate()))));
                // 三级分销 = 提成额 + 商品单价 * 扣点比例
                threeMoney = threeMoney.add(goodsStandard.getExtMoney())
                        .add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getThreeRate()))));
                // 平台扣点 = 平台金额 + 商品单价 * 扣点比例
                deptMoney = deptMoney.add(deptGoodsCategory.getDeptMoney())
                        .add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getDeptRate()))));
                // 截流扣点 = 截流金额 + 商品单价 * 扣点比例
                specialMoney = specialMoney.add(deptGoodsCategory.getRetainMoney())
                        .add(goodsStandard.getGoodsPrice().multiply(BigDecimal.valueOf(Double.parseDouble(deptGoodsCategory.getRetainRate()))));
            }
        }
        // 平台扣点记录
        FinancialDetail deptDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), deptMoney, FinancialDetailType.PLATFORM_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(deptDetail);

        // 剩余服务金额
        serverMoney = serverMoney.subtract(deptMoney);

        // 截流扣点记录
        FinancialDetail retainDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), specialMoney, FinancialDetailType.PLATFORM_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(retainDetail);

        // 剩余服务金额
        serverMoney = serverMoney.subtract(specialMoney);

        // 分销扣点记录
        Long customerPlaceId = customer.getCustomerPlace();
        // 一级分销
        if (customerPlaceId != null) {
            FinancialDetail financialDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                    financialMaster.getId(), financialMaster.getCode(), threeMoney, FinancialDetailType.PLACE_FEE.getCode(), customerPlaceId);
            financialDetailService.insertFinancialDetail(financialDetail);

            // 剩余服务金额
            serverMoney = serverMoney.subtract(threeMoney);
        } else {
            oneMoney = oneMoney.add(threeMoney);
        }

        // 二级分销
        Long parentCustomerPlaceId = customer.getParentCustomerPlace();
        if (parentCustomerPlaceId != null) {
            FinancialDetail financialDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                    financialMaster.getId(), financialMaster.getCode(), twoMoney, FinancialDetailType.PLACE_FEE.getCode(), parentCustomerPlaceId);
            financialDetailService.insertFinancialDetail(financialDetail);

            // 剩余服务金额
            serverMoney = serverMoney.subtract(twoMoney);
        } else {
            oneMoney = oneMoney.add(twoMoney);
        }

        // 平台分销
        FinancialDetail financialDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), oneMoney, FinancialDetailType.PLACE_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(financialDetail);

        // 剩余服务金额
        serverMoney = serverMoney.subtract(oneMoney);

        financialMaster.setServerMoney(serverMoney);
        financialMasterService.updateFinancialMaster(financialMaster);
    }

    /**
     * 生成财务子单
     *
     * @param deptGoodsCategoryId 商品类目id
     * @param deptId              商户ID
     * @param customer            消费者
     * @param payMoney            实付金额
     * @param financialMaster     财务主单
     */
    private void createFinancialDetail(Long deptGoodsCategoryId, Long deptId, Customer customer, BigDecimal payMoney, FinancialMaster financialMaster) {
        // 是否为0元购 是的话下面就不用多级分销了
        if (BigDecimal.ZERO.compareTo(payMoney) == 0) {
            return;
        }

        /* 1 平台服务服务费 */
        DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(deptGoodsCategoryId);
        BigDecimal deptRate = new BigDecimal(deptGoodsCategory.getDeptRate());
        BigDecimal deptMoney = deptGoodsCategory.getDeptMoney();

        BigDecimal deptTotal = payMoney.multiply(deptRate).add(deptMoney);

        FinancialDetail deptDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), deptTotal, FinancialDetailType.PLATFORM_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(deptDetail);

        /* 2 截留扣点 */
        BigDecimal retainRate = new BigDecimal(deptGoodsCategory.getRetainRate());
        BigDecimal retainMoney = deptGoodsCategory.getRetainMoney();

        BigDecimal retainTotal = payMoney.multiply(retainRate).add(retainMoney);

        FinancialDetail retainDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), retainTotal, FinancialDetailType.PLATFORM_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(retainDetail);

        /* 3 分销扣点 */
        BigDecimal deptPlaceTotal = BigDecimal.ZERO;

        Long customerPlaceId = customer.getCustomerPlace();
        BigDecimal onePlaceMoney = payMoney.multiply(new BigDecimal(deptGoodsCategory.getOneRate()));
        BigDecimal twoPlaceMoney = payMoney.multiply(new BigDecimal(deptGoodsCategory.getTwoRate()));
        BigDecimal threePlaceMoney = payMoney.multiply(new BigDecimal(deptGoodsCategory.getThreeRate()));

        // 一级分销
        if (customerPlaceId != null) {
            FinancialDetail financialDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                    financialMaster.getId(), financialMaster.getCode(), onePlaceMoney, FinancialDetailType.PLACE_FEE.getCode(), customerPlaceId);
            financialDetailService.insertFinancialDetail(financialDetail);
        } else {
            deptPlaceTotal = deptPlaceTotal.add(onePlaceMoney);
        }

        // 二级分销
        Long parentCustomerPlaceId = customer.getParentCustomerPlace();
        if (parentCustomerPlaceId != null) {
            FinancialDetail financialDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                    financialMaster.getId(), financialMaster.getCode(), twoPlaceMoney, FinancialDetailType.PLACE_FEE.getCode(), parentCustomerPlaceId);
            financialDetailService.insertFinancialDetail(financialDetail);
        } else {
            deptPlaceTotal = deptPlaceTotal.add(twoPlaceMoney);
        }

        // 平台分销
        deptPlaceTotal = deptPlaceTotal.add(threePlaceMoney);
        FinancialDetail financialDetail = new FinancialDetail(deptId, financialDetailService.createCode(),
                financialMaster.getId(), financialMaster.getCode(), deptPlaceTotal, FinancialDetailType.PLACE_FEE.getCode(), null);
        financialDetailService.insertFinancialDetail(financialDetail);

        // 更新订单服务金额
        financialMaster.setServerMoney(financialMaster.getServerMoney().subtract(deptTotal)
                .subtract(retainTotal).subtract(deptPlaceTotal));
        financialMasterService.updateFinancialMaster(financialMaster);
    }

    /**
     * 订单统计
     */
    @PostMapping("/app/statistics")
    @ResponseBody
    public AjaxResult orderStatistics(@RequestBody OrderStatisticsRequest request) {
        OrderStatisticsResponse response = new OrderStatisticsResponse();

        try {
            OrderMaster orderMaster = new OrderMaster();
            orderMaster.setDeptId(request.getDeptId());
            orderMaster.setOrderStatus(OrderStatus.RECEIVE.code());
            orderMaster.setWorkerId(request.getWorkerId());
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setDeptId(request.getDeptId());
            orderDetail.setOrderStatus(OrderStatus.RECEIVE.code());
            orderDetail.setWorkerId(request.getWorkerId());
            orderDetail.setShelveStatus(0);
            Long newMasterOrderCount = orderMasterService.countOrderMasterList(orderMaster);
            Long newDetailOrderCount = orderDetailService.countOrderDetailList(orderDetail);
            // 统计新订单的单量
            response.setNewOrderNum(newMasterOrderCount + newDetailOrderCount);
            // 统计未排的单量
            orderMaster.setOrderStatus(OrderStatus.PLAIN.code());
            // 如果查询的是未约未排状态，只查询未全部派完的主单
            orderMaster.setHasDispatchedAll(0);
            orderDetail.setOrderStatus(OrderStatus.PLAIN.code());
            Long planMasterOrderCount = orderMasterService.countOrderMasterList(orderMaster);
            Long planDetailOrderCount = orderDetailService.countOrderDetailList(orderDetail);
            response.setPlanOrderNum(planMasterOrderCount + planDetailOrderCount);
            orderMaster.setHasDispatchedAll(null);
            // 统计待上门单量
            orderDetail.setOrderStatus(OrderStatus.GOING.code());
            response.setWaitServOrderNum(orderDetailService.countOrderDetailList(orderDetail));
            // 统计服务中单量
            orderDetail.setOrderStatus(OrderStatus.SERVER.code());
            response.setServingOrderNum(orderDetailService.countOrderDetailList(orderDetail));
            // 统计待确认单量
            orderDetail.setOrderStatus(null);
            orderDetail.setOrderStatusList(new ArrayList<Integer>() {{
                add(4);
                add(5);
            }});
            orderDetail.setWorkFinishTimeExisted(true);
            orderDetail.setDrawCashStatusList(new ArrayList<Integer>() {{
                add(0);
                add(1);
            }});
            response.setWaitSureOrderNum(orderDetailService.countOrderDetailList(orderDetail));
            orderDetail.setOrderStatusList(null);
            // 统计已完成子单量
            orderDetail.setOrderStatus(OrderStatus.FINISH.code());
            orderDetail.setDrawCashStatusList(new ArrayList<Integer>() {{
                add(2);
            }});
            response.setFinishedOrderNum(orderDetailService.countOrderDetailList(orderDetail));
            orderDetail.setWorkFinishTimeExisted(false);
            orderDetail.setDrawCashStatusList(null);
            // 统计已完成主单量
            orderMaster.setOrderStatus(OrderStatus.FINISH.code());
            response.setFinishedMasterOrderNum(orderMasterService.countOrderMasterList(orderMaster));
            // 统计监控单量
            orderMaster.setOrderStatus(null);
            orderMaster.setIsMonitoredOrder(true);
            orderMaster.setAllSelfAssigned(0);
            response.setMonitoredOrderNum(orderMasterService.countOrderMasterList(orderMaster));
            orderMaster.setIsMonitoredOrder(false);
            orderMaster.setAllSelfAssigned(null);
            // 统计搁置单量
            OrderDetail orderDetail1 = new OrderDetail();
            orderDetail1.setDeptId(request.getDeptId());
            orderDetail1.setWorkerId(request.getWorkerId());
            orderDetail1.setShelveStatus(1);
            response.setShelvedOrderNum(orderDetailService.countOrderDetailList(orderDetail1));
            // 统计售后中单量
            OrderListRequest orderListRequest = new OrderListRequest();
            orderListRequest.setDeptId(request.getDeptId());
            orderListRequest.setWorkerId(request.getWorkerId());
            orderListRequest.setIsMonitoredOrder(true);
            orderListRequest.setAllSelfAssigned(0);
            orderListRequest.setNeedImgs(false);
            AjaxResult afterListRes = this.appMixAfterList(orderListRequest);
            List<AfterServiceRecord> afterList = (List<AfterServiceRecord>) afterListRes.get("data");
            response.setAfterListNum(afterList.size());
            // 统计超时单量
            orderListRequest = new OrderListRequest();
            orderListRequest.setDeptId(request.getDeptId());
            orderListRequest.setTimeout(1);
            orderListRequest.setWorkerId(request.getWorkerId());
            orderListRequest.setIsMonitoredOrder(true);
            orderListRequest.setNeedImgs(false);
            response.setOverTimeOrderNum(this.appMixOrderCount(orderListRequest));
            // 统计今日单量
            LocalDateTime today = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
            orderDetail.setOrderStatus(OrderStatus.GOING.code());
            orderDetail.setExpectTimeStart(Date.from(today.atZone(ZoneId.systemDefault()).toInstant()));
            today = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
            orderDetail.setExpectTimeEnd(Date.from(today.atZone(ZoneId.systemDefault()).toInstant()));
            response.setTodayOrderNum(orderDetailService.countOrderDetailList(orderDetail));
            // 统计明日单量
            LocalDateTime tomorrow = LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MIN);
            orderDetail.setOrderStatus(OrderStatus.GOING.code());
            orderDetail.setExpectTimeStart(Date.from(tomorrow.atZone(ZoneId.systemDefault()).toInstant()));
            tomorrow = LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.MAX);
            orderDetail.setExpectTimeEnd(Date.from(tomorrow.atZone(ZoneId.systemDefault()).toInstant()));
            response.setTomorrowOrderNum(orderDetailService.countOrderDetailList(orderDetail));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getStackTrace(e));
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(response);
    }

    public long appMixOrderCount(OrderListRequest orderListRequest) {
        long count = 0;

        // 校验参数
        Assert.notNull(orderListRequest.getWorkerId(), "师傅id不能为空");

        // 查询子单
        OrderDetail orderDetailReq = new OrderDetail();
        BeanUtils.copyProperties(orderListRequest, orderDetailReq);
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetailReq);
        count += list.size();
        List<OrderMaster> allList = new ArrayList<>();
        List<Long> masterIdSet = new ArrayList<Long>();
        if (orderListRequest.getTimeout() != null && orderListRequest.getTimeout() == 1) {
            // 增加主单下子单有超时的
            OrderMaster param = new OrderMaster();
            param.setWorkerId(orderListRequest.getWorkerId());
            List<OrderMaster> allOrderMaster = orderMasterService.selectOrderMasterList(param);
            if(CollectionUtils.isNotEmpty(allOrderMaster)){
                OrderDetail countParam = new OrderDetail();
                countParam.setMasterIds(allOrderMaster.stream().map(OrderMaster::getId).collect(Collectors.toList()));
                List<OrderDetail> orderDetails = orderDetailService.selectOrderDetailList(countParam);
                List<OrderDetail> result = orderDetails.stream().filter(s -> s.getTimeout().equals(1)).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(result)) {
                    masterIdSet = result.stream().map(OrderDetail::getOrderMasterId).collect(Collectors.toList());
                }
            }
            if (CollectionUtils.isNotEmpty(masterIdSet)) {
                OrderMaster timeOutMaster = new OrderMaster();
                timeOutMaster.setOrderMasterIdList(masterIdSet);
                allList.addAll(orderMasterService.selectOrderMasterList(timeOutMaster));
            }
        }

        // 查询主单
        OrderMaster orderMasterReq = new OrderMaster();
        BeanUtils.copyProperties(orderListRequest, orderMasterReq);
        orderMasterReq.setExceptOrderMasterIds(masterIdSet);
        List<OrderMaster> masterList = orderMasterService.selectOrderMasterList(orderMasterReq);
        allList.addAll(masterList);
        // 超时查询需要排除同个师傅的主单子单同时出现
        if (orderListRequest.getTimeout() != null && orderListRequest.getTimeout() == 1) {
            // 需要排除同个师傅的主单子单同时出现
            List<OrderMaster> filteredMasterList = new ArrayList<OrderMaster>();
            for (OrderMaster master : allList) {
                boolean isKeep = false;
                // 查询该主单下所有售后中的子单
                OrderDetail detailAfterListOfMasterQry = new OrderDetail();
                detailAfterListOfMasterQry.setOrderMasterCode(master.getCode());
                List<OrderDetail> tmpDetailListOfMasters = orderDetailService.selectOrderDetailList(detailAfterListOfMasterQry);
                List<OrderListResponse> detailListOfMasters = new ArrayList<>();
                tmpDetailListOfMasters.forEach(model -> {
                    OrderListResponse response = new OrderListResponse();
                    BeanUtils.copyProperties(model, response);
                    detailListOfMasters.add(response);
                });
                for (OrderListResponse detailListOfMaster : detailListOfMasters) {
                    if (!detailListOfMaster.getWorkerId().equals(orderListRequest.getWorkerId())) {
                        isKeep = true;
                        break;
                    }
                }
                if (isKeep) {
                    filteredMasterList.add(master);
                }
            }
            count += filteredMasterList.size();
        } else {
            count += masterList.size();
        }

        return count;
    }

    /**
     * 查询主单子单的混合列表
     */
    @PostMapping("/app/mix/order/list")
    @ResponseBody
    public AjaxResult appMixOrderList(@RequestBody OrderListRequest orderListRequest) {
        List<OrderListResponse> orderListResponses = new ArrayList<>();

        // 校验参数
        Assert.notNull(orderListRequest.getWorkerId(), "师傅id不能为空");

        // 查询子单
        OrderDetail orderDetailReq = new OrderDetail();
        BeanUtils.copyProperties(orderListRequest, orderDetailReq);
        List<OrderListResponse> detailList = this.getDetailList(orderDetailReq);
        orderListResponses.addAll(detailList);
        List<OrderListResponse> allList = new ArrayList<>();
        List<Long> masterIdSet = new ArrayList<Long>();
        if (orderListRequest.getTimeout() != null && orderListRequest.getTimeout() == 1) {
            // 增加主单下子单有超时的
            // 增加主单下子单有超时的
            OrderMaster param = new OrderMaster();
            param.setWorkerId(orderListRequest.getWorkerId());
            List<OrderMaster> allOrderMaster = orderMasterService.selectOrderMasterList(param);
            if(CollectionUtils.isNotEmpty(allOrderMaster)){
                OrderDetail countParam = new OrderDetail();
                countParam.setMasterIds(allOrderMaster.stream().map(OrderMaster::getId).collect(Collectors.toList()));
                List<OrderDetail> orderDetails = orderDetailService.selectOrderDetailList(countParam);
                List<OrderDetail> result = orderDetails.stream().filter(s -> s.getTimeout().equals(1)).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(result)) {
                    masterIdSet = result.stream().map(OrderDetail::getOrderMasterId).collect(Collectors.toList());
                }
            }
            if (CollectionUtils.isNotEmpty(masterIdSet)) {
                OrderMaster timeOutMaster = new OrderMaster();
                timeOutMaster.setOrderMasterIdList(masterIdSet);
                allList.addAll(this.getMasterList(timeOutMaster));
            }
        }

        // 查询主单
        OrderMaster orderMasterReq = new OrderMaster();
        BeanUtils.copyProperties(orderListRequest, orderMasterReq);
        orderMasterReq.setExceptOrderMasterIds(masterIdSet);
        orderMasterReq.setGoodsName(null);
        List<OrderListResponse> masterList = this.getMasterList(orderMasterReq);
        allList.addAll(masterList);
        // 超时查询需要排除同个师傅的主单子单同时出现
        if (orderListRequest.getTimeout() != null && orderListRequest.getTimeout() == 1) {
            // 需要排除同个师傅的主单子单同时出现
            List<OrderListResponse> filteredMasterList = new ArrayList<OrderListResponse>();
            for (OrderListResponse master : allList) {
                boolean isKeep = false;
                // 查询该主单下所有售后中的子单
                OrderDetail detailAfterListOfMasterQry = new OrderDetail();
                detailAfterListOfMasterQry.setOrderMasterCode(master.getOrderMasterCode());
                List<OrderListResponse> detailListOfMasters = this.getDetailList(detailAfterListOfMasterQry);
                for (OrderListResponse detailListOfMaster : detailListOfMasters) {
                    if (!detailListOfMaster.getWorkerId().equals(orderListRequest.getWorkerId())) {
                        isKeep = true;
                        break;
                    }
                }
                if (isKeep) {
                    filteredMasterList.add(master);
                }
            }
            orderListResponses.addAll(filteredMasterList);
        } else {
            orderListResponses.addAll(masterList);
        }

        // 查询新订单列表根据创建时间倒叙排列，查询其他根据更新时间倒序排列
        Collections.sort(orderListResponses, new Comparator<OrderListResponse>() {
            @Override
            public int compare(OrderListResponse o1, OrderListResponse o2) {
                if (Objects.equals(orderListRequest.getOrderStatus(), 0) || Objects.equals(orderListRequest.getTimeout(), 1)) {
                    return o2.getCreateTime().compareTo(o1.getCreateTime());
                } else {
                    return o2.getUpdateTime().compareTo(o1.getUpdateTime());
                }
            }
        });

        return AjaxResult.success(orderListResponses);
    }

    @PostMapping("/app/mix/after/list")
    @ResponseBody
    public AjaxResult appMixAfterList(@RequestBody OrderListRequest orderListRequest) {
        List<OrderListResponse> orderListResponses = new ArrayList<>();

        // 校验参数
        Assert.notNull(orderListRequest.getWorkerId(), "师傅id不能为空");

        // 查询子单
        OrderDetail orderDetailReq = new OrderDetail();
        BeanUtils.copyProperties(orderListRequest, orderDetailReq);
        orderDetailReq.setNeedImgs(orderListRequest.getNeedImgs());
        List<OrderListResponse> detailList = this.getDetailAfterList(orderDetailReq);
        orderListResponses.addAll(detailList);

        // 查询主单
        OrderMaster orderMasterReq = new OrderMaster();
        BeanUtils.copyProperties(orderListRequest, orderMasterReq);
        List<OrderListResponse> masterList = this.getMasterAfterList(orderMasterReq);
        // 需要排除同个师傅的主单子单同时出现
        List<OrderListResponse> filteredMasterList = new ArrayList<OrderListResponse>();
        for (OrderListResponse master : masterList) {
            boolean isKeep = false;
            // 查询该主单下所有售后中的子单
            OrderDetail detailAfterListOfMasterQry = new OrderDetail();
            detailAfterListOfMasterQry.setOrderMasterCode(master.getOrderMasterCode());
            List<OrderListResponse> detailAfterListOfMasters = this.getDetailAfterList(detailAfterListOfMasterQry);
            for (OrderListResponse detailAfterListOfMaster : detailAfterListOfMasters) {
                if (!detailAfterListOfMaster.getWorkerId().equals(orderListRequest.getWorkerId())) {
                    isKeep = true;
                    break;
                }
            }
            if (isKeep) {
                filteredMasterList.add(master);
            }
        }
        orderListResponses.addAll(filteredMasterList);

        // 根据更新时间倒序排列
        Collections.sort(orderListResponses, new Comparator<OrderListResponse>() {
            @Override
            public int compare(OrderListResponse o1, OrderListResponse o2) {
                return o2.getUpdateTime().compareTo(o1.getUpdateTime());
            }
        });

        return AjaxResult.success(orderListResponses);
    }

    public List<OrderListResponse> getMasterAfterList(OrderMaster orderMaster) {

        // 查所有售后的单
        AfterServiceRecord afterServiceRecord = new AfterServiceRecord();
        afterServiceRecord.setExcludeAfterServiceFinished(Boolean.TRUE);
        List<AfterServiceRecord> afterServiceRecordList = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);
        // 踢重后的子单ids
        List<Long> detailIds = afterServiceRecordList.stream().map(AfterServiceRecord::getOrderDetailId).distinct().collect(Collectors.toList());
        StringBuilder orderDetailIds = new StringBuilder();
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCustomerId(orderMaster.getCustomerId());
        orderDetail.setDeptId(orderMaster.getDeptId());
        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");
        }
        // 所有售后的子单
        List<OrderDetail> orderDetailList = orderDetailService.selectOrderDetailList(orderDetail);
        StringBuilder orderMasterIds = new StringBuilder();
        orderDetailList.stream().map(OrderDetail::getOrderMasterId).distinct()
                .collect(Collectors.toList()).forEach(id -> {
                    orderMasterIds.append(id).append(",");
                });
        String orderIds = orderMasterIds.toString();
        if (StringUtils.isNotEmpty(orderIds)) {
            orderMaster.setOrderMasterIds(orderIds.substring(0, orderIds.length() - 1));
        } else {
            orderMaster.setOrderMasterIds("0");
        }

        List<OrderListResponse> orderListResponses = new ArrayList<>();
        List<OrderMaster> list = orderMasterService.selectOrderMasterList(orderMaster);
        list.forEach(master -> {
            //子单
            List<OrderDetail> detailList = orderDetailService.selectByOrderMasterId(master.getId());

            // 初始化属性
            OrderListResponse orderListResponse = new OrderListResponse();
            List<OrderStandard> standardList = new ArrayList<>();

            // 师傅信息
            Worker worker = master.getWorkerId() != null ? workerService.selectById(master.getWorkerId()) : null;

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

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

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

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

            // 财务信息
            FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(master.getId());
            BigDecimal totalPayMoney = financialMaster.getPayMoney();
//            BigDecimal totalChangeMoney = new BigDecimal(0);
//            for (OrderDetail detail: detailList) {
//                // 查询子单加价记录
//                FinancialChangeRecord changeRecordQry = new FinancialChangeRecord();
//                changeRecordQry.setOrderDetailId(detail.getId());
//                List<FinancialChangeRecord> financialChangeRecords = financialChangeRecordService.selectFinancialChangeRecordList(changeRecordQry);
//                if (CollectionUtils.isNotEmpty(financialChangeRecords)) {
//                    totalChangeMoney = totalChangeMoney.add(financialChangeRecords.get(0).getChangeMoney());
//                }
//            }
//            totalPayMoney = totalPayMoney.add(totalChangeMoney);

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(master.getAddressId());
            SysArea sysArea = sysAreaService.selectById(customerAddress.getCountryId());
            String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress();

            // 查询售后记录
            List<AfterServiceRecord> afterServiceRecords = new ArrayList<>();
            List<OrderDetail> detailOrderList = orderDetailService.selectByOrderMasterId(master.getId());
            detailOrderList.stream().forEach(detail -> {
                AfterServiceRecord afterServiceRecordQry = new AfterServiceRecord();
                afterServiceRecordQry.setOrderDetailId(detail.getId());
                List<AfterServiceRecord> records = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecordQry);
                afterServiceRecords.addAll(records);
            });

            for (OrderGoods orderGoods : orderStandardList) {
                OrderStandard orderStandard = new OrderStandard();
                orderStandard.setStandardName(orderGoods.getGoodsName());
                orderStandard.setStandardNum(orderGoods.getGoodsNum());
                standardList.add(orderStandard);
            }

            // 编辑返回属性
            orderListResponse.setOrderMasterId(master.getId());
            orderListResponse.setGoodsName(goods.getGoodsName());
            orderListResponse.setGoodsLogoUrl(goods.getGoodsImgUrl());
            orderListResponse.setDiscountMoney(financialMaster.getDiscountMoney());
            orderListResponse.setTotalMoney(financialMaster.getTotalMoney());
            orderListResponse.setPayMoney(totalPayMoney);
//            orderListResponse.setChangeMoney(totalChangeMoney);
            orderListResponse.setWorkerName(worker == null ? "" : worker.getName());
            orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
            orderListResponse.setCustomerName(customerAddress.getName());
            orderListResponse.setCustomerPhone(customerAddress.getPhone());
            orderListResponse.setServerTime(master.getRevTime());
            orderListResponse.setExpectTimeStart(master.getExpectTimeStart());
            orderListResponse.setExpectTimeEnd(master.getExpectTimeEnd());
            orderListResponse.setExpectTimeStartFront(master.getExpectTimeStart());
            orderListResponse.setExpectTimeEndFront(master.getExpectTimeEnd());
            orderListResponse.setOrderStatus(master.getOrderStatus());
            orderListResponse.setPayStatus(master.getPayStatus());
            orderListResponse.setPayType(master.getPayType());
            orderListResponse.setOrderMasterCode(master.getCode());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(completeAddress);
            orderListResponse.setRemark(goods.getRemark());
            orderListResponse.setCustomerRemark(master.getRemark());
            orderListResponse.setTimeout(master.getTimeout());
            orderListResponse.setTimeoutFineTimes(master.getTimeoutFineTimes());
            orderListResponse.setAfterServiceRecordList(afterServiceRecords);
            orderListResponse.setCreateTime(master.getCreateTime());
            orderListResponse.setUpdateTime(master.getUpdateTime());
            orderListResponses.add(orderListResponse);

        });
        return orderListResponses;
    }

    public List<OrderListResponse> getDetailAfterList(OrderDetail orderDetail) {
        AfterServiceRecord afterServiceRecord = new AfterServiceRecord();
        afterServiceRecord.setExcludeAfterServiceFinished(Boolean.TRUE);
        afterServiceRecord.setNeedImgs(orderDetail.getNeedImgs());
        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");
        }
        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.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();

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
            SysArea 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.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.setWorkerId(detail.getWorkerId());
            orderListResponse.setWorkerName(workerName);
            orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
            orderListResponse.setCustomerName(customerAddress.getName());
            orderListResponse.setCustomerPhone(customerAddress.getPhone());
            orderListResponse.setOrderStatus(detail.getOrderStatus());
            orderListResponse.setPayStatus(orderMaster.getPayStatus());
            orderListResponse.setPayType(orderMaster.getPayType());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(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.setDrawCashStatus(detail.getDrawCashStatus());
            orderListResponse.setDrawCashTime(detail.getDrawCashTime());
            orderListResponse.setCreateTime(detail.getCreateTime());
            orderListResponse.setUpdateTime(detail.getUpdateTime());
            orderListResponses.add(orderListResponse);
        });
        return orderListResponses;
    }

    public List<OrderListResponse> getMasterList(OrderMaster orderMaster) {
        List<OrderListResponse> orderListResponses = new ArrayList<>();
        // 如果查询的是未约未排状态，只查询未全部派完的主单
        if (orderMaster.getOrderStatus() != null && OrderStatus.PLAIN.code() == orderMaster.getOrderStatus()) {
            orderMaster.setHasDispatchedAll(0);
        }
        List<OrderMaster> list = orderMasterService.selectOrderMasterList(orderMaster);
        List<Long> orderMasterIds = list.stream().map(OrderMaster::getId).collect(Collectors.toList());
        // 缓存 orderDetailMap
        OrderDetail masterIdsParam = new OrderDetail();
        masterIdsParam.setMasterIds(orderMasterIds);
        List<OrderDetail> orderDetailsList = orderDetailService.selectOrderDetailList(masterIdsParam);
        Map<Long,List<OrderDetail>> orderMasterIdsMap =  orderDetailsList.stream().collect(Collectors.groupingBy(OrderDetail::getOrderMasterId));

        // -- 都是同一个师傅的,师傅信息
        Worker worker = orderMaster.getWorkerId() != null ? workerService.selectById(orderMaster.getWorkerId()) : null;

        for (OrderMaster master : list) {// 初始化属性
            OrderListResponse orderListResponse = new OrderListResponse();
            List<OrderStandard> standardList = new ArrayList<>();
            // 查询所有子单
            List<OrderDetail> orderDetails = orderMasterIdsMap.get(master.getId());
            if(CollectionUtils.isEmpty(orderDetails)){
                orderDetails = new ArrayList<>();
            }
            List<Long> orderDetailIds = orderDetails.stream().map(OrderDetail::getId).collect(Collectors.toList());

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

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

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

            // 财务信息
            FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(master.getId());

            // 查询加价价格
            BigDecimal changeMoney = BigDecimal.ZERO;
            List<FinancialChangeRecord> changeRecords = financialChangeRecordService.selectByDetailIds(StringUtils.join(orderDetailIds, ","));
            for (FinancialChangeRecord changeRecord : changeRecords) {
                if (PayStatus.WAIT_PAY.getCode().equals(changeRecord.getPayStatus())) {
                    changeMoney = changeMoney.add(changeRecord.getChangeMoney());
                }
            }

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(master.getAddressId());
            SysArea sysArea = sysAreaService.selectById(customerAddress.getCountryId());
            String completeAddress = sysArea.getMergerName().replaceAll(",", "") + customerAddress.getAddress();

            // 查询售后记录
            List<AfterServiceRecord> afterServiceRecords = new ArrayList<>();
            List<OrderDetail> detailOrderList = orderDetailService.selectByOrderMasterId(master.getId());
            detailOrderList.stream().forEach(detail -> {
                AfterServiceRecord afterServiceRecordQry = new AfterServiceRecord();
                afterServiceRecordQry.setOrderDetailId(detail.getId());
                List<AfterServiceRecord> records = afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecordQry);
                afterServiceRecords.addAll(records);
            });

            for (OrderGoods orderGoods : orderStandardList) {
                OrderStandard orderStandard = new OrderStandard();
                orderStandard.setStandardName(orderGoods.getGoodsName());
                orderStandard.setStandardNum(orderGoods.getGoodsNum());
                standardList.add(orderStandard);
            }

            // 编辑返回属性
            orderListResponse.setOrderMasterId(master.getId());
            orderListResponse.setGoodsName(goods.getGoodsName());
            orderListResponse.setGoodsLogoUrl(goods.getGoodsImgUrl());
            orderListResponse.setDiscountMoney(financialMaster.getDiscountMoney());
            orderListResponse.setTotalMoney(financialMaster.getTotalMoney());
            orderListResponse.setPayMoney(financialMaster.getPayMoney());
            orderListResponse.setWorkerName(worker == null ? "" : worker.getName());
            orderListResponse.setWorkerPhone(worker == null ? "" : worker.getPhone());
            orderListResponse.setCustomerName(customerAddress.getName());
            orderListResponse.setCustomerPhone(customerAddress.getPhone());
            orderListResponse.setServerTime(master.getRevTime());
            orderListResponse.setExpectTimeStart(master.getExpectTimeStart());
            orderListResponse.setExpectTimeEnd(master.getExpectTimeEnd());
            orderListResponse.setExpectTimeStartFront(master.getExpectTimeStart());
            orderListResponse.setExpectTimeEndFront(master.getExpectTimeEnd());
            orderListResponse.setOrderStatus(master.getOrderStatus());
            orderListResponse.setPayStatus(master.getPayStatus());
            orderListResponse.setPayType(master.getPayType());
            orderListResponse.setOrderMasterCode(master.getCode());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(completeAddress);
            orderListResponse.setRemark(goods.getRemark());
            orderListResponse.setCustomerRemark(master.getRemark());
            orderListResponse.setTimeout(master.getTimeout());
            orderListResponse.setTimeoutFineTimes(master.getTimeoutFineTimes());
            orderListResponse.setChangeMoney(changeMoney);
            orderListResponse.setServerMoney(financialMaster.getServerMoney());
            orderListResponse.setAfterServiceRecordList(afterServiceRecords);
            orderListResponse.setCreateTime(master.getCreateTime());
            orderListResponse.setUpdateTime(master.getUpdateTime());
            orderListResponse.setIsCall(master.getIsCall());
            orderListResponses.add(orderListResponse);

        }
        return orderListResponses;
    }

    public List<OrderListResponse> getDetailList(OrderDetail orderDetail) {
        List<OrderListResponse> orderListResponses = new ArrayList<>();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        for (OrderDetail detail : list) {// 主单信息
            OrderMaster orderMaster = orderMasterService.selectById(detail.getOrderMasterId());
            // 初始化属性
            OrderListResponse orderListResponse = new OrderListResponse();
            List<OrderStandard> standardList = new ArrayList<>();

            // 师傅信息
            Worker worker = workerService.selectById(detail.getWorkerId());

            // 消费者信息
//            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());

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

            // 地址信息
            CustomerAddress customerAddress = addressService.selectByCustomerAddressId(orderMaster.getAddressId());
            SysArea 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);
            }

            // 查询是否有拨号记录
            OrderCallRecord param = new OrderCallRecord();
            param.setOrderId(detail.getId());
            List<OrderCallRecord> callList = orderCallRecordService.selectOrderCallRecordList(param);
            if (CollectionUtils.isNotEmpty(callList)) {
                orderListResponse.setIsCall("02");
            } else {
                orderListResponse.setIsCall("1");
            }

            // 售后记录
//            AfterServiceRecord afterServiceRecord = new AfterServiceRecord();
//            afterServiceRecord.setOrderDetailId(detail.getId());
//            List<AfterServiceRecord> afterServiceRecordList  =afterServiceRecordService.selectAfterServiceRecordList(afterServiceRecord);

            // 编辑返回属性
            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.setPayMoney(financialDetail.getPayMoney());
            orderListResponse.setWorkerName(worker.getName());
            orderListResponse.setWorkerPhone(worker.getPhone());
            orderListResponse.setWorkerId(detail.getWorkerId());
            orderListResponse.setCustomerName(customerAddress.getName());
            orderListResponse.setCustomerPhone(customerAddress.getPhone());
            orderListResponse.setOrderStatus(detail.getOrderStatus());
            orderListResponse.setPayStatus(orderMaster.getPayStatus());
            orderListResponse.setPayType(orderMaster.getPayType());
            orderListResponse.setStandardList(standardList);
            orderListResponse.setAddress(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.setTimeoutFineTimes(detail.getTimeoutFineTimes());
            orderListResponse.setChangeMoney(changeMoney);
            orderListResponse.setCreateTime(detail.getCreateTime());
            orderListResponse.setUpdateTime(detail.getUpdateTime());
//            orderListResponse.setAfterServiceRecordList(afterServiceRecordList);
            orderListResponses.add(orderListResponse);
        }
        return orderListResponses;
    }

    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;
    }
}
