package com.ensign.crm.module.crm.controller.crm;

import com.ensign.crm.framework.common.pojo.CommonResult;
import com.ensign.crm.module.crm.exception.AllKingdeeException;
import com.ensign.crm.module.crm.service.ProxyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @Description: TODO
 * @Date: 2024/9/23 9:39
 * @Created: by ZZSLL
 */

@RestController
@Tag(name = "CRM - Proxy")
@RequestMapping("/crm-api/proxy")
@Validated
@Slf4j
public class ProxyController {

    @Autowired
    private ProxyService proxyService;

    @RequestMapping(value = "/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary = "转发接口")
    @PreAuthorize("@ss.hasPermission('crm:proxy:all')")
    public void proxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
        proxyService.doProxy(request, response);
    }

    @PostMapping(value = "/file/do/**")
    @Operation(summary = "转发接口_文件")
    @PreAuthorize("@ss.hasPermission('crm:proxy:all')")
    public CommonResult<Object> proxyFile(HttpServletRequest request, MultipartFile file) throws IOException, URISyntaxException, AllKingdeeException {
        return CommonResult.success(proxyService.doProxyFile(request, file));
    }

    @GetMapping("/file/read/do")
    @PermitAll
    @Operation(summary = "获取图片")
    public void proxyRead(@RequestParam String path, HttpServletResponse response) throws AllKingdeeException, IOException {
        proxyService.doGetImage(path, response);
    }

    @GetMapping("/file/read.file/do")
    @PermitAll
    @Operation(summary = "获取图片")
    public void proxyReadFile(@RequestParam String url, @RequestParam String file_name, HttpServletResponse response) throws AllKingdeeException, IOException, URISyntaxException {
        proxyService.doGetFile(url, file_name, response);
    }

//    @RequestMapping(value = "/unauth/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @Operation(summary = "转发接口")
//    @PermitAll
//    public void proxyUnAuth(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
//        String requestURI = request.getRequestURI();
//        String[] unAuthPath = new String[]{
//                // 市场活动详情
//                "/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_receipt/yem_crm_marketactivity/getMarketactivities",
//
//                // 获取机型
//                "/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_crmbasic/yem_crm_region/CRM_yem_crm_region",
//
//                // 活动登记
//                "/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_receipt/api/MarketQRCodeAdd"
//        };
//        if (isAuthorized(requestURI, unAuthPath)) {
//            proxyService.doProxy(request, response);
//        } else {
//            response.sendError(HttpServletResponse.SC_FORBIDDEN);
//        }
//    }

    private boolean isAuthorized(String requestURI, String[] unAuthPath) {
        for (String path : unAuthPath) {
            if (requestURI.equals(path)) {
                return true;
            }
        }
        return false;
    }
}
