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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.main.domain.MainStudentCollection;
import org.dromara.main.domain.bo.MainStudentCollectionBo;
import org.dromara.main.domain.vo.MainStudentCollectionVo;
import org.dromara.main.mapper.MainStudentCollectionMapper;
import org.dromara.main.mapper.MainPositionMapper;
import org.dromara.main.mapper.MainExamEvaluationMapper;
import org.dromara.main.service.IMainStudentCollectionService;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class MainStudentCollectionServiceImpl implements IMainStudentCollectionService {

    private final MainStudentCollectionMapper baseMapper;
    private final MainPositionMapper positionMapper;
    private final MainExamEvaluationMapper assessmentMapper;

    @Override
    public TableDataInfo<MainStudentCollectionVo> queryPageList(MainStudentCollectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MainStudentCollection> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getStudentId()), MainStudentCollection::getStudentId, bo.getStudentId());
        lqw.eq(ObjectUtil.isNotNull(bo.getType()), MainStudentCollection::getType, bo.getType());
        lqw.orderByDesc(MainStudentCollection::getCreateTime);

        Page<MainStudentCollectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<MainStudentCollectionVo> records = result.getRecords();

        // 填充目标数据
        for (MainStudentCollectionVo record : records) {
            if ("job".equals(record.getType())) {
                record.setTargetData(positionMapper.selectVoById(record.getTargetId()));
            } else if ("assessment".equals(record.getType())) {
                record.setTargetData(assessmentMapper.selectVoById(record.getTargetId()));
            }
        }

        return TableDataInfo.build(result);
    }

    @Override
    public Boolean insertByBo(MainStudentCollectionBo bo) {
        // 先检查是否已收藏
        LambdaQueryWrapper<MainStudentCollection> lqw = Wrappers.lambdaQuery();
        lqw.eq(MainStudentCollection::getStudentId, bo.getStudentId())
           .eq(MainStudentCollection::getTargetId, bo.getTargetId())
           .eq(MainStudentCollection::getType, bo.getType());
        if (baseMapper.exists(lqw)) {
            return true; // 已存在视为成功
        }

        MainStudentCollection add = BeanUtil.toBean(bo, MainStudentCollection.class);
        return baseMapper.insert(add) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public MainStudentCollectionVo checkCollection(Long studentId, Long targetId, String type) {
        LambdaQueryWrapper<MainStudentCollection> lqw = Wrappers.lambdaQuery();
        lqw.eq(MainStudentCollection::getStudentId, studentId)
           .eq(MainStudentCollection::getTargetId, targetId)
           .eq(MainStudentCollection::getType, type);
        return baseMapper.selectVoOne(lqw);
    }
}
