package com.njcn.csharmonic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.FileUtil;
import com.njcn.csharmonic.mapper.CsElementMapper;
import com.njcn.csharmonic.pojo.po.CsElement;
import com.njcn.csharmonic.pojo.vo.ElementsVO;
import com.njcn.csharmonic.service.ICsElementService;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 组态图元库 服务实现类
 * </p>
 *
 * @author xuyang
 * @since 2023-06-14
 */
@Service
@AllArgsConstructor
public class CsElementServiceImpl extends ServiceImpl<CsElementMapper, CsElement> implements ICsElementService {

    private final FileStorageUtil fileStorageUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addElement(MultipartFile multipartFile) {
        CsElement csElement = new CsElement();
        if (Objects.isNull(multipartFile)){
            throw new BusinessException("组态图元json缺失");
        }
        LambdaQueryWrapper<CsElement> lambdaQueryWrapper = new LambdaQueryWrapper<CsElement>();
        csElement = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (!Objects.isNull(csElement)){
            fileStorageUtil.deleteFile(csElement.getPath());
        } else {
            csElement = new CsElement();
        }
        try {
            InputStream inputStream = multipartFile.getInputStream();
            String path = fileStorageUtil.uploadStream(inputStream, OssPath.ELEMENT, FileUtil.generateFileName("json"));
            csElement.setPath(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.saveOrUpdate(csElement);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ElementsVO findElement() {
        ElementsVO elementsVo = new ElementsVO();
        LambdaQueryWrapper<CsElement> lambdaQueryWrapper = new LambdaQueryWrapper<CsElement>();
        CsElement csElement = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (!Objects.isNull(csElement)){
            InputStream inputStream = fileStorageUtil.getFileStream(csElement.getPath());
            String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
            elementsVo.setId(csElement.getId());
            elementsVo.setJson(text);
        }
        return elementsVo;
    }


    /**
     * 将json转成流
     * @param jsonString
     * @return
     */
    public  InputStream writeJsonStringToInputStream(String jsonString) {
        return new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
    }

}
