package com.sckw.payment.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.OrderGenerateUtils;
import com.sckw.core.utils.StringTimeUtil;
import com.sckw.payment.dao.KwpLedgerLogisticsMapper;
import com.sckw.payment.model.KwpLedgerLogistics;
import com.sckw.payment.model.KwpLedgerLogisticsTrack;
import com.sckw.payment.model.KwpLedgerLogisticsUnit;
import com.sckw.payment.model.KwpSettlementLogistics;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.payment.model.*;
import com.sckw.payment.model.constant.LedgerEnum;
import com.sckw.payment.model.constant.LedgerTrackEnum;
import com.sckw.payment.model.constant.LogisticsUnitType;
import com.sckw.payment.model.dto.*;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.model.vo.res.LedgerLogisticsDetailVo;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.EntCacheResDto;
import com.sckw.system.api.model.dto.res.SysDictResDto;
import com.sckw.transport.api.dubbo.TransportDubboService;
import com.sckw.transport.api.model.dto.vo.KwtLogisticsOrderVO;
import com.sckw.transport.api.model.dto.vo.LogisticsOrderVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */
@Service
@Slf4j
public class KwpLedgerLogisticsService extends AbsLedger {
    @Resource
    private KwpLedgerLogisticsTrackService logisticsTrackService;
    @Resource
    private KwpSettlementLogisticsService settlementLogisticsService;
    @Resource
    private KwpLedgerLogisticsUnitService logisticsUnitService;
    @Resource
    private KwpLedgerLogisticsOrderService logisticsOrderService;
    @Resource
    private KwpLedgerLogisticsMapper logisticsMapper;
    @DubboReference(version = "2.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;
    @DubboReference(version = "2.0.0", group = "design", check = false)
    private TransportDubboService transportDubboService;

    private void changeDict(List<ILedger> list) {
        List<SysDictResDto> taxRateDict = remoteSystemService.queryDictByType(DictTypeEnum.TAX_RATE.getType());
        Map<String, String> taxRateMap = new HashMap<>();
        Map<String, String> tradingMap = new HashMap<>();

        if (!CollectionUtils.isEmpty(taxRateDict)) {
            taxRateMap = taxRateDict.stream().collect(Collectors.toMap(SysDictResDto::getValue, SysDictResDto::getLabel, (a, b) -> a));
        }
        List<SysDictResDto> tradingDict = remoteSystemService.queryDictByType(DictTypeEnum.TRADE_TYPE.getType());
        if (!CollectionUtils.isEmpty(tradingDict)) {
            tradingMap = tradingDict.stream().collect(Collectors.toMap(SysDictResDto::getValue, SysDictResDto::getLabel, (a, b) -> a));
        }
        for (ILedger logisticsDto : list) {
            Integer trading = logisticsDto.getTrading();
            logisticsDto.setTradingLabel(tradingMap.get(String.valueOf(trading)));
            Integer taxRate = logisticsDto.getTaxRate();
            logisticsDto.setTaxRateLabel(taxRateMap.get(String.valueOf(taxRate)));

            logisticsDto.setStatusLabel(LedgerEnum.getDesc(logisticsDto.getStatus()));
        }
    }

    private void fillPara(LogisticsReq logisticsReq) {
        logisticsReq.setEntId(LoginUserHolder.getEntId());
        if (StringUtils.isNotBlank(logisticsReq.getStartCreateTime())) {
            logisticsReq.setStartCreateTime(StringTimeUtil.fillStart(logisticsReq.getStartCreateTime()));
        }
        if (StringUtils.isNotBlank(logisticsReq.getEndCreateTime())) {
            logisticsReq.setEndCreateTime(StringTimeUtil.fillEnd(logisticsReq.getEndCreateTime()));
        }
    }

    public PageRes<ILedger> shipperList(LogisticsReq logisticsReq) {
        fillPara(logisticsReq);
        PageHelper.startPage(logisticsReq.getPage(), logisticsReq.getPageSize());
        List<LedgerShipperDto> kwpLedgerLogisticsList = logisticsMapper.shipperSelect(logisticsReq);
        //字典转换
        if (!CollectionUtils.isEmpty(kwpLedgerLogisticsList)) {
            List<ILedger> ledgerShipperDto = new ArrayList<>(kwpLedgerLogisticsList);
            changeDict(ledgerShipperDto);
            return new PageRes<>(new PageInfo<>(ledgerShipperDto));
        }
        return new PageRes<>(new PageInfo<>());
    }

    public PageRes<ILedger> carrierList(LogisticsReq logisticsReq) {
        fillPara(logisticsReq);
        PageHelper.startPage(logisticsReq.getPage(), logisticsReq.getPageSize());
        List<LedgerCarrierDto> kwpLedgerLogisticsList = logisticsMapper.carrierSelect(logisticsReq);
        //字典转换
        if (!CollectionUtils.isEmpty(kwpLedgerLogisticsList)) {
            List<ILedger> ledgerShipperDto = new ArrayList<>(kwpLedgerLogisticsList);
            changeDict(ledgerShipperDto);
            return new PageRes<>(new PageInfo<>(ledgerShipperDto));
        }
        return new PageRes<>(new PageInfo<>(kwpLedgerLogisticsList));
    }

    /**
     * 保存物流对账单
     *
     * @param logisticsReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String sendLedger(LogisticsSendReq logisticsReq) {
        String id = logisticsReq.getId();
        if (StringUtils.isBlank(id)) {
            //新增
            logisticsReq.setGenerateTime(LocalDateTime.now());
            logisticsReq.setStatus(LedgerEnum.TO_LEDGER.getStatus());
            Long aLong = saveDraft(logisticsReq);
            logisticsTrackService.saveTrack(aLong, "", LedgerTrackEnum.TO_LEDGER);
        } else {
            removeDraft(Long.parseLong(id));
            logisticsReq.setGenerateTime(LocalDateTime.now());
            logisticsReq.setStatus(LedgerEnum.TO_LEDGER.getStatus());
            Long aLong = saveDraft(logisticsReq);
            logisticsTrackService.saveTrack(aLong, "", LedgerTrackEnum.TO_LEDGER);

        }
        return "保存对账单成功";
    }

    private void removeDraft(Long id) {
        //删除 kwp_ledger_logistics_order
        logisticsOrderService.remove(id);
        //删除 kwp_ledger_logistics_track
        logisticsTrackService.remove(id);
        //删除 kwp_ledger_logistics_unit
        logisticsUnitService.remove(id);
    }

    private Long saveDraft(LogisticsSendReq logisticsSendReq) {
        KwpLedgerLogistics kwpLedgerLogistics = new KwpLedgerLogistics();
        kwpLedgerLogistics.setId(StringUtils.isBlank(logisticsSendReq.getId()) ? new IdWorker(1).nextId() : Long.parseLong(logisticsSendReq.getId()));
        kwpLedgerLogistics.setEntId(LoginUserHolder.getEntId());
        kwpLedgerLogistics.setName(logisticsSendReq.getName());
        if (StringUtils.isNotBlank(logisticsSendReq.getStartTime())) {
            kwpLedgerLogistics.setStartTime(StringTimeUtil.startDateTime(logisticsSendReq.getStartTime()));
        }
        if (StringUtils.isNotBlank(logisticsSendReq.getEndTime())) {
            kwpLedgerLogistics.setEndTime(StringTimeUtil.endDateTime(logisticsSendReq.getEndTime()));
        }
        kwpLedgerLogistics.setTaxRate(logisticsSendReq.getTaxRate());
        kwpLedgerLogistics.setTrading(logisticsSendReq.getTrading());
        kwpLedgerLogistics.setTotalPrice(logisticsSendReq.getTotalPrice());
        kwpLedgerLogistics.setExTaxPrice(logisticsSendReq.getTotalPrice());//todo-xcq 不含税金额计算方式：订单含税金额总和/1.13(税率)
        kwpLedgerLogistics.setSettlePrice(logisticsSendReq.getSettlePrice());
        kwpLedgerLogistics.setActualPrice(new BigDecimal("0.0"));
        kwpLedgerLogistics.setAuditUser("");
        kwpLedgerLogistics.setAuditPhone("");
        kwpLedgerLogistics.setReceiptTime(null);
        kwpLedgerLogistics.setOrderCount(logisticsSendReq.getOrders().size());
        kwpLedgerLogistics.setUrl("");
        kwpLedgerLogistics.setGenerateTime(logisticsSendReq.getGenerateTime());
        kwpLedgerLogistics.setRemark("");
        kwpLedgerLogistics.setStatus(logisticsSendReq.getStatus());
        kwpLedgerLogistics.setDelFlag(0);
        if (Objects.isNull(logisticsSendReq.getId())) {
            kwpLedgerLogistics.setCreateBy(LoginUserHolder.getUserId());
            kwpLedgerLogistics.setCreateTime(LocalDateTime.now());
            kwpLedgerLogistics.setUpdateBy(LoginUserHolder.getUserId());
            kwpLedgerLogistics.setUpdateTime(LocalDateTime.now());
            kwpLedgerLogistics.setLLedgerNo(OrderGenerateUtils.generateOrderNo("LL"));
            logisticsMapper.insert(kwpLedgerLogistics);
        } else {
            KwpLedgerLogistics ledgerLogistics = logisticsMapper.selectById(logisticsSendReq.getId());
            orderCheck(ledgerLogistics);
            updateCheck(ledgerLogistics.getStatus());
            kwpLedgerLogistics.setUpdateBy(LoginUserHolder.getUserId());
            kwpLedgerLogistics.setUpdateTime(LocalDateTime.now());
            logisticsMapper.updateById(kwpLedgerLogistics);
        }
        /*保存物流订单关联数据*/
        List<Long> ids = logisticsSendReq.getOrders();
        //todo-xcq 校验当前订单是否已存在绑定的对账单
        logisticsOrderService.check(ids);
        List<KwtLogisticsOrderVO> acceptCarriageOrderDetail = transportDubboService.getAcceptCarriageOrderDetail(ids.stream().map(a -> Long.toString(a)).toList());
        if (CollectionUtils.isEmpty(acceptCarriageOrderDetail) || acceptCarriageOrderDetail.size() < ids.size()) {
            throw new BusinessException("未找到物流订单!");
        }
        List<KwpLedgerLogisticsOrder> collect = acceptCarriageOrderDetail.stream().map(a -> {
            KwpLedgerLogisticsOrder logisticsOrder = new KwpLedgerLogisticsOrder();
            logisticsOrder.setId(new IdWorker(1).nextId());
            logisticsOrder.setLLedgerId(kwpLedgerLogistics.getId());
            logisticsOrder.setLOrderId(a.getId());
            logisticsOrder.setLOrderNo(a.getLOrderNo());
            logisticsOrder.setRemark("");
            logisticsOrder.setStatus(0);
            logisticsOrder.setCreateBy(LoginUserHolder.getUserId());
            logisticsOrder.setCreateTime(LocalDateTime.now());
            logisticsOrder.setUpdateBy(LoginUserHolder.getUserId());
            logisticsOrder.setUpdateTime(LocalDateTime.now());
            logisticsOrder.setDelFlag(0);
            return logisticsOrder;
        }).collect(Collectors.toList());

        logisticsOrderService.saveList(collect);
        /*保存企业相关信息*/
        Long checkEntId = Long.parseLong(logisticsSendReq.getCheckEntId());

        Map<Long, EntCacheResDto> entMap = remoteSystemService.queryEntTreeByIds(Collections.singletonList(checkEntId));
        EntCacheResDto entCacheResDto = entMap.get(checkEntId);
        if (Objects.isNull(entCacheResDto)) {
            entCacheResDto = new EntCacheResDto();
        }

        List<KwpLedgerLogisticsUnit> logisticsUnits = new ArrayList<>();
        KwpLedgerLogisticsUnit kwpLedgerLogisticsUnit = new KwpLedgerLogisticsUnit();
        kwpLedgerLogisticsUnit.setId(new IdWorker(1).nextId());
        kwpLedgerLogisticsUnit.setLLedgerId(kwpLedgerLogistics.getId());
        kwpLedgerLogisticsUnit.setLLedgerNo(kwpLedgerLogistics.getLLedgerNo());
        kwpLedgerLogisticsUnit.setUnitType(LogisticsUnitType.SHIPPER);
        kwpLedgerLogisticsUnit.setEntId(checkEntId);
        kwpLedgerLogisticsUnit.setTopEntId(entCacheResDto.getId());
        kwpLedgerLogisticsUnit.setFirmName(entCacheResDto.getFirmName());
        kwpLedgerLogisticsUnit.setContacts(entCacheResDto.getContacts());
        kwpLedgerLogisticsUnit.setPhone(entCacheResDto.getPhone());
        kwpLedgerLogisticsUnit.setRemark("");
        kwpLedgerLogisticsUnit.setStatus(0);
        kwpLedgerLogisticsUnit.setCreateBy(LoginUserHolder.getUserId());
        kwpLedgerLogisticsUnit.setCreateTime(LocalDateTime.now());
        kwpLedgerLogisticsUnit.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerLogisticsUnit.setUpdateTime(LocalDateTime.now());
        kwpLedgerLogisticsUnit.setDelFlag(0);

        logisticsUnits.add(kwpLedgerLogisticsUnit);
        Long entId = LoginUserHolder.getEntId();
        Map<Long, EntCacheResDto> entCarrierMap = remoteSystemService.queryEntTreeByIds(Collections.singletonList(entId));
        EntCacheResDto entCacheResDto1 = entCarrierMap.get(entId);
        if (Objects.isNull(entCacheResDto1)) {
            entCacheResDto1 = new EntCacheResDto();
        }
        KwpLedgerLogisticsUnit carrierUnit = new KwpLedgerLogisticsUnit();
        carrierUnit.setId(new IdWorker(1).nextId());
        carrierUnit.setLLedgerId(kwpLedgerLogistics.getId());
        carrierUnit.setLLedgerNo(kwpLedgerLogistics.getLLedgerNo());
        carrierUnit.setUnitType(LogisticsUnitType.CARRIER);
        carrierUnit.setEntId(entId);//顶级企业id
        carrierUnit.setTopEntId(entCacheResDto1.getId());//顶级企业id
        carrierUnit.setFirmName(entCacheResDto1.getFirmName());
        carrierUnit.setContacts(entCacheResDto1.getContacts());
        carrierUnit.setPhone(entCacheResDto1.getPhone());
        carrierUnit.setRemark("");
        carrierUnit.setStatus(0);
        carrierUnit.setCreateBy(LoginUserHolder.getUserId());
        carrierUnit.setCreateTime(LocalDateTime.now());
        carrierUnit.setUpdateBy(LoginUserHolder.getUserId());
        carrierUnit.setUpdateTime(LocalDateTime.now());
        carrierUnit.setDelFlag(0);

        logisticsUnits.add(carrierUnit);
        logisticsUnitService.saveList(logisticsUnits);
        return kwpLedgerLogistics.getId();
    }

    /**
     * 保存物流对账单草稿
     *
     * @param logisticsReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String sendLedgerDraft(LogisticsSendReq logisticsReq) {
        logisticsReq.setGenerateTime(null);
        logisticsReq.setStatus(LedgerEnum.SAVE.getStatus());
        Long aLong = saveDraft(logisticsReq);
        logisticsTrackService.saveTrack(aLong, "", LedgerTrackEnum.SAVE);
        return "草稿保存成功";
    }

    /**
     * 删除物流对账单
     *
     * @param id 物流对账单id
     * @return
     */
    public Integer remove(Long id) {
        KwpLedgerLogistics kwpLedgerLogistics = logisticsMapper.selectById(id);
        if (Objects.isNull(kwpLedgerLogistics)) {
            throw new BusinessException("对账单不存在!");
        }
        deleteCheck(kwpLedgerLogistics.getStatus());
        kwpLedgerLogistics.setDelFlag(1);
        logisticsMapper.updateById(kwpLedgerLogistics);
        return logisticsTrackService.saveTrack(id, "", LedgerTrackEnum.DELETE);
    }

    /**
     * 撤回物流对账单
     *
     * @param id 物流对账单id
     * @return
     */
    public Integer backOrder(Long id) {
        KwpLedgerLogistics kwpLedgerLogistics = logisticsMapper.selectById(id);
        if (Objects.isNull(kwpLedgerLogistics)) {
            throw new BusinessException("对账单不存在!");
        }
        revokeCheck(kwpLedgerLogistics.getStatus());

        kwpLedgerLogistics.setStatus(LedgerEnum.REVOCATION.getStatus());
        kwpLedgerLogistics.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerLogistics.setUpdateTime(LocalDateTime.now());
        logisticsMapper.updateById(kwpLedgerLogistics);
        return logisticsTrackService.saveTrack(id, "", LedgerTrackEnum.REVOKE);
    }

    /**
     * 查询物流对账单驳回记录
     *
     * @param id 物流对账单id
     * @return 物流对账单驳回记录
     */
    public List<KwpLedgerLogisticsTrack> queryBack(Long id) {
        KwpLedgerLogistics kwpLedgerLogistics = logisticsMapper.selectById(id);
        orderCheck(kwpLedgerLogistics);
        backQueryCheck(kwpLedgerLogistics.getStatus());
        return logisticsTrackService.selectList(id, LedgerTrackEnum.REJECT);
    }

    /**
     * 驳回物流对账单
     *
     * @param ledgerReq 驳回参数
     * @return
     */
    public Integer doBack(LedgerReq ledgerReq) {
        Long id = Long.parseLong(ledgerReq.getId());
        KwpLedgerLogistics kwpLedgerLogistics = logisticsMapper.selectById(id);
        if (Objects.isNull(kwpLedgerLogistics)) {
            throw new BusinessException("对账单不存在!");
        }
        backCheck(kwpLedgerLogistics.getStatus());
        kwpLedgerLogistics.setStatus(LedgerEnum.BACK.getStatus());
        kwpLedgerLogistics.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerLogistics.setUpdateTime(LocalDateTime.now());
        logisticsMapper.updateById(kwpLedgerLogistics);
        return logisticsTrackService.saveTrack(id, ledgerReq.getRemark(), LedgerTrackEnum.REJECT);
    }

    /**
     * 对账确认
     * 订单变更为已对账
     *
     * @param confirmReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer confirmOrder(LedgerConfirmReq confirmReq) {
        Long id = Long.parseLong(confirmReq.getId());
        KwpLedgerLogistics kwpLedgerLogistics = logisticsMapper.selectById(id);
        orderCheck(kwpLedgerLogistics);
        confirmCheck(kwpLedgerLogistics.getStatus());
        kwpLedgerLogistics.setStatus(LedgerEnum.LEDGERED.getStatus());
        kwpLedgerLogistics.setAuditPhone(confirmReq.getAuditPhone());
        kwpLedgerLogistics.setAuditUser(confirmReq.getAuditUser());
        kwpLedgerLogistics.setUrl(confirmReq.getUrl());
        kwpLedgerLogistics.setUpdateTime(LocalDateTime.now());
        kwpLedgerLogistics.setUpdateBy(LoginUserHolder.getUserId());
        logisticsMapper.updateById(kwpLedgerLogistics);
        return logisticsTrackService.saveTrack(id, "", LedgerTrackEnum.CONFIRM);
    }

    /**
     * 对账完成
     *
     * @param ledgerReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String orderSuccess(LedgerSuccessReq ledgerReq) {
        Long id = Long.parseLong(ledgerReq.getId());
        KwpLedgerLogistics kwpLedgerLogistics = logisticsMapper.selectById(id);
        orderCheck(kwpLedgerLogistics);

        successCheck(kwpLedgerLogistics.getStatus());

        kwpLedgerLogistics.setReceiptTime(LocalDateTime.of(ledgerReq.getReceiptTime(), LocalTime.MIN));
        kwpLedgerLogistics.setStatus(LedgerEnum.SUCCESS.getStatus());
        kwpLedgerLogistics.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerLogistics.setUpdateTime(LocalDateTime.now());
        logisticsMapper.updateById(kwpLedgerLogistics);
        //生成结算单
        KwpSettlementLogistics kwpSettlementLogistics = new KwpSettlementLogistics();
        kwpSettlementLogistics.setId(new IdWorker(1).nextId());
        kwpSettlementLogistics.setEntId(LoginUserHolder.getEntId());
        kwpSettlementLogistics.setLLedgerId(kwpLedgerLogistics.getId());
        kwpSettlementLogistics.setSlOrderNo(OrderGenerateUtils.generateOrderNo("SL"));
        kwpSettlementLogistics.setName(kwpLedgerLogistics.getName());//取物流对账单名称
        kwpSettlementLogistics.setTotalPrice(kwpLedgerLogistics.getTotalPrice());
        kwpSettlementLogistics.setActualPrice(kwpLedgerLogistics.getActualPrice());
        kwpSettlementLogistics.setRemark("");
        kwpSettlementLogistics.setAuditUser(kwpLedgerLogistics.getAuditUser());
        kwpSettlementLogistics.setAuditPhone(kwpLedgerLogistics.getAuditPhone());
        kwpSettlementLogistics.setSuccessUser(ledgerReq.getSuccessUser());
        kwpSettlementLogistics.setSuccessPhone(ledgerReq.getSuccessPhone());
        kwpSettlementLogistics.setReceiptTime(LocalDateTime.of(ledgerReq.getReceiptTime(), LocalTime.MIN));
        kwpSettlementLogistics.setStatus(1);//1-待结算
        kwpSettlementLogistics.setCreateBy(LoginUserHolder.getUserId());
        kwpSettlementLogistics.setCreateTime(LocalDateTime.now());
        kwpSettlementLogistics.setUpdateBy(LoginUserHolder.getUserId());
        kwpSettlementLogistics.setUpdateTime(LocalDateTime.now());
        kwpSettlementLogistics.setDelFlag(0);

        settlementLogisticsService.save(kwpSettlementLogistics);

        logisticsTrackService.saveTrack(id, "", LedgerTrackEnum.SUCCESS);
        return "对账完成";
    }

    /**
     * @param unitType 1-托运方 2-承运方
     * @return
     */
    public List<LedgerCountVo> orderCount(Integer unitType) {
        Map<String, Long> map = logisticsMapper.countOrder(LoginUserHolder.getEntId(), unitType);
        LedgerCountVo ledgerCountVo;
        List<LedgerCountVo> res = new ArrayList<>();
        for (LedgerEnum value : LedgerEnum.values()) {
            ledgerCountVo = new LedgerCountVo();
            ledgerCountVo.setCount(map.get(String.valueOf(value.getStatus())));
            ledgerCountVo.setLabel(value.getDesc());
            ledgerCountVo.setStatus(value.getStatus());
            res.add(ledgerCountVo);
        }
        return res;
    }

    /**
     * 托运方
     *
     * @param ids
     * @return
     */
    public List<ILedger> selectShipperList(List<Long> ids) {
        List<LedgerShipperDto> ledgerLogisticsDto = logisticsMapper.selectShipperIds(ids);
        List<ILedger> ledgerLogistics = new ArrayList<>(ledgerLogisticsDto);
        changeDict(ledgerLogistics);
        return ledgerLogistics;
    }

    /**
     * 承运方
     *
     * @param ids
     * @return
     */
    public List<ILedger> selectCarrierList(List<Long> ids) {
        List<LedgerCarrierDto> ledgerLogisticsDto = logisticsMapper.selectCarrierIds(ids);
        List<ILedger> ledgerLogistics = new ArrayList<>(ledgerLogisticsDto);
        changeDict(ledgerLogistics);
        return ledgerLogistics;
    }

    public LedgerLogisticsDetailVo queryUpdate(Long id) {
        LedgerLogisticsDto ledgerLogisticsDto = logisticsMapper.selectId(id);
        orderCheck(ledgerLogisticsDto);
        updateCheck(ledgerLogisticsDto.getStatus());
        return getLedgerLogisticsDetailVo(ledgerLogisticsDto);
    }

    public LedgerLogisticsDetailVo queryDetail(Long id) {
        LedgerLogisticsDto ledgerLogisticsDto = logisticsMapper.selectId(id);
        orderCheck(ledgerLogisticsDto);
        return getLedgerLogisticsDetailVo(ledgerLogisticsDto);
    }

    @NotNull
    private LedgerLogisticsDetailVo getLedgerLogisticsDetailVo(LedgerLogisticsDto ledgerLogisticsDto) {
        List<KwpLedgerLogisticsOrder> kwpLedgerLogisticsOrders = logisticsOrderService.queryList(ledgerLogisticsDto.getId());
        LedgerLogisticsDetailVo ledgerLogisticsDetailVo = new LedgerLogisticsDetailVo();
        ledgerLogisticsDetailVo.setId(ledgerLogisticsDto.getId());
        ledgerLogisticsDetailVo.setLLedgerNo(ledgerLogisticsDto.getLLedgerNo());
        ledgerLogisticsDetailVo.setName(ledgerLogisticsDto.getName());
        ledgerLogisticsDetailVo.setFirmName(ledgerLogisticsDto.getFirmName());
        ledgerLogisticsDetailVo.setTrading(ledgerLogisticsDto.getTrading());
        ledgerLogisticsDetailVo.setTaxRate(ledgerLogisticsDto.getTaxRate());
        ledgerLogisticsDetailVo.setTotalPrice(ledgerLogisticsDto.getTotalPrice());
        ledgerLogisticsDetailVo.setActualPrice(ledgerLogisticsDto.getActualPrice());
        ledgerLogisticsDetailVo.setStartTime(ledgerLogisticsDto.getStartTime());
        ledgerLogisticsDetailVo.setEndTime(ledgerLogisticsDto.getEndTime());
        ledgerLogisticsDetailVo.setRemark(ledgerLogisticsDto.getRemark());

        List<LogisticsOrderVO> acceptCarriageOrderList = transportDubboService.getAcceptCarriageOrderList(kwpLedgerLogisticsOrders.stream().map(a -> Long.toString(a.getLOrderId())).collect(Collectors.toList()));
        ledgerLogisticsDetailVo.setOrderList(acceptCarriageOrderList);
        return ledgerLogisticsDetailVo;
    }
}
