package com.prolog.rdc.tms.app.service.impl;

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

import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.prolog.rdc.tms.app.dao.mapper.TmsAppointmentHeaderMapper;
import com.prolog.rdc.tms.app.model.dto.*;
import com.prolog.rdc.tms.app.model.vo.*;
import com.prolog.rdc.tms.app.service.business.LoadingBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.prolog.common.util.ToolKits;
import com.prolog.framework.utils.ObjectUtils;
import com.prolog.rdc.tms.app.dao.mapper.TmsLoadingBillDetailMapper;
import com.prolog.rdc.tms.app.dao.mapper.TmsLoadingBillHeaderMapper;
import com.prolog.rdc.tms.app.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.app.service.QueryReportService;

/**
 *
 *
 * 司机APP-综合查询实现层
 *
 * @className :PendingAppointmentServiceImpl
 * @author :qian.yh
 * @creationTime :2022/07/04
 * @description :
 *
 * @modifier :
 * @modifyTime :
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Service
public class QueryReportServiceImpl implements QueryReportService {

	@Resource
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Resource
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Resource
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Resource
	private TmsAppointmentHeaderMapper tmsAppointmentHeaderMapper;

	@Autowired
	private LoadingBillService loadingBillService;

	/**
	 * 配送单查询
	 */
	@Override
	public DeliveryOrderQueryReesponseDTO deliveryOrderQuery(DeliveryOrderQueryVO deliveryOrderQueryVO) throws Exception {
		ToolKits.isParamNull(deliveryOrderQueryVO, "配送单查询请求参数不为空");
		ToolKits.isParamNull(deliveryOrderQueryVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(deliveryOrderQueryVO.getVehiclePlateNumber(), "车牌号不为空");
		ToolKits.isParamNull(deliveryOrderQueryVO.getEnterpriseId(), "企业ID不能为空");
		ToolKits.isParamNull(deliveryOrderQueryVO.getStartDate(), "开始时间不能为空");
		ToolKits.isParamNull(deliveryOrderQueryVO.getEndDate(), "结束时间不能为空");
		List<DeliveryOrderQueryDTO> dtos = tmsLoadingBillDetailMapper.deliveryOrderQuery(deliveryOrderQueryVO);
		if (ObjectUtils.isEmpty(dtos)) {
			throw new RuntimeException("未找到对应的运单数据！");
		}
		DeliveryOrderQueryReesponseDTO reesponseDTO = new DeliveryOrderQueryReesponseDTO();
		List<DeliveryOrderQueryDTO> returndtos = new ArrayList<DeliveryOrderQueryDTO>();
		int ywcNumber = 0;
		int ycNumber = 0;
		for (DeliveryOrderQueryDTO dto : dtos) {
			ShipbillDetailQueryVO vo = new ShipbillDetailQueryVO();
			vo.setEnterpriseId(dto.getEnterpriseId());
			vo.setShipbillId(dto.getShipbillId());
			List<ShipbillItemDetailQueryDTO> itemDetails = tmsShipbillHeaderMapper.shipbillItemDetailQuery(vo);
			dto.setItemDetail(itemDetails);


			if(dto.getIsYc()==1) {
				// STD-BUG20220824 xiaoyf bug【24012】 start
				if(ObjectUtil.equal(deliveryOrderQueryVO.getType(),"2")){
					returndtos.add(dto);
				}
				if(ObjectUtil.equal(deliveryOrderQueryVO.getType(),"0")){
					returndtos.add(dto);
				}
				// STD-BUG20220824 xiaoyf bug【24012】 end
				ycNumber++;
			} else if(dto.getStatus()>=160){
				// STD-BUG20220824 xiaoyf bug【24012】 start
				if(ObjectUtil.equal(deliveryOrderQueryVO.getType(),"1")){
					returndtos.add(dto);
				}
				if(ObjectUtil.equal(deliveryOrderQueryVO.getType(),"0")){
					returndtos.add(dto);
				}
				// STD-BUG20220824 xiaoyf bug【24012】 end
				ywcNumber++;
			}
		}
		reesponseDTO.setDeliveryOrderList(returndtos);
		reesponseDTO.setYcNumber(ycNumber);
		reesponseDTO.setYwcNumber(ywcNumber);
		return reesponseDTO;
	}

	/**
	 * 派车单查询
	 */
	@Override
	public DispatchOrderQueryResponseDTO dispatchOrderQuery(DispatchOrderQueryVO dispatchOrderQueryVO) throws Exception {
		ToolKits.isParamNull(dispatchOrderQueryVO, "司机派车单查询请求参数不为空");
		ToolKits.isParamNull(dispatchOrderQueryVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(dispatchOrderQueryVO.getVehiclePlateNumber(), "车牌号不为空");
		ToolKits.isParamNull(dispatchOrderQueryVO.getEnterpriseId(), "企业ID不能为空");
		ToolKits.isParamNull(dispatchOrderQueryVO.getStartDate(), "开始时间不能为空");
		ToolKits.isParamNull(dispatchOrderQueryVO.getEndDate(), "结束时间不能为空");
		List<DispatchOrderQueryDTO> dtos = tmsLoadingBillHeaderMapper.dispatchOrderQuery(dispatchOrderQueryVO);
		if (ObjectUtils.isEmpty(dtos)) {
			throw new RuntimeException("未找到对应的派车单数据！");
		}
		DispatchOrderQueryResponseDTO returndto= new DispatchOrderQueryResponseDTO();
		int ywcNumber = 0;
		for (DispatchOrderQueryDTO dto : dtos) {
			if(dto.getWwcNumber()==0){
				ywcNumber++;
			}
		}
		returndto.setDispatchOrderList(dtos);
		returndto.setYwcNumber(ywcNumber);
		return returndto;
	}

	/**
	 * 派车单明细查询
	 */
	@Override
	public List<DispatchOrderDetailQueryDTO> dispatchOrderDetailQuery(DispatchOrderDetailQueryVO dispatchOrderDetailQueryVO) throws Exception {
		ToolKits.isParamNull(dispatchOrderDetailQueryVO, "派车单明细查询请求参数不为空");
		ToolKits.isParamNull(dispatchOrderDetailQueryVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(dispatchOrderDetailQueryVO.getEnterpriseId(), "企业ID不能为空");
		ToolKits.isParamNull(dispatchOrderDetailQueryVO.getLoadingBillId(), "装车单ID不能为空");
		List<DispatchOrderDetailQueryDTO> dtos = tmsLoadingBillDetailMapper.dispatchOrderDetailQuery(dispatchOrderDetailQueryVO);
		if (ObjectUtils.isEmpty(dtos)) {
			throw new RuntimeException("未找到对应的派车单明细！");
		}

		List<DispatchOrderDetailQueryDTO> returndtos = new ArrayList<DispatchOrderDetailQueryDTO>();
		for (DispatchOrderDetailQueryDTO dto : dtos) {
			ShipbillDetailQueryVO vo = new ShipbillDetailQueryVO();
			vo.setEnterpriseId(dto.getEnterpriseId());
			vo.setShipbillId(dto.getShipbillId());
			List<ShipbillItemDetailQueryDTO> itemDetails = tmsShipbillHeaderMapper.shipbillItemDetailQuery(vo);
			dto.setItemDetail(itemDetails);
			returndtos.add(dto);
		}
		return returndtos;
	}

	@Override
	public DriverInformationQueryDTO driverInformationQuery(DriverInformationQueryVO driverInformationQueryVO) throws Exception {
		ToolKits.isParamNull(driverInformationQueryVO, "司机信息查询请求参数不为空");
		ToolKits.isParamNull(driverInformationQueryVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(driverInformationQueryVO.getVehicleId(), "车辆ID不为空");
		ToolKits.isParamNull(driverInformationQueryVO.getEnterpriseId(), "企业ID不能为空");
		List<DriverInformationQueryDTO> dtos = tmsLoadingBillDetailMapper.driverInformationQuery(driverInformationQueryVO);
		if (ObjectUtils.isEmpty(dtos)) {
			throw new RuntimeException("未找到司机信息数据！");
		}
		return dtos.get(0);
	}

	@Override
	public List<DriverGetNumberQueryDTO> driverGetNumberQuery(DriverGetNumberQueryVO driverGetNumberQueryVO) throws Exception {
		ToolKits.isParamNull(driverGetNumberQueryVO, "司机取号信息查询请求参数不为空");
		ToolKits.isParamNull(driverGetNumberQueryVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(driverGetNumberQueryVO.getVehiclePlateNumber(), "车牌号不为空");
		ToolKits.isParamNull(driverGetNumberQueryVO.getEnterpriseId(), "企业ID不能为空");
		List<DriverGetNumberQueryDTO> dtos = tmsAppointmentHeaderMapper.driverGetNumberQuery(driverGetNumberQueryVO);
		if (ObjectUtils.isEmpty(dtos)) {
			throw new RuntimeException("未找到司机信息数据！");
		}
		return dtos;
	}

	@Override
	public List<DriverVehicleDTO> driverVehicleQuery(DriverVehicleQueryVO driverVehicleQueryVO) throws Exception {
		ToolKits.isParamNull(driverVehicleQueryVO, "司机取号信息查询请求参数不为空");
		ToolKits.isParamNull(driverVehicleQueryVO.getDriverId(), "司机ID不为空");
		ToolKits.isParamNull(driverVehicleQueryVO.getEnterpriseId(), "企业ID不能为空");
		List<DriverVehicleDTO> dtos = tmsLoadingBillDetailMapper.driverVehicleQuery(driverVehicleQueryVO);
		if (ObjectUtils.isEmpty(dtos)) {
			throw new RuntimeException("未找到司机信息数据！");
		}
		return dtos;
	}
}
