package com.izouma.dingdong.service.backstage;

import com.izouma.dingdong.domain.backstage.Complaint;
import com.izouma.dingdong.domain.rider.Rider;
import com.izouma.dingdong.enums.ComplaintTarget;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.OrderInfoRepo;
import com.izouma.dingdong.repo.backstage.ComplaintRepo;
import com.izouma.dingdong.repo.rider.RiderRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class ComplaintService {

    private ComplaintRepo complaintRepo;
    private OrderInfoRepo orderInfoRepo;
    private RiderRepo     riderRepo;

    public List<Complaint> riderList(Long userId) {
        Rider rider = riderRepo.findByUserId(userId).orElseThrow(new BusinessException("id错误"));
        List<Complaint> complaints = complaintRepo.findAllByTarget(ComplaintTarget.RIDER);

        return complaints.stream().filter(c ->
                orderInfoRepo.findById(c.getOrderId())
                        .orElseThrow(new BusinessException("无订单"))
                        .getRiderId()
                        .equals(rider.getId())
        ).collect(Collectors.toList());
    }

}
