package cn.reghao.tnb.account.app.rpc;

import cn.reghao.jutil.jdk.db.Page;
import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.tnb.account.api.dto.AccountInfo;
import cn.reghao.tnb.account.api.dto.AccountRegistry;
import cn.reghao.tnb.account.api.dto.AdminCreateAccount;
import cn.reghao.tnb.account.api.iface.AdminAccountService;
import cn.reghao.tnb.account.app.db.mapper.UserAccountMapper;
import cn.reghao.tnb.account.app.db.repository.AccountRepository;
import cn.reghao.tnb.account.app.model.po.UserRegistry;
import cn.reghao.tnb.account.app.service.AccountRegistryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2024-07-31 07:00:22
 */
@Slf4j
@DubboService
@Service
public class AdminAccountServiceImpl implements AdminAccountService {
    private final int pageSize = 100;
    private final AccountRepository accountRepository;
    private final UserAccountMapper userAccountMapper;
    private final AccountRegistryService accountRegistryService;

    public AdminAccountServiceImpl(AccountRepository accountRepository, UserAccountMapper userAccountMapper,
                                   AccountRegistryService accountRegistryService) {
        this.accountRepository = accountRepository;
        this.userAccountMapper = userAccountMapper;
        this.accountRegistryService = accountRegistryService;
    }

    @Override
    public AccountRegistry getAccountRegistry() {
        UserRegistry userRegistry = accountRepository.getUserRegistry();
        return new AccountRegistry(userRegistry.getEnabled(), userRegistry.getRule());
    }

    @Override
    public void setAccountRegistry(AccountRegistry accountRegistry) {
        accountRepository.updateUserRegistry();
    }

    @Override
    public PageList<AccountInfo> getByScreenName(String screenName, int pageNumber, int pageSize) {
        /*int total = accountRepository.countByScreenName(screenName);
        List<AccountInfo> list = accountRepository.getPageByScreenName(screenName, page);*/
        log.info("screenName -> {}", screenName);
        int total = userAccountMapper.count();
        Page page = new Page(pageNumber, pageSize);
        List<AccountInfo> list = userAccountMapper.findAccountInfoByPage(page);
        return PageList.pageList(pageNumber, pageSize, total, list);
    }

    @Override
    public void createAccount(AdminCreateAccount adminCreateAccount) {
        accountRegistryService.createAccount(adminCreateAccount);
    }
}
