package com.yd.oss.api.service.impl;

import com.yd.common.result.Result;
import com.yd.oss.api.service.ApiMaterialService;
import com.yd.oss.feign.dto.ApiMaterialDto;
import com.yd.oss.feign.request.ApiMaterialDownloadRequest;
import com.yd.oss.feign.request.ApiMaterialListRequest;
import com.yd.oss.feign.response.ApiMaterialDownloadResponse;
import com.yd.oss.feign.response.ApiMaterialListResponse;
import com.yd.oss.service.dto.MaterialDto;
import com.yd.oss.service.model.Material;
import com.yd.oss.service.model.RelObjectMaterial;
import com.yd.oss.service.service.CompressedFileService;
import com.yd.oss.service.service.IMaterialService;
import com.yd.oss.service.service.IRelObjectMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ApiMaterialServiceImpl implements ApiMaterialService {

    @Autowired
    private IMaterialService iMaterialService;

    @Autowired
    private CompressedFileService compressedFileService;

    @Autowired
    private IRelObjectMaterialService iRelObjectMaterialService;

    /**
     * 列表查询-材料基础信息
     * @param request
     * @return
     */
    @Override
    public Result<List<ApiMaterialListResponse>> list(ApiMaterialListRequest request) {
        List<Material> materialList = iMaterialService.queryList(MaterialDto.builder().objectType(request.getObjectType()).build());
        if (!CollectionUtils.isEmpty(materialList)) {
            List<ApiMaterialListResponse> responses = materialList.stream().map(dto -> {
                ApiMaterialListResponse response = new ApiMaterialListResponse();
                BeanUtils.copyProperties(dto,response);
                return response;
            }).collect(Collectors.toList());
            return Result.success(responses);
        }
        return Result.success();
    }

    /**
     * 下载-材料列表压缩包
     * @param request
     * @return
     */
    @Override
    public Result<ApiMaterialDownloadResponse> downloadCompressedFile(ApiMaterialDownloadRequest request) {
//        if (StringUtils.isNotBlank(request.getObjectBizId())) {
//            List<ApiMaterialDto> apiMaterialDtoList = new ArrayList<>();
//            List<RelObjectMaterial>  relObjectMaterialList = iRelObjectMaterialService.queryList(request.getObjectBizId());
//            if (!CollectionUtils.isEmpty(relObjectMaterialList)) {
//                List<String> materialBizIdList = relObjectMaterialList.stream().map(RelObjectMaterial::getMaterialBizId).collect(Collectors.toList());
//
//                for (RelObjectMaterial relObjectMaterial : relObjectMaterialList) {
//
//                }
//            }
//        }
        return compressedFileService.downloadCompressedFile(request);
    }


}