package org.dromara.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.main.domain.MainStudentAppendix;
import org.dromara.main.domain.bo.MainStudentAppendixBo;
import org.dromara.main.domain.vo.MainStudentAppendixVo;
import org.dromara.main.mapper.MainStudentAppendixMapper;
import org.dromara.main.service.IMainStudentAppendixService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 学员简历附件Service业务层处理
 *
 * @author antigravity
 * @date 2026-04-08
 */
@RequiredArgsConstructor
@Service
public class MainStudentAppendixServiceImpl implements IMainStudentAppendixService {

    private final MainStudentAppendixMapper baseMapper;

    /**
     * 查询学员简历附件列表
     */
    @Override
    public List<MainStudentAppendixVo> queryList(Long studentId) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<MainStudentAppendix>()
            .eq(MainStudentAppendix::getStudentId, studentId)
            .orderByDesc(MainStudentAppendix::getCreateTime));
    }

    /**
     * 新增学员简历附件
     */
    @Override
    public Boolean insertByBo(MainStudentAppendixBo bo) {
        // 核心逻辑：校验数量
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<MainStudentAppendix>()
            .eq(MainStudentAppendix::getStudentId, bo.getStudentId()));
        if (count >= 3) {
            throw new ServiceException("最多只允许上传3份附件简历");
        }

        MainStudentAppendix add = BeanUtil.toBean(bo, MainStudentAppendix.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 批量删除学员简历附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
