package com.ruoyi.wgz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
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.WgzQuestionsConfigurationQueryBo;
import com.ruoyi.wgz.domain.WgzQuestionsConfiguration;
import com.ruoyi.wgz.mapper.WgzQuestionsConfigurationMapper;
import com.ruoyi.wgz.service.IWgzQuestionsConfigurationService;

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

/**
 * 题库配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class WgzQuestionsConfigurationServiceImpl extends ServicePlusImpl<WgzQuestionsConfigurationMapper, WgzQuestionsConfiguration> implements IWgzQuestionsConfigurationService {

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

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

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

    private LambdaQueryWrapper<WgzQuestionsConfiguration> buildQueryWrapper(WgzQuestionsConfigurationQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WgzQuestionsConfiguration> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

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

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

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

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

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

	@Override
	public WgzQuestionsConfiguration appQueryLimitOne() {
		QueryWrapper<WgzQuestionsConfiguration> queryWrapper = new QueryWrapper<>();
		queryWrapper.last("LIMIT 1");
		WgzQuestionsConfiguration wgzQuestionsConfiguration = baseMapper.selectOne(queryWrapper);
		if(wgzQuestionsConfiguration==null){
			throw new RuntimeException("题库配置为空");
		}
		return wgzQuestionsConfiguration;
	}
}
