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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.exam.common.model.result.TestPaperQuesAndQues;
import com.sundot.airport.exam.common.service.TestPaperQuesService;
import com.sundot.airport.exam.domain.TestPaperQues;
import com.sundot.airport.exam.mapper.TestPaperQuesMapper;
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>TestPaperQuesServiceImpl<br>
 * <b>说明：</b> 试卷题目的计分数据持久业务类 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-22 liangxiao
 */
@Service
public class TestPaperQuesServiceImpl extends ServiceImpl<TestPaperQuesMapper, TestPaperQues> implements TestPaperQuesService {

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

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

        entity.setTpqId(idUtils.generateId("TPQ"));
    }

    @Override
    public boolean delete(String tpqId) {
        return this.remove(Wrappers.<TestPaperQues>lambdaUpdate().eq(TestPaperQues::getTpqId, tpqId));
    }

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

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

    @Override
    public Optional<TestPaperQues> get(String serviceId) {
        // 根据业务ID获取详情数据
        return Optional.ofNullable(this.getOne(Wrappers.<TestPaperQues>lambdaQuery()
                .eq(TestPaperQues::getTpqId, serviceId)));
    }

    @Override
    public boolean byTpIdsDeleteList(List<String> tpIds) {
        if (CollectionUtil.isEmpty(tpIds)) {
            return false;
        }
        if (tpIds.size() == 1) {
            return this.remove(Wrappers.<TestPaperQues>lambdaUpdate().eq(TestPaperQues::getTpId, tpIds.get(0)));
        }
        return this.remove(Wrappers.<TestPaperQues>lambdaUpdate().in(TestPaperQues::getTpId, tpIds));
    }

    @Override
    public List<TestPaperQues> byTpIdFindList(List<String> tpIds) {
        if (CollectionUtil.isEmpty(tpIds)) {
            return new ArrayList<>();
        }
        if (tpIds.size() == 1) {
            return this.queryList(Wrappers.<TestPaperQues>lambdaQuery().eq(TestPaperQues::getTpId, tpIds.get(0)));
        }
        return this.queryList(Wrappers.<TestPaperQues>lambdaQuery().in(TestPaperQues::getTpId, tpIds));
    }

    @Override
    public List<TestPaperQuesAndQues> byTpIdFindAndQuesList(List<String> tpIds) {
        if (CollectionUtil.isEmpty(tpIds)) {
            return new ArrayList<>();
        }
        return this.getBaseMapper().byTpIdFindAndQuesList(tpIds);
    }

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

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

}
