package com.trade.service.trade.salesorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trade.service.filestorage.dto.FileAbstractDto;
import com.trade.service.filestorage.service.TFileStorageService;
import com.trade.service.trade.filingdoc.vo.Logistics4FilingDocVO;
import com.trade.service.trade.salesorder.domain.SalesOrderLogistics;
import com.trade.service.trade.salesorder.mapper.SalesOrderLogisticsMapper;
import com.trade.service.trade.salesorder.service.ISalesOrderContainerService;
import com.trade.service.trade.salesorder.service.ISalesOrderLogisticsService;
import com.trade.service.trade.salesorder.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 销售订单-物流信息 服务实现类
 * </p>
 *
 * @author zhangchenm
 * @since 2024-09-06
 */
@Service
public class SalesOrderLogisticsServiceImpl extends ServiceImpl<SalesOrderLogisticsMapper, SalesOrderLogistics> implements ISalesOrderLogisticsService {

    @Resource
    private ISalesOrderContainerService salesOrderContainerService;
    @Resource
    private TFileStorageService tFileStorageService;


    @Override
    public SalesOrderLogistics getByOrderId(Long orderId) {
        return getBaseMapper().selectOne(new QueryWrapper<SalesOrderLogistics>().eq("order_id", orderId));
    }

    @Override
    public PageInfo<SalesOrderLogisticsVO> queryListBySearch(SalesOrderLogisticsSearch search) {
        PageHelper.startPage(search.getPage(), search.getRows());
        List<SalesOrderLogisticsVO> list = this.baseMapper.queryListBySearch(search);
        return new PageInfo<>(list);
    }

    @Override
    public SalesOrderLogisticsDtlVO getDetailByOrderNo(String orderNo) {
        SalesOrderLogisticsDtlVO salesOrderLogisticsDtlVO = this.baseMapper.getDetailByOrderNo(orderNo);
        if (null != salesOrderLogisticsDtlVO) {
            salesOrderLogisticsDtlVO.setLadingBillFile(new FileAbstractDto(
                    tFileStorageService.getFileStorageById(salesOrderLogisticsDtlVO.getLadingBillFileId())));
            List<SalesOrderContainerVO> salesOrderContainers = salesOrderContainerService
                    .listByLogisticsId(salesOrderLogisticsDtlVO.getId());
            salesOrderContainers.forEach(item -> item
                    .setFile(new FileAbstractDto(tFileStorageService.getFileStorageById(item.getFileId()))));
            salesOrderLogisticsDtlVO.setSalesOrderContainerList(salesOrderContainers);
        }
        return salesOrderLogisticsDtlVO;
    }

    @Override
    public PageInfo<Logistics4FilingDocVO> queryList4FilingDocBySearch(SalesOrderLogisticsSearch search) {
        PageHelper.startPage(search.getPage(), search.getRows());
        List<Logistics4FilingDocVO> list = this.baseMapper.queryList4FilingDocBySearch(search);
        list.forEach(item -> {
            item.setLadingBillFile(
                    new FileAbstractDto(tFileStorageService.getFileStorageById(item.getLadingBillFileId())));
            item.setFile(new FileAbstractDto(tFileStorageService.getFileStorageById(item.getFileId())));
        });
        return new PageInfo<>(list);
    }

    @Override
    public List<String> queryAllLogisticsCompany4Dict() {
        return this.baseMapper.queryAllLogisticsCompany4Dict();
    }
}
