package com.yushu.web.controller.system;

import com.yushu.common.annotation.Log;
import com.yushu.common.core.controller.BaseController;
import com.yushu.common.core.domain.AjaxResult;
import com.yushu.common.core.page.TableDataInfo;
import com.yushu.common.enums.BusinessType;
import com.yushu.common.utils.poi.ExcelUtil;
import com.yushu.system.domain.SysMailConfig;
import com.yushu.system.service.ISysMailConfigService;
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 jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业邮箱配置Controller
 * 
 * @author yushu
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/system/mail/config")
public class SysMailConfigController extends BaseController {

    @Autowired
    private ISysMailConfigService sysMailConfigService;

    /**
     * 查询邮箱配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMailConfig sysMailConfig) {
        startPage();
        List<SysMailConfig> list = sysMailConfigService.selectSysMailConfigList(sysMailConfig);
        return getDataTable(list);
    }

    /**
     * 导出邮箱配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:export')")
    @Log(title = "邮箱配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMailConfig sysMailConfig) {
        List<SysMailConfig> list = sysMailConfigService.selectSysMailConfigList(sysMailConfig);
        ExcelUtil<SysMailConfig> util = new ExcelUtil<SysMailConfig>(SysMailConfig.class);
        util.exportExcel(response, list, "邮箱配置数据");
    }

    /**
     * 获取邮箱配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId) {
        return success(sysMailConfigService.selectSysMailConfigByConfigId(configId));
    }

    /**
     * 新增邮箱配置
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:add')")
    @Log(title = "邮箱配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMailConfig sysMailConfig) {
        sysMailConfig.setCreateBy(getUsername());
        return toAjax(sysMailConfigService.insertSysMailConfig(sysMailConfig));
    }

    /**
     * 修改邮箱配置
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:edit')")
    @Log(title = "邮箱配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysMailConfig sysMailConfig) {
        sysMailConfig.setUpdateBy(getUsername());
        return toAjax(sysMailConfigService.updateSysMailConfig(sysMailConfig));
    }

    /**
     * 删除邮箱配置
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:remove')")
    @Log(title = "邮箱配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        return toAjax(sysMailConfigService.deleteSysMailConfigByConfigIds(configIds));
    }

    /**
     * 获取已启用的邮箱配置列表
     */
    @GetMapping("/enabledList")
    public AjaxResult enabledList() {
        return success(sysMailConfigService.selectEnabledConfigList());
    }

    /**
     * 测试邮箱连接
     */
    @PreAuthorize("@ss.hasPermi('system:mail:config:test')")
    @Log(title = "邮箱配置", businessType = BusinessType.OTHER)
    @PostMapping("/test")
    public AjaxResult testConnection(@RequestBody SysMailConfig sysMailConfig) {
        if (sysMailConfig.getConfigId() == null) {
            return error("请选择邮箱配置");
        }
        String testEmail = sysMailConfig.getRemark(); // 临时用remark存储测试邮箱
        if (testEmail == null || testEmail.isEmpty()) {
            return error("请输入测试邮箱地址");
        }
        boolean result = sysMailConfigService.testConnection(sysMailConfig.getConfigId(), testEmail);
        return result ? success("测试邮件发送成功") : error("测试邮件发送失败");
    }
}
