package com.codvision.monitor.service.impl;

import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.monitor.entity.Website;
import com.codvision.monitor.entity.WebsiteAccessRecord;
import com.codvision.monitor.entity.vo.WebsiteAccessRecordVO;
import com.codvision.monitor.mapper.WebsiteAccessRecordMapper;
import com.codvision.monitor.service.IWebsiteAccessRecordService;
import com.codvision.monitor.service.IWebsiteService;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 站点访问记录 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-20
 */
@Service
public class WebsiteAccessRecordServiceImpl extends BaseServiceImpl<WebsiteAccessRecordMapper, WebsiteAccessRecord, WebsiteAccessRecordVO> implements IWebsiteAccessRecordService {

    @Resource
    private IWebsiteService websiteService;

    @Override
    public void addRecord(WebsiteAccessRecord accessRecord, HttpServletRequest request) {
        Website website = websiteService.getOne(Wrappers.<Website>lambdaQuery().eq(Website::getKey, accessRecord.getWebsiteKey()));
        if (website == null) {
            return;
        }

        accessRecord.setIp(ServletUtil.getClientIP(request));
        this.save(accessRecord);
    }

    @Override
    public List<WebsiteAccessRecordVO> getPvCountList(String websiteKey, String startTime, String endTime) {
        return getBaseMapper().getPvCountList(websiteKey, startTime, endTime);
    }

    @Override
    public List<WebsiteAccessRecordVO> getUvCountList(String websiteKey, String startTime, String endTime) {
        return getBaseMapper().getUvCountList(websiteKey, startTime, endTime);
    }
}
