package com.yuchen.saas.park.service.controller.pldoor;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.pldoor.PlSpPatrolTask;
import com.yuchen.saas.park.api.vo.request.PlPsTaskReq;
import com.yuchen.saas.park.api.vo.request.PlSpPatrolTaskReq;
import com.yuchen.saas.park.service.service.pldoor.PlSpPatrolTaskService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 巡逻任务 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-08-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plSpPatrolTask")
public class PlSpPatrolTaskController {

    private final PlSpPatrolTaskService plSpPatrolTaskService;

    /**
     * 巡逻任务 新增或修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "巡逻任务 新增或修改", notes = "巡逻任务 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlSpPatrolTask plSpPatrolTask) {
        if(Func.isEmpty(plSpPatrolTask)) {
            return R.error("巡逻任务不能为空");
        }
        return R.data(plSpPatrolTaskService.saveOrUpdate(plSpPatrolTask));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "巡逻任务 详情", notes = "巡逻任务详情")
    public R detail(Long id) {
        return R.data(plSpPatrolTaskService.getById(id));
    }

    /**
     * 根据主表id删除巡逻任务
     * @param plSpPatrolTask
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 巡逻任务 删除", notes = "根据主表id 巡逻任务 删除")
    public R removeId(@RequestBody PlSpPatrolTask plSpPatrolTask) {
        plSpPatrolTaskService.removeById(plSpPatrolTask.getId());
        return R.success();
    }

    /**
     * 分页 巡逻任务
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 巡逻任务", notes = "分页 巡逻任务")
    public R<NestPage<PlSpPatrolTask>> pageByparam(PlSpPatrolTaskReq plSpPatrolTaskReq, Query query) {
        IPage<PlSpPatrolTask> pages = plSpPatrolTaskService.pageByparam(plSpPatrolTaskReq, query);
        return  R.data(NestPage.of(pages));
    }

    @PostMapping("/updateOrderState")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改启用状态", notes = "修改启用状态")
    public R updateOrderState(@Valid @RequestBody PlSpPatrolTask plSpPatrolTask) {
        if(Func.isEmpty(plSpPatrolTask) || Func.isEmpty(plSpPatrolTask.getTaskState())) {
            return R.error("启用状态不能为空");
        }
        plSpPatrolTask.setId(plSpPatrolTask.getId());
        plSpPatrolTask.setTaskState(plSpPatrolTask.getTaskState());
        return R.data(plSpPatrolTaskService.updateById(plSpPatrolTask));
    }


}
