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

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.basetest.BaseTestImpl;
import com.xiaomai.cases.polar.course.CourseTools;
import com.xiaomai.cases.polar.setting.courseCategory.CourseCategoryTools;
import com.xiaomai.cases.polar.setting.venue.VenueAreaTools;
import com.xiaomai.cases.polar.studioConfig.HomeTools;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.LoginAccount;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.CommUtil;
import com.xiaomai.utils.XMJSONPath;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

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

/**
 * 创建排课
 *
 * @author adu
 * data 2024/6/12 15:54
 */

public class TestCreateGroupRuleSchedule extends BaseTestImpl {
    @Resource(name = "courseCategoryTools")
    CourseCategoryTools courseCategoryTools;
    @Resource(name = "courseTools")
    CourseTools courseTools;
    @Resource(name = "groupScheduleTools")
    GroupScheduleTools groupScheduleTools;
    @Resource(name = "venueAreaTools")
    VenueAreaTools venueAreaTools;

    //获取本周第一天的时间戳
    String beginTimeTamp = CommUtil.oneKeyGetWeekStartDay();
    //获取当前时间加160天后的时间戳 23:59:59 用于结束日期
    String endDateTimeTamp = CommUtil.getNDayEndTimeTamp(180);
    //获取当天时间时间戳:当天时间戳格式：2024-6-12 00：00：00点，用于开始日期
    String todayStartTimeTamp = CommUtil.oneKeyGetAddDay(0);

    //获取今天结束日期的时间戳
    String todayEndDateTimeTamp = CommUtil.getNDayEndTimeTamp(0);
    String courseName = "阿杜自动化团课课程";

    @BeforeClass
    @Override
    public void beforeTest() {
        setTestInfo(ApiModule.Polar_Schedule, "API_createGroupRuleSchedule", LoginAccount.ADU_PROD, Terminal.B, "adu");
        super.beforeTest();
    }

    @Test(description = "测试新建团课排课")
    public void testCreateGroupRuleSchedule() throws InterruptedException {
        HomeTools homeTools = new HomeTools();

        //获取存量课程ID
        String courseId = groupScheduleTools.getStockGroupCourseId(courseName);
        if (courseId == "") {
            //获取存量课程分类ID
            String categoryId = courseCategoryTools.getCourseCategoryPage("普拉提");
            courseId = courseTools.createGroupCourse(courseName, categoryId, "STUDIO");
        }
        //获取存量场地
        List<String> venueAreaIds = groupScheduleTools.getStockArea("流星花园秘密基地");
        //获取存量教练
        List<String> chiefCoachId = new ArrayList<>();
        chiefCoachId.add("杜君丽");
        String chiefCoachIdS = groupScheduleTools.getStudioAdminId(chiefCoachId).get(0).toString();
        //获取存量助教
        List<String> assistCoachIds = new ArrayList<>();
        assistCoachIds.add("木头");
        assistCoachIds.add("吴彦祖");
        List<String> assistCoachIdS = groupScheduleTools.getStudioAdminId(assistCoachIds);

        //1-创建排课日程
        groupScheduleTools.createGroupRuleSchedule(courseId, chiefCoachIdS, todayStartTimeTamp, endDateTimeTamp, venueAreaIds, assistCoachIdS, true, 1, 480);
        //2-查询排课日程
        JSONArray result = groupScheduleTools.getGroupRuleScheduleList(courseId, chiefCoachIdS);
        String ruldId = XMJSONPath.readPath(result.getJSONObject(0), "$.ruleId");
        // 日程列表中新增一条刚创建的规则，匹配到对应的ruldId
        Long goalRuld = result.toJavaList(JSONObject.class).stream().filter(e -> e.getString("ruleId").equals(ruldId)).count();
        Assert.assertTrue(goalRuld == 1);

        //查看今天的课表，获取刚创建的日程中对应的课次ID
        groupScheduleTools.getGroupScheduleTable(Long.valueOf(todayStartTimeTamp),Long.valueOf(todayEndDateTimeTamp),courseId);
        JSONArray array = XMJSONPath.getJSONArrayByReadPath(dataApi.getApi_response(), "$.result.timetableGroups");
        String scheduleId = array.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"))//获取scheduleId
                .findFirst().orElse(null);

       // Assert.assertTrue(scheduleId!=null||!scheduleId.isEmpty(),"刚创建的规则中，没查询到今天对应的课次");//你这个判断，如果scheduleID != null 不成立，就会走到 !scheduleId.isEmpty(),此时shcdueId是null，使用.isEmpry就空指针了
        Assert.assertTrue(!StringUtils.isEmpty(scheduleId),"刚创建的规则中，没查询到今天对应的课次");



        //创建今日待上课的排课后查询今日待上课的数据
        long num= homeTools.queryTodaySchedule().getJSONArray("result").toJavaList(JSONObject.class).stream()
                 .filter(e ->e.getString("scheduleId").equals(scheduleId)).count();
        Assert.assertEquals(num, 1);

        groupScheduleTools.delGroupRuleSchedule(ruldId, true);
        Thread.sleep(1000);
        JSONArray result2 = groupScheduleTools.getGroupRuleScheduleList(courseId, chiefCoachIdS);
        //获取日程中规则ID=ruleId的日程数量
        Long delRuldCount = result2.toJavaList(JSONObject.class).stream().filter(e -> e.getString("ruleId").equals(ruldId)).count();

        //删除成功后，统计查询到的目标日程数量为0
        Assert.assertTrue(delRuldCount == 0);

        //删除今日待上课的排课后查询今日待上课的数据
        long num1= homeTools.queryTodaySchedule().getJSONArray("result").toJavaList(JSONObject.class).stream()
                .filter(e ->e.getString("scheduleId").equals(scheduleId)).count();
        Assert.assertEquals(num1, 0);

    }


}

    