package com.foreverwin.mesnac.dataimport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.dataimport.mapper.MasterImportLogMapper;
import com.foreverwin.mesnac.dataimport.model.MasterImportLog;
import com.foreverwin.mesnac.dataimport.service.MasterImportLogService;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 主数据导入日志 服务实现类
 * </p>
 *
 * @author Ervin Chen
 * @since 2020-09-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MasterImportLogServiceImpl extends ServiceImpl<MasterImportLogMapper, MasterImportLog> implements MasterImportLogService {


    @Autowired
    private MasterImportLogMapper masterImportLogMapper;

    @Override
    public IPage<MasterImportLog> selectPage(FrontPage<MasterImportLog> frontPage, MasterImportLog masterImportLog) {
        QueryWrapper<MasterImportLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(masterImportLog);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<MasterImportLog> selectList(MasterImportLog masterImportLog) {
        QueryWrapper<MasterImportLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(masterImportLog);
        return super.list(queryWrapper);
    }

    @Override
    public void logMessage(String transId, long index, String message) {
        MasterImportLog masterImportLog = new MasterImportLog();
        masterImportLog.setTransId(transId);
        masterImportLog.setIndex(index);
        masterImportLog.setMessage(message);
        masterImportLog.setLogTime(LocalDateTime.now());
        save(masterImportLog);
    }

    @Override
    public String queryMessage(String transId) {
        List<String> messages = list(new QueryWrapper<MasterImportLog>().eq("TRANS_ID", transId)).stream().map(log -> log.getMessage()).collect(Collectors.toList());
        return String.join("\n", messages);
    }


}