package com.ruoyi.liandao.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.liandao.domain.QuestionnaireResult;
import com.ruoyi.liandao.domain.QuestionnaireSubmit;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireDto;
import com.ruoyi.liandao.service.QuestionnaireResultService;
import com.ruoyi.liandao.service.QuestionnaireSubmitService;
import com.ruoyi.liandao.mapper.QuestionnaireSubmitMapper;
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.Date;
import java.util.List;

/**
* @author admin
* @description 针对表【ld_questionnaire_submit(问卷填写提交)】的数据库操作Service实现
* @createDate 2024-03-14 10:51:09
*/
@Service
@DataSource(DataSourceType.MASTER)
public class QuestionnaireSubmitServiceImpl extends ServiceImpl<QuestionnaireSubmitMapper, QuestionnaireSubmit>
    implements QuestionnaireSubmitService{

    @Autowired
    private QuestionnaireResultService resultService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitQuestionnaire(List<QuestionnaireDto> dtoList) {
        Date nowDate = DateUtils.getNowDate();
        QuestionnaireSubmit submit = new QuestionnaireSubmit();
        submit.setSubmitTime(nowDate);
        save(submit);

        List<QuestionnaireResult> resultList = new ArrayList<>();
        for (QuestionnaireDto questionnaireDto : dtoList) {
            QuestionnaireResult result = QuestionnaireResult.builder()
                    .submitId(submit.getId())
                    .questionId(questionnaireDto.getId())
                    .submitTime(nowDate)
                    .questionContent(questionnaireDto.getQuestion())
                    .result(questionnaireDto.getResult())
                    .build();
            resultList.add(result);
        }
        resultService.saveBatch(resultList);
    }
}




