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.model.result.ExamAndTpResult;
import com.sundot.airport.exam.common.service.ExamService;
import com.sundot.airport.exam.domain.Exam;
import com.sundot.airport.exam.logic.manage.model.params.ExamPageParam;
import com.sundot.airport.exam.mapper.ExamMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * <b>功能名：</b>ExamServiceImpl<br>
 * <b>说明：</b> 考试管理表 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：
 *
 * @author 2021-06-23 liangxiao
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService {

    private static final Logger log = LoggerFactory.getLogger(ExamServiceImpl.class);
    @Resource
    private com.sundot.airport.exam.common.IdUtils idUtils;
    @Override
    public void setServiceId(Exam entity) {
        entity.setExId(idUtils.generateId("EX"));
    }

    @Override
    public boolean delete(String exId) {
        return this.remove(Wrappers.<Exam>lambdaUpdate().eq(Exam::getExId, exId));
    }

    @Override
    public boolean deleteList(List<String> exIds) {
        return this.remove(Wrappers.<Exam>lambdaUpdate().in(Exam::getExId, exIds));
    }

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

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

    @Override
    public PageInfo<Exam> pageList(ExamPageParam param) {
        // 过滤条件
        LambdaQueryWrapper<Exam> queryWrapper = new QueryWrapper<Exam>().lambda()
                .like(StrUtil.isNotBlank(param.getExName()), Exam::getExName, param.getExName())
                .orderByDesc(Exam::getCreateTime);
        // 分页条件查询
        return this.queryPageList(param, queryWrapper);
    }

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

    @Override
    public Optional<ExamAndTpResult> joinDataGet(String serviceId, boolean needJoin) {
        if (needJoin) {
            return Optional.ofNullable(this.getBaseMapper().joinDataByServiceIdGet(serviceId));
        }
        return get(serviceId).map(i -> new ExamAndTpResult() {
            {
                BeanUtils.copyProperties(i, this);
            }
        });
    }

    @Override
    public List<Exam> findByExIds(List<String> exIds) {
        if (exIds != null && !exIds.isEmpty()) {
            return this.queryList(Wrappers.<Exam>lambdaUpdate().in(Exam::getExId, exIds));
        } else {
            return new ArrayList<>();
        }
    }
}
