package com.ruoyi.liandao.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
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.common.utils.StringUtils;
import com.ruoyi.liandao.domain.QuestionnaireConfig;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireConfigAddReqDto;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireConfigEditReqDto;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireDto;
import com.ruoyi.liandao.service.QuestionnaireConfigService;
import com.ruoyi.liandao.mapper.QuestionnaireConfigMapper;
import com.ruoyi.liandao.service.QuestionnaireOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
* @author admin
* @description 针对表【ld_questionnaire_config(问卷配置)】的数据库操作Service实现
* @createDate 2024-03-14 10:51:09
*/
@Service
@DataSource(DataSourceType.MASTER)
public class QuestionnaireConfigServiceImpl extends ServiceImpl<QuestionnaireConfigMapper, QuestionnaireConfig>
    implements QuestionnaireConfigService{

    @Autowired
    private QuestionnaireOptionService optionService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(QuestionnaireConfigAddReqDto dto) {
        QuestionnaireConfig config = Convert.convert(QuestionnaireConfig.class, dto);
        save(config);
        optionService.saveOptionList(config.getId(), dto.getOptionList().stream()
                .map(data -> data.get("option").toString()).collect(Collectors.toList()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(QuestionnaireConfigEditReqDto dto) {
        QuestionnaireConfig config = Convert.convert(QuestionnaireConfig.class, dto);
        updateById(config);
        List<String> optionList = new ArrayList<>();
        if(CollUtil.isNotEmpty(dto.getOptionList())){
            optionList = dto.getOptionList().stream()
                    .map(data -> data.get("option").toString()).collect(Collectors.toList());
        }
        optionService.saveOptionList(config.getId(), optionList);
    }

    @Override
    public QuestionnaireDto detail(Long id) {
        QuestionnaireConfig config = getById(id);
        QuestionnaireDto result = Convert.convert(QuestionnaireDto.class, config);
        result.setOptionList(optionService.selectByConfigId(id));
        return result;
    }

    @Override
    public List<QuestionnaireDto> getQuestionnaire() {
        List<QuestionnaireDto> result = new ArrayList<>();
        List<QuestionnaireConfig> configList = list(new LambdaQueryWrapperX<QuestionnaireConfig>()
                .orderByAsc(QuestionnaireConfig::getOrderNum));
        for (QuestionnaireConfig config : configList) {
            QuestionnaireDto dto = Convert.convert(QuestionnaireDto.class, config);
            List<String> optionList = optionService.selectByConfigId(config.getId());
            dto.setOptionList(optionList);
            result.add(dto);
        }
        return result;
    }
}




