package com.ruoyi.liandao.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.query.LambdaQueryWrapperX;
import com.ruoyi.liandao.domain.QuestionnaireOption;
import com.ruoyi.liandao.mapper.QuestionnaireOptionMapper;
import com.ruoyi.liandao.service.QuestionnaireOptionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【ld_questionnaire_option(问卷选项)】的数据库操作Service实现
 * @createDate 2024-03-14 10:51:09
 */
@Service
@DataSource(DataSourceType.MASTER)
public class QuestionnaireOptionServiceImpl extends ServiceImpl<QuestionnaireOptionMapper, QuestionnaireOption>
        implements QuestionnaireOptionService {

    @Override
    public void saveOptionList(Long configId, List<String> optionList) {
        if (configId == null) {
            return;
        }
        remove(new LambdaQueryWrapperX<QuestionnaireOption>()
                .eq(QuestionnaireOption::getConfigId, configId));
        if (CollUtil.isNotEmpty(optionList)) {
            List<QuestionnaireOption> list = new ArrayList<>();
            for (String option : optionList) {
                list.add(QuestionnaireOption.builder()
                        .configId(configId)
                        .questionnaireOption(option)
                        .build());
            }
            saveBatch(list);
        }
    }

    @Override
    public List<String> selectByConfigId(Long configId) {
        return list(new LambdaQueryWrapperX<QuestionnaireOption>()
                .eq(QuestionnaireOption::getConfigId, configId))
                .stream().map(data -> data.getQuestionnaireOption())
                .collect(Collectors.toList());
    }
}




