package com.fjhx.service.common.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.StorageBaseEntity;
import com.fjhx.entity.common.CommonFile;
import com.fjhx.mapper.common.CommonFileMapper;
import com.fjhx.service.common.CommonFileService;
import com.fjhx.utils.OssUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-08-08
 */
@Service
public class CommonFileServiceImpl extends ServiceImpl<CommonFileMapper, CommonFile> implements CommonFileService {

    @Override
    public Map<String, String> uploadFile(MultipartFile file) {

        String fileName = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileName);

        String filePath = OssUtil.getFileKey(suffix);

        try {
            OssUtil.uploadFile(filePath, file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("文件上传失败");
        }

        CommonFile fileInfo = new CommonFile();
        fileInfo.setFileName(fileName);
        fileInfo.setFileSuffix(suffix);
        fileInfo.setContentType(file.getContentType());
        fileInfo.setFileSize(file.getSize());
        fileInfo.setFilePath(OssUtil.getFilePath(filePath));
        save(fileInfo);

        Map<String, String> map = new HashMap<>();

        map.put("fileName", fileName);
        map.put("filePath", OssUtil.getFilePath(filePath));
        map.put("id", fileInfo.getId().toString());

        return map;
    }

    @Override
    public void add(Long businessId, List<CommonFile> list) {
        if (ObjectUtil.isEmpty(list)) return;
        list.forEach(commonFile -> commonFile.setBusinessId(businessId));
        updateBatchById(list);
    }

    @Override
    public void edit(Long businessId, List<CommonFile> list) {
        add(businessId, list);

        List<Long> idList = list.stream().map(CommonFile::getId).collect(Collectors.toList());

        remove(Wrappers.<CommonFile>lambdaQuery()
                .eq(CommonFile::getBusinessId, businessId)
                .notIn(idList.size() > 0, StorageBaseEntity::getId, idList));
    }

    @Override
    public void delete(Long id) {
        remove(CommonFile::getBusinessId, id);
    }

    @Override
    public void delete(List<Long> idList) {
        remove(CommonFile::getBusinessId, idList);
    }

    @Override
    public List<CommonFile> fileList(Long businessId) {
        return list(CommonFile::getBusinessId, businessId);
    }

    @Override
    public Map<Long, List<CommonFile>> fileMap(List<Long> businessId) {
        return list(CommonFile::getBusinessId, businessId).stream().collect(Collectors.groupingBy(CommonFile::getBusinessId));
    }

}
