package io.hmit.modules.fhjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.modules.fhjw.dao.FhjwAnswerDao;
import io.hmit.modules.fhjw.dao.FhjwQuestionDao;
import io.hmit.modules.fhjw.dto.FhjwAnswerDTO;
import io.hmit.modules.fhjw.dto.FhjwQuestionDTO;
import io.hmit.modules.fhjw.entity.FhjwQuestionEntity;
import io.hmit.modules.fhjw.service.FhjwAnswerService;
import io.hmit.modules.fhjw.service.FhjwQuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 问卷问题表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-02
 */
@Service
public class FhjwQuestionServiceImpl extends CrudServiceImpl<FhjwQuestionDao, FhjwQuestionEntity, FhjwQuestionDTO> implements FhjwQuestionService {

    @Autowired
    private FhjwAnswerService fhjwAnswerService;

    @Override
    public QueryWrapper<FhjwQuestionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String tempId = params.get("tempId")==null?"":params.get("tempId").toString();

        QueryWrapper<FhjwQuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(tempId), "temp_id", tempId);

        return wrapper;
    }


    @Override
    public List<FhjwQuestionDTO> getQuestionList(Long tempId) {
        //根据模版Id 查询出所有问题
        Map<String, Object> params = new HashMap<>();
        params.put("tempId",tempId);
        List<FhjwQuestionDTO> dtos = list(params);
        if(dtos.size()<=0){
            return null;
        }
        dtos.stream().forEach(item ->{
            Map<String, Object> query = new HashMap<>();
            query.put("questionId",item.getId());
            List<FhjwAnswerDTO> answerList = fhjwAnswerService.list(query);
            item.setAnswerList(answerList);
        });

        return dtos;
    }
}
