/*
 * 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.DispatchDto;
import com.tofly.dispatch.dto.UpdateDispatchDto;
import com.tofly.dispatch.entity.Dispatch;
import com.tofly.dispatch.service.DispatchService;
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.List;

/**
 * 供水调度（日常->名称->详情；开关阀->方案->名称->详情）
 *
 * @author xqf
 * @date Mon Mar 14 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dispatch")
@Api(tags="调度方案基础数据-接口")
public class DispatchController {

    private final  DispatchService dispatchService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param dispatch 供水调度（日常->名称->详情；开关阀->方案->名称->详情）
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getDispatchPage(Page page, @ModelAttribute Dispatch dispatch) {
      return ResultRespone.success(dispatchService.page(page, Wrappers.query(dispatch).lambda()
              .eq(Dispatch::getIsDel, FlowConstant.SHORT_ZERO)));
    }


    /**
     * 通过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(dispatchService.getById(id));
    }

    /**
     * 新增供水调度（日常->名称->详情；开关阀->方案->名称->详情）
     * @param dto 供水调度（日常->名称->详情；开关阀->方案->名称->详情）
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增调度方案" )
    @ApiOperation(value = "新增调度方案")
    @PostMapping
    public ResultRespone save(@RequestBody @Valid DispatchDto dto) {
        Object obj = dispatchService.saveDispatch(dto);
        if(obj instanceof Long){
            return ResultRespone.success(obj,FlowConstant.SUCCESS);
        }else {
            return ResultRespone.failed(String.valueOf(obj),FlowConstant.FAILED);
        }
    }

    /**
     * 修改供水调度（日常->名称->详情；开关阀->方案->名称->详情）
     * @param dto 供水调度（日常->名称->详情；开关阀->方案->名称->详情）
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改调度方案" )
    @ApiOperation(value = "修改调度方案")
    @PutMapping
    public ResultRespone updateById(@RequestBody @Valid UpdateDispatchDto dto) {
        Object obj = dispatchService.updateDispatchById(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) {
        boolean up= dispatchService.removeUpdateById(id);
        if(up){
            return ResultRespone.success(true,FlowConstant.SUCCESS);
        }else {
            return ResultRespone.failed(false,FlowConstant.FAILED);
        }
    }

    /**
     * 通过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) {
        Object obj = dispatchService.removeUpdateByIds(ids);
        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));
        }
    }

    /**
     * 根据类型返回方案树
     * @param types 类型id 多个用逗号分隔
     * @return ResultRespone
     */
    @ToFlyAppLog(title="根据类型返回方案树" )
    @ApiOperation(value = "根据类型返回方案树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "types", value = "类型id 多个用逗号分隔", required = false, dataType = "String")
    })
    @GetMapping("/getDispatchTree" )
    public ResultRespone getDispatchTree(String types) {
        Object obj = dispatchService.getDispatchTree(types);
        if(obj instanceof List){
            return ResultRespone.success(obj,FlowConstant.SUCCESS);
        }else {
            return ResultRespone.failed(false,FlowConstant.FAILED);
        }
    }

}
