package jnpf.helpailog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import jnpf.helpailog.mapper.FtbHelpAiChatLogMapper;
import jnpf.helpailog.service.FtbHelpAiChatLogService;
import jnpf.model.helpailog.dto.HelpAiLogReq;
import jnpf.model.helpailog.po.FtbHelpAiChatLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class FtbHelpAiChatLogServiceImpl extends ServiceImpl<FtbHelpAiChatLogMapper, FtbHelpAiChatLog> implements FtbHelpAiChatLogService {
    /**
     * 添加
     *
     * @param entity 实体
     */
    @Override
    public void record(FtbHelpAiChatLog entity) {
        baseMapper.insert(entity);
    }

    /**
     * 分页查询
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<FtbHelpAiChatLog> getPageList(HelpAiLogReq req) {
        if (StringUtils.isNotEmpty(req.getSessionId())) {
            req.setSessionId(StringUtils.trim(req.getSessionId()));
        }
        //构建分页
        Page<FtbHelpAiChatLog> page = new Page(req.getCurrentPage(), req.getPageSize());
        //构建查询
        LambdaQueryWrapper<FtbHelpAiChatLog> wrapper = new LambdaQueryWrapper<FtbHelpAiChatLog>()
                .eq(!StringUtils.isEmpty(req.getSessionId()), FtbHelpAiChatLog::getSessionId, req.getSessionId())
                .orderByDesc(FtbHelpAiChatLog::getCreatorTime);
        Page<FtbHelpAiChatLog> queryPage = baseMapper.selectPage(page, wrapper);
        List<FtbHelpAiChatLog> records = queryPage.getRecords();


        //构建分页返回数据
        PageInfo<FtbHelpAiChatLog> pageInfo = new PageInfo<>();
        pageInfo.setList(records);
        pageInfo.setPageSize((int) queryPage.getSize());
        pageInfo.setPageNum((int) queryPage.getCurrent());
        pageInfo.setTotal((int) queryPage.getTotal());
        return pageInfo;
    }

}
