package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerCommonQuestion;
import shop.alien.mapper.LawyerCommonQuestionMapper;
import shop.alien.store.service.LawyerCommonQuestionService;

import java.util.List;

/**
 * 常见问题 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerCommonQuestionServiceImpl extends ServiceImpl<LawyerCommonQuestionMapper, LawyerCommonQuestion> implements LawyerCommonQuestionService {

    private final LawyerCommonQuestionMapper commonQuestionMapper;

    @Override
    public R<IPage<LawyerCommonQuestion>> getCommonQuestionList(int pageNum, int pageSize, String questionText,
                                                           Integer categoryType, Integer problemScenarioId, Integer status) {
        log.info("LawyerCommonQuestionServiceImpl.getCommonQuestionList?pageNum={},pageSize={},questionText={},categoryType={},problemScenarioId={},status={}",
                pageNum, pageSize, questionText, categoryType, problemScenarioId, status);
        Page<LawyerCommonQuestion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LawyerCommonQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerCommonQuestion::getDeleteFlag, 0);
        if (StringUtils.hasText(questionText)) {
            queryWrapper.like(LawyerCommonQuestion::getQuestionText, questionText);
        }
        if (categoryType != null) {
            queryWrapper.eq(LawyerCommonQuestion::getCategoryType, categoryType);
        }
        if (problemScenarioId != null) {
            queryWrapper.eq(LawyerCommonQuestion::getProblemScenarioId, problemScenarioId);
        }
        if (status != null) {
            queryWrapper.eq(LawyerCommonQuestion::getStatus, status);
        }
        queryWrapper.orderByAsc(LawyerCommonQuestion::getSortOrder);
        IPage<LawyerCommonQuestion> pageResult = this.page(page, queryWrapper);
        return R.data(pageResult);
    }

    @Override
    public List<LawyerCommonQuestion> getListByCategoryType(Integer categoryType) {
        log.info("LawyerCommonQuestionServiceImpl.getListByCategoryType?categoryType={}", categoryType);
        LambdaQueryWrapper<LawyerCommonQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerCommonQuestion::getCategoryType, categoryType)
                .eq(LawyerCommonQuestion::getDeleteFlag, 0)
                .eq(LawyerCommonQuestion::getStatus, 1)
                .orderByAsc(LawyerCommonQuestion::getSortOrder);
        return this.list(queryWrapper);
    }

    @Override
    public R<LawyerCommonQuestion> addCommonQuestion(LawyerCommonQuestion commonQuestion) {
        log.info("LawyerCommonQuestionServiceImpl.addCommonQuestion?commonQuestion={}", commonQuestion);
        boolean result = this.save(commonQuestion);
        if (result) {
            return R.data(commonQuestion);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<LawyerCommonQuestion> editCommonQuestion(LawyerCommonQuestion commonQuestion) {
        log.info("LawyerCommonQuestionServiceImpl.editCommonQuestion?commonQuestion={}", commonQuestion);
        boolean result = this.updateById(commonQuestion);
        if (result) {
            return R.data(commonQuestion);
        }
        return R.fail("修改失败");
    }

    @Override
    public R<Boolean> deleteCommonQuestion(Integer id) {
        log.info("LawyerCommonQuestionServiceImpl.deleteCommonQuestion?id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @Override
    public R<List<LawyerCommonQuestion>> getCommonQuestionListByNum(Integer num) {
        log.info("LawyerCommonQuestionServiceImpl.getCommonQuestionListByNum?num={}", num);
        
        // 参数校验：如果 num 小于等于0，返回空列表
        if (num == null || num <= 0) {
            return R.data(new java.util.ArrayList<>());
        }
        
        // 构建查询条件：只查询未删除的记录，随机排序
        LambdaQueryWrapper<LawyerCommonQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerCommonQuestion::getDeleteFlag, 0)
                .last("ORDER BY RAND() LIMIT " + num);
        
        List<LawyerCommonQuestion> list = this.list(queryWrapper);
        return R.data(list);
    }
}

