# BudgetDatabaseConfigEmailServiceImpl.java

    package cloud.wconnect.rateshop.service.budgetdatabase.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.FunctionCons;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.rateshop.cons.budgetdatabase.BudgetDatabaseNotifyTypeEnum;
    import cloud.wconnect.rateshop.entity.budgetdatabase.BudgetDatabaseConfigEmail;
    import cloud.wconnect.rateshop.mapper.budgetdatabase.BudgetDatabaseConfigEmailMapper;
    import cloud.wconnect.rateshop.pojo.dto.budgetdatabase.BudgetDatabaseEmailConfigDTO;
    import cloud.wconnect.rateshop.service.budgetdatabase.IBudgetDatabaseConfigEmailService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import jakarta.annotation.Nullable;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.i18n.LocaleContextHolder;
    import org.springframework.stereotype.Service;

    import java.util.Locale;
    import java.util.Objects;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2025-08-13
     */
    @Service
    @DS("rate_shop")
    public class BudgetDatabaseConfigEmailServiceImpl extends ServiceImpl<BudgetDatabaseConfigEmailMapper, BudgetDatabaseConfigEmail> implements IBudgetDatabaseConfigEmailService {

        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private BeanMapper beanMapper;

        /**
         * 获取邮件通知默认主题
         *
         * @param propertyDTO 资产
         * @param type        1：到期通知
         * @param locale      多语言
         * @return 默认主题
         * @author yyt
         * @date 2025/8/16
         **/
        @Override
        public String getDefaultSubject(PropertyDTO propertyDTO, int type, Locale locale) {
            String propertyName = propertyDTO.getName();
            return i18nMessageUtils.message(BudgetDatabaseNotifyTypeEnum.getDefaultSubjectI18nCode(type), locale,
                    i18nMessageUtils.message(FunctionCons.BUDGET_DATABASE, locale),
                    DateTimeUtils.dateNow(propertyDTO.getTimezone()).plusYears(1).getYear() + "",
                    propertyName);
        }

        /**
         * 获取邮件通知默认主题
         *
         * @param propertyDTO 资产
         * @param type        1：到期通知
         * @return 默认主题
         * @author yyt
         * @date 2025/8/16
         **/
        @Override
        public String getDefaultSubject(PropertyDTO propertyDTO, int type) {
            String language = propertyDTO.getLanguage();
            Locale locale = new Locale(language.split("_")[0], language.split("_")[1]);
            return getDefaultSubject(propertyDTO, type, locale);
        }

        /**
         * 获取邮件配置
         *
         * @param propertyId 资产 id
         * @param type       通知类型
         * @return 邮件配置
         * @author yyt
         * @date 2025/8/16
         */
        @Override
        public BudgetDatabaseConfigEmail getEmailConfig(Integer propertyId, int type) {
            BudgetDatabaseConfigEmail emailConfig = getOne(new QueryWrapper<BudgetDatabaseConfigEmail>().lambda()
                    .eq(BudgetDatabaseConfigEmail::getType, type)
                    .eq(BudgetDatabaseConfigEmail::getPropertyId, propertyId));
            if (Objects.isNull(emailConfig)) {
                // 默认创建一下
                // （因为通知的开关默认是开启的，给用户的感觉是通知默认就存在，
                // 此配置也不需要配置收件人，只需要在页面配置定时执行时间+开始通知日，所以防止用户操作了这俩配置就离开页面，造成了邮件配置默认存在的假象）
                emailConfig = new BudgetDatabaseConfigEmail().setPropertyId(propertyId).setSubject(StringUtils.EMPTY)
                        .setType(type).setStatus(true);
                save(emailConfig);
            }
            return emailConfig;
        }

        /**
         * 获取邮件配置
         *
         * @param propertyId 资产 id
         * @param type       通知类型
         * @param status     状态（false：关闭，true：开启）
         * @return 邮件配置
         * @author yyt
         * @date 2025/8/16
         */
        @Override
        public BudgetDatabaseConfigEmail getEmailConfig(Integer propertyId, int type, @Nullable Boolean status) {
            return getOne(new QueryWrapper<BudgetDatabaseConfigEmail>().lambda()
                    .eq(BudgetDatabaseConfigEmail::getType, type)
                    .eq(BudgetDatabaseConfigEmail::getPropertyId, propertyId)
                    .eq(Objects.nonNull(status), BudgetDatabaseConfigEmail::getStatus, status));
        }

        /**
         * 保存邮件配置
         *
         * @param propertyId 资产 id
         * @param configDTO  邮件配置
         * @return 配置 id
         * @author yyt
         * @date 2025/8/16
         */
        @Override
        public Integer save(int propertyId, BudgetDatabaseEmailConfigDTO configDTO) {
            BudgetDatabaseConfigEmail config = beanMapper.map(configDTO, BudgetDatabaseConfigEmail.class);
            config.setPropertyId(propertyId);
            saveOrUpdate(config);

            return config.getId();
        }

        /**
         * 获取邮件配置
         *
         * @param propertyId 资产 id
         * @param type       邮件类型
         * @return 邮件配置
         * @author yyt
         * @date 2026/1/19
         */
        @Override
        public BudgetDatabaseEmailConfigDTO getEmailConfigDTO(int propertyId, int type) {
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            Locale locale = LocaleContextHolder.getLocale();
            String defaultSubject = getDefaultSubject(propertyDTO, type, locale);

            BudgetDatabaseConfigEmail config = getEmailConfig(propertyId, type);
            if (Objects.isNull(config)) {
                return new BudgetDatabaseEmailConfigDTO(defaultSubject);
            }

            BudgetDatabaseEmailConfigDTO configDTO = beanMapper.map(config, BudgetDatabaseEmailConfigDTO.class);
            configDTO.setDefaultSubject(defaultSubject);
            return configDTO;
        }

    }