/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.dispatch.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.dispatch.common.FlowConstant;
import com.tofly.dispatch.dto.BatchDispatchSchemeDto;
import com.tofly.dispatch.entity.DispatchSchemeInfo;
import com.tofly.dispatch.service.DispatchSchemeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
/**
 * 调度方案执行设备
 *
 * @author xqf
 * @date Mon Mar 14 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dispatchschemeinfo")
@Api(tags="调度方案执行设备-接口")
public class DispatchSchemeInfoController {

    private final  DispatchSchemeInfoService dispatchSchemeInfoService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param dispatchSchemeInfo 调度方案执行设备
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getDispatchSchemeInfoPage(Page page, @ModelAttribute DispatchSchemeInfo dispatchSchemeInfo) {
      return ResultRespone.success(dispatchSchemeInfoService.page(page, Wrappers.query(dispatchSchemeInfo).lambda()
              .eq(DispatchSchemeInfo::getIsDel, FlowConstant.SHORT_ZERO).orderByAsc(DispatchSchemeInfo::getId)));
    }


    /**
     * 通过id查询调度方案执行设备
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(dispatchSchemeInfoService.getById(id));
    }

    /**
     * 新增调度方案执行设备
     * @param dto 调度方案执行设备
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增调度方案执行设备" )
    @ApiOperation(value = "新增调度方案执行设备")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid BatchDispatchSchemeDto dto) {
        Object obj = dispatchSchemeInfoService.saveDispatchScheme(dto);
        if(obj instanceof Boolean){
            if((boolean) obj){
                return ResultRespone.success(true,FlowConstant.SUCCESS);
            }else {
                return ResultRespone.failed(false,FlowConstant.FAILED);
            }
        }else {
            return ResultRespone.failed(false,String.valueOf(obj));
        }
    }

    /**
     * 通过id删除调度方案执行设备
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除调度方案执行设备" )
    @ApiOperation(value = "通过ID删除调度方案执行设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(dispatchSchemeInfoService.removeById(id));
    }

    /**
     * 通过id批量删除调度方案执行设备
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除调度方案执行设备" )
    @ApiOperation(value = "通过ID批量删除调度方案执行设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(dispatchSchemeInfoService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
