package com.ai.da.service.impl;

import com.ai.da.mapper.primary.AccountLoginLogMapper;
import com.ai.da.mapper.primary.entity.AccountLoginLog;
import com.ai.da.service.AccountLoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

//import com.ai.da.common.utils.SendSmsUtil;

/**
 * 服务实现类
 *
 * @author easy-generator
 * @since 2022-07-06
 */
@Slf4j
@Service
public class AccountLoginLogServiceImpl extends ServiceImpl<AccountLoginLogMapper, AccountLoginLog> implements AccountLoginLogService {

    @Resource
    AccountLoginLogMapper accountLoginLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveLoginLog(String ip, Long accountId) {
        AccountLoginLog accountLoginLog = new AccountLoginLog();
        accountLoginLog.setAccountId(accountId);
        accountLoginLog.setIp(ip);
        accountLoginLog.setCreateDate(new Date());
        return accountLoginLogMapper.insert(accountLoginLog) > 0;
    }

    @Override
    public List<AccountLoginLog> getByUserId(Long AccountId) {
        QueryWrapper<AccountLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", AccountId);
        return accountLoginLogMapper.selectList(queryWrapper);
    }

    public List<Long> getByDate(String startTime, String endTime){
        QueryWrapper<AccountLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("create_date", startTime).lt("create_date", endTime);
        queryWrapper.select("distinct account_id");

        List<AccountLoginLog> accountLoginLogs = baseMapper.selectList(queryWrapper);
        return accountLoginLogs.stream().map(AccountLoginLog::getAccountId).collect(Collectors.toList());
    }

}
