package com.sckw.payment.service;

import com.alibaba.fastjson.JSONObject;
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.core.web.context.LoginUserHolder;
import com.sckw.order.api.dubbo.TradeOrderInfoService;
import com.sckw.order.api.model.OrderDetailRes;
import com.sckw.payment.dao.KwpLedgerTradeMapper;
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.TradeUnitType;
import com.sckw.payment.model.dto.*;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.model.vo.res.LedgerTradeDetailVo;
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 jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
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 KwpLedgerTradeService extends AbsLedger {
    @Resource
    private KwpLedgerTradeMapper tradeMapper;
    @Resource
    private KwpLedgerTradeTrackService tradeTrackService;
    @Resource
    private KwpLedgerTradeOrderService tradeOrderService;
    @Resource
    private KwpSettlementTradeService settlementTradeService;
    @Resource
    private KwpLedgerTradeUnitService tradeUnitService;
    @DubboReference(version = "2.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;
    @DubboReference(version = "2.0.0", group = "design", check = false)
    private TradeOrderInfoService tradeOrderInfoService;

    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 tradeDto : list) {
            Integer trading = tradeDto.getTrading();
            tradeDto.setTradingLabel(tradingMap.get(String.valueOf(trading)));
            Integer taxRate = tradeDto.getTaxRate();
            tradeDto.setTaxRateLabel(taxRateMap.get(String.valueOf(taxRate)));

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

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

    /**
     * 销售方
     *
     * @param tradeReq
     * @return
     */
    public PageRes<ILedger> sellList(TradeReq tradeReq) {
        fillPara(tradeReq);
        PageHelper.startPage(tradeReq.getPage(), tradeReq.getPageSize());
        List<LedgerSellDto> ledgerTradeDto = tradeMapper.sellList(tradeReq);
        //字典转换
        if (!CollectionUtils.isEmpty(ledgerTradeDto)) {
            List<ILedger> ledgerList = new ArrayList<>(ledgerTradeDto);
            changeDict(new ArrayList<>(ledgerList));
            return new PageRes<>(new PageInfo<>(ledgerList));
        }
        return new PageRes<>(new PageInfo<>());
    }

    /**
     * 采购方
     *
     * @param tradeReq
     * @return
     */
    public PageRes<ILedger> purchaseList(TradeReq tradeReq) {
        fillPara(tradeReq);
        PageHelper.startPage(tradeReq.getPage(), tradeReq.getPageSize());
        List<LedgerPurchaseDto> ledgerTradeDto = tradeMapper.purchaseList(tradeReq);
        //字典转换
        if (!CollectionUtils.isEmpty(ledgerTradeDto)) {
            List<ILedger> ledgerPurchaseDto = new ArrayList<>(ledgerTradeDto);
            changeDict(ledgerPurchaseDto);
            return new PageRes<>(new PageInfo<>(ledgerPurchaseDto));
        }
        return new PageRes<>(new PageInfo<>());
    }

    private void removeDraft(Long id) {
        //删除kwp_ledger_trade_order
        tradeOrderService.remove(id);
        //删除kwp_ledger_trade_track
        tradeTrackService.remove(id);
        //删除kwp_ledger_trade_unit
        tradeUnitService.remove(id);
    }

    private Long saveDraft(TradeSendReq tradeSendReq) {
        KwpLedgerTrade kwpLedgerTrade = new KwpLedgerTrade();
        kwpLedgerTrade.setId(StringUtils.isBlank(tradeSendReq.getId()) ? new IdWorker(1).nextId() : Long.parseLong(tradeSendReq.getId()));
        kwpLedgerTrade.setEntId(LoginUserHolder.getEntId());
        kwpLedgerTrade.setName(tradeSendReq.getName());
        if (StringUtils.isNotBlank(tradeSendReq.getStartTime())) {
            kwpLedgerTrade.setStartTime(StringTimeUtil.startDateTime(tradeSendReq.getStartTime()));
        }
        if (StringUtils.isNotBlank(tradeSendReq.getEndTime())) {
            kwpLedgerTrade.setEndTime(StringTimeUtil.endDateTime(tradeSendReq.getEndTime()));
        }
        kwpLedgerTrade.setTaxRate(tradeSendReq.getTaxRate());
        kwpLedgerTrade.setTrading(tradeSendReq.getTrading());
        kwpLedgerTrade.setTotalPrice(tradeSendReq.getTotalPrice());
        kwpLedgerTrade.setExTaxPrice(tradeSendReq.getTotalPrice());//todo-xcq 不含税金额计算方式：订单含税金额总和/1.13(税率)
        kwpLedgerTrade.setSettlePrice(tradeSendReq.getSettlePrice());
        kwpLedgerTrade.setActualPrice(new BigDecimal("0.0"));
        kwpLedgerTrade.setAuditUser("");
        kwpLedgerTrade.setAuditPhone("");
        kwpLedgerTrade.setGenerateTime(tradeSendReq.getGenerateTime());
        kwpLedgerTrade.setReceiptTime(null);
        kwpLedgerTrade.setOrderCount(tradeSendReq.getOrders().size());
        kwpLedgerTrade.setUrl("");
        kwpLedgerTrade.setRemark("");
        kwpLedgerTrade.setStatus(tradeSendReq.getStatus());

        kwpLedgerTrade.setDelFlag(0);
        if (Objects.isNull(tradeSendReq.getId())) {
            kwpLedgerTrade.setTLedgerNo(OrderGenerateUtils.generateOrderNo("TL"));
            kwpLedgerTrade.setCreateBy(LoginUserHolder.getUserId());
            kwpLedgerTrade.setCreateTime(LocalDateTime.now());
            kwpLedgerTrade.setUpdateBy(LoginUserHolder.getUserId());
            kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
            tradeMapper.insert(kwpLedgerTrade);
        } else {
            KwpLedgerTrade ledgerTrade = tradeMapper.selectById(tradeSendReq.getId());
            orderCheck(ledgerTrade);
            updateCheck(ledgerTrade.getStatus());
            kwpLedgerTrade.setUpdateBy(LoginUserHolder.getUserId());
            kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
            tradeMapper.updateById(kwpLedgerTrade);
        }

        /*保存贸易订单关联数据*/
        List<Long> ids = tradeSendReq.getOrders();
        //todo-xcq 校验当前订单是否已存在绑定的对账单
        tradeOrderService.check(ids);
        List<OrderDetailRes> resList = new ArrayList<>();
        for (Long id : ids) {
            OrderDetailRes orderDetailById = tradeOrderInfoService.getOrderDetailById(id);
            if (Objects.isNull(orderDetailById)) {
                throw new BusinessException("订单未找到");
            }
            resList.add(orderDetailById);
        }
        List<KwpLedgerTradeOrder> collect = resList.stream().map(a -> {
            KwpLedgerTradeOrder kwpLedgerTradeOrder = new KwpLedgerTradeOrder();
            kwpLedgerTradeOrder.setId(new IdWorker(1).nextId());
            kwpLedgerTradeOrder.setTLedgerId(kwpLedgerTrade.getId());
            kwpLedgerTradeOrder.setTOrderId(a.getId());
            kwpLedgerTradeOrder.setRemark("");
            kwpLedgerTradeOrder.setStatus(0);
            kwpLedgerTradeOrder.setCreateBy(LoginUserHolder.getUserId());
            kwpLedgerTradeOrder.setCreateTime(LocalDateTime.now());
            kwpLedgerTradeOrder.setUpdateBy(LoginUserHolder.getUserId());
            kwpLedgerTradeOrder.setUpdateTime(LocalDateTime.now());
            kwpLedgerTradeOrder.setDelFlag(0);
            return kwpLedgerTradeOrder;
        }).collect(Collectors.toList());
        tradeOrderService.saveList(collect);
        /*保存企业单位相关信息*/
        //todo-xcq dubbo拿取采购客户单位的信息
        Long purchaseEntId = Long.parseLong(tradeSendReq.getPurchaseEntId());
        Map<Long, EntCacheResDto> entMap = remoteSystemService.queryEntTreeByIds(Collections.singletonList(purchaseEntId));
        EntCacheResDto entCacheResDto = entMap.get(purchaseEntId);
        if (Objects.isNull(entCacheResDto)) {
            entCacheResDto = new EntCacheResDto();
        }
        List<KwpLedgerTradeUnit> list = new ArrayList<>();
        KwpLedgerTradeUnit kwpLedgerTradeUnit = new KwpLedgerTradeUnit();
        kwpLedgerTradeUnit.setId(new IdWorker(1).nextId());
        kwpLedgerTradeUnit.setTLedgerId(kwpLedgerTrade.getId());
        kwpLedgerTradeUnit.setTLedgerNo(kwpLedgerTrade.getTLedgerNo());
        kwpLedgerTradeUnit.setUnitType(TradeUnitType.PURCHASE);
        kwpLedgerTradeUnit.setEntId(purchaseEntId);
        kwpLedgerTradeUnit.setTopEntId(entCacheResDto.getId());
        kwpLedgerTradeUnit.setFirmName(entCacheResDto.getFirmName());
        kwpLedgerTradeUnit.setContacts(entCacheResDto.getContacts());
        kwpLedgerTradeUnit.setPhone(entCacheResDto.getPhone());
        kwpLedgerTradeUnit.setRemark("");
        kwpLedgerTradeUnit.setStatus(0);
        kwpLedgerTradeUnit.setCreateBy(LoginUserHolder.getUserId());
        kwpLedgerTradeUnit.setCreateTime(LocalDateTime.now());
        kwpLedgerTradeUnit.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerTradeUnit.setUpdateTime(LocalDateTime.now());
        kwpLedgerTradeUnit.setDelFlag(0);

        list.add(kwpLedgerTradeUnit);
        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();
        }
        //非客户企业方，通过登录用户获取顶级企业
        KwpLedgerTradeUnit sellLedgerTradeUnit = new KwpLedgerTradeUnit();
        sellLedgerTradeUnit.setId(new IdWorker(1).nextId());
        sellLedgerTradeUnit.setTLedgerId(kwpLedgerTrade.getId());
        sellLedgerTradeUnit.setTLedgerNo(kwpLedgerTrade.getTLedgerNo());
        sellLedgerTradeUnit.setUnitType(TradeUnitType.SELL);
        sellLedgerTradeUnit.setEntId(entId);
        sellLedgerTradeUnit.setTopEntId(entCacheResDto1.getId());
        sellLedgerTradeUnit.setFirmName(entCacheResDto1.getFirmName());
        sellLedgerTradeUnit.setContacts(entCacheResDto1.getContacts());
        sellLedgerTradeUnit.setPhone(entCacheResDto1.getPhone());
        sellLedgerTradeUnit.setRemark("");
        sellLedgerTradeUnit.setStatus(0);
        sellLedgerTradeUnit.setCreateBy(LoginUserHolder.getUserId());
        sellLedgerTradeUnit.setCreateTime(LocalDateTime.now());
        sellLedgerTradeUnit.setUpdateBy(LoginUserHolder.getUserId());
        sellLedgerTradeUnit.setUpdateTime(LocalDateTime.now());
        sellLedgerTradeUnit.setDelFlag(0);
        list.add(sellLedgerTradeUnit);

        tradeUnitService.saveList(list);
        return kwpLedgerTrade.getId();
    }

    /**
     * 保存对账单
     *
     * @param tradeSendReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String sendLedger(TradeSendReq tradeSendReq) {
        log.info("保存对账单入参：{}", JSONObject.toJSONString(tradeSendReq));
        String id = tradeSendReq.getId();
        if (StringUtils.isBlank(id)) {
            //新增
            tradeSendReq.setGenerateTime(LocalDateTime.now());
            tradeSendReq.setStatus(LedgerEnum.TO_LEDGER.getStatus());
            Long aLong = saveDraft(tradeSendReq);
            tradeTrackService.saveTrack(aLong, "", LedgerTrackEnum.TO_LEDGER);
        } else {
            //先删除
            removeDraft(Long.parseLong(id));
            //在新增
            tradeSendReq.setGenerateTime(LocalDateTime.now());
            tradeSendReq.setStatus(LedgerEnum.TO_LEDGER.getStatus());
            Long aLong = saveDraft(tradeSendReq);
            tradeTrackService.saveTrack(aLong, "", LedgerTrackEnum.TO_LEDGER);

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

    /**
     * 保存草稿
     *
     * @param tradeSendReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String sendLedgerDraft(TradeSendReq tradeSendReq) {
        log.info("贸易对账单保存草稿：{}", JSONObject.toJSONString(tradeSendReq));
        tradeSendReq.setGenerateTime(null);
        tradeSendReq.setStatus(LedgerEnum.SAVE.getStatus());
        Long aLong = saveDraft(tradeSendReq);
        tradeTrackService.saveTrack(aLong, "", LedgerTrackEnum.SAVE);
        return "草稿保存成功";
    }


    public String remove(Long id) {
        KwpLedgerTrade kwpLedgerTrade = tradeMapper.selectById(id);
        orderCheck(kwpLedgerTrade);
        deleteCheck(kwpLedgerTrade.getStatus());
        kwpLedgerTrade.setDelFlag(1);
        kwpLedgerTrade.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
        tradeMapper.updateById(kwpLedgerTrade);
        tradeTrackService.saveTrack(kwpLedgerTrade.getId(), "", LedgerTrackEnum.DELETE);
        return "删除成功";
    }

    /**
     * 撤回对账单
     *
     * @param id
     * @return
     */
    public String backOrder(Long id) {
        KwpLedgerTrade kwpLedgerTrade = tradeMapper.selectById(id);
        orderCheck(kwpLedgerTrade);
        revokeCheck(kwpLedgerTrade.getStatus());
        kwpLedgerTrade.setStatus(LedgerEnum.REVOCATION.getStatus());
        kwpLedgerTrade.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
        tradeMapper.updateById(kwpLedgerTrade);

        tradeTrackService.saveTrack(id, "", LedgerTrackEnum.REVOKE);
        return "撤回成功";
    }

    /**
     * 查询贸易对账单驳回记录
     *
     * @param id
     * @return
     */
    public List<KwpLedgerTradeTrack> queryBack(Long id) {
        KwpLedgerTrade kwpLedgerTrade = tradeMapper.selectById(id);
        orderCheck(kwpLedgerTrade);
        backQueryCheck(kwpLedgerTrade.getStatus());
        return tradeTrackService.selectList(id, LedgerTrackEnum.REJECT);
    }

    /**
     * 驳回贸易对账单
     *
     * @param ledgerReq
     * @return
     */
    public String doBack(LedgerReq ledgerReq) {
        Long id = Long.parseLong(ledgerReq.getId());
        KwpLedgerTrade kwpLedgerTrade = tradeMapper.selectById(id);
        orderCheck(kwpLedgerTrade);
        backCheck(kwpLedgerTrade.getStatus());
        kwpLedgerTrade.setStatus(LedgerEnum.BACK.getStatus());
        kwpLedgerTrade.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
        tradeMapper.updateById(kwpLedgerTrade);
        tradeTrackService.saveTrack(id, ledgerReq.getRemark(), LedgerTrackEnum.REJECT);
        return "驳回成功";
    }

    /**
     * 对账确认
     *
     * @param confirmReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String confirmOrder(LedgerConfirmReq confirmReq) {
        Long id = Long.parseLong(confirmReq.getId());
        KwpLedgerTrade kwpLedgerTrade = tradeMapper.selectById(id);
        orderCheck(kwpLedgerTrade);
        confirmCheck(kwpLedgerTrade.getStatus());
        kwpLedgerTrade.setStatus(LedgerEnum.LEDGERED.getStatus());
        kwpLedgerTrade.setAuditPhone(confirmReq.getAuditPhone());
        kwpLedgerTrade.setAuditUser(confirmReq.getAuditUser());
        kwpLedgerTrade.setUrl(confirmReq.getUrl());
        kwpLedgerTrade.setUpdateBy(LoginUserHolder.getUserId());
        kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
        tradeMapper.updateById(kwpLedgerTrade);
        tradeTrackService.saveTrack(id, "", LedgerTrackEnum.CONFIRM);
        return "对账确认成功";
    }

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

        KwpLedgerTrade kwpLedgerTrade = tradeMapper.selectById(id);
        orderCheck(kwpLedgerTrade);
        successCheck(kwpLedgerTrade.getStatus());

        kwpLedgerTrade.setReceiptTime(LocalDateTime.of(ledgerReq.getReceiptTime(), LocalTime.MIN));
        kwpLedgerTrade.setStatus(LedgerEnum.SUCCESS.getStatus());
        kwpLedgerTrade.setUpdateTime(LocalDateTime.now());
        tradeMapper.updateById(kwpLedgerTrade);
        //生成结算单
        KwpSettlementTrade kwpSettlementTrade = new KwpSettlementTrade();
        kwpSettlementTrade.setId(new IdWorker(1).nextId());
        kwpSettlementTrade.setEntId(LoginUserHolder.getEntId());
        kwpSettlementTrade.setTLedgerId(kwpLedgerTrade.getId());
        kwpSettlementTrade.setStOrderNo(OrderGenerateUtils.generateOrderNo("ST"));
        kwpSettlementTrade.setName(kwpLedgerTrade.getName());
        kwpSettlementTrade.setTotalPrice(kwpLedgerTrade.getTotalPrice());
        kwpSettlementTrade.setActualPrice(kwpLedgerTrade.getActualPrice());
        kwpSettlementTrade.setRemark("");
        kwpSettlementTrade.setAuditUser(kwpLedgerTrade.getAuditUser());
        kwpSettlementTrade.setAuditPhone(kwpLedgerTrade.getAuditPhone());
        kwpSettlementTrade.setSuccessUser(ledgerReq.getSuccessUser());
        kwpSettlementTrade.setSuccessPhone(ledgerReq.getSuccessPhone());
        kwpSettlementTrade.setReceiptTime(LocalDateTime.of(ledgerReq.getReceiptTime(), LocalTime.MIN));
        kwpSettlementTrade.setStatus(1);//1-待结算
        kwpSettlementTrade.setCreateBy(LoginUserHolder.getUserId());
        kwpSettlementTrade.setCreateTime(LocalDateTime.now());
        kwpSettlementTrade.setUpdateBy(LoginUserHolder.getUserId());
        kwpSettlementTrade.setUpdateTime(LocalDateTime.now());
        kwpSettlementTrade.setDelFlag(0);
        settlementTradeService.save(kwpSettlementTrade);

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

    public List<LedgerCountVo> orderCount(Integer unitType) {
        Map<String, Long> map = tradeMapper.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;
    }

    public List<ILedger> selectSellList(List<Long> ids) {
        List<LedgerSellDto> ledgerTradeDto = tradeMapper.selectSellIds(ids);
        List<ILedger> ledgerSellDto = new ArrayList<>(ledgerTradeDto);
        changeDict(ledgerSellDto);
        return ledgerSellDto;
    }

    public List<ILedger> selectPurchaseList(List<Long> ids) {
        List<LedgerPurchaseDto> ledgerTradeDto = tradeMapper.selectPurchaseIds(ids);
        List<ILedger> ledgerSellDto = new ArrayList<>(ledgerTradeDto);
        changeDict(ledgerSellDto);
        return ledgerSellDto;
    }

    public LedgerTradeDetailVo queryUpdate(Long id) {
        LedgerTradeDto ledgerTradeDto = tradeMapper.selectId(id);
        orderCheck(ledgerTradeDto);
        updateCheck(ledgerTradeDto.getStatus());
        List<KwpLedgerTradeOrder> kwpLedgerTradeOrders = tradeOrderService.queryList(ledgerTradeDto.getId());
        LedgerTradeDetailVo ledgerTradeDetailVo = new LedgerTradeDetailVo();
        ledgerTradeDetailVo.setId(ledgerTradeDto.getId());
        ledgerTradeDetailVo.setTLedgerNo(ledgerTradeDto.getTLedgerNo());
        ledgerTradeDetailVo.setName(ledgerTradeDto.getName());
        ledgerTradeDetailVo.setFirmName(ledgerTradeDto.getFirmName());
        ledgerTradeDetailVo.setTrading(ledgerTradeDto.getTrading());
        ledgerTradeDetailVo.setTaxRate(ledgerTradeDto.getTaxRate());
        ledgerTradeDetailVo.setTotalPrice(ledgerTradeDto.getTotalPrice());
        ledgerTradeDetailVo.setActualPrice(ledgerTradeDto.getActualPrice());
        ledgerTradeDetailVo.setStartTime(ledgerTradeDto.getStartTime());
        ledgerTradeDetailVo.setEndTime(ledgerTradeDto.getEndTime());
        ledgerTradeDetailVo.setRemark(ledgerTradeDto.getRemark());
        List<Long> collect = kwpLedgerTradeOrders.stream().map(KwpLedgerTradeOrder::getTOrderId).toList();
        List<OrderDetailRes> detailRes = new ArrayList<>();
        OrderDetailRes orderDetailById;
        for (Long aLong : collect) {
            orderDetailById = tradeOrderInfoService.getOrderDetailById(aLong);
            detailRes.add(orderDetailById);
        }
        ledgerTradeDetailVo.setOrderList(detailRes);
        return ledgerTradeDetailVo;
    }

    public LedgerTradeDetailVo queryDetail(Long id) {
        LedgerTradeDto ledgerTradeDto = tradeMapper.selectId(id);
        orderCheck(ledgerTradeDto);

        List<KwpLedgerTradeOrder> kwpLedgerTradeOrders = tradeOrderService.queryList(ledgerTradeDto.getId());
        LedgerTradeDetailVo ledgerTradeDetailVo = new LedgerTradeDetailVo();
        ledgerTradeDetailVo.setId(ledgerTradeDto.getId());
        ledgerTradeDetailVo.setTLedgerNo(ledgerTradeDto.getTLedgerNo());
        ledgerTradeDetailVo.setName(ledgerTradeDto.getName());
        ledgerTradeDetailVo.setFirmName(ledgerTradeDto.getFirmName());
        ledgerTradeDetailVo.setTrading(ledgerTradeDto.getTrading());
        ledgerTradeDetailVo.setTaxRate(ledgerTradeDto.getTaxRate());
        ledgerTradeDetailVo.setTotalPrice(ledgerTradeDto.getTotalPrice());
        ledgerTradeDetailVo.setActualPrice(ledgerTradeDto.getActualPrice());
        ledgerTradeDetailVo.setStartTime(ledgerTradeDto.getStartTime());
        ledgerTradeDetailVo.setEndTime(ledgerTradeDto.getEndTime());
        ledgerTradeDetailVo.setRemark(ledgerTradeDto.getRemark());
        List<Long> collect = kwpLedgerTradeOrders.stream().map(KwpLedgerTradeOrder::getTOrderId).toList();
        List<OrderDetailRes> detailRes = new ArrayList<>();
        OrderDetailRes orderDetailById;
        for (Long aLong : collect) {
            orderDetailById = tradeOrderInfoService.getOrderDetailById(aLong);
            detailRes.add(orderDetailById);
        }
        ledgerTradeDetailVo.setOrderList(detailRes);
        return ledgerTradeDetailVo;
    }
}
