package com.ycwl.basic.stats.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ycwl.basic.stats.entity.StatsEntity;
import com.ycwl.basic.stats.entity.StatsRecordEntity;
import com.ycwl.basic.stats.mapper.StatsMapper;
import com.ycwl.basic.stats.mapper.StatsRecordMapper;
import com.ycwl.basic.stats.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StatsServiceImpl implements StatsService {
    @Autowired
    private StatsMapper statsMapper;

    @Autowired
    private StatsRecordMapper statsRecordMapper;

    @Override
    public void addStats(String traceId, Long memberId) {
        StatsEntity entity = new StatsEntity();
        entity.setTraceId(traceId);
        entity.setMemberId(memberId);
        entity.setCreateTime(new Date());
        statsMapper.insert(entity);
    }

    @Override
    public void updateStats(String traceId, Long memberId) {
        StatsEntity entity = new StatsEntity();
        entity.setMemberId(memberId);
        LambdaQueryWrapper<StatsEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StatsEntity::getTraceId, traceId);
        statsMapper.update(entity, queryWrapper);
    }

    @Override
    public void addRecord(String traceId, String action, String identifier, String params) {
        StatsRecordEntity entity = new StatsRecordEntity();
        entity.setTraceId(traceId);
        entity.setAction(action);
        entity.setIdentifier(identifier);
        entity.setParams(params);
        entity.setCreateTime(new Date());
        statsRecordMapper.insert(entity);
    }
}
