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.Gateway;
import com.huimv.env.admin.entity.TerminalPort;
import com.huimv.env.admin.entity.Terminal;
import com.huimv.env.admin.entity.vo.*;
import com.huimv.env.admin.mapper.TerminalMapper;
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.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 监测终端 前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2024-05-07
 */
@RestController
@CrossOrigin
@RequestMapping("/terminal")
public class TerminalController {

    @Autowired
    private IGatewayService gatewayService;
    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private ITerminalPortService portService;
    @Autowired
    private TerminalMapper terminalMapper;

    @PostMapping("/add")
    @Transactional
    public Result add(@RequestBody TerminalAddParam terminalAddParam){
        List<Terminal> terminalList = terminalService.list(new QueryWrapper<Terminal>().lambda()
                .eq(Terminal::getDeviceCode, terminalAddParam.getDeviceCode()));
        if (ObjectUtil.isNotEmpty(terminalList)){
            return new Result(10001,"终端编号已存在",false);
        }
        Integer gatewayId = terminalAddParam.getGatewayId();
        Gateway gateway = gatewayService.getById(gatewayId);

        Integer maxOrder = terminalMapper.getMaxOrder(gatewayId);
        maxOrder += 1;
        Terminal terminal = new Terminal();
        terminal.setDeviceCode(gateway.getDeviceCode()+maxOrder);
        terminal.setDeviceName(terminalAddParam.getDeviceName());
        terminal.setGatewayId(gatewayId);
        terminal.setOrders(maxOrder);
        terminal.setType(terminalAddParam.getType());
        terminal.setFarmId(terminalAddParam.getFarmId());
        terminalService.save(terminal);

        gateway.setTerminalNum(gateway.getTerminalNum()+1);
        gatewayService.updateById(gateway);

        List portList = new ArrayList();
        for (int i = 1; i < 9; i++){
            TerminalPort terminalPort = new TerminalPort();
            terminalPort.setDeviceCode(gateway.getDeviceCode()+terminal.getDeviceCode()+i);
            terminalPort.setTerminalId(terminal.getId());
            terminalPort.setFarmId(terminal.getFarmId());
            portList.add(terminalPort);
        }
        portService.saveBatch(portList);
        return Result.SUCCESS();
    }

    @PostMapping("/list")
    public Result list(@RequestBody TerminalListParam terminalListParam){
        List<Terminal> terminalList = terminalService.list(new QueryWrapper<Terminal>().lambda()
                .eq(ObjectUtil.isNotEmpty(terminalListParam.getGatewayId()), Terminal::getGatewayId, terminalListParam.getGatewayId()));
        return new Result(ResultCode.SUCCESS,terminalList);

    }

    @PostMapping("/listByLocationId")
    public Result listByLocationId(@RequestBody TerminalListByLocationIdParam terminalListByLocationIdParam){
        Page<TerminalPageResult> page = new Page<>(terminalListByLocationIdParam.getPageNum(),terminalListByLocationIdParam.getPageSize());
        Page<TerminalPageResult> resultPage = terminalMapper.listByLocationId(page,terminalListByLocationIdParam.getLocationId());
        return new Result(ResultCode.SUCCESS,resultPage);
    }

    @PostMapping("/page")
    public Result page(@RequestBody TerminalPageParam terminalPageParam){
        Page<Terminal> page = new Page<>(terminalPageParam.getPageNum(),terminalPageParam.getPageSize());
        Page<Terminal> terminalPage = terminalService.page(page,new QueryWrapper<Terminal>().lambda()
                .eq(Terminal::getFarmId,terminalPageParam.getFarmId())
                .eq(ObjectUtil.isNotEmpty(terminalPageParam.getGatewayId()),Terminal::getGatewayId,terminalPageParam.getGatewayId()));
        return new Result(ResultCode.SUCCESS,terminalPage);
    }

    @PostMapping("/update")
    public Result update(@RequestBody TerminalUpdateParam terminalUpdateParam){
        List<Terminal> terminalList = terminalService.list(new QueryWrapper<Terminal>().lambda()
                .eq(Terminal::getDeviceCode, terminalUpdateParam.getDeviceCode())
                .ne(Terminal::getId,terminalUpdateParam.getId()));
        if (ObjectUtil.isNotEmpty(terminalList)){
            return new Result(10001,"终端编号已存在",false);
        }
        Terminal terminal = terminalService.getById(terminalUpdateParam.getId());
        terminal.setDeviceCode(terminalUpdateParam.getDeviceCode());
        terminal.setDeviceName(terminalUpdateParam.getDeviceName());
        terminal.setGatewayId(terminalUpdateParam.getGatewayId());
        terminal.setFloorId(terminalUpdateParam.getFloorId());
        terminal.setType(terminalUpdateParam.getType());
        terminal.setFarmId(terminalUpdateParam.getFarmId());
        terminalService.updateById(terminal);
        return Result.SUCCESS();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody TerminalDeleteParam terminalDeleteParam){
        List<TerminalPort> terminalList = portService.list(new QueryWrapper<TerminalPort>().lambda()
                .eq(TerminalPort::getTerminalId, terminalDeleteParam.getId()).isNotNull(TerminalPort::getLocationId));
        if (ObjectUtil.isNotEmpty(terminalList)){
            return new Result(10001,"下属端口已绑定住户",false);
        }
        terminalService.removeById(terminalDeleteParam.getId());
        return Result.SUCCESS();
    }


}
