package com.easyproject.clientbus.service.operate.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyproject.clientbus.domain.dto.SelectPageDto;
import com.easyproject.clientbus.domain.vo.OperateCommonQuestionsVo;
import com.easyproject.clientbus.domain.vo.OperateHotQuestionsVo;
import com.easyproject.clientbus.domain.vo.OperateMindReaderVo;
import com.easyproject.clientbus.service.operate.IQuestionsService;
import com.easyproject.common.core.domain.AjaxClientResult;
import com.easyproject.service.domain.operate.OperateCommonQuestions;
import com.easyproject.service.domain.operate.OperateHotQuestions;
import com.easyproject.service.domain.operate.OperateMindReader;
import com.easyproject.service.mapper.operate.OperateCommonQuestionsMapper;
import com.easyproject.service.mapper.operate.OperateHotQuestionsMapper;
import com.easyproject.service.mapper.operate.OperateMindReaderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 方法说明: 问题相关
 * @param
 * @return
 * @author java-101
 * @date 2025-03-23 16:39:14
 */
@Slf4j
@Service
public class QuestionsServiceImpl implements IQuestionsService {

    @Resource
    private OperateCommonQuestionsMapper operateCommonQuestionsMapper;
    @Autowired
    private OperateMindReaderMapper operateMindReaderMapper;
    @Autowired
    private OperateHotQuestionsMapper operateHotQuestionsMapper;

    /**
     * 方法说明: 常见问题
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 16:42:52
     */
    @Override
    public AjaxClientResult selCommonQuestions(SelectPageDto dto) {
        LambdaQueryWrapper<OperateCommonQuestions> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OperateCommonQuestions::getStatus, 1);
        if(ObjectUtil.isNotEmpty(dto.getKeyword())){
            wrapper.like(OperateCommonQuestions::getQuestionContent, dto.getKeyword());
        }
        wrapper.orderByAsc(OperateCommonQuestions::getSort);

        List<OperateCommonQuestionsVo> voList = null;

        IPage<OperateCommonQuestions> questionsList = operateCommonQuestionsMapper.selectPage(new Page(dto.getPageNum(), dto.getPageSize()), wrapper);
        if(ObjectUtil.isNotEmpty(questionsList.getRecords())){
            voList = BeanUtil.copyToList(questionsList.getRecords(), OperateCommonQuestionsVo.class);
        }

        //自定义分页信息
        IPage<OperateCommonQuestionsVo> voIPage = new Page<>();
        voIPage.setRecords(voList);
        voIPage.setPages(questionsList.getPages());
        voIPage.setSize(questionsList.getSize());
        voIPage.setTotal(questionsList.getTotal());
        voIPage.setCurrent(questionsList.getCurrent());

        return AjaxClientResult.success(voIPage);
    }

    /**
     * 方法说明: 猜你所想
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 16:53:08
     */
    @Override
    public AjaxClientResult guessWhatYouWant(SelectPageDto dto) {
        LambdaQueryWrapper<OperateMindReader> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OperateMindReader::getStatus, 1);
        wrapper.orderByAsc(OperateMindReader::getSort);

        List<OperateMindReaderVo> voList = null;

        IPage<OperateMindReader> questionsList = operateMindReaderMapper.selectPage(new Page(dto.getPageNum(), dto.getPageSize()), wrapper);
        if(ObjectUtil.isNotEmpty(questionsList.getRecords())){
            voList = BeanUtil.copyToList(questionsList.getRecords(), OperateMindReaderVo.class);
        }

        //自定义分页信息
        IPage<OperateMindReaderVo> voIPage = new Page<>();
        voIPage.setRecords(voList);
        voIPage.setPages(questionsList.getPages());
        voIPage.setSize(questionsList.getSize());
        voIPage.setTotal(questionsList.getTotal());
        voIPage.setCurrent(questionsList.getCurrent());

        return AjaxClientResult.success(voIPage);
    }

    /**
     * 方法说明: 热门问题
     * @param
     * @return
     * @author java-101
     * @date 2025-03-23 16:53:19
     */
    @Override
    public AjaxClientResult hotQuestions(SelectPageDto dto) {
        LambdaQueryWrapper<OperateHotQuestions> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OperateHotQuestions::getStatus, 1);
        wrapper.orderByAsc(OperateHotQuestions::getSort);

        List<OperateHotQuestionsVo> voList = null;

        IPage<OperateHotQuestions> questionsList = operateHotQuestionsMapper.selectPage(new Page(dto.getPageNum(), dto.getPageSize()), wrapper);
        if(ObjectUtil.isNotEmpty(questionsList.getRecords())){
            voList = BeanUtil.copyToList(questionsList.getRecords(), OperateHotQuestionsVo.class);
        }

        //自定义分页信息
        IPage<OperateHotQuestionsVo> voIPage = new Page<>();
        voIPage.setRecords(voList);
        voIPage.setPages(questionsList.getPages());
        voIPage.setSize(questionsList.getSize());
        voIPage.setTotal(questionsList.getTotal());
        voIPage.setCurrent(questionsList.getCurrent());

        return AjaxClientResult.success(voIPage);
    }

    /**
     * 方法说明: 常见问题-随机查询
     * @param
     * @return
     * @author java-101
     * @date 2025-03-29 20:12:28
     */
    @Override
    public AjaxClientResult selCommonQuestionsRandom(SelectPageDto dto) {
        LambdaQueryWrapper<OperateCommonQuestions> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OperateCommonQuestions::getStatus, 1);
        if(ObjectUtil.isNotEmpty(dto.getKeyword())){
            wrapper.like(OperateCommonQuestions::getQuestionContent, dto.getKeyword());
        }
        wrapper.last("ORDER BY RAND()");

        List<OperateCommonQuestionsVo> voList = null;

        IPage<OperateCommonQuestions> questionsList = operateCommonQuestionsMapper.selectPage(new Page(dto.getPageNum(), dto.getPageSize()), wrapper);
        if(ObjectUtil.isNotEmpty(questionsList.getRecords())){
            voList = BeanUtil.copyToList(questionsList.getRecords(), OperateCommonQuestionsVo.class);
        }

        //自定义分页信息
        IPage<OperateCommonQuestionsVo> voIPage = new Page<>();
        voIPage.setRecords(voList);
        voIPage.setPages(questionsList.getPages());
        voIPage.setSize(questionsList.getSize());
        voIPage.setTotal(questionsList.getTotal());
        voIPage.setCurrent(questionsList.getCurrent());

        return AjaxClientResult.success(voIPage);
    }
}
