package com.xiaomai.cases.polar.schedule.camp;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.LoginAccount;
import com.xiaomai.enums.RequestType;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.CommUtil;
import com.xiaomai.utils.XMJSONPath;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 删除训练营课次（单个/当前及后续）
 *
 * @author adu
 * data 2024/7/12 17:05
 */

public class TestDelCurrentCampScheduleItem extends SelectTargetTrainingCampAndGetInfo {
    @Resource(name = "campScheduleTools")
    CampScheduleTools campScheduleTools;
    String campId = "";
    String chiefCoachId = "";
    String ruldId = "";
    int loopNum = 0;
    String[] weekDaysArray = {"TUESDAY", "THURSDAY", "SATURDAY", "SUNDAY"};
    List<String> weekdaysList = Arrays.asList(weekDaysArray);

    @BeforeClass
    @Override
    public void beforeTest() {
        setTestInfo(ApiModule.Polar_Schedule, "API_delCampScheduleItem", LoginAccount.ADU_PROD, Terminal.B, "adu");
        super.beforeTest();
        // 获取选择的训练营ID
        campId = super.campId;
        //1.2获取训练营活动的指定教练ID
        chiefCoachId = super.chiefCoachId;
        //获取目标时间段内 最大有效课时数
        loopNum = campScheduleTools.queryExpectedLoopNum(CommUtil.oneKeyGetAddDay(3), super.openEndDate, weekdaysList);

        //创建训练营排课：按次数 星期：2，4，6，7
        campScheduleTools.oneKeyCreateCampRuleSchedule(campId, chiefCoachId, super.courseId,
                super.traineeLowerLimit, super.traineeUpperLimit,
                "1140", super.openEndDate, true,
                String.valueOf(loopNum));
        //查询排课日程:根据训练营和关联课程查询
        campScheduleTools.getCampRuleScheduleList(campId, super.courseId, "");
        Assert.assertTrue(XMJSONPath.getJSONArrayByReadPath(dataApi.getApi_response(), "$.result").size() == 1, "排课创建成功后，日程中没查询到对应数据");
        ruldId = XMJSONPath.getJSONArrayByReadPath(dataApi.getApi_response(), "$.result").getJSONObject(0).getString("ruleId");
    }

    @Test(description = "删除当前课次")
    public void testDelCurrentCampScheduleItem() {
        //获取下周日课次ID(只删除匹配到ruldId的课次)
        String scheduleId7 = campScheduleTools.getCampTableDate(13, ruldId);
        if (!StringUtils.isEmpty(scheduleId7)) {
            //删除本次规则创建的下周日所有的课次
            JSONObject body = new JSONObject();
            body.put("operatorId", xmAppApi.getLoginInfo().getAdminId());
            body.put("studioId", xmAppApi.getLoginInfo().getStudioId());
            body.put("brandId", xmAppApi.getLoginInfo().getBrandId());
            body.put("applyRestRule", false);
            body.put("scheduleId", scheduleId7);
            xmAppApi.doRequest(RequestType.JSON, params, body.toString(), headers);
            Assert.assertTrue(Boolean.valueOf(XMJSONPath.readPath(xmAppApi.getApi_response(),"$.success"))==true,"调用接口返回结果："+XMJSONPath.readPath(xmAppApi.getApi_response(),"$.message"));


            //删除成功后check 下周日课表是否包含删除的课次
            String startDate = CommUtil.oneKeyGetNextWeekXDay(13);//下周日00点时间
            String endDate = CommUtil.oneKeyGetNextWeekEndXDay(7);//下周日23：59
            campScheduleTools.getCampScheduleTable(startDate, endDate);

            Long goalCount = XMJSONPath.getJSONArrayByReadPath(dataApi.getApi_response(), "$.result.timetableGroups").toJavaList(JSONObject.class).stream()
                    .flatMap(e -> e.getJSONArray("timetableList").toJavaList(JSONObject.class).stream())//对过滤出的数据再次取目标数据重新组合成一个新的数据流
                    .filter(e -> null != e.getString("ruleId"))
                    .filter(e -> e.getString("ruleId").equals(ruldId))
                    .map(e -> e.getString("scheduleId")).count();//获取scheduleId 的数量
            Assert.assertTrue(goalCount == 0, "删除当前课次后，再去课表中查看，后端仍然返回数据");

        }

    }


    @Test(description = "删除此case创建的训练营排课日程",priority = 1)
    public void delData() {
        //先删除规则，再操作删除对应的训练营活动，此处顺序不能换，如果先删除训练营活动，对应的排课会自动删除，所以就不用删除排课日程了，这个场景其他case中会体现
        if (null != ruldId || !ruldId.isEmpty()) {
            campScheduleTools.delCampRuleSchedule(ruldId);
            //删除日程后check 日程列表数据是否还能搜索到，期望搜索结果数据为空
            campScheduleTools.getCampRuleScheduleList(campId, super.courseId, "");
            Assert.assertTrue(XMJSONPath.getJSONArrayByReadPath(dataApi.getApi_response(), "$.result").size() == 0, "日程被删除后，对应的排课日程列表还可以查到相关数据");
        }

        if (null != campId || !campId.isEmpty()) {
            this.getTrainingTools().deleteTraining(campId);
        }

    }
}

    