package cn.superdesk.uniorder.controller._common;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
import cn.superdesk.uniorder.common.rest.response.AnResponseStreamUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.rest.response.ResponseResultOuterUtils;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto._constant.ResourceConstant;
import cn.superdesk.uniorder.dubbo.consumer._common.ProviderEnvConstantDubboCustomerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Description: 统一订单 公共 文件下载
 * @Author: linjinyu
 * @CreateDate: 9/17/20 4:12 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 9/17/20 4:12 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "公共接口-文件下载-特定 Excel", tags = {"公共接口-文件下载-特定 Excel"})
@Slf4j
@RestController
@RequestMapping("/common/excel/export")
public class CommonExcelExportDownloadController extends BaseController {

	@Resource
	private ProviderEnvConstantDubboCustomerService providerEnvConstantDubboCustomerService;

	/**
	 * test
	 *
	 * @param response
	 * @param map
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/d", method = RequestMethod.GET)
	public R<String> d(HttpServletResponse response, @RequestParam Map<String, Object> map) throws IOException {

		log.info("\nCommonExcelExportDownloadController.d => {}", JSONUtil.toJsonStr(map));

		if (map.isEmpty()) {
			return ResponseResultOuterUtils.createErrorResult("500", "文件不能为空");
		}

		ResourceConstant excelExportConstant = providerEnvConstantDubboCustomerService.getResourceConstant();

		String excelExportPathRoot             = excelExportConstant.getPathRoot();
		String excelExportRelativePathTemplate = excelExportConstant.getPathTemplate();
		String excelExportRelativePathStorage  = excelExportConstant.getPathStorage();

		String storagePath = excelExportPathRoot + excelExportRelativePathStorage;

		String fileName = AnStringUtils.getString(map.get("fileName"));
		String path     = AnStringUtils.getString(map.get("path"));

		String fileUrl = storagePath + path + fileName;

//		String fileUrl  = "/Users/linjinyu/tmp/_storage/excel/invoice/customer/客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx";
//		String fileName = "客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx";

		AnResponseStreamUtils.download(fileUrl, fileName, response);

		return null;
	}

	/**
	 * Test
	 * http://192.168.6.223:8089/web/common/excel/export/download/excel/invoice/customer/客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx
	 * <p>
	 * http://192.168.6.223:8089/web/common/excel/export/download/excel%2finvoice%2fcustomer%2f客户结算_开票申请(发票)详情_电子发票_createAt_2021-03-02 14%2f56%2f36.xlsx
	 *
	 * @param response
	 * @param path
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/e/{path}", method = RequestMethod.GET)
	public void e(HttpServletResponse response, @PathVariable String path) throws IOException {

		log.info("\nCommonExcelExportDownloadController.download => {}", path);

		String fileUrl  = "/Users/linjinyu/tmp/_storage/excel/invoice/customer/客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx";
		String fileName = "客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx";

		AnResponseStreamUtils.download(fileUrl, fileName, response);

	}

	/**
	 * TODO Curr
	 * http://192.168.6.223:8089/web/common/excel/export/f/excel/invoice/customer/客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/f/**", method = RequestMethod.GET)
	public void f(HttpServletRequest request, HttpServletResponse response) throws IOException {


		ResourceConstant excelExportConstant = providerEnvConstantDubboCustomerService.getResourceConstant();

		String excelExportPathRoot             = excelExportConstant.getPathRoot();
		String excelExportRelativePathTemplate = excelExportConstant.getPathTemplate();
		String excelExportRelativePathStorage  = excelExportConstant.getPathStorage();

		String storagePath = excelExportPathRoot + excelExportRelativePathStorage;

		// /common/excel/export/f/excel/invoice/customer/客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx
		String input = request.getServletPath();

		// excel/invoice/customer/客户结算_开票申请(发票)详情_电子发票_createAt_202103021503.xlsx
		String inputRelativePath = input.replace("/common/excel/export/f", "");
		String fileFullPath           = storagePath + inputRelativePath;
		String fileName      = FileUtil.getName(fileFullPath);

		log.info("\nCommonExcelExportDownloadController.f => \n{}\n{}\n{}\n{}\n{}",
				request.getServerName(),
				request.getServletPath(),
				inputRelativePath,
				fileFullPath,
				fileName
		);


		AnResponseStreamUtils.download(fileFullPath, fileName, response);


//		CommonExcelExportDownloadController.f =>
//		test.api.uniorder.avic-s.com
//		/common/excel/export/f/excel/order/manager/订单管理_订单(商品)_导出成本_createAt_202104251201.xlsx
//		                                  /excel/order/manager/订单管理_订单(商品)_导出成本_createAt_202104251201.xlsx
//		/data/wwwroot/default/tmp/_storage/excel/order/manager/订单管理_订单(商品)_导出成本_createAt_202104251201.xlsx


	}


}
