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.TerminalPort;
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.ITerminalPortService;
import com.huimv.env.admin.service.ITerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 采集网关 前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2024-05-07
 */
@RestController
@CrossOrigin
@RequestMapping("/gateway")
public class GatewayController {
    @Autowired
    private IGatewayService gatewayService;
    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private ITerminalPortService portService;

    @PostMapping("/add")
    @Transactional
    public Result add(@RequestBody GateWayAddParam gateWayAddParam) {
        String deviceCode = gateWayAddParam.getDeviceCode();
        List<Gateway> gatewayList = gatewayService.list(new QueryWrapper<Gateway>().lambda().eq(Gateway::getDeviceCode, deviceCode));
        if (ObjectUtil.isNotEmpty(gatewayList)) {
            return new Result(10001, "网关编号已存在", false);
        }
        Gateway gateway = new Gateway();
        gateway.setDeviceCode(deviceCode);
        gateway.setDeviceName(gateWayAddParam.getDeviceName());
        gateway.setLocationId(gateWayAddParam.getLocationId());
        gateway.setType(gateWayAddParam.getType());
        gateway.setFarmId(gateWayAddParam.getFarmId());
        gateway.setVersion(String.valueOf(gateWayAddParam.getVersion()));
        gateway.setTerminalNum(gateWayAddParam.getTerminalNum());
        gatewayService.save(gateway);


        for (int i = 1; i <= gateWayAddParam.getTerminalNum(); i++) {
            Terminal terminal = new Terminal();
            terminal.setDeviceCode(deviceCode + i);
            terminal.setGatewayId(gateway.getId());
            terminal.setBuildId(gateway.getLocationId());
            terminal.setFarmId(gateWayAddParam.getFarmId());
            terminal.setOrders(i);
            terminalService.save(terminal);
            List portList = new ArrayList();
            if (gateWayAddParam.getVersion() == 2) {
                TerminalPort terminalPort = new TerminalPort();
                terminalPort.setDeviceCode(terminal.getDeviceCode() + 1);
                terminalPort.setTerminalId(terminal.getId());
                terminalPort.setFarmId(terminal.getFarmId());
                portList.add(terminalPort);
            } else {
                for (int j = 1; j < 9; j++) {
                    TerminalPort terminalPort = new TerminalPort();
                    terminalPort.setDeviceCode(terminal.getDeviceCode() + j);
                    terminalPort.setTerminalId(terminal.getId());
                    terminalPort.setFarmId(terminal.getFarmId());
                    portList.add(terminalPort);
                }
            }
            portService.saveBatch(portList);
        }
        return Result.SUCCESS();
    }

    @PostMapping("/page")
    public Result page(@RequestBody GateWayPageParam gateWayPageParam) {
        Page<Gateway> page = new Page<>(gateWayPageParam.getPageNum(), gateWayPageParam.getPageSize());
        Page<Gateway> gatewayPage = gatewayService.page(page, new QueryWrapper<Gateway>().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<Gateway> gatewayList = gatewayService.list(new QueryWrapper<Gateway>().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<Gateway> gatewayList = gatewayService.list(new QueryWrapper<Gateway>().lambda().eq(Gateway::getDeviceCode, gateWayUpdateParam.getDeviceCode()).ne(Gateway::getId, gateWayUpdateParam.getId()));
        if (ObjectUtil.isNotEmpty(gatewayList)) {
            return new Result(10001, "网关编号已存在", false);
        }
        gateway.setDeviceCode(gateWayUpdateParam.getDeviceCode());
        gateway.setDeviceName(gateWayUpdateParam.getDeviceName());
        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<Terminal> terminalList = terminalService.list(new QueryWrapper<Terminal>().lambda().eq(Terminal::getGatewayId, gateWayDeleteParam.getId()));
        if (ObjectUtil.isNotEmpty(terminalList)) {
            return new Result(10001, "网关存在下属终端", false);
        }
        gatewayService.removeById(gateWayDeleteParam.getId());
        return Result.SUCCESS();
    }


}
