package com.izouma.dingdong.service.rider;

import com.izouma.dingdong.domain.rider.RiderSign;
import com.izouma.dingdong.repo.rider.RiderSignRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@AllArgsConstructor
public class RiderSignService {

    private RiderSignRepo riderSignRepo;


    /*
    骑手开始停止接单
    */
    public void riderOrder(Long riderId, Boolean isStart, Double longitude, Double latitude) {
        RiderSign sign = riderSignRepo.findByRiderIdAndTakingOrders(riderId, true);
        if (isStart) {
            if (sign != null) {
                return;
            }
            sign = RiderSign.builder()
                    .takingOrders(true)
                    .signTime(LocalDateTime.now())
                    .riderId(riderId)
                    .longitude(longitude)
                    .latitude(latitude)
                    .build();
        } else {
            if (sign == null) {
                return;
            } else {
                sign.setTakingOrders(false);
            }
        }
        riderSignRepo.save(sign);
    }

}
