package com.xydl.cac.controller;

import com.xydl.cac.entity.Bdz;
import com.xydl.cac.entity.IcdConfigTypeInst;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.BindDetail;
import com.xydl.cac.model.BindingModel;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.ParamBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(tags = {"参数绑定相关接口"})
@RequestMapping("parambind")
@Slf4j
public class ParamBindController extends BasicController {

    @Resource
    ParamBindService bindService;

    @GetMapping("getTree")
    @ApiOperation("查询树")
    public Response<List<Bdz>> getTree() throws Exception {
        List<Bdz> result = bindService.getTree(true);
        return Response.success(result);
    }

    @GetMapping("instList")
    @ApiOperation("查询逻辑设备实例列表")
    public Response<List<IcdConfigTypeInst>> instList(@Validated @NotNull(message = "iedName不能为空!") @Param("iedName") String iedName) throws Exception {
        List<IcdConfigTypeInst> result = bindService.instList(iedName);
        return Response.success(result);
    }

    @PostMapping("preview")
    @ApiOperation("预览")
    public Response preview(@Validated @RequestBody BindingModel item) throws Exception {
        return bindService.preview(item);
    }

    @PostMapping("bind")
    @ApiOperation("绑定")
    public Response<String> bind(@Validated @RequestBody BindingModel item) throws Exception {
        bindService.bind(item);
        return Response.success("OK");
    }

    @PostMapping("unbind")
    @ApiOperation("解绑")
    public Response<String> unbind(@Validated @NotNull(message = "sensorId不能为空!") @Param("sensorId") Integer sensorId) throws Exception {
        if (sensorId == null) {
            throw new BusinessException("sensorId不能为空!");
        }
        bindService.unbind(sensorId);
        return Response.success("OK");
    }

    @GetMapping("getBind")
    @ApiOperation("查询绑定信息")
    public Response<BindDetail> getBind(@Validated @NotNull(message = "sensorId不能为空!") @Param("sensorId") Integer sensorId) throws Exception {
        if (sensorId == null) {
            throw new BusinessException("sensorId不能为空!");
        }
        BindDetail result = bindService.getBind(sensorId);
        return Response.success(result);
    }

    @PostMapping("updateParamindex")
    @ApiOperation("更新rptparamindex表的相关绑定数据")
    public Response<String> updateParamindex() throws Exception {
        String msg = bindService.updateParamindex();
        return Response.success(msg);
    }

    @PostMapping("clearAll")
    @ApiOperation("清空所有绑定")
    public Response<String> clearAll() throws Exception {
        bindService.clearAll();
        return Response.success("OK");
    }

}
