package org.xyzh.crontab.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xyzh.api.crontab.EmailDefaultService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.crontab.TbCrontabEmailDefault;
import org.xyzh.common.utils.IDUtils;
import org.xyzh.crontab.mapper.EmailDefaultMapper;

import java.util.Date;
import java.util.List;

/**
 * @description 定时任务邮件通知默认接收人服务实现类
 * @filename EmailDefaultServiceImpl.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-11-18
 */
@Service
public class EmailDefaultServiceImpl implements EmailDefaultService {

    private static final Logger logger = LoggerFactory.getLogger(EmailDefaultServiceImpl.class);

    @Autowired
    private EmailDefaultMapper emailDefaultMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDomain<TbCrontabEmailDefault> createDefault(TbCrontabEmailDefault emailDefault) {
        ResultDomain<TbCrontabEmailDefault> resultDomain = new ResultDomain<>();
        try {

            // 生成ID
            emailDefault.setID(IDUtils.generateID());
            if (emailDefault.getDefaultId() == null || emailDefault.getDefaultId().isEmpty()) {
                emailDefault.setDefaultId(IDUtils.generateID());
            }
            emailDefault.setCreateTime(new Date());
            emailDefault.setDeleted(false);

            int count = emailDefaultMapper.insertDefault(emailDefault);
            if (count > 0) {
                resultDomain.success("创建默认接收人成功", emailDefault);
            } else {
                resultDomain.fail("创建默认接收人失败");
            }
        } catch (Exception e) {
            logger.error("创建默认接收人异常: {}", e.getMessage(), e);
            resultDomain.fail("创建默认接收人失败: " + e.getMessage());
        }
        return resultDomain;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDomain<TbCrontabEmailDefault> updateDefault(TbCrontabEmailDefault emailDefault) {
        ResultDomain<TbCrontabEmailDefault> resultDomain = new ResultDomain<>();
        try {
            emailDefault.setUpdateTime(new Date());
            int count = emailDefaultMapper.updateDefault(emailDefault);
            if (count > 0) {
                resultDomain.success("更新默认接收人成功", emailDefault);
            } else {
                resultDomain.fail("更新默认接收人失败");
            }
        } catch (Exception e) {
            logger.error("更新默认接收人异常: {}", e.getMessage(), e);
            resultDomain.fail("更新默认接收人失败: " + e.getMessage());
        }
        return resultDomain;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDomain<Boolean> deleteDefault(String defaultId) {
        ResultDomain<Boolean> resultDomain = new ResultDomain<>();
        try {
            int count = emailDefaultMapper.deleteDefault(defaultId);
            if (count > 0) {
                resultDomain.success("删除默认接收人成功", true);
            } else {
                resultDomain.fail("删除默认接收人失败");
            }
        } catch (Exception e) {
            logger.error("删除默认接收人异常: {}", e.getMessage(), e);
            resultDomain.fail("删除默认接收人失败: " + e.getMessage());
        }
        return resultDomain;
    }

    @Override
    public ResultDomain<TbCrontabEmailDefault> getDefaultById(String defaultId) {
        ResultDomain<TbCrontabEmailDefault> resultDomain = new ResultDomain<>();
        try {
            TbCrontabEmailDefault emailDefault = emailDefaultMapper.selectDefaultById(defaultId);
            if (emailDefault != null) {
                resultDomain.success("查询默认接收人成功", emailDefault);
            } else {
                resultDomain.fail("默认接收人不存在");
            }
        } catch (Exception e) {
            logger.error("查询默认接收人异常: {}", e.getMessage(), e);
            resultDomain.fail("查询默认接收人失败: " + e.getMessage());
        }
        return resultDomain;
    }

    @Override
    public ResultDomain<TbCrontabEmailDefault> getDefaultByMetaId(String metaId) {
        ResultDomain<TbCrontabEmailDefault> resultDomain = new ResultDomain<>();
        try {
            List<TbCrontabEmailDefault> emailDefault = emailDefaultMapper.selectDefaultByMetaId(metaId);
            if (emailDefault != null) {
                resultDomain.success("查询默认接收人成功", emailDefault);
            } else {
                resultDomain.fail("该任务元数据未配置默认接收人");
            }
        } catch (Exception e) {
            logger.error("查询默认接收人异常: {}", e.getMessage(), e);
            resultDomain.fail("查询默认接收人失败: " + e.getMessage());
        }
        return resultDomain;
    }
}
