package com.ruoyi.project.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.domain.TrsPolicyResults;
import com.ruoyi.project.domain.TrsSiteconfig;
import com.ruoyi.project.service.ITrsSiteconfigService;
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.project.domain.TrsUrlResult;
import com.ruoyi.project.service.ITrsUrlResultService;
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 2025-03-30
 */
@Controller
@RequestMapping("/project/urlresult")
public class TrsUrlResultController extends BaseController
{
    private String prefix = "project/urlresult";

    @Autowired
    private ITrsUrlResultService trsUrlResultService;
    @Autowired
    private ITrsSiteconfigService trsSiteconfigService;
    @RequiresPermissions("project:wxurlresult:view")
    @GetMapping()
    public String urlresult(ModelMap mmap)
    {
        // 获取站点列表并添加到Model中
        List<Map<String, Object>> sites = new ArrayList<>();
        TrsSiteconfig trsSiteconfig = new TrsSiteconfig();
        trsSiteconfig.setMsg("site");
        List<TrsSiteconfig> list = trsSiteconfigService.selectTrsSiteconfigList(trsSiteconfig);
        for (TrsSiteconfig siteconfig : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("sitename", siteconfig.getSitename());
            sites.add(map);
        }
        mmap.put("sites", sites);
        return prefix + "/urlresult";
    }

    /**
     * 查询错外链监测列表
     */
    @RequiresPermissions("project:wxurlresult:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsUrlResult trsUrlResult)
    {
        startPage();
        List<TrsUrlResult> list = trsUrlResultService.selectTrsUrlResultList(trsUrlResult);
        return getDataTable(list);
    }

    /**
     * 导出错外链监测列表
     */
    @RequiresPermissions("project:wxurlresult:export")
    @Log(title = "错外链监测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsUrlResult trsUrlResult)
    {
        List<TrsUrlResult> list = trsUrlResultService.selectTrsUrlResultList(trsUrlResult);
        for (TrsUrlResult item : list) {
            if ("site".equals(item.getSitetag())) {
                item.setSitetag("网站");
            } else if ("weixin".equals(item.getSitetag())) {
                item.setSitetag("微信");
            } else if ("weibo".equals(item.getSitetag())) {
                item.setSitetag("微博");
            } else if ("douyin".equals(item.getSitetag())) {
                item.setSitetag("抖音");
            } else if ("kuaishou".equals(item.getSitetag())) {
                item.setSitetag("快手");
            } else if ("toutiao".equals(item.getSitetag())) {
                item.setSitetag("头条");
            }
        }
        ExcelUtil<TrsUrlResult> util = new ExcelUtil<TrsUrlResult>(TrsUrlResult.class);
        return util.exportExcel(list, "错外链监测数据");
    }

    /**
     * 新增错外链监测
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存错外链监测
     */
    @RequiresPermissions("project:wxurlresult:add")
    @Log(title = "错外链监测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsUrlResult trsUrlResult)
    {
        return toAjax(trsUrlResultService.insertTrsUrlResult(trsUrlResult));
    }

    /**
     * 修改错外链监测
     */
    @RequiresPermissions("project:wxurlresult:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsUrlResult trsUrlResult = trsUrlResultService.selectTrsUrlResultById(id);
        mmap.put("trsUrlResult", trsUrlResult);
        return prefix + "/edit";
    }

    /**
     * 修改保存错外链监测
     */
    @RequiresPermissions("project:wxurlresult:edit")
    @Log(title = "错外链监测", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsUrlResult trsUrlResult)
    {
        return toAjax(trsUrlResultService.updateTrsUrlResult(trsUrlResult));
    }

    /**
     * 删除错外链监测
     */
    @RequiresPermissions("project:wxurlresult:remove")
    @Log(title = "错外链监测", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsUrlResultService.deleteTrsUrlResultByIds(ids));
    }
}
