package com.jeeplus.pubmodules.editor.service;


import com.aliyun.oss.OSSClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jeeplus.pubmodules.editor.domain.EditorFiles;
import com.jeeplus.pubmodules.editor.mapper.EditorFilesMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Transactional
public class EditorFilesService extends ServiceImpl<EditorFilesMapper, EditorFiles> {



    @Resource
    private EditorFilesMapper editorFilesMapper;

    /**
     * 将富文本内容中的临时图片地址替换为新的
     * @param content 富文本内容
     * @param sourceId 数据id
     * @return
     */
    public String getNewContent(String content,String sourceId){
        List<EditorFiles> list = this.list(new QueryWrapper<EditorFiles>().lambda().eq(EditorFiles::getSourceId, sourceId));
        AtomicReference<String> replace = new AtomicReference<>(content.replace("amp;", ""));
        list.stream().forEach(item->{
            boolean contains = replace.get().contains(item.getTemporaryUrl());
            if(contains){
                String fileTemporaryLookUrl = this.getFileTemporaryLookUrl(item.getUrl());
                String r = replace.get().replace(item.getTemporaryUrl(),fileTemporaryLookUrl);
                replace.set(r);
                EditorFiles editorFiles = new EditorFiles();
                editorFiles.setTemporaryUrl(fileTemporaryLookUrl);
                editorFiles.setSourceId(sourceId);
                this.update(editorFiles,new QueryWrapper<EditorFiles>().lambda().eq(EditorFiles::getSourceId,sourceId).eq(EditorFiles::getTemporaryUrl,item.getTemporaryUrl()));
            }
        });
        return replace.get();
    }

    /**
     * 富文本图片新增
     * @param temporaryUrl
     * @param url
     * @param sourceId
     * @return
     */
    public void saveUrl(String temporaryUrl,String url,String sourceId){
        EditorFiles editorFiles = new EditorFiles();
        editorFiles.setSourceId(sourceId);
        editorFiles.setTemporaryUrl(temporaryUrl);
        editorFiles.setUrl(url);
        this.save(editorFiles);
    }


    @Value("${config.accessory.aliyun.aliyunUrl}")
    private String aliyunUrl;
    @Value("${config.accessory.aliyun.aliyunDownloadUrl}")
    private String aliyunDownloadUrl;
    @Value("${config.accessory.aliyun.endpoint}")
    private String endpoint;
    @Value("${config.accessory.aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${config.accessory.aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Value("${config.accessory.aliyun.bucketName}")
    private String bucketName;
    /**
     * 阿里云获取临时文件查看url
     * @param url
     */
    public String getFileTemporaryLookUrl(String url){
        url = url.replace("amp;","");
        String cons = "";
        if (url.contains(aliyunUrl)){
            cons = aliyunUrl;
        }else if (url.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
            cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
        }else {
            cons = aliyunDownloadUrl;
        }
        String key = "";
        String[] split = url.split(cons + "/");
        if(split.length>1){
            key = split[1];
        }else{
            key = url;
        }
        // 指定过期时间为24小时。
        Date expiration = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 );
        //初始化OSSClient
        OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
        return ossClient.generatePresignedUrl(bucketName, key, expiration).toString();
    }
}
