package com.bs.cm.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bs.cm.mapper.CmAttachMapper;
import com.bs.cm.domain.CmAttach;
import com.bs.cm.service.ICmAttachService;
import com.bs.common.constant.Constants;
import com.bs.common.exception.ServiceException;
import com.bs.common.utils.FileUtil;
import com.bs.common.utils.OsInfoUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateUtil.format;
import static com.bs.common.config.BsConfig.*;

/**
 * 附件信息Service业务层处理
 *
 * @author bs
 * @date 2024-03-03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CmAttachServiceImpl  extends MPJBaseServiceImpl<CmAttachMapper, CmAttach> implements ICmAttachService {
    @Resource
    private ICmAttachService cmAttachService;
    public static final String SEPARATOR = "/";
    public static final String SUFFIX = Constants.RESOURCE_PREFIX + SEPARATOR;
    @Autowired
    private Environment env;

    @Override
    public List<CmAttach> uploadFileBatch(MultipartFile[] files, String fileId) {
        List<CmAttach> attachs = new ArrayList<>();
        for (MultipartFile file : files) {
            //插入附件表
            attachs.add(uploadFile(file, fileId));
        }
        if (attachs.size() == 0) {
            return null;
        }
        // 插入附件中间表
        List<CmAttach>  attachVos = new ArrayList<>();
        cmAttachService.saveBatch(attachs);
//        cmAttachService.saveBatch(getAttachRef(attachs,attachVos, fileId, businessUnits));
        return attachVos;
    }

    /**
     * 保存附件及插入附件表，返回附件对象
     */
    @Transactional
    public CmAttach uploadFile(MultipartFile multipartFile, String fileId) {
        FileUtil.checkSize(Long.parseLong(env.getProperty("chain.maxSize")), multipartFile.getSize());
        String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        String type = FileUtil.getFileType(suffix);
        //新的文件名+后缀
        String fileName = FileUtil.fileRenameMos(null, null) + "." + suffix;
        //绝对路径+年份+文件类型+文件名
        String filePath = format(new Date(), "yyyy") + SEPARATOR +
                type + SEPARATOR + fileName;
        File file = FileUtil.uploadMos(multipartFile, getFilePath() + SEPARATOR + filePath);
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传失败");
        }
        try {
            CmAttach attach = new CmAttach();
            attach.setFileId(fileId);
//            attach.setAttachId(UUIDUtils.generatorUUID());
            attach.setAttachName(fileName);
            attach.setAttachFileType(type);
            attach.setAttachContentType(multipartFile.getContentType());
            attach.setAttachFileSize((long) multipartFile.getSize());
            attach.setAttachFileUrl(SUFFIX + filePath);
            attach.setOldName(multipartFile.getOriginalFilename());
//            cmAttachMapper.insert(attach);
            return attach;
        } catch (Exception e) {
            FileUtil.del(file);
            throw e;
        }
    }

    /**
     * 根据系统切换存放附件绝对地址
     *
     * @return 存放附件绝对地址
     */
    public static String getFilePath() {
        if (OsInfoUtil.isLinux()) {
            return getLinuxLocalFilePath();
        }
        if (OsInfoUtil.isMacOS() || OsInfoUtil.isMacOSX()) {
            return getMacLocalFilePath();
        }
        return getWinLocalFilePath();
    }
}
