/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.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.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.entity.Plot;
import com.tofly.xrtygis.entity.Valveandplot;
import com.tofly.xrtygis.service.PlotService;
import com.tofly.xrtygis.service.ValveandplotService;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 小区配置管理表
 *
 * @author admin
 * @date Thu Mar 17 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/plot")
@Api(tags = "小区配置管理表接口")
public class PlotController {

    private final PlotService plotService;
    private final ValveandplotService valveandplotService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param plotName 小区配置管理表
     * @return
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParam(name = "plotName",value = "小区名称，不传分页查询，传模糊查询",dataType = "string")
    public ResultRespone getPlotPage(Page page, String plotName) {
       return ResultRespone.success(plotService.page(page,Wrappers.<Plot>lambdaQuery()
               .like(!StringUtils.isEmpty(plotName),Plot::getName,plotName)));
    }

    /**
     * 获取小区
     */
    @GetMapping("/getPlot")
    @ApiOperation(value = "获取小区信息")
    @ApiImplicitParam(name = "plotName",value = "小区名称，不传全部查询，传模糊查询",dataType = "string")
    public ResultRespone getPlot(String plotName) {
        return ResultRespone.success(plotService.list(Wrappers.<Plot>lambdaQuery()
                .like(!StringUtils.isEmpty(plotName),Plot::getName,plotName)));
    }


    /**
     * 新增小区配置管理表
     *
     * @param plot 小区配置管理表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增小区配置管理表")
    @ApiOperation(value = "新增小区配置管理表")
    @PostMapping("/save")
    public ResultRespone save(@RequestBody Plot plot) {
        Plot plotOne = plotService.getOne(Wrappers.<Plot>lambdaQuery().eq(Plot::getName, plot.getName()));
        if (plotOne != null){
            throw new ToflyDeniedException("已存在" + plot.getName() + "小区");
        }
        return ResultRespone.success(plotService.save(plot));
    }

    /**
     * 修改小区配置管理表
     *
     * @param plot 小区配置管理表
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改小区配置管理表")
    @ApiOperation(value = "修改小区配置管理表")
    @PutMapping("/update")
    public ResultRespone updateById(@RequestBody Plot plot) {
        return ResultRespone.success(plotService.updateById(plot));
    }

    /**
     * 通过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) {

        if (StringUtils.isEmpty(ids)){
            throw new ToflyDeniedException("传入id为空");
        }
        List<String> idList = Arrays.asList(ids.split(","));

        List<Valveandplot> valveandplotList = valveandplotService.list(Wrappers.<Valveandplot>lambdaQuery()
                .in(Valveandplot::getPlotId, idList).select(Valveandplot::getValveId));

        if (!valveandplotList.isEmpty()){
            String valve = valveandplotList.stream().map(Valveandplot::getValveId).collect(Collectors.joining(","));
            throw new ToflyDeniedException(valve + "已绑定小区");
        }

        return ResultRespone.success(plotService.removeByIds(idList));
    }
}
