package cd.casic.ci.process.process.service.taskLog.impl;

import cd.casic.ci.process.process.dao.pipeline.PipTaskLogLineDao;
import cd.casic.ci.process.process.dataObject.log.PipTaskLogLine;
import cd.casic.ci.process.process.service.taskLog.PipTaskLogLineService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class PipTaskLogLineServiceImpl extends ServiceImpl<PipTaskLogLineDao,PipTaskLogLine> implements PipTaskLogLineService {
    @Resource
    private PipTaskLogLineDao logLineDao;
    @Override
    public String getLineContentByLogId(String logId,Integer count){
        List<PipTaskLogLine> lineList = getLineListByLogId(logId, count);
        if (CollectionUtils.isEmpty(lineList)) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (PipTaskLogLine line : lineList) {
            stringBuilder.append(line.getLineContent());
        }
        return stringBuilder.toString();
    }
    @Override
    public List<PipTaskLogLine> getLineListByLogId(String logId, Integer count){
        LambdaQueryWrapper<PipTaskLogLine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PipTaskLogLine::getLogId,logId);
        wrapper.last("LIMIT "+count);
        return logLineDao.selectList(wrapper);
    }
}
