package com.tanpu.community.service;

import com.tanpu.common.constant.BizStatus;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.AliyunOSSHelper;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.FileTypeEnum;
import com.tanpu.community.api.enums.OssRelType;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.dao.mapper.community.FileRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
public class OSSFileService {

    @Value("${aliyun.oss.endpoint}")
    public String endpoint;

    @Value("${aliyun.oss.accessId}")
    public String accessId;

    @Value("${aliyun.oss.accessSK}")
    public String accessSK;

    @Value("${aliyun.oss.bucketName}")
    public String bucketName;

    private AliyunOSSHelper ossHelper;

    @Resource
    private FileRecordMapper fileRecordMapper;


    @Autowired
    private UuidGenHelper uuidGenHelper;

    @PostConstruct
    public void init() {
        ossHelper = AliyunOSSHelper.build(endpoint, accessId, accessSK);
    }


    @Transactional
    public FileRecordEntity uploadFile(byte[] data, String fileName) {
        String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
        return uploadFile(data, fileName, fileSuffix, null, OssRelType.None);
    }

    @Transactional
    public FileRecordEntity uploadFile(byte[] data, String fileName, String relId, OssRelType relType) {
        String fileSuffix = fileName.substring(fileName.lastIndexOf('.') + 1);
        return uploadFile(data, fileName, fileSuffix, relId, relType);
    }

    @Transactional
    public FileRecordEntity uploadFile(byte[] data, String fileName, String fileSuffix,
                                       String relId, OssRelType relType) {
        // todo uniqueLong
        String[] arr = StringUtils.split(fileName, ".");
        String suffix = arr[arr.length - 1];
        if (FileTypeEnum.imageTypeSet.contains(suffix)) {

            String id = uuidGenHelper.getUuidStr();
            String key = id + "." + suffix;
            ossHelper.writeFile(bucketName, key, data, fileSuffix);

            FileRecordEntity record = new FileRecordEntity();
            record.setFileId(uuidGenHelper.getUuidStr());
            record.setDeleteTag(BizStatus.DeleteTag.tag_init);
            record.setFileOssKey(key);
            record.setFileName(fileName);
            record.setFileId(id);
            record.setPreviewUrl(ossHelper.getPreSignedUrl(bucketName, key));
            record.setFileType(relType.type);


            fileRecordMapper.insert(record);

            return record;
        }
        throw new BizException("文件格式中暂不支持");

    }

    public FileRecordEntity queryById(String fileId) {
        return fileRecordMapper.selectById(fileId);
    }

    public List<FileRecordEntity> queryByIds(List<String> fileIds) {
        return fileRecordMapper.selectBatchIds(fileIds);
    }
}