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.github.pagehelper.PageInfo;
import com.sundot.airport.exam.common.IdUtils;
import com.sundot.airport.exam.common.model.result.QuesCatPageResult;
import com.sundot.airport.exam.common.service.QuesCatService;
import com.sundot.airport.exam.domain.QuesCat;
import com.sundot.airport.exam.logic.manage.model.params.QuesCatPageParam;
import com.sundot.airport.exam.mapper.QuesCatMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * <b>功能名：</b>QuesCatServiceImpl<br>
 * <b>说明：</b> 题目分类表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-17 liangxiao
 */
@Service
public class QuesCatServiceImpl extends ServiceImpl<QuesCatMapper, QuesCat> implements QuesCatService {

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

    @Resource
    private IdUtils idUtils;
    @Override
    public void setServiceId(QuesCat entity) {

        entity.setQcId(idUtils.generateId("QC"));
    }

    @Override
    public boolean delete(String qcId) {
        return this.remove(Wrappers.<QuesCat>lambdaUpdate().eq(QuesCat::getQcId, qcId));
    }

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

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

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

    @Override
    public PageInfo<QuesCatPageResult> pageList(QuesCatPageParam param) {
        // todo 这里需要后续拼接分页参数生成QueryWrapper
        PageInfo<QuesCatPageResult> res = new PageInfo<>();
        // 分页条件查询
        List<QuesCatPageResult> dataList = getBaseMapper().getQueCatList(param);
        Optional.ofNullable(dataList).orElse(new ArrayList<>()).sort(Comparator.comparing(QuesCatPageResult::getUpdateTime).reversed());
        res.setList(dataList);
        return res;
    }

    @Override
    public Optional<QuesCat> byNameCheckExistsGet(String qcName) {
        return Optional.ofNullable(this.getOne(Wrappers.<QuesCat>lambdaQuery()
                .eq(QuesCat::getQcName, qcName)
                .eq(QuesCat::getDelStatus, 0)
                .last("limit 1")));
    }

    @Override
    public List<QuesCat> byQcIds(List<String> qcIds) {
        return this.queryList(Wrappers.<QuesCat>lambdaUpdate().in(QuesCat::getQcId, qcIds));
    }
}
