package com.yc.ship.module.trade.controller.app.ota;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.controller.app.ota.vo.OtaStandardOrderRespVO;
import com.yc.ship.module.trade.controller.app.ota.vo.OtaStandardOrderVO;
import com.yc.ship.module.trade.service.ota.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.TRADE;


/**
 * &#064;Description ：ota标准服务controller
 * &#064;Author ：qsl
 * &#064;Date ：2025/6/17 20:52
 */
@Tag(name = "ota标准服务")
@RestController
@RequestMapping("/ship/ota")
@Validated
@Slf4j
public class OtaRelationController {

    @Resource
    private XieChengService xieChengService;
    @Resource
    private MeiTuanService meiTuanService;
    @Resource
    private TiktokLifeService tiktokLifeService;
    @Resource
    private TongChengService tongChengService;
    @Resource
    private TiktokCalendarService tiktokCalendarService;
    @Resource
    private OtaStandardService otaStandardService;
    @Resource
    private ZshlService zshlService;

    @PostMapping("/createOrder")
    @Operation(summary = "创建订单")
    @OperateLog(type = TRADE)
    public CommonResult<OtaStandardOrderRespVO> createOrder(@Valid @RequestBody OtaStandardOrderVO otaStandardOrderVO){
        return CommonResult.success(null);
    }


    @PostMapping(value = "/xieCheng/{tenantId}/{path}", consumes = {
            MediaType.APPLICATION_JSON_UTF8_VALUE}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary = "携程-请求")
    @OperateLog(type = TRADE)
    public String xieCheng(
            @PathVariable("tenantId") Long tenantId, @PathVariable("path") String path,
            HttpServletRequest request, @RequestBody String body) {
        log.info("携程-请求：tenantId{};path=;body={}", tenantId, body);
        return xieChengService.doRequest(tenantId, path, body, request);
    }

    /**
     * @description 美团-请求
     * @param tenantId 租户ID
     * @param path       请求名
     * @param request    HttpServletRequest
     * @param jsonStr    请求参数
     * @return java.lang.String
     */
    @PostMapping(value = "/meiTuan/{tenantId}/{path}", consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary ="美团-请求")
    @OperateLog(type = TRADE)
    public String meiTuan(@PathVariable("tenantId") Long tenantId, @PathVariable("path") String path, HttpServletRequest request, @RequestBody String jsonStr) {
        log.error("[meiTuan]开始请求jsonStr:{}", jsonStr);
        return meiTuanService.doRequest(request, path, jsonStr, tenantId);
    }


    /**
     * @param tenantId 租户ID
     * @param path       请求名
     * @param request    HttpServletRequest
     * @param body       请求参数
     * @return java.lang.String
     * @description 同程-请求
     *
     * @date 2025/12/7 09:00
     */
    @PostMapping(value = "/tongCheng/{tenantId}/{path}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary ="同程-请求")
    @OperateLog(type = TRADE)
    public String tongCheng(
            @PathVariable("tenantId") Long tenantId, @PathVariable("path") String path,
            HttpServletRequest request, @RequestBody String body) {
        log.info("同程-请求：tenantId {};body={}", tenantId, body);
        return tongChengService.doRequest(tenantId, path, request, body);
    }


    /**
     * @description 抖音三方码-请求
     * @param tenantId 租户
     * @param path 路径
     * @param request HttpServletRequest
     * @param body 请求参数
     * @return java.lang.String
     */
    @PostMapping(value="/tiktok/{tenantId}/{path}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary ="抖音三方码-请求")
    @OperateLog(type = TRADE)
    public String tiktok(@PathVariable("tenantId") Long tenantId, @PathVariable("path") String path, HttpServletRequest request, @RequestBody String body) {
        log.info("抖音三方码-请求：tenantId {};body={}", tenantId, body);
        return tiktokLifeService.doRequest(tenantId, path, request, body, "");
    }

    /**
     * @description 抖音三方码(抖音来客版本)-请求
     * @param tenantId 租户
     * @param path 路径
     * @param request HttpServletRequest
     * @param body 请求参数
     * @return java.lang.String
     */
    @PostMapping(value="/tiktokLife/{tenantId}/{path}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary ="抖音三方码(抖音来客版本)-请求")
    @OperateLog(type = TRADE)
    public String tiktokLife(@PathVariable("tenantId") Long tenantId, @PathVariable("path") String path, HttpServletRequest request, @RequestBody String body) {
        log.info("抖音三方码(抖音来客版本)-请求：tenantId {};body={}", tenantId, body);
        return tiktokLifeService.doRequest(tenantId, path, request, body, "life");
    }

    @PostMapping(value="/tiktokCalendar/{tenantId}/{path}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary ="抖音日历票(抖音来客版本)-请求")
    @OperateLog(type = TRADE)
    public String tiktokCalendar(@PathVariable("tenantId") Long tenantId, @PathVariable("path") String path, HttpServletRequest request, @RequestBody String body) {
        log.info("抖音日历票(抖音来客版本)-请求：tenantId {};body={}", tenantId, body);
        String result = tiktokCalendarService.doRequestTiktokCalendar(tenantId, path, request, body);
        log.info("抖音日历票(抖音来客版本)-返回:{}",result);
        return result;

    }


    @PostMapping(value = "/zshl/{tenementId}/{method}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Operation(summary ="OTA统一分销接口")
    @OperateLog(type = TRADE)
    @ResponseBody
    public String doRequest(HttpServletRequest request, @PathVariable("tenementId") Long tenantId, @PathVariable("method") String method, @RequestBody String jsonStr) {
        log.info("OTA统一分销接口：tenementId {};method={};body={}", tenantId, method, jsonStr);
        return zshlService.doRequest(request,tenantId,method, jsonStr);
    }

    @PostMapping("notifyFailedTicketInfoOtaPush")
    @Operation(summary ="核销通知失败手动重推")
    @OperateLog(type = TRADE)
    @ResponseBody
    public CommonResult<?> notify(@RequestBody String jsonStr){
        try{
            JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
            String voucherCode = jsonObject.getStr("voucherCode");
            boolean flag = otaStandardService.notifyFailedVoucherInfoOtaPush(voucherCode);
            return CommonResult.success(flag);
        }catch (Exception e){
            log.error("手动推送核销通知失败，",e);
            throw exception0(500,e.getMessage());
        }
    }

    @PostMapping("notify")
    @Operation(summary ="手动通知")
    @OperateLog(type = TRADE)
    @ResponseBody
    public CommonResult<?> notify1(@RequestBody String jsonStr){
        try{
            return CommonResult.success(true);
        }catch (Exception e){
            log.error("手动推送核销通知失败，",e);
            throw exception0(500,e.getMessage());
        }
    }
}
