package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.RmRegisterMsgRemote;
import com.ruoyi.system.api.domain.RmResourceRegistrationRemote;
import com.ruoyi.system.domain.RmResourceRegistration;
import com.ruoyi.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 gyt
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/registration")
public class RmResourceRegistrationController extends BaseController
{
    @Autowired
    private IRmResourceRegistrationService rmResourceRegistrationService;

    /**
     * 查询资源注册列表
     */
    @RequiresPermissions("system:registration:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(rmResourceRegistration.getPageNum());
        pageDomain.setPageSize(rmResourceRegistration.getPageSize());
        startPage(pageDomain);
        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
        return getDataTable(list);
    }

    /**
     * 导出资源注册列表
     */
    @RequiresPermissions("system:registration:export")
    @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, "资源注册数据");
    }

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

    /**
     * 新增资源注册
     */
    @RequiresPermissions("system:registration:add")
    @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);
    }

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

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

    /**
     * 查询所有资源名称（包含设备使用）
     * @return 资源注册集合
     */
    @RequiresPermissions("system:group:list")
    @GetMapping("/selectAllResourceName")
    public List<Map> selectAllResourceName()
    {
        List<Map> list = rmResourceRegistrationService.selectAllResourceName();
        return list;
    }
    /**
     * 查询所有资源名称
     * @return 资源注册集合
     */
    @RequiresPermissions("system:group:list")
    @PostMapping("/selectAllResourceNameByType")
    public List<Map> selectAllResourceNameByType(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        List<Map> list = rmResourceRegistrationService.selectAllResourceNameByType(rmResourceRegistration);
        return list;
    }

    /**
     * 检测到服务器离线，修改状态为离线
     * @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());
    }
    /**
     * 查询资源注册列表
     */
    @RequiresPermissions("system:registration:list")
    @PostMapping("/getRegistList")
    public AjaxResult getRegistList(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        List<RmResourceRegistration> list = rmResourceRegistrationService.selectRmResourceRegistrationList(rmResourceRegistration);
        return success(list);
    }

    /**
     * 自动注册服务器
     * @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
     */
    @RequiresPermissions("system:registration:update")
    @PostMapping("/bindBusinessPubulicIp")
    public AjaxResult bindBusinessPubulicIp(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        int rows = rmResourceRegistrationService.bindBusinessPublicIp(rmResourceRegistration);
        return toAjax(rows);
    }
    /**
     * 查询所有逻辑标识节点
     * @return 资源注册集合
     */
    @RequiresPermissions("system:registration:list")
    @GetMapping("/getAllLogicalNodeId")
    public List<Map> getAllLogicalNodeId()
    {
        List<Map> list = rmResourceRegistrationService.getAllLogicalNodeId();
        return list;
    }
    /**
     * 绑定业务名称
     * @return 资源注册集合
     */
    @RequiresPermissions("system:registration:list")
    @PostMapping("/bindBusinessByClientIds")
    public AjaxResult bindBusinessByClientIds(@RequestBody RmResourceRegistration rmResourceRegistration)
    {
        int rows =  rmResourceRegistrationService.bindBusinessByClientIds(rmResourceRegistration);
        return toAjax(rows);
    }

}
