package com.yfd.platform.modules.robotapi.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.annotation.Log;
import com.yfd.platform.component.nettyclient.BootNettyClientChannel;
import com.yfd.platform.component.nettyclient.BootNettyClientChannelCache;
import com.yfd.platform.component.nettyclient.HeartBeatTask;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.basedata.domain.AlgorithmModel;
import com.yfd.platform.modules.robotapi.domain.PlatformParentSystem;
import com.yfd.platform.modules.robotapi.service.IPlatformParentSystemService;
import com.yfd.platform.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2024-06-24
 */
@RestController
@RequestMapping("/robotapi/platform-parent-system")
@Api(value = "PlatformParentSystemController", tags = "上级平台")
public class PlatformParentSystemController {

    @Resource
    private HeartBeatTask heartBeatTask;
    @Resource
    private IPlatformParentSystemService platformParentSystemService;

    @ApiOperation("获取上级平台信息")
    @GetMapping("/getParentSystemPage")
    public ResponseResult getParentSystemPage(Page<Map<String, Object>> page, String parentName) {
        LambdaQueryWrapper<PlatformParentSystem> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(parentName)) {
            queryWrapper.like(PlatformParentSystem::getParentName, parentName);
        }
        Page<Map<String, Object>> mapPage = platformParentSystemService.pageMaps(page, queryWrapper);
        return ResponseResult.successData(mapPage);
    }

    @Log(module = "上级平台", value = "新增或修改上级参数", type = "1")
    @PostMapping("/addOrUpdateParentSystem")
    @ApiOperation("新增或修改算法模型")
    public ResponseResult addOrUpdateParentSystem(@RequestBody PlatformParentSystem platformParentSystem) {
        String isFlag = platformParentSystem.getIsFlag();
        if (StrUtil.isBlank(isFlag)) {
            platformParentSystem.setIsFlag("0");
        }
        String isEnable = platformParentSystem.getIsEnable();
        if (StrUtil.isBlank(isEnable)) {
            platformParentSystem.setIsEnable("0");
        }
        if (StrUtil.isBlank(platformParentSystem.getId())) {
            platformParentSystem.setCreateDate(DateUtil.now());
        }
        platformParentSystemService.saveOrUpdate(platformParentSystem);
        return ResponseResult.success();
    }

    @Log(module = "上级平台", value = "删除上级列表", type = "1")
    @PostMapping("/deleteParentSystem")
    @ApiOperation("删除上级列表")
    public ResponseResult deleteParentSystem(String ids) {
        List<String> idList = StrUtil.split(ids, ",");
        platformParentSystemService.removeByIds(idList);
        return ResponseResult.success();
    }

    @PostMapping("/stopSendHeartBeat")
    @ApiOperation("停止上级系统心跳发送")
    public ResponseResult stopSendHeartBeat(String code) throws InterruptedException {
        heartBeatTask.stop("HeartBeatTask");
        BootNettyClientChannel bootNettyClientChannel = BootNettyClientChannelCache.channelMapCache.get(code);
        if (bootNettyClientChannel != null) {
            bootNettyClientChannel.getChannel().close();
            BootNettyClientChannelCache.channelMapCache.remove(code);
        }
        //        bootNettyClientChannel.getChannel().closeFuture()
        platformParentSystemService.update(new LambdaUpdateWrapper<PlatformParentSystem>().eq(PlatformParentSystem::getChildCode, code).set(PlatformParentSystem::getIsFlag, "0"));
        return ResponseResult.success();
    }

    @PostMapping("/sendRegisterToSuper")
    @ApiOperation("发送注册信息到上级系统")
    public ResponseResult sendRegisterToSuper(String id) {
        platformParentSystemService.sendRegisterToSuper(id);
        return ResponseResult.success();
    }

}
