package ink.wgink.module.examine.service.question.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.examine.dao.question.IQuestionOptionsDao;
import ink.wgink.module.examine.pojo.dtos.question.QuestionOptionsDTO;
import ink.wgink.module.examine.pojo.vos.question.QuestionOptionsVO;
import ink.wgink.module.examine.service.question.IQuestionOptionsService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: QuestionOptionsServiceImpl
 * @Description: 试题选项
 * @Author: WenG
 * @Date: 2020-09-09 21:00
 * @Version: 1.0
 **/
@Service
public class QuestionOptionsServiceImpl extends DefaultBaseService implements IQuestionOptionsService {

    @Autowired
    private IQuestionOptionsDao questionOptionsDao;

    @Override
    public void save(QuestionOptionsVO questionOptionsVO) throws Exception {
        saveAndReturnId(questionOptionsVO);
    }

    @Override
    public String saveAndReturnId(QuestionOptionsVO questionOptionsVO) throws Exception {
        String questionOptionsId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(questionOptionsVO);
        params.put("questionOptionsId", questionOptionsId);
        questionOptionsDao.save(params);
        return questionOptionsId;
    }

    @Override
    public void save(String questionId, List<QuestionOptionsVO> options) throws Exception {
        // 删除原有选项
        deleteByQuestionId(questionId);
        // 重新添加新选项
        for (QuestionOptionsVO questionOptionsVO : options) {
            questionOptionsVO.setQuestionId(questionId);
            save(questionOptionsVO);
        }
    }

    @Override
    public void delete(List<String> ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("questionOptionsIds", ids);
        questionOptionsDao.delete(params);
    }

    @Override
    public void deleteByQuestionId(String questionId) throws RemoveException {
        questionOptionsDao.deleteByQuestionId(questionId);
    }

    @Override
    public void update(String questionOptionsId, QuestionOptionsVO questionOptionsVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(questionOptionsVO);
        params.put("questionOptionsId", questionOptionsId);
        questionOptionsDao.update(params);
    }

    @Override
    public QuestionOptionsDTO getById(String questionOptionsId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("questionOptionsId", questionOptionsId);
        return questionOptionsDao.get(params);
    }

    @Override
    public List<QuestionOptionsDTO> list(Map<String, Object> params) throws SearchException {
        return questionOptionsDao.list(params);
    }

    @Override
    public SuccessResultList<List<QuestionOptionsDTO>> listPage(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<QuestionOptionsDTO> questionOptionsDTOs = questionOptionsDao.list(page.getParams());
        PageInfo<QuestionOptionsDTO> pageInfo = new PageInfo<>(questionOptionsDTOs);
        return new SuccessResultList<>(questionOptionsDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) throws SearchException {
        Integer count = questionOptionsDao.count(params);
        return count == null ? 0 : count;
    }

    @Override
    public List<QuestionOptionsDTO> listByQuestionId(String questionId) throws SearchException {
        Map<String, Object> params = getHashMap(2);
        params.put("questionId", questionId);
        return list(params);
    }

    @Override
    public List<QuestionOptionsDTO> listByQuestionIds(List<String> questionIds) throws SearchException {
        Map<String, Object> params = getHashMap(2);
        params.put("questionIds", questionIds);
        return list(params);
    }
}