package com.qmth.teachcloud.mark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.qmth.teachcloud.common.service.FileUploadService;
import com.qmth.teachcloud.common.util.FileUtil;
import com.qmth.teachcloud.mark.entity.ScanLog;
import com.qmth.teachcloud.mark.mapper.ScanLogMapper;
import com.qmth.teachcloud.mark.service.MarkFileService;
import com.qmth.teachcloud.mark.service.ScanLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xf
 * @since 2023-12-08
 */
@Service
public class ScanLogServiceImpl extends MppServiceImpl<ScanLogMapper, ScanLog> implements ScanLogService {

    @Resource
    private MarkFileService markFileService;
    @Resource
    private FileUploadService fileUploadService;

    @Override
    public boolean uploadLogFile(String schoolCode, String deviceCode, MultipartFile file, String md5, String loginName, String type) {
        try {
            String fileName = file.getOriginalFilename();
            ScanLog scanLog = this.selectByMultiId(new ScanLog(schoolCode, deviceCode, fileName, type));
            if (scanLog == null) {
                scanLog = new ScanLog();
                scanLog.setSchoolCode(schoolCode);
                scanLog.setDeviceCode(deviceCode);
                scanLog.setFileName(fileName);
                scanLog.setType(type);
            }
            scanLog.setLoginName(loginName);
            scanLog.setMd5(FileUtil.md5File(file.getInputStream()));
            String path = markFileService.uploadScanLog(file.getInputStream(), md5, schoolCode, deviceCode, fileName);
            scanLog.setPath(path);
            scanLog.setUploadTime(System.currentTimeMillis());
            return this.saveOrUpdateByMultiId(scanLog);
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    public IPage<ScanLog> listScanLog(String schoolCode, String loginName, Integer pageNumber, Integer pageSize) {
        Page<ScanLog> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<ScanLog> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ScanLog> lambdaQueryWrapper = queryWrapper.lambda();
        if (StringUtils.isNotBlank(schoolCode)) {
            lambdaQueryWrapper.eq(ScanLog::getSchoolCode, schoolCode);
        }
        if (StringUtils.isNotBlank(loginName)) {
            lambdaQueryWrapper.likeRight(ScanLog::getLoginName, loginName);
        }
        lambdaQueryWrapper.orderByDesc(ScanLog::getUploadTime);
        Page<ScanLog> scanLogPage = this.page(page, queryWrapper);
        for (ScanLog record : scanLogPage.getRecords()) {
            record.setPathUrl(fileUploadService.filePreview(record.getPath()));
            record.setPath(null);
        }
        return scanLogPage;
    }
}
