package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.MatchmakerApplyMapper;
import com.zhentao.pojo.MatchmakerApply;
import com.zhentao.service.MatchmakerApplyService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 红娘申请Service实现类
 */
@Service
public class MatchmakerApplyServiceImpl extends ServiceImpl<MatchmakerApplyMapper, MatchmakerApply>
        implements MatchmakerApplyService {

    @Override
    public boolean submitApply(MatchmakerApply matchmakerApply) {
        // 设置创建时间
        matchmakerApply.setCreateTime(LocalDateTime.now());
        matchmakerApply.setUpdateTime(LocalDateTime.now());

        // 更新人创建人
        matchmakerApply.setCreateMan("系统管理员");
        matchmakerApply.setUpdateMan("系统管理员");
        
        // 设置默认状态为待审核（2）
        if (matchmakerApply.getStatus() == null) {
            matchmakerApply.setStatus(2);
        }
        
        // 保存申请信息
        return this.save(matchmakerApply);
    }

    @Override
    public MatchmakerApply getApplyByUserId(Long userId) {
        if (userId == null) {
            return null;
        }

        QueryWrapper<MatchmakerApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("create_time"); // 按创建时间倒序，获取最新的申请记录
        queryWrapper.last("LIMIT 1"); // 只获取一条记录

        return this.getOne(queryWrapper);
    }
}