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.model.result.TestPaperCatPageResult;
import com.sundot.airport.exam.common.service.TestPaperCatService;
import com.sundot.airport.exam.domain.TestPaperCat;
import com.sundot.airport.exam.logic.manage.model.params.TestPaperCatPageParam;
import com.sundot.airport.exam.mapper.TestPaperCatMapper;
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>TestPaperCatServiceImpl<br>
 * <b>说明：</b> 试卷分类表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-18 liangxiao
 */
@Service
public class TestPaperCatServiceImpl extends ServiceImpl<TestPaperCatMapper, TestPaperCat> implements TestPaperCatService {

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

    @Resource
    private com.sundot.airport.exam.common.IdUtils idUtils;
    @Override
    public void setServiceId(TestPaperCat entity) {
        entity.setTpcId(idUtils.generateId("TPC"));
    }

    @Override
    public boolean delete(String tpcId) {
        return this.remove(Wrappers.<TestPaperCat>lambdaUpdate().eq(TestPaperCat::getTpcId, tpcId));
    }

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

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

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

    @Override
    public PageInfo<TestPaperCatPageResult> pageList(TestPaperCatPageParam param) {
        // todo 这里需要后续拼接分页参数生成QueryWrapper
        // 分页条件查询
        PageInfo<TestPaperCatPageResult> res = new PageInfo<>();

        List<TestPaperCatPageResult> dataList = getBaseMapper().getTestPaperCatList(param);
        Optional.ofNullable(dataList).orElse(new ArrayList<>()).sort(Comparator.comparing(TestPaperCatPageResult::getUpdateTime).reversed());
        res.setList(dataList);
        return res;
    }

    @Override
    public Optional<TestPaperCat> byNameCheckExistsGet(String tpcName) {
        return Optional.ofNullable(this.getOne(Wrappers.<TestPaperCat>lambdaQuery()
                .eq(TestPaperCat::getTpcName, tpcName)
                .last("limit 1")));
    }

}
