package com.rongwei.bscommon.sys.service.impl;

import com.rongwei.bscommon.sys.service.SysMindMappingBsService;
import com.rongwei.commonservice.service.SysConfigService;
import com.rongwei.commonservice.service.SysMindMappingService;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommonentity.commonservers.domain.SysMindMappingDo;
import com.rongwei.rwcommonentity.commonservers.vo.SysMindMappingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@Service
public class SysMindMappingBsServiceImpl implements SysMindMappingBsService {

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private SysMindMappingService sysMindMappingService;

    /**
     * 问题分析故障树分析图片存储
     * @param sysMindMappingVo
     */
    @Override
    public void saveMindImg(SysMindMappingVo sysMindMappingVo) throws IOException {
        // 文件base64内容
        String imgContent = sysMindMappingVo.getImgContent();
        // 替换到头部标识信息
        imgContent = imgContent.replace("data:image/png;base64,","");
        // 获取存储文件路径
        String fileRootPath = sysConfigService.getContentByConfigCode("fileRootPath");
        String imgPath = fileRootPath + File.separator + "mindImg" + File.separator + SecurityUtil.getUUID() + ".png";
        // 写入文件内容
        File file = new File(imgPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        byte[] buffer = new BASE64Decoder().decodeBuffer(imgContent);
        FileOutputStream out = new FileOutputStream(imgPath);
        out.write(buffer);
        out.close();
        // 图片存储
        SysMindMappingDo sysMindMappingDo = new SysMindMappingDo();
        sysMindMappingDo.setId(sysMindMappingVo.getId());
        sysMindMappingDo.setMindimg(imgPath);
        sysMindMappingService.saveOrUpdate(sysMindMappingDo);
    }
}
