package com.mortals.xhx.module.access.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.mortals.xhx.common.key.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.access.dao.AccessLogDao;
import com.mortals.xhx.module.access.model.AccessLogEntity;
import com.mortals.xhx.module.access.service.AccessLogService;
import org.springframework.util.ObjectUtils;

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

/**
* AccessLogService
* 访问日志 service实现
*
* @author zxfei
* @date 2022-08-17
*/
@Service("accessLogService")
@Slf4j
public class AccessLogServiceImpl extends AbstractCRUDServiceImpl<AccessLogDao, AccessLogEntity, Long> implements AccessLogService {


    @Override
    public AccessLogEntity save(AccessLogEntity entity, Context context) throws AppException {
        entity.setId(IdUtil.getSnowflake(0, 1).nextId());
        if(ObjectUtils.isEmpty(entity.getCreateUserId())){
            entity.setCreateUserId(1L);
            entity.setCreateUserName("system");
            entity.setCreateTime(new Date());
        }

        if (!ObjectUtils.isEmpty(entity.getDuration()) && entity.getDuration() < Constant.ACCESS_EXPIRE_TIME) {
           // log.info("Duration:{}",entity.getDuration());
            return new AccessLogEntity() ;
        }
        return super.save(entity, context);
    }

    public static void main(String[] args) {
        Snowflake snowflake = IdUtil.getSnowflake(0, 1);
        long id = snowflake.nextId();

        System.out.println(id);

        long generateDateTime = snowflake.getGenerateDateTime(id);
        System.out.println(DateUtil.date(generateDateTime).toString());

    }
}