package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.UserFirstTimeDao;
import com.yonge.cooleshow.biz.dal.dto.search.UserFirstTimeSearch;
import com.yonge.cooleshow.biz.dal.entity.UserFirstTime;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.UserFirstTimeService;
import com.yonge.cooleshow.common.enums.UserFirstTimeTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class UserFirstTimeServiceImpl extends ServiceImpl<UserFirstTimeDao, UserFirstTime> implements UserFirstTimeService {
    private final static Logger log = LoggerFactory.getLogger(UserFirstTimeServiceImpl.class);

    @Override
    public List<UserFirstTime> selectAllList(UserFirstTimeSearch param) {
        return baseMapper.selectAllList(param);
    }

    @Override
    public Boolean recordTime(Long userId, ClientEnum userType, UserFirstTimeTypeEnum firstTimeTypeEnum) {
        return recordTime(userId, userType, firstTimeTypeEnum, new Date());
    }

    @Override
    public Boolean recordTime(Long userId, ClientEnum userType, UserFirstTimeTypeEnum firstTimeTypeEnum, Date time) {
        if (null == userId || null == userType || null == firstTimeTypeEnum) {
            return false;
        }

        UserFirstTime old = getOne(Wrappers.<UserFirstTime>lambdaQuery()
                .eq(UserFirstTime::getId, userId)
                .eq(UserFirstTime::getUserType, userType)
                .eq(UserFirstTime::getTimeType, firstTimeTypeEnum)
        );
        if (old != null) {
            return true;
        }
        UserFirstTime userFirstTime = new UserFirstTime(userId, userType, firstTimeTypeEnum, time);
        save(userFirstTime);
        return true;
    }
}