package org.springblade.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.auth.enums.ThirdPartyLoginType;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.modules.system.entity.ThirdLoginConfig;
import org.springblade.modules.system.mapper.ThirdLoginConfigMapper;
import org.springblade.modules.system.service.IThirdLoginConfigService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ThirdLoginConfigServiceImpl extends ServiceImpl<ThirdLoginConfigMapper, ThirdLoginConfig> implements IThirdLoginConfigService {

    @Override
    public Page<ThirdLoginConfig> getPage(ThirdLoginConfig loginConfig, Query query) {
        Page<ThirdLoginConfig> page = new Page<>(query.getCurrent(),query.getSize());
        LambdaQueryWrapper<ThirdLoginConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (loginConfig != null) {
            if (StrUtil.isNotEmpty(loginConfig.getTenantId())){
                queryWrapper.eq(ThirdLoginConfig::getTenantId,loginConfig.getTenantId());
            }
            if (loginConfig.getStatus() != null) {
                queryWrapper.eq(ThirdLoginConfig::getStatus,loginConfig.getStatus());
            }
            if (StrUtil.isNotEmpty(loginConfig.getCreateUser())) {
                queryWrapper.like(ThirdLoginConfig::getCreateUser,loginConfig.getCreateUser());
            }
            if (loginConfig.getLoginType() != null) {
                queryWrapper.eq(ThirdLoginConfig::getLoginType,loginConfig.getLoginType());
            }
        }
        return this.page(page,queryWrapper);
    }

    @Override
    public boolean doSaveOrupdate(ThirdLoginConfig loginConfig, BladeUser user) {
        if (loginConfig.getId() == null || loginConfig.getId() < 1) {
            loginConfig.setCreateUser(user.getNickName());
            loginConfig.setCreateUserId(user.getUserId());
        } else {
            loginConfig.setUpdateTime(LocalDateTime.now());
        }

        return this.saveOrUpdate(loginConfig);
    }
}
