package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseApiInfoMapper;
import org.wcs.mapper.intf.AppBaseApiInfoService;
import org.wcs.model.dto.serve.api.QueryApiBaseInfoReq;
import org.wcs.model.po.app.AppBaseApiInfo;
import org.wcs.utils.AppStringUtils;

import java.util.List;

/**
 * API 信息的基础资料表操作类
 */
@Service
public class AppBaseApiInfoDao extends ServiceImpl<AppBaseApiInfoMapper, AppBaseApiInfo> implements AppBaseApiInfoService {

    /**
     * 查询所有API信息
     * @return 查询结果
     */
    @Override
    public List<AppBaseApiInfo> queryAll() {
        try {
            return super.list();
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据条件查询API信息
     * @param queryApiBaseInfoReq 查询条件
     * @return 查询结果
     */
    @Override
    public List<AppBaseApiInfo> query(QueryApiBaseInfoReq queryApiBaseInfoReq) {
        LambdaQueryWrapper<AppBaseApiInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(queryApiBaseInfoReq.getApiName()), AppBaseApiInfo::getApiName, queryApiBaseInfoReq.getApiName())
                .like(AppStringUtils.isNotEmpty(queryApiBaseInfoReq.getApiKey()), AppBaseApiInfo::getApiKey, queryApiBaseInfoReq.getApiKey());
        queryWrapper.orderByAsc(AppBaseApiInfo::getApiKey);
        try {
            return super.list(queryWrapper);
        } catch ( Exception e) {
            return null;
        }
    }

    /**
     * 根据API键查询API信息
     * @param apiKey API键
     * @return 查询结果
     */
    @Override
    public List<AppBaseApiInfo> queryWithApiKey(String apiKey) {
        if(AppStringUtils.isEmpty(apiKey)) {
            return null;
        }
        try {
            LambdaQueryWrapper<AppBaseApiInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppBaseApiInfo::getApiKey, apiKey);
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 新增API信息
     * @param appBaseApiInfo API信息
     * @return 新增结果
     */
    @Override
    public boolean insert(AppBaseApiInfo appBaseApiInfo) {
        if(appBaseApiInfo == null) {
            return false;
        }
        try {
            return super.save(appBaseApiInfo);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 删除API信息
     * @param apiKey API键
     * @return 删除结果
     */
    @Override
    public boolean delete(String apiKey) {
        try {
            return super.removeById(apiKey);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 更新API信息
     * @param appBaseApiInfo API信息
     * @return 更新结果
     */
    @Override
    public boolean update(AppBaseApiInfo appBaseApiInfo) {
        LambdaUpdateWrapper<AppBaseApiInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppBaseApiInfo::getApiKey, appBaseApiInfo.getApiKey());
        updateWrapper.set(AppStringUtils.isNotEmpty(appBaseApiInfo.getApiName()), AppBaseApiInfo::getApiName, appBaseApiInfo.getApiName())
                .set(AppStringUtils.isNotEmpty(appBaseApiInfo.getAddress()), AppBaseApiInfo::getAddress, appBaseApiInfo.getAddress())
                .set(AppStringUtils.isNotEmpty(appBaseApiInfo.getTag()), AppBaseApiInfo::getTag, appBaseApiInfo.getTag())
                .set(AppStringUtils.isNotEmpty(appBaseApiInfo.getRootKey()), AppBaseApiInfo::getRootKey, appBaseApiInfo.getRootKey())
                .set(AppStringUtils.isNotEmpty(appBaseApiInfo.getRemark()), AppBaseApiInfo::getRemark, appBaseApiInfo.getRemark());
        try {
            return super.update(appBaseApiInfo, updateWrapper);
        } catch (Exception e) {
            return false;
        }
    }
}
