package com.kym.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.common.utils.CommUtil;
import com.kym.common.utils.OssUtil;
import com.kym.entity.Attachment;
import com.kym.mapper.AttachmentMapper;
import com.kym.service.AttachmentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;


/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 * @author zuy
 * @since 2023-08-12
 */
@Service
public class AttachmentServiceImpl extends MPJBaseServiceImpl<AttachmentMapper, Attachment> implements AttachmentService {

    @Value("${upload.file.storage}")
    private String fileStoragePath;

    @Value("${upload.file.url}")
    private String url;

    @Value("${upload.file.size}")
    private int fileSize;


    @Override
    public Attachment upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        StpUtil.checkLogin();
        long size = file.getSize();
        CommUtil.asserts((long) fileSize << 20 > size, "文件超出上传大小");
        Long userId = CommUtil.null2Long(StpUtil.getSession().get("userId"));

        Attachment attachment = new Attachment();
        attachment.setName(fileName);
        attachment.setSize(file.getSize());
        attachment.setUuid(UUID.fastUUID().toString(true) + "." + FileUtil.getSuffix(fileName));
        attachment.setUrl(url.concat(attachment.getUuid()));
        attachment.setUploadBy(userId);
//        attachment.setUploadName("admin");
        attachment.setUploadName(CommUtil.null2String(StpUtil.getSession().get("username")));
        attachment.setIsDelete(false);

        String applicationPath = System.getProperty("user.dir");

        File dir = new File(applicationPath, fileStoragePath);
        if (!dir.exists()) {
            CommUtil.asserts(dir.mkdirs(), "文件上传异常");
        }
        File storeFile = new File(dir, attachment.getUuid());
        file.transferTo(storeFile);

        OssUtil.upload(attachment.getUuid(), storeFile);

        baseMapper.insert(attachment);
        return attachment;
    }

    @Override
    public void delete(String fileId) {
        StpUtil.checkLogin();
        Long userId = CommUtil.null2Long(StpUtil.getSession().get("userId"));
        Attachment attachment = baseMapper.selectOne(Wrappers.query(Attachment.class).eq("uuid", fileId));
        CommUtil.asserts(null != attachment && Objects.equals(attachment.getUploadBy(), userId), "无删除权限");
        String applicationPath = System.getProperty("user.dir");

        File dir = new File(applicationPath, fileStoragePath);
        File storeFile = new File(dir, fileId);
        storeFile.delete();
        OssUtil.delete(fileId);
    }
}
