package com.yushu.system.service.impl;

import com.yushu.common.constant.UserConstants;
import com.yushu.common.utils.DateUtils;
import com.yushu.common.utils.StringUtils;
import com.yushu.system.domain.SysMailTemplate;
import com.yushu.system.mapper.SysMailTemplateMapper;
import com.yushu.system.service.ISysMailTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 邮件模板Service业务层处理
 * 
 * @author yushu
 * @date 2024-12-13
 */
@Service
public class SysMailTemplateServiceImpl implements ISysMailTemplateService {

    @Autowired
    private SysMailTemplateMapper sysMailTemplateMapper;

    /** 模板变量正则表达式 */
    private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");

    @Override
    public SysMailTemplate selectSysMailTemplateByTemplateId(Long templateId) {
        return sysMailTemplateMapper.selectSysMailTemplateByTemplateId(templateId);
    }

    @Override
    public SysMailTemplate selectSysMailTemplateByCode(String templateCode) {
        return sysMailTemplateMapper.selectSysMailTemplateByCode(templateCode);
    }

    @Override
    public List<SysMailTemplate> selectSysMailTemplateList(SysMailTemplate sysMailTemplate) {
        return sysMailTemplateMapper.selectSysMailTemplateList(sysMailTemplate);
    }

    @Override
    public int insertSysMailTemplate(SysMailTemplate sysMailTemplate) {
        sysMailTemplate.setCreateTime(DateUtils.getNowDate());
        return sysMailTemplateMapper.insertSysMailTemplate(sysMailTemplate);
    }

    @Override
    public int updateSysMailTemplate(SysMailTemplate sysMailTemplate) {
        sysMailTemplate.setUpdateTime(DateUtils.getNowDate());
        return sysMailTemplateMapper.updateSysMailTemplate(sysMailTemplate);
    }

    @Override
    public int deleteSysMailTemplateByTemplateIds(Long[] templateIds) {
        return sysMailTemplateMapper.deleteSysMailTemplateByTemplateIds(templateIds);
    }

    @Override
    public int deleteSysMailTemplateByTemplateId(Long templateId) {
        return sysMailTemplateMapper.deleteSysMailTemplateByTemplateId(templateId);
    }

    @Override
    public boolean checkTemplateCodeUnique(SysMailTemplate sysMailTemplate) {
        Long templateId = StringUtils.isNull(sysMailTemplate.getTemplateId()) ? -1L : sysMailTemplate.getTemplateId();
        SysMailTemplate info = sysMailTemplateMapper.checkTemplateCodeUnique(sysMailTemplate.getTemplateCode());
        if (StringUtils.isNotNull(info) && info.getTemplateId().longValue() != templateId.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public String renderTemplate(String templateCode, Map<String, Object> variables) {
        SysMailTemplate template = sysMailTemplateMapper.selectSysMailTemplateByCode(templateCode);
        if (template == null) {
            throw new RuntimeException("邮件模板不存在: " + templateCode);
        }
        return renderContent(template.getContent(), variables);
    }

    @Override
    public String previewTemplate(Long templateId, Map<String, Object> variables) {
        SysMailTemplate template = sysMailTemplateMapper.selectSysMailTemplateByTemplateId(templateId);
        if (template == null) {
            throw new RuntimeException("邮件模板不存在");
        }
        return renderContent(template.getContent(), variables);
    }

    /**
     * 渲染模板内容，替换变量
     */
    private String renderContent(String content, Map<String, Object> variables) {
        if (StringUtils.isEmpty(content) || variables == null || variables.isEmpty()) {
            return content;
        }
        
        StringBuffer result = new StringBuffer();
        Matcher matcher = VARIABLE_PATTERN.matcher(content);
        
        while (matcher.find()) {
            String variableName = matcher.group(1);
            Object value = variables.get(variableName);
            String replacement = value != null ? value.toString() : "";
            // 转义特殊字符
            replacement = Matcher.quoteReplacement(replacement);
            matcher.appendReplacement(result, replacement);
        }
        matcher.appendTail(result);
        
        return result.toString();
    }
}
