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.ExamRecOptService;
import com.sundot.airport.exam.domain.ExamRecOpt;
import com.sundot.airport.exam.mapper.ExamRecOptMapper;
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>ExamRecOptServiceImpl<br>
 * <b>说明：</b> 测试记录选项表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-29 liangxiao
 */
@Service
public class ExamRecOptServiceImpl extends ServiceImpl<ExamRecOptMapper, ExamRecOpt> implements ExamRecOptService {

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

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

        entity.setEroId(idUtils.generateId("ERO"));
    }

    @Override
    public boolean delete(String eroId) {
        return this.remove(Wrappers.<ExamRecOpt>lambdaUpdate().eq(ExamRecOpt::getEroId, eroId));
    }

    @Override
    public boolean deleteList(List<String> eroIds) {
        return this.remove(Wrappers.<ExamRecOpt>lambdaUpdate().in(ExamRecOpt::getEroId, eroIds));
    }

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

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

    @Override
    public List<ExamRecOpt> byQuIdList(List<String> quIds) {
        if (quIds.isEmpty()) {
            return new ArrayList<>();
        }
        if (quIds.size() == 1) {
            return this.queryList(Wrappers.<ExamRecOpt>lambdaQuery()
                    .eq(ExamRecOpt::getEroQuId, quIds.get(0))
                    .orderByAsc(ExamRecOpt::getEroSort, ExamRecOpt::getId)
            );
        }
        return this.queryList(Wrappers.<ExamRecOpt>lambdaQuery()
                .in(ExamRecOpt::getEroQuId, quIds)
                .orderByAsc(ExamRecOpt::getEroSort, ExamRecOpt::getId)
        );
    }

    @Override
    public List<ExamRecOpt> byErId(String erId) {
        return this.queryList(Wrappers.<ExamRecOpt>lambdaQuery()
                .eq(ExamRecOpt::getErId, erId)
                .orderByAsc(ExamRecOpt::getEroSort, ExamRecOpt::getId)
        );
    }

    @Override
    public void updateEroConnNoBatch(List<ExamRecOpt> list) {
        if (list.size() > 0) {
            this.getBaseMapper().updateEroConnNoBatch(list);
        }
    }

    @Override
    public boolean updateEroConnNo(ExamRecOpt examRecOpt) {
        return this.update(Wrappers
                .<ExamRecOpt>lambdaUpdate()
                .eq(ExamRecOpt::getEroId, examRecOpt.getEroId())
                .set(ExamRecOpt::getEroConnNo, examRecOpt.getEroConnNo())
                .set(ExamRecOpt::getEroCorrectStatus, examRecOpt.getEroCorrectStatus())
                .set(ExamRecOpt::getEroCorrectBlankIdx, examRecOpt.getEroCorrectBlankIdx())
        );
    }
}
