package com.huatek.busi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.huatek.busi.common.FileTypeEnum;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.FileUtils;
import com.huatek.busi.dto.SysFileDto;
import com.huatek.busi.entity.*;
import com.huatek.busi.init.SftpInit;
import com.huatek.busi.mapper.SysFileMapper;
import com.huatek.busi.service.*;
import com.huatek.busi.vo.SysFileDiagramVo;
import com.huatek.busi.vo.SysFileVo;
import com.huatek.busi.where.SysFileDiagramSelectVo;
import com.huatek.busi.where.SysFileSelectVo;
import com.huatek.frame.base.util.Constants;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Component
@Service(interfaceClass = ISysFileDiagramService.class)
@Transactional(rollbackFor = Exception.class)
public class SysFileDiagramServiceImpl extends BaseDao<SysFileDiagram, String> implements ISysFileDiagramService {

    @Resource
    private SysFileMapper sysFileMapper;

    @Resource
    private SftpInit sftpInit;


    @Reference(check = false)
    ILabReportBasisDataService labReportBasisDataService;


    @Reference(check = false)
    IGlobalSettingItemService globalSettingItemService;

    @Reference(check = false)
    IRMasterDataService rMasterDataService;

    @Reference(check = false)
    IRMasterDataRecordService rMasterDataRecordService;

    @Override
    public void saveOrUpdate(SysFileDiagramVo sysFileVo){
        SysFileDiagram sysFile = new SysFileDiagram();
        sysFileVo.setId(Uuid.getUUID());
        sysFileVo.setCreateDate(LocalDateTime.now());
        sysFileVo.setDeleted(Constants.DEFAULT_NO);
        BeanUtils.copyProperties(sysFileVo, sysFile);
        this.add(sysFile);
    }
    @Override
    @Transactional(propagation= Propagation.SUPPORTS)
    public Map<String, Object> deleteFiles(List<String> refIds, String equType){
        // 去除重复元素
        refIds = refIds.stream().distinct().collect(Collectors.toList());


        GlobalSettingItem globalSettingItem = globalSettingItemService.findOne();
        String localTempPath = globalSettingItem.getLocalTempPath();

        List<SysFileDto> sysFileDtoList = sysFileMapper.findAllByDeleted(refIds, equType);
        if(CollectionUtil.isNotEmpty(sysFileDtoList)){
            for (SysFileDto item :sysFileDtoList){
                String id = item.getId();
                String filePath = item.getFilePath();
                String fileName = item.getFileName();
                String suffixFilePath = item.getSuffixFilePath();

                if(StrUtil.isNotEmpty(filePath) && StrUtil.isNotEmpty(fileName) && StrUtil.isNotEmpty(suffixFilePath)){
                    // 远程下载文件至本地
                    String returnCode = sftpInit.download(filePath, fileName, localTempPath, fileName);
                    if(Objects.equals(returnCode, "1")){
                        return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.NOT.FIND.FILEPATH", "");
                    }
                    // 远程删除文件
                    sftpInit.delete("/" + filePath + "/", fileName);
                    // 本地文件上传远程服务器上
                    filePath = sftpInit.upload(suffixFilePath, localTempPath, File.separator + fileName, "delete");
                    sysFileMapper.updateFilePathById(id, filePath);
                    // 删除本地临时文件
                    FileUtils.deleteFile(localTempPath.replace("/", "\\") + "\\" + fileName);
                }
            }
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.DELETE.SUCCESS", "");
        }
        return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.DELETE.FAIL", "");
    }

}
