package com.ruoyi.wgz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.wgz.bo.req.WgzQuestionReq;
import com.ruoyi.wgz.bo.rests.WgzAppGetTestPaperThree;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wgz.bo.WgzQuestionBankQueryBo;
import com.ruoyi.wgz.domain.WgzQuestionBank;
import com.ruoyi.wgz.mapper.WgzQuestionBankMapper;
import com.ruoyi.wgz.service.IWgzQuestionBankService;

import java.util.*;

/**
 * 题库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class WgzQuestionBankServiceImpl extends ServicePlusImpl<WgzQuestionBankMapper, WgzQuestionBank> implements IWgzQuestionBankService {

    @Override
    public WgzQuestionBank queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<WgzQuestionBank> queryPageList(WgzQuestionBankQueryBo bo) {
        Page<WgzQuestionBank> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<WgzQuestionBank> queryList(WgzQuestionBankQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<WgzQuestionBank> buildQueryWrapper(WgzQuestionBankQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WgzQuestionBank> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, WgzQuestionBank::getCategoryId, bo.getCategoryId());
        lqw.eq(StrUtil.isNotBlank(bo.getQuestionType()), WgzQuestionBank::getQuestionType, bo.getQuestionType());
        return lqw;
    }

    @Override
    public Boolean insert(WgzQuestionReq bo) {
        WgzQuestionBank add = BeanUtil.toBean(bo, WgzQuestionBank.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(WgzQuestionBank bo) {
        WgzQuestionBank update = BeanUtil.toBean(bo, WgzQuestionBank.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WgzQuestionBank entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	/**
	 * 务工者APP相关
	 * =================================================================================================================
	 * =================================================================================================================
	 * =================================================================================================================
	 */

	@Override
	public List<WgzAppGetTestPaperThree> appQueryList(int type, int num) {
		LambdaQueryWrapper<WgzQuestionBank> last = new LambdaQueryWrapper<WgzQuestionBank>().
			eq(WgzQuestionBank::getQuestionType, type).
			last("order by rand() limit " + num);
		List<WgzQuestionBank> wgzQuestionBanks = baseMapper.selectList(last);
		List<WgzAppGetTestPaperThree> wgzAppGetTestPaperThrees = new ArrayList<>();
		BeanUtils.copyProperties(wgzQuestionBanks, wgzAppGetTestPaperThrees);
		return wgzAppGetTestPaperThrees;
	}

	@Override
	public WgzQuestionBank selectById(Long id) {
		return baseMapper.selectById(id);
	}
}
