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.DataProvider;
import org.testng.annotations.Test;

public class TestPageDeduction extends BaseTestImpl {
    AXFTools axfTools=new AXFTools();

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

    @DataProvider
    public Object[][] dataProvider() {
        return new Object[][]{
                {"SUCCESS"},
                {"FAIL"},
                {"REFUND"},
                {"REFUND_F"}
        };
    }
    @Test(description = "获取先享后付扣款明细",dataProvider = "dataProvider")
    public void testPageDeduction(String state) {
        //  如果运行的环境为灰度则不执行，原因是灰度环境先享后付没有数据
        if (xmAppApi.getUrl().contains("canary-fit.xiaomai5.com"))
            return;
        //获取订购单
        JSONObject getAXFPageOrder = axfTools.getAXFPageOrder("{\"current\":0,\"size\":20,\"studioId\":\""+xmAppApi.getLoginInfo().getStudioId()+"\",\"brandId\":\""+xmAppApi.getLoginInfo().getBrandId()+"\",\"commodityCardType\":\"DEPOSIT_CARD\",\"voucherBusinessNo\":null,\"thirdOrderNo\":null,\"existFailDeduction\":null,\"operatorId\":\""+xmAppApi.getLoginInfo().getAdminId()+"\"}");
        if (getAXFPageOrder.getJSONObject("result").getJSONArray("records").size() > 0) {

            //倒数第一条订购单
            JSONObject data = getAXFPageOrder.getJSONObject("result").getJSONArray("records").getJSONObject(getAXFPageOrder.getJSONObject("result").getJSONArray("records").size()-1);
            String businessNo = data.getString("voucherBusinessNo");
            String thirdOrderNo = data.getString("thirdOrderNo");
            String memberId = data.getString("memberId");
            String memberName = data.getString("memberName");
            String totalPeriod = data.getString("totalPeriod");


            //倒数第一条订购单的扣款明细
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("studioId",xmAppApi.getLoginInfo().getStudioId());
            jsonObject.put("brandId",xmAppApi.getLoginInfo().getBrandId());
            jsonObject.put("operatorId",xmAppApi.getLoginInfo().getAdminId());
            jsonObject.put("current", 1);
            jsonObject.put("size", 10);
            jsonObject.put("state", state);
            jsonObject.put("businessNo", businessNo);
            jsonObject.put("thirdOrderNo",null);

            xmAppApi.doRequest(RequestType.JSON, params, jsonObject.toJSONString(), headers).assetsSuccess(true);
            JSONObject response = xmAppApi.getBodyInJSON();
            String success = response.getString("success");
            Assert.assertEquals(success, "true", "获取接先享后付扣款明细失败");

            if (response.getJSONObject("result").getJSONArray("records").size() > 0) {

                JSONObject dataDeduction = response.getJSONObject("result").getJSONArray("records").getJSONObject(0);
                String id=dataDeduction.getString("id");
                // 校验扣款明细列表和对应的订购单数据一致
                Assert.assertEquals(dataDeduction.getString("businessNo"), businessNo, "订购单和对应的扣款明细的订单号应一致");
                Assert.assertEquals(dataDeduction.getString("thirdOrderNo"), thirdOrderNo, "订购单和对应的扣款明细的支付宝订购单号应一致");
                Assert.assertEquals(dataDeduction.getString("buyerId"), memberId, "订购单和对应的扣款明细的会员id应一致");
                Assert.assertEquals(dataDeduction.getString("buyerName"), memberName, "订购单和对应的扣款明细的会员姓名应一致");
                Assert.assertEquals(dataDeduction.getString("totalPeriod"), totalPeriod, "订购单和对应的扣款明细的扣款总期数应一致");

                //获取扣款明细列表和对应的扣款明细详情数据一致
                //获取扣款明细详情
                JSONObject detail=axfTools.getDeduction("{\"id\":\""+id+"\",\"brandId\":\""+xmAppApi.getLoginInfo().getBrandId()+"\",\"studioId\":\""+xmAppApi.getLoginInfo().getStudioId()+"\",\"operatorId\":\""+xmAppApi.getLoginInfo().getAdminId()+"\"}");
                Assert.assertEquals(detail.getString("success"),"true","获取先享后付扣款明细详情接口失败");
                Assert.assertEquals(detail.getJSONObject("result").getString("buyerId"),dataDeduction.getString("buyerId"),"扣款明细和对应的扣款明细详情的会员id应一致");
                Assert.assertEquals(detail.getJSONObject("result").getString("buyerName"),dataDeduction.getString("buyerName"),"扣款明细和对应的扣款明细详情的会员名称应一致");
                Assert.assertEquals(detail.getJSONObject("result").getString("totalPeriod"),dataDeduction.getString("totalPeriod"),"扣款明细和对应的扣款明细详情的总期数应一致");
                Assert.assertEquals(detail.getJSONObject("result").getString("businessNo"),dataDeduction.getString("businessNo"),"扣款明细和对应的扣款明细详情的订单号应一致");
                Assert.assertEquals(detail.getJSONObject("result").getString("thirdOrderNo"),dataDeduction.getString("thirdOrderNo"),"扣款明细和对应的扣款明细详情的支付宝订购单号应一致");

            }
        }

    }}
