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.SysMailTemplate;
import com.yushu.system.service.ISysMailTemplateService;
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;
import java.util.Map;

/**
 * 邮件模板Controller
 * 
 * @author yushu
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/system/mail/template")
public class SysMailTemplateController extends BaseController {

    @Autowired
    private ISysMailTemplateService sysMailTemplateService;

    /**
     * 查询邮件模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMailTemplate sysMailTemplate) {
        startPage();
        List<SysMailTemplate> list = sysMailTemplateService.selectSysMailTemplateList(sysMailTemplate);
        return getDataTable(list);
    }

    /**
     * 导出邮件模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:export')")
    @Log(title = "邮件模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMailTemplate sysMailTemplate) {
        List<SysMailTemplate> list = sysMailTemplateService.selectSysMailTemplateList(sysMailTemplate);
        ExcelUtil<SysMailTemplate> util = new ExcelUtil<SysMailTemplate>(SysMailTemplate.class);
        util.exportExcel(response, list, "邮件模板数据");
    }

    /**
     * 获取邮件模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId) {
        return success(sysMailTemplateService.selectSysMailTemplateByTemplateId(templateId));
    }

    /**
     * 新增邮件模板
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:add')")
    @Log(title = "邮件模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMailTemplate sysMailTemplate) {
        if (!sysMailTemplateService.checkTemplateCodeUnique(sysMailTemplate)) {
            return error("新增邮件模板'" + sysMailTemplate.getTemplateName() + "'失败，模板代码已存在");
        }
        sysMailTemplate.setCreateBy(getUsername());
        return toAjax(sysMailTemplateService.insertSysMailTemplate(sysMailTemplate));
    }

    /**
     * 修改邮件模板
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:edit')")
    @Log(title = "邮件模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysMailTemplate sysMailTemplate) {
        if (!sysMailTemplateService.checkTemplateCodeUnique(sysMailTemplate)) {
            return error("修改邮件模板'" + sysMailTemplate.getTemplateName() + "'失败，模板代码已存在");
        }
        sysMailTemplate.setUpdateBy(getUsername());
        return toAjax(sysMailTemplateService.updateSysMailTemplate(sysMailTemplate));
    }

    /**
     * 删除邮件模板
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:remove')")
    @Log(title = "邮件模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds) {
        return toAjax(sysMailTemplateService.deleteSysMailTemplateByTemplateIds(templateIds));
    }

    /**
     * 预览邮件模板
     */
    @PreAuthorize("@ss.hasPermi('system:mail:template:preview')")
    @PostMapping("/preview")
    public AjaxResult preview(@RequestBody Map<String, Object> params) {
        Long templateId = Long.valueOf(params.get("templateId").toString());
        @SuppressWarnings("unchecked")
        Map<String, Object> variables = (Map<String, Object>) params.get("variables");
        String content = sysMailTemplateService.previewTemplate(templateId, variables);
        return success(content);
    }

    /**
     * 获取所有启用的模板列表（下拉选择用）
     */
    @GetMapping("/selectList")
    public AjaxResult selectList() {
        SysMailTemplate query = new SysMailTemplate();
        query.setStatus("0");
        return success(sysMailTemplateService.selectSysMailTemplateList(query));
    }
}
