package com.miaxis.app.controller.film;

import com.alibaba.fastjson.JSONObject;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.exception.CustomException;
import com.miaxis.feign.dto.FilmDTO;
import com.miaxis.wx.service.IWxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;

import static com.miaxis.common.constant.Constants.FILM_BLACK_LIST;

/**
 * 【小程序-客户信息】Controller
 *
 * @author miaxis
 * @date 2021-03-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.STUDENT_PREFIX+"/film/order")
@Api(tags = {"【小程序-电影】"})
@Slf4j
public class FilmController extends BaseController {


    @Autowired
    private IWxOrderService wxOrderService;

    /**
     * 以form表单形式提交数据，发送post请求
     * @explain
     *   1.请求头：httppost.setHeader("Content-Type","application/x-www-form-urlencoded")
     *   2.提交的数据格式：key1=value1&key2=value2...
     * @return 服务器返回数据
     */
    @PostMapping(value = "/fileCommonApi")
    @ApiOperation("电影通用接口")
    public  Response<JSONObject>  httpPostWithForm(@RequestBody FilmDTO filmDTO) throws IOException {

        //检查api
        if (Arrays.asList(FILM_BLACK_LIST).contains(filmDTO.getUrl())){
            throw new CustomException("该api不允许用户直接访问");
        }
        // 用于接收返回的结果
        String resultData = wxOrderService.excuteFilmApi(filmDTO);
        return Response.success(JSONObject.parseObject(resultData));
    }











//    /**
//     * 微信支付查单接口
//     */
//    @GetMapping(value = "/wxorder/{outTradeNo}")
//    @ApiOperation("根据订单号查询订单状态")
//    public Response<String> getOrderInfo(
//            @ApiParam(name = "outTradeNo", value = "商户订单号", required = true)
//            @PathVariable("outTradeNo") String outTradeNo
//    ) throws IOException {
//        HttpGet get = new HttpGet("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/"
//                +outTradeNo+"?mchid="+wxpayConfig.getMerchantId());
//        get.addHeader("Accept", "application/json");
//        HttpResponse response = httpClient.execute(get);
//        String bodyAsString = EntityUtils.toString(response.getEntity());
//        JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
//        int statusCode = response.getStatusLine().getStatusCode();
//        if (statusCode == 200) {
//            return Response.success(jsonObject.getString("trade_state"));
//        } else {
//            throw new CustomException(EntityUtils.toString(response.getEntity()));
//        }
//    }















}
