package com.sundot.airport.exam.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.exam.common.service.QuesOptService;
import com.sundot.airport.exam.domain.QuesOpt;
import com.sundot.airport.exam.mapper.QuesOptMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <b>功能名：</b>QuesOptServiceImpl<br>
 * <b>说明：</b> 题目选项表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-18 liangxiao
 */
@Service
public class QuesOptServiceImpl extends ServiceImpl<QuesOptMapper, QuesOpt> implements QuesOptService {

    private static final Logger log = LoggerFactory.getLogger(QuesOptServiceImpl.class);

    @Resource
    private com.sundot.airport.exam.common.IdUtils idUtils;
    @Override
    public void setServiceId(QuesOpt entity) {

        entity.setQoId(idUtils.generateId("QO"));
    }

    @Override
    public boolean delete(String qoId) {
        return this.remove(Wrappers.<QuesOpt>lambdaUpdate().eq(QuesOpt::getQoId, qoId));
    }

    @Override
    public boolean deleteList(List<String> qoIds) {
        if (qoIds.isEmpty()) {
            return false;
        }
        if (qoIds.size() == 1) {
            return this.delete(qoIds.get(0));
        }
        return this.remove(Wrappers.<QuesOpt>lambdaUpdate().in(QuesOpt::getQoId, qoIds));
    }

    @Override
    public boolean update(QuesOpt entity) {
        return this.update(
                entity,
                Wrappers.<QuesOpt>lambdaUpdate().eq(QuesOpt::getQoId, entity.getQoId())
        );
    }

    @Override
    public Optional<QuesOpt> get(String serviceId) {
        // 根据题目分类表业务ID获取详情数据
        return Optional.ofNullable(this.getOne(Wrappers.<QuesOpt>lambdaQuery()
                .eq(QuesOpt::getQoId, serviceId)));
    }

    @Override
    public boolean byQuIdsDeleteList(List<String> quIds) {
        if (quIds.isEmpty()) {
            return false;
        }
        if (quIds.size() == 1) {
            return this.remove(Wrappers.<QuesOpt>lambdaUpdate().eq(QuesOpt::getQuId, quIds.get(0)));
        }
        return this.remove(Wrappers.<QuesOpt>lambdaUpdate().in(QuesOpt::getQuId, quIds));
    }

    @Override
    public List<QuesOpt> byQuIdFindList(List<String> quIds) {
        if (quIds.isEmpty()) {
            return new ArrayList<>();
        }
        if (quIds.size() == 1) {
            return this.queryList(Wrappers.<QuesOpt>lambdaQuery().eq(QuesOpt::getQuId, quIds.get(0)).orderByAsc(QuesOpt::getSort));
        }
        return this.queryList(Wrappers.<QuesOpt>lambdaQuery().in(QuesOpt::getQuId, quIds).orderByAsc(QuesOpt::getSort));
    }

    @Override
    public List<QuesOpt> byQuIdFindCorrectList(List<String> quIds) {
        if (quIds.isEmpty()) {
            return new ArrayList<>();
        }
        if (quIds.size() == 1) {
            return this.queryList(Wrappers.<QuesOpt>lambdaQuery()
                    .eq(QuesOpt::getQuId, quIds.get(0)).eq(QuesOpt::getQoCorrect, true));
        }
        return this.queryList(Wrappers.<QuesOpt>lambdaQuery()
                .in(QuesOpt::getQuId, quIds)
                .eq(QuesOpt::getQoCorrect, true));
    }
}
