package com.lutao.cloud.consume.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutao.cloud.consume.mapper.CollectLogMapper;
import com.lutao.cloud.consume.service.ConsumerLogService;
import com.lutao.entity.CollectLog;
import org.springframework.stereotype.Service;

@Service
public class ConsumerLogServiceImpl extends ServiceImpl<CollectLogMapper, CollectLog> implements ConsumerLogService {
    @Override
    public boolean saveConsumeLog(String requestId,String appId,String command,Integer consumeNumber,String tableName) {
        CollectLog collectLog = new CollectLog();
        collectLog.setRequestId(requestId);
        collectLog.setAppId(appId);
        collectLog.setCommand(command);
        collectLog.setTableName(tableName);
        CollectLog one = this.getOne(Wrappers.lambdaQuery(CollectLog.class)
                .eq(CollectLog::getRequestId, requestId)
                .eq(CollectLog::getAppId, appId)
                .eq(CollectLog::getCommand, command)
                .eq(CollectLog::getTableName, tableName)
        );
        if (one == null) {
            collectLog.setConsumeNumber(consumeNumber);
            return this.saveOrUpdate(collectLog,
                    Wrappers.lambdaQuery(CollectLog.class)
                            .eq(CollectLog::getRequestId, requestId)
                            .eq(CollectLog::getAppId, appId)
                            .eq(CollectLog::getCommand, command)
                            .eq(CollectLog::getTableName, tableName)
            );
        } else {
            return baseMapper.updateConsumeLogNumber(requestId, appId, command, consumeNumber, tableName);
//            collectLog.setConsumeNumber(one.getConsumeNumber() == null ? 1 : one.getConsumeNumber() + consumeNumber);
//            return this.saveOrUpdate(collectLog,
//                    Wrappers.lambdaQuery(CollectLog.class)
//                            .eq(CollectLog::getRequestId, requestId)
//                            .eq(CollectLog::getAppId, appId)
//                            .eq(CollectLog::getCommand, command)
//                            .eq(CollectLog::getTableName, tableName)
//            );
        }
    }
}
