package com.ruoyi.web.controller.record;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DataRecord;
import com.ruoyi.system.service.IDataRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 外链扫描情况记录Controller
 * 
 * @author ruoyi
 * @date 2024-09-29
 */
@Controller
@RequestMapping("/system/record")
public class DataRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private IDataRecordService dataRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询外链扫描情况记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataRecord dataRecord)
    {
        startPage();
        List<DataRecord> list = null;
        String domain = extractDomain(dataRecord.getUrl());
        dataRecord.setUrl(domain);
        if("外链".equals(dataRecord.getUrltype())){
            list = dataRecordService.selectWlDataRecordAllList(dataRecord);
        }else if("全部".equals(dataRecord.getUrltype())){
            list = dataRecordService.selectDataRecordList(dataRecord);
        }else {
            list = dataRecordService.selectDataRecordAllList(dataRecord);
        }

        return getDataTable(list);
    }

    public static String extractDomain(String url) {
        // 去掉协议（http:// 或 https://）
        if (url.startsWith("http://")) {
            url = url.substring(7);
        } else if (url.startsWith("https://")) {
            url = url.substring(8);
        }

        // 获取第一个 '/' 的位置
        int slashIndex = url.indexOf('/');
        if (slashIndex != -1) {
            // 截取到第一个 '/' 之前的内容，即为域名
            return url.substring(0, slashIndex);
        } else {
            // 如果没有 '/'，说明没有路径部分，直接返回剩下的内容
            return url;
        }
    }

    /**
     * 导出外链扫描情况记录列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "外链扫描情况记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DataRecord dataRecord)
    {
        String domain = extractDomain(dataRecord.getUrl());
        dataRecord.setUrl(domain);
        List<DataRecord> list = null;
        if("外链".equals(dataRecord.getUrltype())){
            list = dataRecordService.selectWlDataRecordAllList(dataRecord);
        }else if("全部".equals(dataRecord.getUrltype())){
            list = dataRecordService.selectDataRecordList(dataRecord);
        }else {
            list = dataRecordService.selectDataRecordAllList(dataRecord);
        }
        ExcelUtil<DataRecord> util = new ExcelUtil<DataRecord>(DataRecord.class);
        return util.exportExcel(list, "外链扫描情况记录数据");
    }

    /**
     * 新增外链扫描情况记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存外链扫描情况记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "外链扫描情况记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DataRecord dataRecord)
    {
        return toAjax(dataRecordService.insertDataRecord(dataRecord));
    }

    /**
     * 修改外链扫描情况记录
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DataRecord dataRecord = dataRecordService.selectDataRecordById(id);
        mmap.put("dataRecord", dataRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存外链扫描情况记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "外链扫描情况记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DataRecord dataRecord)
    {
        return toAjax(dataRecordService.updateDataRecord(dataRecord));
    }

    /**
     * 删除外链扫描情况记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "外链扫描情况记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dataRecordService.deleteDataRecordByIds(ids));
    }
}
