package com.sckw.transport.dubbo;

import com.sckw.core.common.enums.NumberConstant;
import com.sckw.core.model.enums.CarWaybillEnum;
import com.sckw.core.model.enums.LogisticsOrderEnum;
import com.sckw.transport.api.dubbo.TransportStatisticsService;
import com.sckw.transport.dao.KwtLogisticsOrderMapper;
import com.sckw.transport.dao.KwtWaybillOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @author lfdc
 * @description 运输服务统计dubbo Service
 * @date 2023-09-08 11:09:34
 */
@Slf4j
@Service
@DubboService(group = "design", version = "1.0.0")
public class TransportStatisticsServiceImpl implements TransportStatisticsService {

    @Autowired
    KwtWaybillOrderMapper waybillOrderMapper;

    @Autowired
    KwtLogisticsOrderMapper logisticsOrderMapper;

    /**
     * 统计调度派车次数
     *
     * @param topEntId 集团企业id
     * @return
     */
    @Override
    public Integer statisticsWaybillSendCar(Long topEntId) {
        Integer returnCount = 0;
//        if (Objects.isNull(topEntId)) {
//            throw new BusinessException("请求参数不能为空");
//        }
        List<Long> ids = logisticsOrderMapper.statisticsLogistics(topEntId);
        if (CollectionUtils.isEmpty(ids)) {
            return returnCount;
        }
        List<Integer> orderList = new ArrayList<>(NumberConstant.TWELVE);
        orderList.add(CarWaybillEnum.PENDING_ORDER.getCode());
        orderList.add(CarWaybillEnum.PENDING_VEHICLE.getCode());
        orderList.add(CarWaybillEnum.EXIT_COMPLETED.getCode());
        orderList.add(CarWaybillEnum.WAIT_LOADING.getCode());
        orderList.add(CarWaybillEnum.COMPLETION_LOADING.getCode());
        orderList.add(CarWaybillEnum.WAIT_UNLOADING.getCode());
        orderList.add(CarWaybillEnum.COMPLETION_UNLOADING.getCode());
        orderList.add(CarWaybillEnum.APPROVAL_PASS.getCode());
        orderList.add(CarWaybillEnum.APPROVAL_IN.getCode());
        returnCount = logisticsOrderMapper.statisticsWaybillSendCar(ids, orderList);
        return returnCount;
    }

    /**
     * 统计待核运单
     *
     * @param topEntId 集团企业id
     * @return
     */
    @Override
    public Integer statisticsPendingVerificationWaybill(Long topEntId) {
        Integer returnCount = 0;
//        if (Objects.isNull(topEntId)) {
//            throw new BusinessException("请求参数不能为空");
//        }
        List<Long> ids = logisticsOrderMapper.statisticsLogistics(topEntId);
        if (CollectionUtils.isEmpty(ids)) {
            return returnCount;
        }
        List<Integer> orderList = new ArrayList<>(NumberConstant.TWELVE);
        orderList.add(CarWaybillEnum.COMPLETION_UNLOADING.getCode());
        orderList.add(CarWaybillEnum.APPROVAL_IN.getCode());
        returnCount = logisticsOrderMapper.statisticsWaybillSendCar(ids, orderList);
        return returnCount;
    }

    /**
     * 统计承运接单数量
     *
     * @param topEntId 集团企业id
     * @return
     */
    @Override
    public Integer statisticsLogistics(Long topEntId) {
        //统计维度
        List<Integer> orderList = new ArrayList<>();
        orderList.add(LogisticsOrderEnum.WAIT_DELIVERY.getCode());
        orderList.add(LogisticsOrderEnum.IN_TRANSIT.getCode());
        orderList.add(LogisticsOrderEnum.HAVE_FINISHED.getCode());
        orderList.add(LogisticsOrderEnum.HAVE_RECONCILED.getCode());
        orderList.add(LogisticsOrderEnum.HAVE_ALREADY_SETTLED.getCode());
        Integer count = logisticsOrderMapper.statisticsLogisticsByTopEntIdAndOrderStatus(topEntId, orderList,"2");
        return count;
    }

    /**
     * 统计托运订单数量
     *
     * @param topEntId 集团企业id
     * @return
     */
    @Override
    public Integer statisticsLogisticsByConsign(Long topEntId) {
        //统计维度
        List<Integer> orderList = new ArrayList<>();
        orderList.add(LogisticsOrderEnum.WAIT_DELIVERY.getCode());
        orderList.add(LogisticsOrderEnum.IN_TRANSIT.getCode());
        orderList.add(LogisticsOrderEnum.HAVE_FINISHED.getCode());
        orderList.add(LogisticsOrderEnum.HAVE_RECONCILED.getCode());
        orderList.add(LogisticsOrderEnum.HAVE_ALREADY_SETTLED.getCode());
        Integer count = logisticsOrderMapper.statisticsLogisticsByTopEntIdAndOrderStatus(topEntId, orderList,"1");
        return count;
    }
}
