package com.ruoyi.web.controller.wocom;

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.excel.wecom.domain.CorpInfo;
import com.ruoyi.excel.wecom.service.ICorpInfoService;
import com.ruoyi.common.utils.CorpContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业信息Controller
 */
@RestController
@RequestMapping("/wecom/corp")
public class CorpInfoController extends BaseController {

    @Autowired
    private ICorpInfoService corpInfoService;

    /**
     * 查询企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('wecom:corp:list')")
    @GetMapping("/list")
    public TableDataInfo list(CorpInfo corpInfo) {
        startPage();
        List<CorpInfo> list = corpInfoService.selectCorpInfoList(corpInfo);
        return getDataTable(list);
    }

    /**
     * 获取企业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wecom:corp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(corpInfoService.selectCorpInfoById(id));
    }

    /**
     * 新增企业信息
     */
    @PreAuthorize("@ss.hasPermi('wecom:corp:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CorpInfo corpInfo) {
        return toAjax(corpInfoService.insertCorpInfo(corpInfo));
    }

    /**
     * 修改企业信息
     */
    @PreAuthorize("@ss.hasPermi('wecom:corp:edit')")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CorpInfo corpInfo) {
        return toAjax(corpInfoService.updateCorpInfo(corpInfo));
    }

    /**
     * 删除企业信息
     */
    @PreAuthorize("@ss.hasPermi('wecom:corp:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(corpInfoService.deleteCorpInfoByIds(ids));
    }

    /**
     * 切换当前使用的企业
     */
    @PostMapping("/switch/{corpId}")
    public AjaxResult switchCorp(@PathVariable("corpId") String corpId) {
        // 1. 验证企业是否存在
        CorpInfo corpInfo = corpInfoService.selectCorpInfoByCorpId(corpId);
        if (corpInfo == null) {
            return error("企业不存在");
        }

        // 2. 设置当前企业到 Redis
        CorpContextHolder.setCurrentCorpId(corpId);

        return success("切换企业成功");
    }

    /**
     * 获取当前使用的企业信息
     */
    @GetMapping("/current")
    public AjaxResult getCurrentCorp() {
        try {
            // 1. 从 Redis 获取当前企业ID
            String corpId = CorpContextHolder.getCurrentCorpId();

            // 2. 查询企业信息
            CorpInfo corpInfo = corpInfoService.selectCorpInfoByCorpId(corpId);
            if (corpInfo == null) {
                return error("当前企业不存在，请重新切换");
            }

            return success(corpInfo);
        } catch (RuntimeException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 清除当前企业上下文
     */
    @PostMapping("/clear")
    public AjaxResult clearCurrentCorp() {
        CorpContextHolder.clear();
        return success("清除成功");
    }
}
