package com.vetti.hotake.service.impl;

import com.vetti.common.core.service.BaseServiceImpl;
import com.vetti.common.enums.FillTypeEnum;
import com.vetti.hotake.domain.HotakeNotificationPreferences;
import com.vetti.hotake.mapper.HotakeNotificationPreferencesMapper;
import com.vetti.hotake.service.IHotakeNotificationPreferencesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户通知偏好设置Service业务层处理
 *
 * @author vetti
 * @date 2026-02-01
 */
@Service
public class HotakeNotificationPreferencesServiceImpl extends BaseServiceImpl implements IHotakeNotificationPreferencesService {

    @Autowired
    private HotakeNotificationPreferencesMapper hotakeNotificationPreferencesMapper;

    /**
     * 查询用户通知偏好设置
     *
     * @param id 用户通知偏好设置主键
     * @return 用户通知偏好设置
     */
    @Transactional(readOnly = true)
    @Override
    public HotakeNotificationPreferences selectHotakeNotificationPreferencesById(Long id) {
        return hotakeNotificationPreferencesMapper.selectHotakeNotificationPreferencesById(id);
    }

    /**
     * 查询用户通知偏好设置列表
     *
     * @param hotakeNotificationPreferences 用户通知偏好设置
     * @return 用户通知偏好设置
     */
    @Transactional(readOnly = true)
    @Override
    public List<HotakeNotificationPreferences> selectHotakeNotificationPreferencesList(HotakeNotificationPreferences hotakeNotificationPreferences) {
        return hotakeNotificationPreferencesMapper.selectHotakeNotificationPreferencesList(hotakeNotificationPreferences);
    }

    /**
     * 新增用户通知偏好设置
     *
     * @param hotakeNotificationPreferences 用户通知偏好设置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeNotificationPreferences insertHotakeNotificationPreferences(HotakeNotificationPreferences hotakeNotificationPreferences) {
        fill(FillTypeEnum.INSERT.getCode(), hotakeNotificationPreferences);
        hotakeNotificationPreferencesMapper.insertHotakeNotificationPreferences(hotakeNotificationPreferences);
        return hotakeNotificationPreferences;
    }

    /**
     * 修改用户通知偏好设置
     *
     * @param hotakeNotificationPreferences 用户通知偏好设置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeNotificationPreferences updateHotakeNotificationPreferences(HotakeNotificationPreferences hotakeNotificationPreferences) {
        fill(FillTypeEnum.UPDATE.getCode(), hotakeNotificationPreferences);
        hotakeNotificationPreferencesMapper.updateHotakeNotificationPreferences(hotakeNotificationPreferences);
        return hotakeNotificationPreferences;
    }

    /**
     * 批量删除用户通知偏好设置
     *
     * @param ids 需要删除的用户通知偏好设置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeNotificationPreferencesByIds(Long[] ids) {
        return hotakeNotificationPreferencesMapper.deleteHotakeNotificationPreferencesByIds(ids);
    }

    /**
     * 删除用户通知偏好设置信息
     *
     * @param id 用户通知偏好设置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteHotakeNotificationPreferencesById(Long id) {
        return hotakeNotificationPreferencesMapper.deleteHotakeNotificationPreferencesById(id);
    }

    /**
     * 获取当前登录用户的通知偏好设置
     * 如果不存在则自动创建默认设置
     *
     * @param username 用户名
     * @return 用户通知偏好设置
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public HotakeNotificationPreferences getCurrentUserPreferences(String username) {
        // 查询当前用户的设置
        HotakeNotificationPreferences query = new HotakeNotificationPreferences();
        query.setCreateBy(username);
        List<HotakeNotificationPreferences> list = hotakeNotificationPreferencesMapper.selectHotakeNotificationPreferencesList(query);
        
        // 如果存在，返回第一条
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        
        // 如果不存在，创建默认设置
        HotakeNotificationPreferences defaultPreferences = new HotakeNotificationPreferences();
        // 设置默认值（根据数据库表定义）
        defaultPreferences.setEmailJobAlerts(1);  // 默认开启
        defaultPreferences.setEmailApplicationUpdates(1);  // 默认开启
        defaultPreferences.setEmailAiMatchSuggestions(0);  // 默认关闭
        defaultPreferences.setEmailNewsletter(0);  // 默认关闭
        defaultPreferences.setPushNewMessages(1);  // 默认开启
        defaultPreferences.setPushInterviewReminders(1);  // 默认开启
        defaultPreferences.setMailMarketingEmails(0);  // 默认关闭
        defaultPreferences.setMailPartnerOffers(0);  // 默认关闭
        
        // 保存并返回
        return insertHotakeNotificationPreferences(defaultPreferences);
    }
}
