package com.ruoyi.web.controller.jarvis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.domain.SuperAdmin;
import com.ruoyi.jarvis.service.SuperAdminService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 超级管理员Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/jarvis/superadmin")
public class SuperAdminController extends BaseController
{
    @Autowired
    private SuperAdminService superAdminService;

    /**
     * 查询超级管理员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SuperAdmin superAdmin)
    {
        startPage();
        List<SuperAdmin> list = superAdminService.selectSuperAdminList(superAdmin);
        return getDataTable(list);
    }

    /**
     * 导出超级管理员列表
     */
    @Log(title = "超级管理员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperAdmin superAdmin)
    {
        List<SuperAdmin> list = superAdminService.selectSuperAdminList(superAdmin);
        ExcelUtil<SuperAdmin> util = new ExcelUtil<SuperAdmin>(SuperAdmin.class);
        util.exportExcel(response, list, "超级管理员数据");
    }

    /**
     * 获取超级管理员详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superAdminService.selectSuperAdminById(id));
    }

    /**
     * 新增超级管理员
     */
    @Log(title = "超级管理员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperAdmin superAdmin)
    {
        return toAjax(superAdminService.insertSuperAdmin(superAdmin));
    }

    /**
     * 修改超级管理员
     */
    @Log(title = "超级管理员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperAdmin superAdmin)
    {
        return toAjax(superAdminService.updateSuperAdmin(superAdmin));
    }

    /**
     * 删除超级管理员
     */
    @Log(title = "超级管理员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superAdminService.deleteSuperAdminByIds(ids));
    }
    /**
     * 获取超级管理员下拉选项列表（unionId 和 name）
     */
    @GetMapping("/select/adminUnionId")
    public AjaxResult optionSelect()
    {
        SuperAdmin superAdmin = new SuperAdmin();
        superAdmin.setIsActive(1); // 只查询激活的管理员
        List<SuperAdmin> list = superAdminService.selectSuperAdminList(superAdmin);
        List<Map<String, Object>> optionList = list.stream()
                .filter(item -> item.getUnionId() != null ).map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("label", item.getName());
                    map.put("value", item.getUnionId());
                    return map;
                })
                .collect(Collectors.toList());
        return AjaxResult.success(optionList);
    }
}
