package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.PartnerRequirementMapper;
import com.zhentao.pojo.PartnerRequirement;
import com.zhentao.service.PartnerRequirementService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 择偶要求Service实现类
 */
@Service
public class PartnerRequirementServiceImpl extends ServiceImpl<PartnerRequirementMapper, PartnerRequirement> 
        implements PartnerRequirementService {
    
    @Override
    public PartnerRequirement getByUserId(Long userId) {
        LambdaQueryWrapper<PartnerRequirement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PartnerRequirement::getUserId, userId);
        return this.getOne(queryWrapper);
    }
    
    @Override
    public boolean saveOrUpdateRequirement(PartnerRequirement requirement) {
        // 查询是否已存在该用户的择偶要求
        PartnerRequirement existing = getByUserId(requirement.getUserId());
        
        if (existing != null) {
            // 更新现有记录
            requirement.setId(existing.getId());
            requirement.setCreatedAt(existing.getCreatedAt());
            requirement.setUpdatedAt(LocalDateTime.now());
            return this.updateById(requirement);
        } else {
            // 创建新记录
            requirement.setCreatedAt(LocalDateTime.now());
            requirement.setUpdatedAt(LocalDateTime.now());
            return this.save(requirement);
        }
    }
}