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.SysMailLog;
import com.yushu.system.service.ISysMailLogService;
import com.yushu.system.service.ISysMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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/log")
public class SysMailLogController extends BaseController {

    @Autowired
    private ISysMailLogService sysMailLogService;

    @Autowired
    private ISysMailService sysMailService;

    /**
     * 查询邮件发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:log:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMailLog sysMailLog) {
        startPage();
        List<SysMailLog> list = sysMailLogService.selectSysMailLogList(sysMailLog);
        return getDataTable(list);
    }

    /**
     * 导出邮件发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:log:export')")
    @Log(title = "邮件发送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMailLog sysMailLog) {
        List<SysMailLog> list = sysMailLogService.selectSysMailLogList(sysMailLog);
        ExcelUtil<SysMailLog> util = new ExcelUtil<SysMailLog>(SysMailLog.class);
        util.exportExcel(response, list, "邮件发送记录数据");
    }

    /**
     * 获取邮件发送记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mail:log:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId) {
        return success(sysMailLogService.selectSysMailLogByLogId(logId));
    }

    /**
     * 删除邮件发送记录
     */
    @PreAuthorize("@ss.hasPermi('system:mail:log:remove')")
    @Log(title = "邮件发送记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds) {
        return toAjax(sysMailLogService.deleteSysMailLogByLogIds(logIds));
    }

    /**
     * 清空邮件发送记录
     */
    @PreAuthorize("@ss.hasPermi('system:mail:log:remove')")
    @Log(title = "邮件发送记录", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        sysMailLogService.cleanMailLog();
        return success();
    }

    /**
     * 重新发送失败的邮件
     */
    @PreAuthorize("@ss.hasPermi('system:mail:log:resend')")
    @Log(title = "邮件发送记录", businessType = BusinessType.OTHER)
    @PostMapping("/resend/{logId}")
    public AjaxResult resend(@PathVariable Long logId) {
        boolean result = sysMailService.resendMail(logId);
        return result ? success("邮件重发成功") : error("邮件重发失败");
    }
}
