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.FhjwAnswerListDao;
import io.hmit.modules.fhjw.dto.FhjwAnswerDTO;
import io.hmit.modules.fhjw.dto.FhjwAnswerListDTO;
import io.hmit.modules.fhjw.entity.FhjwAnswerEntity;
import io.hmit.modules.fhjw.service.FhjwAnswerListService;
import io.hmit.modules.fhjw.service.FhjwAnswerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

/**
 * 问题答案表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-02
 */
@Service
public class FhjwAnswerServiceImpl extends CrudServiceImpl<FhjwAnswerDao, FhjwAnswerEntity, FhjwAnswerDTO> implements FhjwAnswerService {

    @Autowired
    private FhjwAnswerListService fhjwAnswerListService;

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

        QueryWrapper<FhjwAnswerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(questionId), "question_id", questionId);

        return wrapper;
    }

    @Override
    public void save(FhjwAnswerDTO dto) {
        Optional<FhjwAnswerListDTO>  result = Optional.ofNullable(fhjwAnswerListService.getByAnswer(dto.getAnswer()));
        result.ifPresent(a -> dto.setAnswerlistId(a.getId()));
        super.save(dto);
    }
}
