package com.project.zcustom.service.plat.impl;

import com.project.common.core.domain.search.PageParamVo;
import com.project.zcustom.domain.plat.PlatApiRecord;
import com.project.zcustom.mapper.plat.PlatApiRecordMapper;
import com.project.zcustom.service.plat.IPlatApiRecordService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;

import java.util.List;

/**
 * 接口请求记录Service业务层处理
 *
 * @author change
 * @date 2024-10-31
 */
@Service
public class PlatApiRecordServiceImpl extends ServiceImpl<PlatApiRecordMapper, PlatApiRecord> implements IPlatApiRecordService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatApiRecord> getLambdaQueryWrapper(PlatApiRecord platApiRecord) {
        LambdaQueryWrapper<PlatApiRecord> lqw = new LambdaQueryWrapper<PlatApiRecord>();
        if (StringUtils.isNotBlank(platApiRecord.getApiUrl())) {
            lqw.eq(PlatApiRecord::getApiUrl, platApiRecord.getApiUrl());
        }
        return lqw;
    }

    /**
     * 分页查询接口请求记录列表
     *
     * @param platApiRecord 接口请求记录
     * @return 接口请求记录集合
     */
    @Override
    public IPage<PlatApiRecord> pagingPlatApiRecordList(PlatApiRecord platApiRecord) {
        IPage<PlatApiRecord> page = new Page<>(platApiRecord.getPageNum(), platApiRecord.getPageSize());
        LambdaQueryWrapper<PlatApiRecord> lqw = getLambdaQueryWrapper(platApiRecord);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatApiRecord::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询接口请求记录列表
     *
     * @param lqw
     * @return 接口请求记录集合
     */
    @Override
    public IPage<PlatApiRecord> pagingPlatApiRecordList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatApiRecord> lqw) {
        IPage<PlatApiRecord> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatApiRecord::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询接口请求记录列表
     *
     * @param platApiRecord 接口请求记录
     * @return 接口请求记录
     */
    @Override
    public List<PlatApiRecord> selectPlatApiRecordList(PlatApiRecord platApiRecord) {
        LambdaQueryWrapper<PlatApiRecord> lqw = getLambdaQueryWrapper(platApiRecord);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatApiRecord::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询接口请求记录列表
     *
     * @param platApiRecord 接口请求记录
     * @return 接口请求记录
     */
    @Override
    public List<PlatApiRecord> selectPlatApiRecordList(PlatApiRecord platApiRecord, Long limitNum) {
        LambdaQueryWrapper<PlatApiRecord> lqw = getLambdaQueryWrapper(platApiRecord);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatApiRecord::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 接口请求记录
     */
    @Override
    public PlatApiRecord getPlatApiRecordByField(String field, String filedValue) {
        QueryWrapper<PlatApiRecord> lqw = new QueryWrapper<PlatApiRecord>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 接口请求记录
     */
    @Override
    public List<PlatApiRecord> selectPlatApiRecordListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatApiRecord> lqw = new QueryWrapper<PlatApiRecord>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 接口请求记录
     */
    @Override
    public List<PlatApiRecord> selectPlatApiRecordListByField(String field, String filedValue) {
        QueryWrapper<PlatApiRecord> lqw = new QueryWrapper<PlatApiRecord>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatApiRecord> lqw = new QueryWrapper<PlatApiRecord>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 真删接口
     */
    @Override
    public void reallyDeletePlatApiRecord(LambdaQueryWrapper<PlatApiRecord> lqw) {
        baseMapper.reallyDeleteByWrapper(lqw);
    }
}
