package org.dromara.main.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.main.domain.MainStudentDislike;
import org.dromara.main.mapper.MainStudentDislikeMapper;
import org.dromara.main.service.IMainStudentDislikeService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class MainStudentDislikeServiceImpl extends ServiceImpl<MainStudentDislikeMapper, MainStudentDislike>
    implements IMainStudentDislikeService {
    @Override
    public boolean dislikePosition(Long studentId, Long positionId) {
        // 防重复插入
        long count = baseMapper.selectCount(Wrappers.<MainStudentDislike>lambdaQuery()
            .eq(MainStudentDislike::getStudentId, studentId)
            .eq(MainStudentDislike::getDislikeType, "1")
            .eq(MainStudentDislike::getPositionId, positionId));
        if (count > 0) return true;
        MainStudentDislike record = new MainStudentDislike();
        record.setStudentId(studentId);
        record.setDislikeType("1");
        record.setPositionId(positionId);
        record.setCreateTime(new Date());
        record.setCreateBy(studentId);
        return baseMapper.insert(record) > 0;
    }
    @Override
    public boolean dislikeCompany(Long studentId, String tenantId) {
        long count = baseMapper.selectCount(Wrappers.<MainStudentDislike>lambdaQuery()
            .eq(MainStudentDislike::getStudentId, studentId)
            .eq(MainStudentDislike::getDislikeType, "2")
            .eq(MainStudentDislike::getTenantId, tenantId));
        if (count > 0) return true;
        MainStudentDislike record = new MainStudentDislike();
        record.setStudentId(studentId);
        record.setDislikeType("2");
        record.setTenantId(tenantId);
        record.setCreateTime(new Date());
        record.setCreateBy(studentId);
        return baseMapper.insert(record) > 0;
    }
    @Override
    public List<Long> getDislikedPositionIds(Long studentId) {
        return baseMapper.selectList(Wrappers.<MainStudentDislike>lambdaQuery()
                .eq(MainStudentDislike::getStudentId, studentId)
                .eq(MainStudentDislike::getDislikeType, "1"))
            .stream().map(MainStudentDislike::getPositionId).collect(Collectors.toList());
    }
    @Override
    public List<String> getDislikedTenantIds(Long studentId) {
        return baseMapper.selectList(Wrappers.<MainStudentDislike>lambdaQuery()
                .eq(MainStudentDislike::getStudentId, studentId)
                .eq(MainStudentDislike::getDislikeType, "2"))
            .stream().map(MainStudentDislike::getTenantId).collect(Collectors.toList());
    }
}
