package com.fuyuanshen.app.service;

import com.fuyuanshen.app.domain.bo.AppBusinessFileBo;
import com.fuyuanshen.app.domain.dto.AppFileDto;
import com.fuyuanshen.app.domain.vo.AppBusinessFileVo;
import com.fuyuanshen.app.domain.vo.AppFileVo;
import com.fuyuanshen.common.core.exception.ServiceException;
import com.fuyuanshen.common.oss.core.OssClient;
import com.fuyuanshen.common.oss.factory.OssFactory;
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
import com.fuyuanshen.equipment.utils.FileHashUtil;
import com.fuyuanshen.system.domain.vo.SysOssVo;
import com.fuyuanshen.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 登录校验方法
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class AppFileService {

    private final ISysOssService sysOssService;

    private final IAppBusinessFileService appBusinessFileService;

    private final FileHashUtil fileHashUtil;
    private final ISysOssService ossService;


    public List<AppFileVo> list(AppBusinessFileBo bo) {
//        bo.setCreateBy(AppLoginHelper.getUserId());
        return appBusinessFileService.queryAppFileList(bo);
    }


    /**
     * APP文件上传
     */
    public Boolean add(AppFileDto bo) throws IOException {

        MultipartFile[] files = bo.getFiles();
        if (files == null || files.length == 0) {
            throw new ServiceException("请选择要上传的文件");
        }
        if (files.length > 5) {
            throw new ServiceException("最多只能上传5个文件");
        }
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            // 上传文件
            // SysOssVo upload = sysOssService.upload(file);
            String fileHash = fileHashUtil.hash(file);
            SysOssVo upload = ossService.updateHash(file, fileHash);
            // 强制将HTTP替换为HTTPS
            if (upload.getUrl() != null && upload.getUrl().startsWith("http://")) {
                upload.setUrl(upload.getUrl().replaceFirst("^http://", "https://"));
            }

            if (upload == null) {
                return false;
            }

            AppBusinessFileBo appBusinessFileBo = new AppBusinessFileBo();
            appBusinessFileBo.setFileId(upload.getOssId());
            appBusinessFileBo.setBusinessId(bo.getDeviceId());
            appBusinessFileBo.setFileType(bo.getFileType());
            appBusinessFileBo.setCreateBy(AppLoginHelper.getUserId());
            appBusinessFileService.insertByBo(appBusinessFileBo);
        }


        return true;
    }


    public Boolean delete(Long[] ids) {
        AppBusinessFileBo bo = new AppBusinessFileBo();
//        bo.setCreateBy(AppLoginHelper.getUserId());
        bo.setIds(List.of(ids));
        List<AppBusinessFileVo> appBusinessFileVos = appBusinessFileService.queryList(bo);
        List<Long> fileIds = appBusinessFileVos.stream().map(AppBusinessFileVo::getFileId).toList();
        List<SysOssVo> list = sysOssService.listByIds(fileIds);
        for (SysOssVo sysOss : list) {
            OssClient storage = OssFactory.instance(sysOss.getService());
            storage.delete(sysOss.getUrl());
        }
        return appBusinessFileService.deleteWithValidByIds(List.of(ids), true);
    }

}
