package org.dromara.system.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.TblDzbcDriverTripBo;
import org.dromara.system.domain.vo.TblDzbcDriverTripVo;
import org.dromara.system.service.ITblDzbcDriverTripService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dzbc/driverTrip")
public class TblDzbcDriverTripController extends BaseController {

    private final ITblDzbcDriverTripService tblDzbcDriverTripService;

    @GetMapping("/list")
    public TableDataInfo<TblDzbcDriverTripVo> list(TblDzbcDriverTripBo bo, PageQuery pageQuery) {
        return tblDzbcDriverTripService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/qr/{tripNo}")
    public R<TblDzbcDriverTripVo> getByQrCode(@NotBlank(message = "任务编号不能为空") @PathVariable String tripNo) {
        return R.ok(tblDzbcDriverTripService.queryByTripNo(tripNo));
    }

    @Log(title = "司机任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblDzbcDriverTripBo bo, HttpServletResponse response) {
        List<TblDzbcDriverTripVo> list = tblDzbcDriverTripService.queryList(bo);
        ExcelUtil.exportExcel(list, "司机任务", TblDzbcDriverTripVo.class, response);
    }

    @GetMapping("/{id}")
    public R<TblDzbcDriverTripVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(tblDzbcDriverTripService.queryById(id));
    }

    @Log(title = "创建司机任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<TblDzbcDriverTripVo> add(@Validated(AddGroup.class) @RequestBody TblDzbcDriverTripBo bo) {
        return R.ok(tblDzbcDriverTripService.createTrip(bo));
    }

    @Log(title = "开始任务", businessType = BusinessType.UPDATE)
    @PostMapping("/start/{id}")
    public R<Void> start(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(tblDzbcDriverTripService.startTrip(id));
    }

    @Log(title = "根据排班ID开始任务", businessType = BusinessType.UPDATE)
    @PostMapping("/startByScheduling/{schedulingId}")
    public R<Void> startByScheduling(@NotNull(message = "排班ID不能为空") @PathVariable Long schedulingId) {
        return toAjax(tblDzbcDriverTripService.startTripBySchedulingId(schedulingId));
    }

    @Log(title = "到达站点", businessType = BusinessType.UPDATE)
    @PostMapping("/arrive/{id}")
    public R<Void> arrive(@NotNull(message = "主键不能为空") @PathVariable Long id,
                           @NotNull(message = "站点ID不能为空") @RequestParam Long siteId) {
        return toAjax(tblDzbcDriverTripService.arriveSite(id, siteId));
    }

    @Log(title = "完成任务", businessType = BusinessType.UPDATE)
    @PostMapping("/complete/{id}")
    public R<Void> complete(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(tblDzbcDriverTripService.completeTrip(id));
    }

    @Log(title = "取消任务", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{id}")
    public R<Void> cancel(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(tblDzbcDriverTripService.cancelTrip(id));
    }

    @Log(title = "司机任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblDzbcDriverTripBo bo) {
        return toAjax(tblDzbcDriverTripService.updateByBo(bo));
    }

    @Log(title = "司机任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(tblDzbcDriverTripService.deleteWithValidByIds(List.of(ids), true));
    }
}
