package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.csdevice.constant.DataParam;
import com.njcn.csdevice.enums.AlgorithmResponseEnum;
import com.njcn.csdevice.mapper.CsTopologyDiagramTemplateMapper;
import com.njcn.csdevice.pojo.po.CsTopologyDiagramTemplate;
import com.njcn.csdevice.pojo.vo.AppTopologyDiagramVO;
import com.njcn.csdevice.pojo.vo.CsLineTopologyTemplateVO;
import com.njcn.csdevice.pojo.vo.CsTopologyDiagramTemplateVO;
import com.njcn.csdevice.service.CsLineTopologyTemplateService;
import com.njcn.csdevice.service.CsTopologyDiagramTemplateService;
import com.njcn.oss.constant.OssPath;
import com.njcn.oss.utils.FileStorageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * Description:
 * Date: 2023/7/10  18:30【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsTopologyDiagramTemplateServiceImpl extends ServiceImpl<CsTopologyDiagramTemplateMapper, CsTopologyDiagramTemplate> implements CsTopologyDiagramTemplateService {

    private final FileStorageUtil fileStorageUtil;
    private final CsLineTopologyTemplateService csLineTopologyTemplateService;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public CsTopologyDiagramTemplate uploadImage(MultipartFile issuesFile) {
        CsTopologyDiagramTemplate csTopologyDiagramTemplate = new CsTopologyDiagramTemplate();

        if (issuesFile.getSize() > DataParam.FILE_SIZE) {
            throw new BusinessException(AlgorithmResponseEnum.FILE_SIZE_ERROR);
        }
        String filePath = fileStorageUtil.uploadMultipart(issuesFile, OssPath.TOPOLOGY);
        csTopologyDiagramTemplate.setFilePath(filePath);
        csTopologyDiagramTemplate.setName(issuesFile.getOriginalFilename());
        csTopologyDiagramTemplate.setStatus("1");
        boolean save = this.save(csTopologyDiagramTemplate);
//        String fileUrl = fileStorageUtil.getFileUrl(filePath);
        csTopologyDiagramTemplate.setFilePath(filePath);
        return csTopologyDiagramTemplate;
    }

    @Override
    public List<CsTopologyDiagramTemplateVO> queryImage() {
        List<CsTopologyDiagramTemplate> list = this.list(null);

        List<CsTopologyDiagramTemplateVO> collect = list.stream().map(temp -> {
            CsTopologyDiagramTemplateVO vo = new CsTopologyDiagramTemplateVO();
            BeanUtils.copyProperties(temp, vo);

            vo.setFilePath((vo.getFilePath()));
            List<CsLineTopologyTemplateVO> csLineTopologyTemplateVOS = csLineTopologyTemplateService.queryByTopoId(temp.getId());
            vo.setCsLineTopologyTemplateVOList(csLineTopologyTemplateVOS);
            return vo;
        }).collect(Collectors.toList());
        return collect;

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean deleteImage(String id) {
        this.removeById(id);
        csLineTopologyTemplateService.deleteByTopoId(id);
        return true;
    }
}
