package com.xiaomai.cases.polar.finance.axf;

import com.alibaba.fastjson.JSONObject;
import com.xiaomai.basetest.BaseTestImpl;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.LoginAccount;
import com.xiaomai.enums.RequestType;
import com.xiaomai.enums.Terminal;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestListRelationDeduction extends BaseTestImpl {

    AXFTools axfTools=new AXFTools();

    @BeforeMethod
    @Override
    public void beforeTest(){
        setTestInfo(ApiModule.Polar_Finance,"API_listRelationDeduction", LoginAccount.GYM_PROD, Terminal.B,"yff");
        super.beforeTest();
    }


    @Test(description = "扣款明细的关联交易")
    public void testListRelationDeduction(){

        //  如果运行的环境为灰度则不执行，原因是灰度环境先享后付没有数据
        if (xmAppApi.getUrl().contains("canary-fit.xiaomai5.com"))
            return;

        //获取扣款状态为退款成功的，一定会有关联交易
        JSONObject dataRefund=axfTools.pageDeduction("{\"current\":0,\"size\":20,\"studioId\":\""+xmAppApi.getLoginInfo().getStudioId()+"\",\"brandId\":\""+xmAppApi.getLoginInfo().getBrandId()+"\",\"operatorId\":\""+xmAppApi.getLoginInfo().getAdminId()+"\",\"businessNo\":null,\"thirdOrderNo\":null,\"state\":\"REFUND\"}");
        int total= Integer.parseInt(dataRefund.getJSONObject("result").getString("total"));

        if(total>0){
            JSONObject dataRefund1=dataRefund.getJSONObject("result").getJSONArray("records").getJSONObject(0);
            String idRefund=dataRefund1.getString("id");
            String buyerIdRefund=dataRefund1.getString("buyerId");
            String buyerNameRefund=dataRefund1.getString("buyerName");
            String businessNoRefund=dataRefund1.getString("businessNo");
            String thirdOrderNoRefund=dataRefund1.getString("thirdOrderNo");


            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",idRefund);
            jsonObject.put("studioId",xmAppApi.getLoginInfo().getStudioId());
            jsonObject.put("brandId",xmAppApi.getLoginInfo().getBrandId());
            jsonObject.put("operatorId",xmAppApi.getLoginInfo().getAdminId());
            xmAppApi.doRequest(RequestType.JSON,params,jsonObject.toJSONString(),headers).assetsSuccess(true);
            JSONObject response=xmAppApi.getBodyInJSON();
            String success=response.getString("success");
            JSONObject dataPay=response.getJSONArray("result").getJSONObject(0);
            Assert.assertEquals(success,"true","获取扣款明细的关联交易失败");
            Assert.assertEquals(dataPay.getString("state"),"SUCCESS","查询退款成功扣款明细的关联交易状态应是扣款成功");
            Assert.assertEquals(dataPay.getString("buyerId"),buyerIdRefund,"查询退款成功扣款明细的关联交易收款中的会员id应与退款一致");
            Assert.assertEquals(dataPay.getString("buyerName"),buyerNameRefund,"查询退款成功扣款明细的关联交易收款中的会员姓名应与退款一致");
            Assert.assertEquals(dataPay.getString("businessNo"),businessNoRefund,"查询退款成功扣款明细的关联交易收款中的订单号应与退款一致");
            Assert.assertEquals(dataPay.getString("thirdOrderNo"),thirdOrderNoRefund,"查询退款成功扣款明细的关联交易收款中的支付宝订购单号应与退款一致");
        }

    }

}
