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.PlPsWarning;
import com.yuchen.saas.park.api.vo.request.PlPsWarningReq;
import com.yuchen.saas.park.service.service.pldoor.PlPsWarningService;
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-07-31
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plPsWarning")
public class PlPsWarningController {

    private final PlPsWarningService plPsWarningService;

    /**
     * 智慧配送告警 新增或修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "智慧配送告警 新增或修改", notes = "智慧配送告警 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlPsWarning plPsWarning) {
        if(Func.isEmpty(plPsWarning)) {
            return R.error("智慧配送告警不能为空");
        }
        return R.data(plPsWarningService.saveOrUpdate(plPsWarning));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "智慧配送告警 详情", notes = "智慧配送告警详情")
    public R detail(Long id) {
        return R.data(plPsWarningService.getById(id));
    }

    /**
     * 根据主表id删除智慧配送告警
     * @param plPsWarning
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 智慧配送告警 删除", notes = "根据主表id 智慧配送告警 删除")
    public R removeId(@RequestBody PlPsWarning plPsWarning) {
        plPsWarningService.removeById(plPsWarning.getId());
        return R.success();
    }

    /**
     * 分页 智慧配送告警
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 智慧配送告警", notes = "分页 智慧配送告警")
    public R<NestPage<PlPsWarning>> pageByparam(PlPsWarningReq plPsWarningReq, Query query) {
        IPage<PlPsWarning> pages = plPsWarningService.pageByparam(plPsWarningReq, query);
        return  R.data(NestPage.of(pages));
    }

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