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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.service.TestPaperService;
import com.sundot.airport.exam.constant.TestPaperCatConstant;
import com.sundot.airport.exam.domain.TestPaper;
import com.sundot.airport.exam.logic.manage.model.params.TestPaperPageParam;
import com.sundot.airport.exam.mapper.TestPaperMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * <b>功能名：</b>TestPaperServiceImpl<br>
 * <b>说明：</b> 试卷表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-22 liangxiao
 */
@Service
public class TestPaperServiceImpl extends ServiceImpl<TestPaperMapper, TestPaper> implements TestPaperService {

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

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

        entity.setTpId(idUtils.generateId("TP"));
    }

    @Override
    public boolean delete(String tpId) {
        return this.remove(Wrappers.<TestPaper>lambdaUpdate().eq(TestPaper::getTpId, tpId));
    }

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

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

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

    @Override
    public PageInfo<TestPaper> pageList(TestPaperPageParam param) {
        // 过滤条件
        LambdaQueryWrapper<TestPaper> queryWrapper = new QueryWrapper<TestPaper>().lambda()
                .eq(StrUtil.isNotBlank(param.getTpcId()), TestPaper::getTpcId, TestPaperCatConstant.OTHER.equals(param.getTpcId()) ? "" : param.getTpcId())
                .like(StrUtil.isNotBlank(param.getTpName()), TestPaper::getTpName, param.getTpName())
                .orderByDesc(TestPaper::getCreateTime);
        // 分页条件查询
        return this.queryPageList(param, queryWrapper);
    }

    @Override
    public boolean batchCatUpdate(List<String> tpIds, String tpcId) {
        return this.update(
                Wrappers.<TestPaper>lambdaUpdate()
                        .in(TestPaper::getTpId, tpIds)
                        .set(TestPaper::getTpcId, tpcId)
        );
    }

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

    @Override
    public List<TestPaper> byNameStartWithList(String tpName) {
        return this.list(new QueryWrapper<TestPaper>().lambda()
                .likeRight(StrUtil.isNotBlank(tpName), TestPaper::getTpName, tpName));
    }

    @Override
    public List<TestPaper> byTpIdsFindTestPaper(List<String> tpIds) {
        return this.list(new QueryWrapper<TestPaper>().lambda()
                .in(CollectionUtil.isNotEmpty(tpIds), TestPaper::getTpId, tpIds));
    }

    @Override
    public boolean batchUpdate(List<TestPaper> updateTestPaperList) {
        if (CollectionUtils.isEmpty(updateTestPaperList)) {
            return false;
        }
        return this.updateBatchById(updateTestPaperList);
    }

    @Override
    public boolean byTpcIdUpdateTpcId(String tpcId, String updateTpcId) {
        return this.update(
                Wrappers.<TestPaper>lambdaUpdate()
                        .eq(TestPaper::getTpcId, tpcId)
                        .set(TestPaper::getTpcId, updateTpcId)
        );
    }

}
