package com.huimv.env.admin.controller;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.env.admin.common.utils.Result;
import com.huimv.env.admin.common.utils.ResultCode;
import com.huimv.env.admin.entity.Terminal;
import com.huimv.env.admin.entity.vo.*;
import com.huimv.env.admin.entity.Gateway;
import com.huimv.env.admin.service.IGatewayService;
import com.huimv.env.admin.service.ITerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
* 采集网关 前端控制器
*
*
* @author newspaper
* @since 2024-05-07
*/
@RestController
@CrossOrigin
@RequestMapping("/gateway")
public class GatewayController {
@Autowired
private IGatewayService gatewayService;
@Autowired
private ITerminalService terminalService;
@PostMapping("/add")
public Result add(@RequestBody GateWayAddParam gateWayAddParam){
List gatewayList = gatewayService.list(new QueryWrapper().lambda().eq(Gateway::getDeviceCode, gateWayAddParam.getDeviceCode()));
if (ObjectUtil.isEmpty(gatewayList)){
return new Result(10001,"网关编号已存在",false);
}
Gateway gateway = new Gateway();
gateway.setDeviceCode(gateWayAddParam.getDeviceCode());
gateway.setLocationId(gateWayAddParam.getLocationId());
gateway.setType(gateWayAddParam.getType());
gateway.setFarmId(gateWayAddParam.getFarmId());
gatewayService.save(gateway);
return Result.SUCCESS();
}
@PostMapping("/page")
public Result page(@RequestBody GateWayPageParam gateWayPageParam){
Page page = new Page<>(gateWayPageParam.getPageNum(),gateWayPageParam.getPageSize());
Page gatewayPage = gatewayService.page(page,new QueryWrapper().lambda()
.eq(Gateway::getFarmId,gateWayPageParam.getFarmId())
.like(ObjectUtil.isNotEmpty(gateWayPageParam.getDeviceCode()),Gateway::getDeviceCode,gateWayPageParam.getDeviceCode()));
return new Result(ResultCode.SUCCESS,gatewayPage);
}
@PostMapping("/list")
public Result list(@RequestBody GateWayListParam gateWayListParam){
List gatewayList = gatewayService.list(new QueryWrapper().lambda().eq(ObjectUtil.isNotEmpty(gateWayListParam.getFarmId()), Gateway::getFarmId, gateWayListParam.getFarmId()));
return new Result(ResultCode.SUCCESS,gatewayList);
}
@PostMapping("/update")
public Result update(@RequestBody GateWayUpdateParam gateWayUpdateParam){
Gateway gateway = gatewayService.getById(gateWayUpdateParam.getId());
List gatewayList = gatewayService.list(new QueryWrapper().lambda().eq(Gateway::getDeviceCode, gateWayUpdateParam.getDeviceCode()).ne(Gateway::getId,gateWayUpdateParam.getId()));
if (ObjectUtil.isEmpty(gatewayList)){
return new Result(10001,"网关编号已存在",false);
}
gateway.setDeviceCode(gateWayUpdateParam.getDeviceCode());
gateway.setLocationId(gateWayUpdateParam.getLocationId());
gateway.setType(gateWayUpdateParam.getType());
gateway.setFarmId(gateWayUpdateParam.getFarmId());
gatewayService.updateById(gateway);
return Result.SUCCESS();
}
@PostMapping("/delete")
public Result delete(@RequestBody GateWayDeleteParam gateWayDeleteParam){
List terminalList = terminalService.list(new QueryWrapper().lambda().eq(Terminal::getGatewayId, gateWayDeleteParam.getId()));
if (ObjectUtil.isEmpty(terminalList)){
return new Result(10001,"网关存在下属终端",false);
}
gatewayService.removeById(gateWayDeleteParam.getId());
return Result.SUCCESS();
}
}