package com.blueland.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blueland.model.TPfApplicationDb;
import com.blueland.service.IAppDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.blueland.bo.AppDatabaseEntity;
import com.blueland.dao.AppDatabaseDao;

import java.util.List;

@Service
public class AppDatabaseServiceImpl extends ServiceImpl<AppDatabaseDao, TPfApplicationDb> implements IAppDatabaseService {
	@Autowired
	//PfAppDatabase对应的DAO类,主要用于数据库的增删改查等操作
	private AppDatabaseDao pfAppDatabaseDao;
	
	/**
	 * 通过appId 删除数据
	 * @param
	 * @return
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int batchDeleteByAppId(String[] appIds) {
		if(appIds == null || appIds.length == 0) {
			return 0;
		}
		return pfAppDatabaseDao.batchDeleteByAppId(appIds);
	}

	@Override
	public Page<AppDatabaseEntity> findPage(AppDatabaseEntity bo) {
		Page<AppDatabaseEntity> page = new Page<>(bo.getPageNum(), bo.getPageSize());
		return this.baseMapper.findPage(page, bo);
	}

	@Override
	public TPfApplicationDb queryOne(AppDatabaseEntity bo) {
		return pfAppDatabaseDao.selectOne(buildQueryWrapper(bo));
	}

	private LambdaQueryWrapper<TPfApplicationDb> buildQueryWrapper(AppDatabaseEntity bo) {
		LambdaQueryWrapper<TPfApplicationDb> lqw = Wrappers.lambdaQuery();;
		lqw.eq(ObjectUtil.isNotEmpty(bo.getAppId()), TPfApplicationDb::getAppId, bo.getAppId());
		lqw.like(ObjectUtil.isNotEmpty(bo.getTypeName()), TPfApplicationDb::getTypeName, bo.getTypeName());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getUrl()), TPfApplicationDb::getUrl, bo.getUrl());
		lqw.like(ObjectUtil.isNotEmpty(bo.getInstanceName()), TPfApplicationDb::getInstanceName, bo.getInstanceName());
		lqw.like(ObjectUtil.isNotEmpty(bo.getUsername()), TPfApplicationDb::getUsername, bo.getUsername());
		lqw.like(ObjectUtil.isNotEmpty(bo.getDriverName()), TPfApplicationDb::getDriverName, bo.getDriverName());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getPassword()), TPfApplicationDb::getPassword, bo.getPassword());
		return lqw;
	}
}
