package net.northking.cctp.executePlan.feign;


import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.utils.JSONUtil;
import io.swagger.annotations.ApiParam;
import net.northking.cctp.common.dto.AssociatedFilesDto;
import net.northking.cctp.common.feign.FeignCctpConfig;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.executePlan.utils.MinioPathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

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

@FeignClient(name = "cctp-files", path = "/cctp-files", configuration = {FeignCctpConfig.class})
public interface AttachmentFeignClient {

    Logger logger = LoggerFactory.getLogger(AttachmentFeignClient.class);

    @PostMapping(value = "/v1/update",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<String> update(Map<String,String> file);

    @PostMapping(value = "/pub/associatedFiles")
    ResultWrapper<String> associatedFiles(@ApiParam(value = "关联文件请求对象") @RequestBody AssociatedFilesDto associatedFilesDto);

    @GetMapping(value = "/pub/deleteFileByObjIdAndBusinessCode")
    ResultWrapper<String> deleteFileByObjId(@RequestParam("objectId") String objectId, @RequestParam("businessCode") String businessCode);

    @PostMapping(value = "/pub/deleteFileByObjIds")
    ResultWrapper<String> deleteFileByObjIds(@RequestBody List<String> objectId);

    @PostMapping(value = "/pub/clearSceneTaskAttachments")
    ResultWrapper<String> clearSceneTaskAttachments(@RequestParam(value = "objId") String objId,
                                      @RequestParam("businessCode") String businessCode,
                                      @RequestParam("noDeleteFileId") String noDeleteFileId);

    default void associatedFiles(List<String> fileIds, String objId) {
        if (!CollectionUtils.isEmpty(fileIds)) {
            fileIds = fileIds.stream().map(fileId -> {
                if (fileId.contains("_")){
                    return "/" + MinioPathUtils.idToPath(fileId)[1];
                }
                return fileId;
            }).collect(Collectors.toList());
            AssociatedFilesDto associatedFilesDto = new AssociatedFilesDto();
            associatedFilesDto.setObjId(objId);
            associatedFilesDto.setFileIds(fileIds);
            try {
                ResultWrapper<String> wrapper = this.associatedFiles(associatedFilesDto);
                if (wrapper == null || !wrapper.isSuccess()) {
                    logger.error("文件关联失败:{}", JSONUtil.toJSONString(wrapper));
                }
            } catch (Exception e) {
                logger.error("文件关联失败");
                e.printStackTrace();
            }
        }
    }
}
