package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.SysTenantAccount;
import org.dromara.system.domain.bo.SysTenantAccountBo;
import org.dromara.system.domain.vo.SysTenantAccountStatisticVO;
import org.dromara.system.domain.vo.SysTenantAccountVo;
import org.dromara.system.mapper.SysTenantAccountMapper;
import org.dromara.system.service.ISysTenantAccountService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysTenantAccountServiceImpl  extends ServiceImpl<SysTenantAccountMapper,SysTenantAccount> implements ISysTenantAccountService{
    private final SysTenantAccountMapper mapper;

    @Override
    public IPage<SysTenantAccount> pageWithTenant(Page<SysTenantAccount> page, SysTenantAccountBo bo) {
        LambdaQueryWrapper<SysTenantAccount> wrapper = buildWrapper(bo);
        return mapper.selectPage(page, wrapper);
    }

    @Override
    public IPage<SysTenantAccountVo> selectTenantAccountPage(Page<SysTenantAccount> page, SysTenantAccountBo bo) {
        return mapper.selectTenantAccountPage(page, bo);
    }

    @Override
    public List<SysTenantAccount> list(SysTenantAccountBo bo) {
        LambdaQueryWrapper<SysTenantAccount> wrapper = buildWrapper(bo);
        return mapper.selectList(wrapper);
    }

    @Override
    public SysTenantAccount getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public boolean save(SysTenantAccount po) {
        return mapper.insert(po) > 0;
    }

    @Override
    public boolean updateById(SysTenantAccount po) {
        return mapper.updateById(po) > 0;
    }

    @Override
    public boolean removeById(Long id) {
        return mapper.deleteById(id) > 0;
    }

    private LambdaQueryWrapper<SysTenantAccount> buildWrapper(SysTenantAccountBo bo) {
        LambdaQueryWrapper<SysTenantAccount> wrapper = new LambdaQueryWrapper<>();
        if (bo == null) return wrapper;
        if (bo.getTenantId() != null) wrapper.eq(SysTenantAccount::getTenantId, bo.getTenantId());
        if (bo.getType() != null) wrapper.eq(SysTenantAccount::getType, bo.getType());
        // 可根据实际业务补充更多条件
        return wrapper;
    }

    @Override
    public SysTenantAccount getByTenantId(String tenantId) {
        return mapper.selectOne(new LambdaQueryWrapper<SysTenantAccount>().eq(SysTenantAccount::getTenantId, tenantId));
    }



}
