package com.ruoyi.project.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.TrsNameResults;
import com.ruoyi.project.domain.TrsSiteconfig;
import com.ruoyi.project.service.ITrsNameResultsService;
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.*;

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

/**
 * 人名职务监测Controller
 * 
 * @author ruoyi
 * @date 2025-03-24
 */
@Controller
@RequestMapping("/project/wxnameresults")
public class TrsWxNameResultsController extends BaseController
{
    private String prefix = "project/wxnameresults";

    @Autowired
    private ITrsNameResultsService trsNameResultsService;
    @Autowired
    private ITrsSiteconfigService trsSiteconfigService;

    @RequiresPermissions("project:wxnameresults:view")
    @GetMapping()
    public String nameresults(ModelMap mmap)
    {
        // 获取站点列表并添加到Model中
        List<Map<String, Object>> sites = new ArrayList<>();
        TrsSiteconfig trsSiteconfig = new TrsSiteconfig();
        trsSiteconfig.setMsg("weixin");
        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 + "/nameresults";
    }

    @RequiresPermissions("project:wxnameresults:view")
    @GetMapping("/getSitesByType")
    @ResponseBody
    public AjaxResult getSitesByType(String type) {
        List<Map<String, Object>> sites = new ArrayList<>();
        TrsSiteconfig trsSiteconfig = new TrsSiteconfig();
        trsSiteconfig.setMsg(type); // 使用传入的type参数
        List<TrsSiteconfig> list = trsSiteconfigService.selectTrsSiteconfigList(trsSiteconfig);

        for (TrsSiteconfig siteconfig : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("sitename", siteconfig.getSitename());
            sites.add(map);
        }

        return AjaxResult.success(sites);
    }

    /**
     * 查询人名职务监测列表
     */
    @RequiresPermissions("project:wxnameresults:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsNameResults trsNameResults)
    {
        startPage();
        List<TrsNameResults> list = trsNameResultsService.selectTrsNameResultsList(trsNameResults);
        return getDataTable(list);
    }

    /**
     * 查询常见错误监测统计
     */
    @RequiresPermissions("project:wxnameresults:list")
    @RequestMapping ("/statistics")
    @ResponseBody
    public AjaxResult  getStatistics(TrsNameResults trsSensitiveResults)
    {
        Map<String, Integer> stats = trsNameResultsService.getStatistics(trsSensitiveResults);
        return AjaxResult.success(stats);
    }

    /**
     * 导出人名职务监测列表
     */
    @RequiresPermissions("project:wxnameresults:export")
    @Log(title = "人名职务监测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsNameResults trsNameResults)
    {
        List<TrsNameResults> list = trsNameResultsService.selectTrsNameResultsList(trsNameResults);
        ExcelUtil<TrsNameResults> util = new ExcelUtil<TrsNameResults>(TrsNameResults.class);
        return util.exportExcel(list, "人名职务监测数据");
    }

    /**
     * 新增人名职务监测
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存人名职务监测
     */
    @RequiresPermissions("project:wxnameresults:add")
    @Log(title = "人名职务监测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsNameResults trsNameResults)
    {
        return toAjax(trsNameResultsService.insertTrsNameResults(trsNameResults));
    }

    /**
     * 修改人名职务监测
     */
    @RequiresPermissions("project:wxnameresults:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsNameResults trsNameResults = trsNameResultsService.selectTrsNameResultsById(id);
        mmap.put("trsNameResults", trsNameResults);
        return prefix + "/edit";
    }

    /**
     * 修改保存人名职务监测
     */
    @RequiresPermissions("project:wxnameresults:edit")
    @Log(title = "人名职务监测", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsNameResults trsNameResults)
    {
        return toAjax(trsNameResultsService.updateTrsNameResults(trsNameResults));
    }

    /**
     * 修改保存常见错误监测
     */
    @RequiresPermissions("project:wxnameresults:edit")
    @Log(title = "常见错误监测", businessType = BusinessType.UPDATE)
    @PostMapping("/biaoji")
    @ResponseBody
    public AjaxResult biaoji(String ids, TrsNameResults trsSensitiveResults)
    {
        Integer count = 0;
        for (String id : ids.split(",")) {
            trsSensitiveResults.setId(Long.parseLong(id));
            trsSensitiveResults.setStatus(trsSensitiveResults.getStatus());
            trsNameResultsService.updateTrsNameResults(trsSensitiveResults);
        }
        return toAjax(count);
    }

    /**
     * 删除人名职务监测
     */
    @RequiresPermissions("project:wxnameresults:remove")
    @Log(title = "人名职务监测", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsNameResultsService.deleteTrsNameResultsByIds(ids));
    }
}
