package com.sckw.transport.service.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.core.common.enums.NumberConstant;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.system.api.RemoteSystemService;
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 com.sckw.transport.dao.KwtLogisticsOrderGoodsMapper;
import com.sckw.transport.dao.KwtLogisticsOrderMapper;
import com.sckw.transport.dao.KwtLogisticsOrderUnitMapper;
import com.sckw.transport.dao.KwtWaybillOrderMapper;
import com.sckw.transport.model.KwtLogisticsOrder;
import com.sckw.transport.model.KwtLogisticsOrderUnit;
import com.sckw.transport.model.KwtWaybillOrder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lfdc
 * @description 运输服务dubbo接口
 * @date 2023-07-11 14:07:28
 */
@Service
@DubboService(group = "design", version = "2.0.0", timeout = 50000)
public class TransportDubboServiceImpl implements TransportDubboService {

    @DubboReference(version = "2.0.0", group = "design", check = false)
    RemoteSystemService remoteSystemService;

    @Autowired
    private KwtLogisticsOrderMapper logisticsOrderMapper;

    @Autowired
    private KwtLogisticsOrderUnitMapper logisticsOrderUnitMapper;

    @Autowired
    private KwtLogisticsOrderGoodsMapper logisticsOrderGoodsMapper;

    @Autowired
    private KwtWaybillOrderMapper waybillOrderMapper;

    /**
     * 对账管理-ids查询
     *
     * @param ids
     * @return
     */
    public List<KwtLogisticsOrderVO> getAcceptCarriageOrderDetail(List<String> ids) {
        List<KwtLogisticsOrderVO> list = new ArrayList<>();
        List<KwtLogisticsOrder> kwtLogisticsOrders = logisticsOrderMapper.selectList(new LambdaQueryWrapper<KwtLogisticsOrder>().in(KwtLogisticsOrder::getId, ids));
        if (CollectionUtils.isNotEmpty(kwtLogisticsOrders)) {
            for (KwtLogisticsOrder kwtLogisticsOrder : kwtLogisticsOrders) {
                KwtLogisticsOrderVO vo = new KwtLogisticsOrderVO();
                BeanUtils.copyProperties(kwtLogisticsOrder, vo);
                list.add(vo);
            }
        }
        return list;
    }

    /**
     * 对账页面数据接口
     *
     * @param ids
     * @return
     */
    @Override
    public List<LogisticsOrderVO> getAcceptCarriageOrderList(List<String> ids) {
        List<LogisticsOrderVO> list = logisticsOrderMapper.selectLogisticOrderList(ids);
        if (CollectionUtils.isNotEmpty(list)) {
            for (LogisticsOrderVO logisticsOrderVO : list) {
                KwtLogisticsOrderUnit orderUnit = logisticsOrderUnitMapper.selectOne(new LambdaQueryWrapper<KwtLogisticsOrderUnit>()
                        .eq(KwtLogisticsOrderUnit::getLOrderId, logisticsOrderVO.getLOrderId())
                        .eq(KwtLogisticsOrderUnit::getUnitType, NumberConstant.ONE));
                KwtLogisticsOrderUnit orderUnit1 = logisticsOrderUnitMapper.selectOne(new LambdaQueryWrapper<KwtLogisticsOrderUnit>()
                        .eq(KwtLogisticsOrderUnit::getLOrderId, logisticsOrderVO.getLOrderId())
                        .eq(KwtLogisticsOrderUnit::getUnitType, NumberConstant.TWO));
                logisticsOrderVO.setCarrierCompany(orderUnit1 == null ? null : orderUnit1.getFirmName());
                logisticsOrderVO.setConsignCompany(orderUnit == null ? null : orderUnit.getFirmName());
                logisticsOrderVO.setCount(waybillOrderMapper.selectCount(new LambdaQueryWrapper<KwtWaybillOrder>().eq(KwtWaybillOrder::getLOrderId, logisticsOrderVO.getLOrderId())));
            }
        }
        return list;
    }
}
