package cn.iocoder.yudao.module.asset.controller.admin.delivery;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.asset.controller.admin.delivery.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.delivery.DeliveryTaskDO;
import cn.iocoder.yudao.module.asset.service.delivery.DeliveryTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 交车任务")
@RestController
@RequestMapping("/asset/delivery-task")
@Validated
public class DeliveryTaskController {

    @Resource
    private DeliveryTaskService deliveryTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建交车任务")
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:create')")
    public CommonResult<Long> createDeliveryTask(@Valid @RequestBody DeliveryTaskSaveReqVO createReqVO) {
        return success(deliveryTaskService.createDeliveryTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新交车任务")
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:update')")
    public CommonResult<Boolean> updateDeliveryTask(@Valid @RequestBody DeliveryTaskSaveReqVO updateReqVO) {
        deliveryTaskService.updateDeliveryTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除交车任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:delete')")
    public CommonResult<Boolean> deleteDeliveryTask(@RequestParam("id") Long id) {
        deliveryTaskService.deleteDeliveryTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得交车任务详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:query')")
    public CommonResult<DeliveryTaskRespVO> getDeliveryTask(@RequestParam("id") Long id) {
        return success(deliveryTaskService.getDeliveryTaskDetail(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得交车任务分页")
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:query')")
    public CommonResult<PageResult<DeliveryTaskRespVO>> getDeliveryTaskPage(@Valid DeliveryTaskPageReqVO pageReqVO) {
        PageResult<DeliveryTaskDO> pageResult = deliveryTaskService.getDeliveryTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryTaskRespVO.class));
    }

    @GetMapping("/contract-page")
    @Operation(summary = "获得交车任务按合同分组分页")
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:query')")
    public CommonResult<PageResult<DeliveryTaskContractGroupRespVO>> getDeliveryTaskContractPage(
            @Valid DeliveryTaskPageReqVO pageReqVO) {
        return success(deliveryTaskService.getDeliveryTaskContractPage(pageReqVO));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得交车任务精简列表", description = "用于下拉选择")
    public CommonResult<java.util.List<DeliveryTaskSimpleRespVO>> getDeliveryTaskSimpleList() {
        java.util.List<DeliveryTaskDO> list = deliveryTaskService.getDeliveryTaskSimpleList();
        return success(BeanUtils.toBean(list, DeliveryTaskSimpleRespVO.class));
    }

    @PutMapping("/suspend")
    @Operation(summary = "挂起交车任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:update')")
    public CommonResult<Boolean> suspendDeliveryTask(@RequestParam("id") Long id) {
        deliveryTaskService.suspendDeliveryTask(id);
        return success(true);
    }

    @PutMapping("/activate")
    @Operation(summary = "激活交车任务")
    @PreAuthorize("@ss.hasPermission('asset:delivery-task:update')")
    public CommonResult<Boolean> activateDeliveryTask(@Valid @RequestBody DeliveryTaskActivateReqVO reqVO) {
        deliveryTaskService.activateDeliveryTask(reqVO);
        return success(true);
    }

}
