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

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.QuesService;
import com.sundot.airport.exam.constant.QuesCatConstant;
import com.sundot.airport.exam.domain.Ques;
import com.sundot.airport.exam.logic.manage.model.params.QuesPageParam;
import com.sundot.airport.exam.mapper.QuesMapper;
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>QuesServiceImpl<br>
 * <b>说明：</b> 题干表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-18 liangxiao
 */
@Service
public class QuesServiceImpl extends ServiceImpl<QuesMapper, Ques> implements QuesService {

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

    @Resource
    private com.sundot.airport.exam.common.IdUtils idUtils;
    @Override
    public void setServiceId(Ques entity) {
        entity.setQuId(idUtils.generateId("Q"));
    }

    @Override
    public boolean delete(String quId) {
        return this.remove(Wrappers.<Ques>lambdaUpdate().eq(Ques::getQuId, quId));
    }

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

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

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

    @Override
    public PageInfo<Ques> pageList(QuesPageParam param) {
        // 过滤条件
        LambdaQueryWrapper<Ques> queryWrapper = new QueryWrapper<Ques>().lambda()
                .eq(param.getQuType() != null, Ques::getQuType, param.getQuType())
                .eq(StrUtil.isNotBlank(param.getQcId()), Ques::getQcId, QuesCatConstant.OTHER.equals(param.getQcId()) ? "" : param.getQcId())
                .like(StrUtil.isNotBlank(param.getQuContent()), Ques::getQuContent, param.getQuContent())
                .orderByDesc(Ques::getUpdateTime);
        // 分页条件查询
        return this.queryPageList(param, queryWrapper);
    }

    @Override
    public boolean batchCatUpdate(List<String> quIds, String qcId) {
        return this.update(
                Wrappers.<Ques>lambdaUpdate()
                        .in(Ques::getQuId, quIds)
                        .set(Ques::getQcId, qcId)
        );
    }

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

    @Override
    public boolean byQcIdUpdateQcId(String qcId, String updateQcId) {
        return this.update(
                Wrappers.<Ques>lambdaUpdate()
                        .eq(Ques::getQcId, qcId)
                        .set(Ques::getQcId, updateQcId)
        );
    }
}
