package com.qmth.paper.library.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.paper.library.business.bean.result.PictureManageDetailResult;
import com.qmth.paper.library.business.entity.PaperScanTask;
import com.qmth.paper.library.business.mapper.PictureManageMapper;
import com.qmth.paper.library.business.service.PaperLibraryService;
import com.qmth.paper.library.business.service.PaperScanTaskService;
import com.qmth.paper.library.business.service.PictureManageService;
import com.qmth.paper.library.common.bean.params.DownloadPictureParams;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.util.FileStoreUtil;
import com.qmth.paper.library.common.util.ServletUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description 图片管理服务类
 */
@Service
public class PictureManageServiceImpl implements PictureManageService {

    @Resource
    private PictureManageMapper pictureManageMapper;
    @Resource
    private PaperScanTaskService paperScanTaskService;

    @Resource
    private PaperLibraryService paperLibraryService;

    @Resource
    private FileStoreUtil fileStoreUtil;

    @Override
    public IPage<PictureManageDetailResult> pageData(Long semesterId, Long examId, String courseCode, String collegeName,
                                                     String majorName, String className, String teacher, String teachClass, String param, String startStudentCode, String endStudentCode,
                                                     String pictureSymbol, Integer pictureCount, Integer pageNumber, Integer pageSize) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        IPage<PictureManageDetailResult> picturePage = pictureManageMapper.pageData(new Page<>(pageNumber, pageSize), schoolId, semesterId, examId,
                courseCode, collegeName, majorName, className, teacher,
                teachClass, param, startStudentCode, endStudentCode, pictureSymbol, pictureCount);
        List<PictureManageDetailResult> records = picturePage.getRecords();
        for (PictureManageDetailResult record : records) {
            //一个考生 可能会存在多个批次号
            List<String> batchNoList = paperLibraryService.listBatchNo(record.getStudentId());
            if (!batchNoList.isEmpty()) {
                record.setBatchNo(String.join(",", batchNoList));
            }
            PaperScanTask paperScanTask = paperScanTaskService.getByExamIdAndCourseCode(record.getExamId(), record.getCourseCode());
            record.setPaperScanTaskId(paperScanTask != null ? paperScanTask.getId() : null);
        }
        return picturePage;
    }

    @Override
    public boolean savePicture(MultipartFile file, String fileMd5, String fileUrl, Long paperLibraryId) {
        if (Objects.isNull(file) || file.getSize() == 0) {
            throw ExceptionResultEnum.ERROR.exception("图片为空");
        }
        String frontFileMd5;
        try {
            frontFileMd5 = DigestUtils.md5Hex(file.getBytes());
            if (!Objects.equals(fileMd5, frontFileMd5)) {
                throw ExceptionResultEnum.MD5_EQUALS_FALSE.exception();
            }
            //覆盖图片
            fileStoreUtil.fileUpload(fileUrl, file.getInputStream(), fileMd5);
        } catch (Exception e) {
            throw ExceptionResultEnum.ERROR.exception("[图片管理]保存图片失败：" + e.getMessage());
        }
        return true;
    }

    @Override
    public List<PictureManageDetailResult> listPictureResult(DownloadPictureParams params) {
        List<Long> studentIds = params.getStudentIds();
        List<PictureManageDetailResult> pictureList = pictureManageMapper.pageDataByStudentIds(studentIds);
        for (PictureManageDetailResult result : pictureList) {
            //一个考生 可能会存在多个批次号
            List<String> batchNoList = paperLibraryService.listBatchNo(result.getStudentId());
            if (!batchNoList.isEmpty()) {
                result.setBatchNo(String.join(",", batchNoList));
            }
        }
        return pictureList;
    }


}
