package com.storlead.sales.mail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.sales.mail.entity.EmailFolderRuleEntity;
import com.storlead.sales.mail.entity.EmailsEntity;
import com.storlead.sales.mail.enums.EmailFolderlRuleEnum;
import com.storlead.sales.mail.mapper.EmailFolderRuleMapper;
import com.storlead.sales.mail.service.EmailFolderRuleService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 用户邮件文件夹出入规则 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2024-11-12
 */
@Service
public class EmailFolderRuleServiceImpl extends MyBaseServiceImpl<EmailFolderRuleMapper, EmailFolderRuleEntity> implements EmailFolderRuleService {

    @Override
    public List<EmailFolderRuleEntity> getUserFolderRules(Long userId) {
        LambdaQueryWrapper<EmailFolderRuleEntity> queryWrapper = new LambdaQueryWrapper();
        if (Objects.nonNull(userId)) {
            queryWrapper.eq(EmailFolderRuleEntity::getOwnerBy,userId);
        }
        queryWrapper.eq(EmailFolderRuleEntity::getIsDelete,Integer.valueOf(0));
        queryWrapper.orderByAsc(EmailFolderRuleEntity::getSort);
        List<EmailFolderRuleEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public void carveUpMailCustomFolder(EmailsEntity emails,List<EmailFolderRuleEntity> rolderRules) {
        if (CollectionUtils.isEmpty(rolderRules)) {
            return;
        }
        for (EmailFolderRuleEntity rule : rolderRules) {
            if (EmailFolderlRuleEnum.EMAIL_FROM_EQ.code.equals(rule.getFolderRuleCode())) {
                // 发件箱等于
                if (emails.getFrom().equals(rule.getCompareContent())) {
                    emails.setCustomFolderId(rule.getFolderId());
                    break;
                }
            } else if (EmailFolderlRuleEnum.EMAIL_SUBJECT_CONTAINS.code.equals(rule.getFolderRuleCode())) {
                // 邮件主题包含
                if (emails.getSubject().contains(rule.getCompareContent())) {
                    emails.setCustomFolderId(rule.getFolderId());
                    break;
                }
            } else if (EmailFolderlRuleEnum.EMAIL_FROM_SUFFIX_EQ.code.equals(rule.getFolderRuleCode())) {
                //发件箱后缀等于
                String domainRegex = "@(.+)$";
                // 创建 Pattern 对象
                Pattern pattern = Pattern.compile(domainRegex);
                // 创建 Matcher 对象
                Matcher matcher = pattern.matcher(emails.getFrom());
                // 查找并提取 @ 后面的部分
                if (matcher.find()) {
                    String domain = matcher.group(1);  // 获取 @ 后面的部分
                    if (rule.getCompareContent().equals(domain)) {
                        emails.setCustomFolderId(rule.getFolderId());
                        break;
                    }
                }
            }
        }
    }
}
