package com.xinelu.familydoctor.service.impl;

import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.familydoctor.entity.EvaluateOption;
import com.xinelu.familydoctor.entity.EvaluateQuestion;
import com.xinelu.familydoctor.entity.EvaluateSurvey;
import com.xinelu.familydoctor.mapper.EvaluateOptionMapper;
import com.xinelu.familydoctor.mapper.EvaluateQuestionMapper;
import com.xinelu.familydoctor.mapper.EvaluateSurveyMapper;
import com.xinelu.familydoctor.service.EvaluateSurveyService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @author gaoyu
 * @description 自评问卷业务实现类
 * @date 2023-10-11 13:24
 */
@Service
public class EvaluateSurveyServiceImpl implements EvaluateSurveyService {
    @Resource
    private EvaluateSurveyMapper evaluateSurveyMapper;
    @Resource
    private EvaluateQuestionMapper evaluateQuestionMapper;
    @Resource
    private EvaluateOptionMapper evaluateOptionMapper;

    @Override
    public List<EvaluateSurvey> findList(EvaluateSurvey entity) {
        return evaluateSurveyMapper.findList(entity);
    }

    @Override
    public EvaluateSurvey getSurvey(String surveySubject) {
        return evaluateSurveyMapper.getSurvey(surveySubject);
    }

    @Override
    public void insert(EvaluateSurvey entity) {
        entity.setCreateBy(SecurityUtils.getUserId());
        entity.setCreateTime(new Date());
        entity.setDelFlag("1");
        int row = evaluateSurveyMapper.insert(entity);
        if (row > 0) {
            List<EvaluateQuestion> questions = entity.getQuestions();
            if (!CollectionUtils.isEmpty(questions)) {
                for (EvaluateQuestion question : questions) {
                    question.setSurveyId(entity.getId());
                    row = evaluateQuestionMapper.insert(question);
                    if (row > 0) {
                        List<EvaluateOption> options = question.getOptions();
                        if (!CollectionUtils.isEmpty(options)) {
                            for (EvaluateOption option : options) {
                                option.setSurveyId(entity.getId());
                                option.setQuestionId(question.getId());
                                evaluateOptionMapper.insert(option);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void update(EvaluateSurvey entity) {
        entity.setUpdateBy(SecurityUtils.getUserId());
        entity.setUpdateTime(new Date());
        entity.setDelFlag("1");
        int row = evaluateSurveyMapper.update(entity);
        if (row > 0) {
            List<EvaluateQuestion> questions = entity.getQuestions();
            if (!CollectionUtils.isEmpty(questions)) {
                evaluateQuestionMapper.clearSurveyId(entity.getId());
                for (EvaluateQuestion question : questions) {
                    question.setSurveyId(entity.getId());
                    if (Objects.isNull(question.getId())) {
                        row = evaluateQuestionMapper.insert(question);
                    } else {
                        row = evaluateQuestionMapper.update(question);
                    }
                    if (row > 0) {
                        List<EvaluateOption> options = question.getOptions();
                        if (!CollectionUtils.isEmpty(options)) {
                            evaluateOptionMapper.clearRelationId(question.getId(), entity.getId());
                            for (EvaluateOption option : options) {
                                option.setSurveyId(entity.getId());
                                option.setQuestionId(question.getId());
                                if (Objects.isNull(option.getId())) {
                                    evaluateOptionMapper.insert(option);
                                } else {
                                    evaluateOptionMapper.update(option);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
