package com.ruoyi.ibs.task.service;

import com.ruoyi.common.utils.file.ImageUtils;
import com.ruoyi.ibs.task.domain.entity.TaskVisitImage;
import com.ruoyi.ibs.task.mapper.TaskVisitImageMapper;
import com.ruoyi.system.domain.OssFileUploadDTO;
import com.ruoyi.system.enums.OssFileEnum;
import com.ruoyi.system.service.OssFileService;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Author 吴凯程
 * @Date 2025/7/7
 **/
@Service
@Async
@Slf4j
public class TaskVisitImageService {

    @Resource
    private TaskVisitImageMapper taskVisitImageMapper;

    @Resource
    private OssFileService ossFileService;

    @Async
    public void uploadImagesToOss(String recordId, String signType, List<MultipartFile> imageList, List<String> msgList, String userName) {
        imageList.forEach(image -> {
            OssFileUploadDTO ossFileUploadDTO = new OssFileUploadDTO();
            MultipartFile compressImage = ImageUtils.compressPictureForScale(image, 512);
            MultipartFile file = ImageUtils.addMultilineTextWatermark(compressImage, msgList, Positions.CENTER, 0.7f);
            ossFileUploadDTO.setFile(file);
            ossFileUploadDTO.setOssFileEnum(OssFileEnum.VISIT_IMAGE);
            ossFileUploadDTO.setUserName(userName);
            String uuid = ossFileService.uploadFileToOss(ossFileUploadDTO);

            TaskVisitImage taskVisitImage = new TaskVisitImage();
            taskVisitImage.setFileId(uuid);
            taskVisitImage.setRecordId(recordId);
            taskVisitImage.setSignType(signType);
            taskVisitImage.setCreateTime(new Date());
            taskVisitImageMapper.insert(taskVisitImage);

        });
        log.info("上传图片到OSS成功");
    }
}
