package com.tongran.system.controller;

import com.tongran.common.core.domain.R;
import com.tongran.common.core.utils.poi.ExcelUtil;
import com.tongran.common.core.web.controller.BaseController;
import com.tongran.common.core.web.domain.AjaxResult;
import com.tongran.common.core.web.page.TableDataInfo;
import com.tongran.common.log.annotation.Log;
import com.tongran.common.log.enums.BusinessType;
import com.tongran.common.security.annotation.InnerAuth;
import com.tongran.common.security.annotation.RequiresPermissions;
import com.tongran.system.api.domain.RmRegisterMsgRemote;
import com.tongran.system.api.domain.RmResourceRegistrationRemote;
import com.tongran.system.domain.RmResourceRegistration;
import com.tongran.system.service.IRmResourceRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 资源注册Controller
 * 
 * @author tongran
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/registration")
@RequiresPermissions("system:registration")
public class RmResourceRegistrationController extends BaseController
{
    @Autowired
    private IRmResourceRegistrationService rmResourceRegistrationService;

    /**
     * 查询资源注册列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        Map<String, Object> resultMap = rmResourceRegistrationService.getRegistrationTableInfoList(rmResourceRegistration);
        if((rmResourceRegistration.getQueryParam() != null &&
                rmResourceRegistration.getQueryParam() != "") ||
                (rmResourceRegistration.getBandwidthResultSort() != null &&
                rmResourceRegistration.getBandwidthResultSort() != 3)){
            return getDataTable((List<RmResourceRegistration>) resultMap.get("list"), (int) resultMap.get("total"));
        }
        return getDataTable((List<RmResourceRegistration>) resultMap.get("list"));
    }

    /**
     * 导出资源注册列表
     */
    @Log(title = "资源注册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody RmResourceRegistration rmResourceRegistration)
    {
        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
        ExcelUtil<RmResourceRegistration> util = new ExcelUtil<RmResourceRegistration>(RmResourceRegistration.class);
        util.showColumn(rmResourceRegistration.getProperties());
        util.exportExcel(response, list, "资源注册数据");
    }

    /**
     * 获取资源注册详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rmResourceRegistrationService.selectRmResourceRegistrationById(id));
    }

    /**
     * 新增资源注册
     */
    @Log(title = "资源注册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        int rows = rmResourceRegistrationService.insertRmResourceRegistration(rmResourceRegistration);
        if(-1==rows){
            return AjaxResult.error(500,"硬件SN不可重复");
        }
        return toAjax(rows);
    }

    /**
     * 修改资源注册
     */
    @Log(title = "资源注册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.updateRmResourceRegistration(rmResourceRegistration));
    }

    /**
     * 资源注册-注册
     */
    @Log(title = "资源注册-注册", businessType = BusinessType.UPDATE)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.register(rmResourceRegistration));
    }


    /**
     * 检测到服务器离线，修改状态为离线
     * @param rmResourceRegistration
     * @return
     */
    @InnerAuth
    @PostMapping("/updateStatusByResource")
    public R<String> updateStatusByResource(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        R<String> rows = rmResourceRegistrationService.updateStatusByResource(rmResourceRegistration);
        return rows;
    }

    /**
     * 根据id查询资源信息
     * @param ids
     * @return
     */
    @PostMapping("/getRegistrationByIds")
    public R<List<RmResourceRegistration>> getRegistrationByIds(@RequestBody String[] ids)
    {
        List<RmResourceRegistration> list = rmResourceRegistrationService.getRegistrationByIds(ids);
        return R.ok(list);
    }
    /**
     * 查询资源注册列表
     */
    @InnerAuth
    @PostMapping("/getListByHardwareSn")
    public R<RmResourceRegistration> getListByHardwareSn(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
        if(list != null && !list.isEmpty()){
            return R.ok(list.get(0));
        }
        return R.ok(new RmResourceRegistration());
    }
    /**
     * 查询资源注册列表
     */
    @PostMapping("/getRegistList")
    public AjaxResult getRegistList(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
        return success(list);
    }
    @GetMapping("/getAlarmFlag")
    public AjaxResult getAlarmFlag()
    {
//        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(new RmResourceRegistration());
//        for (RmResourceRegistration item : list) {
//
//            boolean isAlarm = (item.getCpuUtil() != null && item.getCpuUtil().compareTo(BigDecimal.valueOf(80)) >= 0) ||
//                    (item.getMemUtil() != null && item.getMemUtil().compareTo(BigDecimal.valueOf(60)) >= 0);
//
//            if (isAlarm) {
//                return success(true); // 只要有一个告警就返回
//            }
//        }
        return success(false);
    }

    /**
     * 自动注册服务器
     * @param rmResourceRegistration mq接收的消息
     * @return
     */
    @PostMapping("/innerAddRegist")
    @InnerAuth
    public R<Integer> innerAddRegist(@RequestBody RmRegisterMsgRemote rmResourceRegistration)
    {
        int rows = rmResourceRegistrationService.innerAddRegist(rmResourceRegistration);
        return R.ok(rows);
    }
    /**
     * 添加节点标识
     * @param rmResourceRegistration mq接收的消息
     * @return
     */
    @PostMapping("/innerUpdateRegist")
    @InnerAuth
    public R<Integer> innerUpdateRegist(@RequestBody RmResourceRegistrationRemote rmResourceRegistration)
    {
        int rows = rmResourceRegistrationService.innerUpdateRegist(rmResourceRegistration);
        return R.ok(rows);
    }
    /**
     * 选择公网业务IP
     */
    @PostMapping("/bindBusinessPubulicIp")
    public AjaxResult bindBusinessPubulicIp(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        int rows = rmResourceRegistrationService.bindBusinessPublicIp(rmResourceRegistration);
        return toAjax(rows);
    }
    /**
     * 查询所有逻辑标识节点
     * @return 资源注册集合
     */
    @GetMapping("/getAllLogicalNodeId")
    public List<Map> getAllLogicalNodeId()
    {
        List<Map> list = rmResourceRegistrationService.getAllLogicalNodeId();
        return list;
    }
    /**
     * 绑定业务名称
     * @return 资源注册集合
     */
    @PostMapping("/bindBusinessByClientIds")
    public AjaxResult bindBusinessByClientIds(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        int rows =  rmResourceRegistrationService.bindBusinessByClientIds(rmResourceRegistration);
        return toAjax(rows);
    }
    /**
     * 根据clientId查询详情
     * @return 资源注册集合
     */
    @PostMapping("/getServerMsgByClientId")
    public AjaxResult getServerMsgByClientId(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
        if(list != null && !list.isEmpty()){
            return success(list.get(0));
        }
        return success(list);
    }
    /**
     * 添加标签
     */
    @Log(title = "添加服务器标签", businessType = BusinessType.UPDATE)
    @PostMapping("/addRemark")
    public AjaxResult addRemark(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.addRemark(rmResourceRegistration));
    }
    /**
     * 添加标签
     */
    @Log(title = "添加服务器标签", businessType = BusinessType.UPDATE)
    @PostMapping("/updateRemark")
    public AjaxResult updateRemark(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.updateRemark(rmResourceRegistration));
    }
    /**
     * 添加上报带宽值
     */
    @Log(title = "添加上报带宽值", businessType = BusinessType.UPDATE)
    @PostMapping("/addReportedBandwidth")
    public AjaxResult addReportedBandwidth(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.addReportedBandwidth(rmResourceRegistration));
    }
    /**
     * 取消闪烁
     */
    @Log(title = "取消闪烁", businessType = BusinessType.UPDATE)
    @PostMapping("/removeAlarmFlag")
    public AjaxResult removeAlarmFlag(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.removeAlarmFlag(rmResourceRegistration));
    }

    /**
     * 下架
     */
    @Log(title = "下架", businessType = BusinessType.UPDATE)
    @PostMapping("/removeServer")
    public AjaxResult removeServer(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        return toAjax(rmResourceRegistrationService.removeServer(rmResourceRegistration.getIds()));
    }

}
