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.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.exam.common.model.params.ExamRecPageParam;
import com.sundot.airport.exam.common.model.result.ExamCountResult;
import com.sundot.airport.exam.common.service.ExamRecService;
import com.sundot.airport.exam.domain.ExamRec;
import com.sundot.airport.exam.mapper.ExamRecMapper;
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>功能名：ExamRecServiceImpl</b><br>
 * <b>说明：</b> 考试记录表实现层 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：</b><br>
 *
 * @author 2021-06-25 Mokou
 */
@Service
public class ExamRecServiceImpl extends ServiceImpl<ExamRecMapper, ExamRec> implements ExamRecService {

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

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

        entity.setErId(idUtils.generateId("ER"));
    }

    @Override
    public boolean delete(String serviceId) {
        return this.remove(Wrappers.<ExamRec>lambdaUpdate().eq(ExamRec::getErId, serviceId));
    }

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

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

    @Override
    public Optional<ExamRec> get(String serviceId) {
        return Optional.ofNullable(this.getOne(Wrappers.<ExamRec>lambdaQuery()
                .eq(ExamRec::getErId, serviceId)));
    }

    @Override
    public List<ExamCountResult> getCountExamRec(String uid, List<String> pcIds, String clientType) {
        return getBaseMapper().getCountExamRec(uid, pcIds, clientType);
    }

    @Override
    public PageInfo<ExamRec> pageList(ExamRecPageParam pageParam) {
        // 过滤条件
        LambdaQueryWrapper<ExamRec> queryWrapper = new QueryWrapper<ExamRec>().lambda()
                .eq(StrUtil.isNotBlank(pageParam.getClientType()), ExamRec::getErClientType, pageParam.getClientType())
                .eq(StrUtil.isNotBlank(pageParam.getExId()), ExamRec::getErExId, pageParam.getExId())
                .eq(StrUtil.isNotBlank(pageParam.getErWzId()), ExamRec::getErWzId, pageParam.getErWzId())
                .eq(StrUtil.isNotBlank(pageParam.getErKiProvinceCode()), ExamRec::getErKiProvinceCode, pageParam.getErKiProvinceCode())
                .eq(StrUtil.isNotBlank(pageParam.getErKiCityCode()), ExamRec::getErKiCityCode, pageParam.getErKiCityCode())
                .eq(StrUtil.isNotBlank(pageParam.getErKiCountyCode()), ExamRec::getErKiCountyCode, pageParam.getErKiCountyCode())
                .eq(ExamRec::getEnableStatus, true)
                .eq(StrUtil.isNotBlank(pageParam.getUsrId()),ExamRec::getUsrId,pageParam.getUsrId())
                .like(StrUtil.isNotBlank(pageParam.getUsrName()), ExamRec::getErUsrName, pageParam.getUsrName())
                .orderByDesc(ExamRec::getCreateTime);
        // 分页条件查询
        return this.queryPageList(pageParam, queryWrapper);
    }

    @Override
    public Optional<ExamRec> getByIdAndUserId(String serviceId, String uid) {
        return Optional.ofNullable(this.getOne(Wrappers.<ExamRec>lambdaQuery()
                .eq(ExamRec::getErId, serviceId)
                .eq(StringUtils.isNotBlank(uid) && !SecurityUtils.isAdmin(SecurityUtils.getUserId()),ExamRec::getUsrId, uid)
                .last("LIMIT 1")));
    }

    @Override
    public long byPcIdUsrIdEnableCount(String pcId, String usrId, Boolean enableStatus) {
        return this.count(new QueryWrapper<ExamRec>().lambda()
                .eq(StrUtil.isNotBlank(pcId), ExamRec::getPcId, pcId)
                .eq(StrUtil.isNotBlank(usrId), ExamRec::getUsrId, usrId)
                .eq(enableStatus != null, ExamRec::getEnableStatus, enableStatus));
    }

    @Override
    public long countExamNumEnable(String uid, String exId) {
        return this.count(
                new QueryWrapper<ExamRec>().lambda()
                        .eq(ExamRec::getUsrId, uid)
                        .eq(ExamRec::getErExId, exId)
                        .eq(ExamRec::getEnableStatus, true)
        );
    }

    @Override
    public List<ExamRec> getLatestByExIds(List<String> exIds, String uid) {
        if (exIds == null || exIds.isEmpty()) {
            return new ArrayList<>();
        }
        return this.queryList(
                Wrappers.<ExamRec>lambdaQuery()
                        .in(ExamRec::getErExId, exIds)
                        .eq(ExamRec::getUsrId, uid)
                        .orderByDesc(ExamRec::getCreateTime)
                        .groupBy(ExamRec::getErExId)
        );
    }
}
