package com.zd.exam.service.impl;

import com.zd.common.security.service.TokenService;
import com.zd.exam.domain.ElQuAnswer;
import com.zd.exam.mapper.ElQuAnswerMapper;
import com.zd.exam.service.ElQuAnswerService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 候选答案(ElQuAnswer)表服务实现类
 *
 * @author liubo
 * @since 2021-08-30 17:10:21
 */
@Service("elQuAnswerService")
public class ElQuAnswerServiceImpl implements ElQuAnswerService {
    @Resource
    private ElQuAnswerMapper elQuAnswerMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ElQuAnswer queryById(Long id) {
        return this.elQuAnswerMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ElQuAnswer> queryAllByLimit(int offset, int limit) {
        return this.elQuAnswerMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param elQuAnswer 实例对象
     * @return 实例对象
     */
    @Override
    public ElQuAnswer insert(ElQuAnswer elQuAnswer) {
        this.elQuAnswerMapper.insert(elQuAnswer);
        return elQuAnswer;
    }

    /**
     * 修改数据
     *
     * @param elQuAnswer 实例对象
     * @return 实例对象
     */
    @Override
    public ElQuAnswer update(ElQuAnswer elQuAnswer) {
        this.elQuAnswerMapper.update(elQuAnswer);
        return this.queryById(elQuAnswer.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.elQuAnswerMapper.deleteById(id) > 0;
    }

    @Override
    public List<ElQuAnswer> queryByQuId(Long quId) {
        ElQuAnswer elQuAnswer = new ElQuAnswer();
        elQuAnswer.setQuId(quId);
        return elQuAnswerMapper.queryAll(elQuAnswer);
    }

    @Override
    public void saveAll(@NotNull Long quId, List<ElQuAnswer> answerList) {
        //最终要保存的列表
        List<ElQuAnswer> saveList = new ArrayList<>();

        //已存在的标签列表
        ElQuAnswer elQuAnswer = new ElQuAnswer();
        elQuAnswer.setQuId(quId);
        List<ElQuAnswer> answers = elQuAnswerMapper.queryAll(elQuAnswer);
        List<Long> ids = new ArrayList<>();
        for (ElQuAnswer answer : answers) {
            ids.add(answer.getId());
        }

        if (!CollectionUtils.isEmpty(answerList)) {
            for (ElQuAnswer answer : answerList) {

                //标签ID
                Long id = answer.getId();
                answer.setQuId(quId);

                //补全ID避免新增
                if (ids.contains(id)) {
                    ids.remove(id);
                }
                saveList.add(answer);
            }

            //删除已移除
            if (!ids.isEmpty()) {
                elQuAnswerMapper.deleteByIds(ids);
            }

            //保存标签列表
            if (!CollectionUtils.isEmpty(saveList)) {
                elQuAnswerMapper.insertOrUpdateBatch(saveList);
            }

        } else {
            // 清空问题
            elQuAnswerMapper.deleteByQuId(quId);
        }
    }
}
