/*
 * 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.xrtygis.entity.Valveandplot;
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.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 阀门-小区关联表
 *
 * @author admin
 * @date Thu Mar 17 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/valveandplot")
@Api(tags = "阀门-小区关联表接口")
public class ValveandplotController {

    private final ValveandplotService valveandplotService;

    /**
     * 通过id查询阀门-小区关联表
     *
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/byValveCode")
    @ApiOperation(value = "通过阀门编号查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone getByValveId(Page page, String id) {
        List<String> valveIdList = Arrays.asList(id.split(","));
        /*if (valveIdList.size() == 1) {
            return ResultRespone.success(valveandplotService.page(page, Wrappers.<Valveandplot>lambdaQuery().in(Valveandplot::getValveId, valveIdList)));
        } else {
            ArrayList<Valveandplot> result = valveandplotService.list(Wrappers.<Valveandplot>lambdaQuery()
                    .in(Valveandplot::getValveId, valveIdList))
                    .stream().collect(Collectors.collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(Valveandplot::getPlotName))),
                            ArrayList::new));
        }*/

        if (page.getSize() > 0) {
            Page result = valveandplotService.page(page, Wrappers.<Valveandplot>lambdaQuery()
                    .in(Valveandplot::getValveId, valveIdList));
            return ResultRespone.success(result);
        } else {
            List<Valveandplot> result = valveandplotService.list(Wrappers.<Valveandplot>lambdaQuery()
                    .in(Valveandplot::getValveId, valveIdList));
            return ResultRespone.success(result);
        }

    }

    @GetMapping("/valveIdGetPlot")
    @ApiOperation("阀门id获取小区")
    public ResultRespone valveIdGetPlot(String id){
        return ResultRespone.success(valveandplotService.valveIdGetPlot(id));
    }

    /**
     * 新增阀门-小区关联
     */
    @ApiOperation(value = "阀门-小区绑定")
    @PostMapping("bindValveAndPlot")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "valveCode", value = "阀门编号，多个','分隔", required = true, dataType = "string"),
            @ApiImplicitParam(name = "plotName", value = "小区名称", required = true, dataType = "string")
    })
    public ResultRespone<Boolean> bindValveAndPlot(String valveCode, String plotName) {
        return ResultRespone.success(valveandplotService.bindValveAndPlot(valveCode, plotName));
    }

    /**
     * 修改阀门-小区关联表
     *
     * @param valveandplot 阀门-小区关联表
     * @return ResultRespone
     */
    @ApiOperation(value = "修改阀门-小区关联表")
    @PutMapping
    public ResultRespone<Boolean> updateById(@RequestBody Valveandplot valveandplot) {
        return ResultRespone.success(valveandplotService.updateById(valveandplot));
    }

    /**
     * 解绑阀门-小区关联
     */
    @ApiOperation(value = "阀门-小区解绑")
    @DeleteMapping("removeValveAndPlot")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "valveId", value = "阀门编号，多个','分隔", required = true, dataType = "string"),
            @ApiImplicitParam(name = "plotId", value = "小区id,多个‘,’分隔", dataType = "string")
    })
    public ResultRespone<Boolean> removeValveAndPlot(String valveId, String plotId) {
        return ResultRespone.success(valveandplotService.removeValveAndPlot(valveId, plotId));
    }
}
