package com.izouma.dingdong.web.rider;

import com.izouma.dingdong.dto.RiderDTO;
import com.izouma.dingdong.enums.ApplyStatus;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.rider.Rider;
import com.izouma.dingdong.service.rider.RiderService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.rider.RiderRepo;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.excel.ExcelUtils;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/rider")
@AllArgsConstructor
public class RiderController extends BaseController {
    private RiderService riderService;
    private RiderRepo    riderRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Rider save(@RequestBody Rider record) {
        if (record.getId() != null) {
            Rider orig = riderRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            if (ApplyStatus.DENY.equals(orig.getStatus())) {
                record.setStatus(ApplyStatus.PENDING);
            }
            ObjUtils.merge(orig, record);
            return riderRepo.save(orig);
        }
        return riderRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<Rider> all(PageQuery pageQuery) {
        return riderRepo.findAll(toSpecification(pageQuery, Rider.class), toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public Rider get(@PathVariable Long id) {
        return riderRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        riderRepo.deleteById(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Rider> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/receiver")
    @ApiOperation("骑手手动接单")
    public void riderReceiveOrder(Long orderId) {
        riderService.riderReceiveOrder(SecurityUtils.getAuthenticatedUser().getId(), orderId);
    }

    @GetMapping("/autoReceive")
    @ApiOperation("无骑手接单，自动分配订单")
    public void autoReceive(Long orderId) {
        riderService.dispatch(orderId);
    }

    @GetMapping("/info")
    @ApiOperation("骑手信息")
    public Rider riderInfo(Long riderId) {
        return riderRepo.findById(riderId).orElseThrow(new BusinessException("无骑手"));
    }

    @GetMapping("/my")
    @ApiOperation("骑手信息")
    public Rider riderInfo() {
        return riderRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId())
                .orElseThrow(new BusinessException("无骑手"));
    }

    @GetMapping("/apply")
    @ApiOperation("骑手注册")
    public Rider apply(RiderDTO riderDTO) {
        return riderService.riderApply(riderDTO);
    }

    @GetMapping("/audit")
    @ApiOperation("审核骑手")
    public String audit(Long id, Boolean pass, String reason) {
        return riderService.riderAudit(id, pass, reason);
    }
}

