package com.jsowell.api.uniapp;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.common.util.file.AliyunOssUploadUtils;
import com.jsowell.common.util.file.FileUtils;
import com.jsowell.pile.dto.QueryOrderDTO;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.vo.uniapp.PileConnectorVO;
import com.jsowell.service.PileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Anonymous
@RestController
@RequestMapping("/app-xcx-h5")
public class JumpController extends BaseController {

	@Autowired
	private PileService pileService;

	@Autowired
	private IOrderBasicInfoService orderBasicInfoService;

	/**
	 * 查询充电桩详情
	 * http://localhost:8080/app-xcx-h5/pile/pileDetail/{pileSn}
	 */
	@GetMapping("/pile/pileDetail/{pileSn}")
	public RestApiResponse<?> getPileDetail(HttpServletRequest request, @PathVariable("pileSn") String pileSn) {
		logger.info("app-xcx-h5查询充电桩详情 param:{}", pileSn);
		logger.info("User-Agent:{}", request.getHeader("user-agent"));
		RestApiResponse<?> response = null;
		try {
			PileConnectorVO vo = pileService.getPileDetailByPileSn(pileSn);
			response = new RestApiResponse<>(vo);
		} catch (BusinessException e) {
			logger.warn("app-xcx-h5查询充电桩详情 warn", e);
			response = new RestApiResponse<>(e.getCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("app-xcx-h5查询充电桩详情 error", e);
			response = new RestApiResponse<>(ReturnCodeEnum.CODE_GET_PILE_DETAIL_ERROR);
		}
		logger.info("app-xcx-h5查询充电桩详情 result:{}", JSONObject.toJSONString(response));
		return response;
	}

	/**
	 * 查询充电枪口详情
	 * http://localhost:8080/app-xcx-h5/pile/connectorDetail/{pileConnectorCode}
	 */
	@GetMapping("/pile/connectorDetail/{pileConnectorCode}")
	public RestApiResponse<?> getConnectorDetail(HttpServletRequest request, @PathVariable("pileConnectorCode") String pileConnectorCode) {
		logger.info("app-xcx-h5查询充电枪口详情 param:{}", pileConnectorCode);
		logger.info("User-Agent:{}", request.getHeader("user-agent"));
		RestApiResponse<?> response = null;
		try {
			PileConnectorVO vo = pileService.getConnectorDetail(pileConnectorCode);
			response = new RestApiResponse<>(vo);
		} catch (BusinessException e) {
			logger.warn("app-xcx-h5查询充电枪口详情 warn param:{}", pileConnectorCode, e);
			response = new RestApiResponse<>(e.getCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("app-xcx-h5查询充电枪口详情 error param:{}", pileConnectorCode, e);
			response = new RestApiResponse<>(ReturnCodeEnum.CODE_GET_PILE_DETAIL_ERROR);
		}
		logger.info("app-xcx-h5查询充电枪口详情 param:{} result:{}", pileConnectorCode, JSONObject.toJSONString(response));
		return response;
	}

	/**
	 * 上传到阿里云oss
	 * http://localhost:8080/app-xcx-h5/uploadOSS
	 * @param file
	 * @return
	 */
	@CrossOrigin
	@PostMapping("/uploadOSS")
	public AjaxResult uploadFileOSS(MultipartFile file) {
		try {
			String url = AliyunOssUploadUtils.uploadFile(file);
			AjaxResult ajax = AjaxResult.success();
			ajax.put("url", url);
			ajax.put("fileName", FileUtils.getName(url));
			ajax.put("newFileName", FileUtils.getName(url));
			ajax.put("originalFilename", file.getOriginalFilename());
			return ajax;
		}catch (Exception e){
			return AjaxResult.error(e.getMessage());
		}
	}

	/**
	 * 临时刷数据接口
	 * http://localhost:8080/app-xcx-h5/tempUpdateVirtualAmount
	 * @param request
	 * @return
	 */
	@PostMapping("tempUpdateVirtualAmount")
	public RestApiResponse<?> tempUpdateVirtualAmount(HttpServletRequest request, @RequestBody QueryOrderDTO dto) {
		logger.info("临时刷数据接口 param:{}", dto);
		RestApiResponse<?> response = null;
		try {
			String s = orderBasicInfoService.tempUpdateVirtualAmount(dto);
			response = new RestApiResponse<>(s);
		} catch (BusinessException e) {
			logger.warn("临时刷数据接口 warn", e);
			response = new RestApiResponse<>(e.getCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("临时刷数据接口 error", e);
			response = new RestApiResponse<>(ReturnCodeEnum.CODE_GET_PILE_DETAIL_ERROR);
		}
		logger.info("临时刷数据接口 result:{}", JSONObject.toJSONString(response));
		return response;
	}

	/**
	 * 临时订单退款
	 * http://localhost:8080/app-xcx-h5/tempOrderRefund
	 * @return
	 */
	@GetMapping("tempOrderRefund")
	public RestApiResponse<?> tempOrderRefund() {
		RestApiResponse<?> response = null;
		try {
			orderBasicInfoService.tempOrderRefund();
			response = new RestApiResponse<>();
		} catch (BusinessException e) {
			logger.warn("临时订单退款接口 warn", e);
			response = new RestApiResponse<>(e.getCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("临时订单退款接口 error", e);
			response = new RestApiResponse<>(ReturnCodeEnum.CODE_WEIXIN_REFUND_ERROR);
		}
		logger.info("临时刷数据接口 result:{}", JSONObject.toJSONString(response));
		return response;
	}

}
