package com.xiaomai.cases.polar.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.client.DataUserInfo;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.RequestType;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.XMBaseTest;
import com.xiaomai.utils.XMJSONPath;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @BelongsProject: xm-sportstest
 * @BelongsPackage: com.xiaomai.cases.polar.course
 * @Author: xuyamei
 * @CreateTime: 2024-04-29  20:21
 * @Description: 课程工具类
 * @Version: 1.0
 */
@Component("courseTools")
public class CourseTools extends XMBaseTest {

    /**
     * @description:创建私教课程
     * @author: xuyamei
     * @date:  2024/5/6 10:53
     * @param courseName:课程名称
     * @param categoryId：课程分类id
     * @param sourceType：课程来源类型 1：品牌BRAND 2：场馆STUDIO
     * @param sizeType：课程大小类型 1：1对1 ONE_TO_ONE 2：1v2 ONE_TO_MANY
     * @param capacity：课次容量
     * @param minCapacity：开课人数
     * @return: 课程id
     **/
    public String createPersonalCourse(String courseName,String categoryId,String sourceType,String sizeType,int capacity,int minCapacity,DataUserInfo...dataUserInfo){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_personalCreate")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);

        JSONObject body= new JSONObject();
        body.put("courseName", courseName);
        body.put("duration", 60);      // 时长
        body.put("difficulty", 3);     // 难度
        body.put("color", "#AACF53");
        body.put("coverId", "0");
        body.put("timeUnit", "MINUTE");
        body.put("categoryId", categoryId);
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("sourceId", dataApi.getLoginInfo().getStudioId());  // 来源：场馆、品牌
        body.put("sourceType", sourceType);
        body.put("brandId", dataApi.getLoginInfo().getBrandId());  // 品牌
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("sizeType", sizeType);  // 私教模式
        body.put("allArea", false); // 场地
        body.put("singleReserve", true);
        body.put("reservePrice", 0);
        JSONArray coachIds = new JSONArray(); // 教练
        coachIds.add(dataApi.getLoginInfo().getAdminId());
        body.put("coachIds", coachIds);
        if (sizeType.equals("ONE_TO_MANY")){  // 一对多需要传课次容量及开课人数
            body.put("capacity", capacity); // 课容量
            body.put("minCapacity", minCapacity); // 开课人数
        }


        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getString("result");
    }
    /**
     * @description:创建团课
     * @author: xuyamei
     * @date:  2024/5/7 10:18
     * @param courseName ：课程名称
     * @param categoryId ：课程类型
     * @param sourceType：课程来源类型 1：品牌BRAND 2：场馆STUDIO
     * @return: java.lang.String
     **/
    public String createGroupCourse(String courseName,String categoryId,String sourceType,DataUserInfo...dataUserInfo){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_createGroupCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);
        String introduction = "{\"items\":[{\"content\":\"花花世界的\",\"aspect\":\"\",\"type\":\"TEXT\"}]}";
        JSONObject body= new JSONObject();
        body.put("courseName", courseName);
        body.put("duration", 60);      // 时长
        body.put("difficulty", 2);     // 难度
        body.put("color", "#AACF53");
        body.put("coverId", "0");
        body.put("timeUnit", "MINUTE");
        body.put("categoryId", categoryId);
        body.put("introduction", introduction);
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("sourceId", dataApi.getLoginInfo().getStudioId());  // 来源：场馆、品牌
        body.put("sourceType", sourceType);
        body.put("brandId", dataApi.getLoginInfo().getBrandId());  // 品牌
        body.put("studioId", dataApi.getLoginInfo().getStudioId()); // 场馆
        body.put("calorie", 0);  // 卡路里 传0对于后端是未设置
        body.put("singleReserve", true);
        body.put("reservePrice", 0);

        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getString("result");
    }

    /**
     * @description:获取存量的课程id，没有则新建
     * @author: xuyamei
     * @date:  2024/5/7 10:11
     * @param courseType ：课程类型 PERSONAL私教;GROUP团课
     * @param courseName：课程名称
     * @param categoryId: 课程分类id
     * @param sourceType:课程来源类型 1：品牌BRAND 2：场馆STUDIO
     * @param sizeType:课程大小类型 1：1对1 ONE_TO_ONE 2：1v2 ONE_TO_MANY （团课无需传）
     * @param capacity: 课次容量（团课无需传）
     * @param minCapacity:开课人数（团课无需传）
     * @return: 返回课程id和姓名数组
     **/
    public JSONArray getPersonalOrGroupCourseId(String courseType, String courseName, String categoryId, String sourceType, String sizeType, int capacity, int minCapacity, DataUserInfo...dataUserInfo) {
        JSONObject result = new JSONObject();
        JSONArray getNameAndId = new JSONArray();

        // 查询存量的课程
        if (courseType.equals("GROUP")) { // 查询的是团课
            result = getGroupClassPage("", 0, courseName, "",dataUserInfo);
            String total = result.getString("total");
            if (Integer.parseInt(total) > 0) {
                result = getGroupClassPage("", 0, courseName, "", dataUserInfo).getJSONArray("records").getJSONObject(0);
                String courseId = result.getString("brandCourseId");
                getNameAndId.add(courseId);
                getNameAndId.add(result.getString("courseName"));
            } else {
                getNameAndId.add(createGroupCourse(courseName, categoryId, sourceType,dataUserInfo));
                getNameAndId.add(courseName);
            }
        } else if (courseType.equals("PERSONAL")) { // 查询的是私教课
            result = getPersonalPage("", 0, courseName, "", "", "",dataUserInfo);
            String total = result.getString("total");
            if (Integer.parseInt(total) > 0) {
                result = getPersonalPage("", 0, courseName, "", sizeType, "").getJSONArray("records").getJSONObject(0);
                String courseId = result.getString("brandCourseId");
                getNameAndId.add(courseId);
                getNameAndId.add(result.getString("courseName"));
            } else {
                getNameAndId.add(createPersonalCourse(courseName, categoryId, sourceType, sizeType, capacity, minCapacity,dataUserInfo));
                getNameAndId.add(courseName);
            }
        }
        return getNameAndId;
    }


    /**
     * @description: 编辑课程状态
     * @author: xuyamei
     * @date:  2024/5/6 15:24
     * @param courseId:课程id
     * @param courseState：课程状态  启用ON, 停用OFF, 删除DELETE
     * @param checkOnly：是否只校验：true只做校验操作，false做修改操作
     * @return: 返回code，如果success 则允许删除
     **/
    public String modifyCourseStatus(String courseId,String courseState,boolean checkOnly){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_modifyCourseStatus")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();

        JSONObject body= new JSONObject();
        body.put("brandCourseId", courseId);
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId()); // 场馆
        body.put("courseState",courseState);
        body.put("checkOnly",checkOnly); // 是否只校验
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getJSONObject("result").getString("resultCode");

    }

    /**
     * @description:获取课次详情
     * @author: xuyamei
     * @date:  2024/5/6 16:19
     * @param courseId：课程id
     * @return: com.alibaba.fastjson.JSONObject
     **/
    public JSONObject getCourseDetail(String courseId){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_getCourseDetail")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();

        JSONObject body= new JSONObject();
        body.put("brandCourseId", courseId);
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getJSONObject("result");
    }

    /**
     * @description:团课列表
     * @author: xuyamei
     * @date:  2024/5/6 16:41
     * @param courseState ：课程状态 ON启用;OFF停用
     * @param current:页码
     * @param courseNameLike：课程名称
     * @param categoryId：课程分类id
     * @return: com.alibaba.fastjson.JSONObject
     **/
    public JSONObject getGroupClassPage(String courseState,int current,String courseNameLike,String categoryId,DataUserInfo...dataUserInfo){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_getGroupClassPage")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);

        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("current", current);
        body.put("size", 20);
        if (!courseState.equals("")){
            body.put("courseState",courseState); // 状态
        }
        body.put("courseNameLike",courseNameLike);
        if (!categoryId.equals("")){
            JSONArray categoryIds = new JSONArray();
            categoryIds.add(categoryId);
            body.put("categoryIds",categoryIds);
        }
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getJSONObject("result");
    }

    /**
     * @description:获取私教课列表
     * @author: xuyamei
     * @date:  2024/5/6 16:59
     * @param courseState：课程状态
     * @param current：页码 第一页从0开始
     * @param courseNameLike：课程名称
     * @param categoryId：课程分类id
     * @param sizeType：私教模式 {"1对1":ONE_TO_ONE;"1对多":ONE_TO_MANY;}
     * @param coachId：教练id
     * @return: com.alibaba.fastjson.JSONObject
     **/
    public JSONObject getPersonalPage(String courseState,int current,String courseNameLike,String categoryId,String sizeType,String coachId,DataUserInfo...dataUserInfo){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_getPersonalPage")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);

        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("current", current);
        body.put("size", 20);
        if (!courseState.equals("")){
            body.put("courseState",courseState); // 状态
        }
        body.put("courseNameLike",courseNameLike);
        if (!categoryId.equals("")){
            JSONArray categoryIds = new JSONArray();
            categoryIds.add(categoryId);
            body.put("categoryIds",categoryIds);
        }
        if (!sizeType.equals("")){
            body.put("sizeType",sizeType);
        }
        if(!coachId.equals("")){
            body.put("coachId",coachId);
        }
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getJSONObject("result");
    }


    /**
     * @description:校验场馆课程名称是否重复
     * @author: xuyamei
     * @date:  2024/5/6 18:17
     * @param courseName :课程名称
     * @return: boolean：返回false表示没有重复
     **/
    public boolean checkCourseNameDup(String courseName){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_checkCourseNameDup")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();

        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("courseName",courseName);
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON().getBoolean("result");
    }

    /**
     * 创建训练营课程
     * @param courseName 课程名称
     * @param categoryId 课程分类
     * @param duration 课程时长
     * @param color 课程对应的课表颜色
     * @return 返回课程ID
     */
    public String createTrainingCampCourse(String courseName,String categoryId,String duration,String color){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_createTrainingCampCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();
        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("courseName",courseName);
        body.put("duration",duration);
        body.put("color",color);
        body.put("categoryId",categoryId);
        body.put("timeUnit","MINUTE");
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return XMJSONPath.readPath(dataApi.getApi_response(),"$.result");

    }

    /**
     * 编辑训练营课程
     * @param brandCourseId 课程ID
     * @param courseName
     * @param categoryId 课程分类ID
     * @param duration 课程时长
     * @param color 课表颜色
     * @return 返回课程ID
     */
    public String editTrainingCampCourse(String brandCourseId,String courseName,String categoryId,String duration,String color){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_editTrainingCampCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();
        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("courseName",courseName);
        body.put("duration",duration);
        body.put("color",color);
        body.put("categoryId",categoryId);
        body.put("timeUnit","MINUTE");
        body.put("brandCourseId",brandCourseId);
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);
        return XMJSONPath.readPath(dataApi.getApi_response(),"$.result");
    }

    /**
     * 训练营课程列表查询
     * @param categoryIds 不根据课程分类查询时传null即 new ArrayList<>();
     * @param courseState 停用OFF，启用ON
     * @param courseNameLike 根据课程名称 此场景是创建训练营活动时选择课程然后根据课程名称搜索
     */
    public void getTrainingCampCourseList(List<String> categoryIds,String courseState,String courseNameLike){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_getTrainingCampCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();

        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("categoryIds",categoryIds);
        body.put("size",100);//每页100条数据
        body.put("current",0);
        body.put("courseState",courseState);
        body.put("courseNameLike",courseNameLike);
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);


    }

    /**
     * 根据课程类型和课程名称 全局搜索课程
     * @param courseNameLike 课程名称
     * @param courseType  课程类型 团课：GROUP_CLASS；私教：PERSONAL；训练营课：TRAINING_CAMP
     */
    public void globalSearchCourse(String courseNameLike,String courseType){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_globalSearchCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest();

        JSONObject body= new JSONObject();
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandId", dataApi.getLoginInfo().getBrandId());
        body.put("courseNameLike",courseNameLike);
        body.put("size",20);//每页20条数据
        body.put("current",0);
        body.put("courseType",courseType);
        dataApi.doRequest(RequestType.JSON,dataparams,body.toString(),dataheadrs).assetsSuccess(true);

    }

    /**
     * @description: 编辑私教课程
     * @author: xuyamei
     * @date:  2024/7/26 11:57
     * @param categoryId 课程类型
     * @param courseId 课程id
     * @param courseName 课程名称
     * @param duration 时长
     * @param coachId 教练
     * @param capacity 容量
     * @param dataUserInfo
     * @return: void
     **/

    public void editPersonalCourse(String categoryId,String courseId,String courseName,int duration,String coachId,String capacity,DataUserInfo...dataUserInfo){

        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_editPersonalCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);

        JSONObject body = new JSONObject();
        body.put("categoryId", categoryId);
        body.put("courseName", courseName);
        body.put("duration", duration);      // 时长
        body.put("difficulty", 3);     // 难度
        body.put("color", "#AACF53");
        body.put("coverId", "0");
        body.put("timeUnit", "MINUTE");
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("sourceId", dataApi.getLoginInfo().getStudioId());  // 来源：场馆、品牌
        body.put("sourceType", "STUDIO");  // 来源类型：场馆、品牌
        body.put("brandId", dataApi.getLoginInfo().getBrandId());  // 品牌
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("sizeType", "ONE_TO_MANY");  // 私教模式
        body.put("allArea", false); // 场地
        JSONArray coachIds = new JSONArray();
        coachIds.add(coachId);
        body.put("coachIds", coachIds);
        body.put("capacity", capacity); // 课容量
        body.put("minCapacity", 1); // 开课人数
        body.put("brandCourseId", courseId);
        dataApi.doRequest(RequestType.JSON, dataparams, body.toString(), dataheadrs).assetsSuccess(true);
    }

    /**
     * 编辑私教课的单次约课价格
     * @param categoryId 课程分类
     * @param courseId 课程id
     * @param courseName 课程名称
     * @param coachId 私教id
     * @param price 设置单次约课价格
     * @param dataUserInfo
     */
    public void editPersonalCourse(String categoryId,String courseId,String courseName,String coachId,String price,DataUserInfo...dataUserInfo){
        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_editPersonalCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);

        JSONObject body = new JSONObject();
        body.put("courseName", courseName);
        body.put("duration", 10);      // 时长
        body.put("difficulty", 3);     // 难度
        body.put("color", "#AACF53");
        body.put("coverId", "0");
        body.put("timeUnit", "MINUTE");
        body.put("categoryId", categoryId);
        body.put("sourceId", dataApi.getLoginInfo().getStudioId());  // 来源：场馆、品牌
        body.put("sourceType", "STUDIO");  // 来源类型：场馆、品牌
        body.put("sizeType", "ONE_TO_MANY");  // 私教模式
        body.put("allArea", false); // 场地
        JSONArray coachIds = new JSONArray();
        coachIds.add(coachId);
        body.put("coachIds", coachIds);
        body.put("capacity", 20); // 课容量
        body.put("singleReserve", true); // 允许单次约课
        body.put("minCapacity", 1); // 开课人数
        body.put("reservePrice", price); // 单次约课的价格
        body.put("brandCourseId", courseId);
        body.put("brandId", dataApi.getLoginInfo().getBrandId());  // 品牌
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        dataApi.doRequest(RequestType.JSON, dataparams, body.toString(), dataheadrs).assetsSuccess(true);
    }

    /**
     * @description:编辑团课
     * @author: xuyamei
     * @date:  2024/7/30 10:57
     * @param categoryId 课程分类
     * @param courseId 课程id
     * @param courseName 课程名称
     * @param duration 时长
     * @param singleReserve 是否支持单次预约
     * @param reservePrice 单次预约价格
     * @param dataUserInfo
     * @return: void
     **/
    public void editGroupCourse(String categoryId,String courseId,String courseName,int duration,boolean singleReserve,int reservePrice,DataUserInfo...dataUserInfo){

        dataApi.setApiModule(ApiModule.Polar_Course)
                .setApiName("API_editGroupCourse")
                .setTerminal(Terminal.B);
        super.beforeDataRequest(dataUserInfo);

        JSONObject body = new JSONObject();
        body.put("categoryId", categoryId);
        body.put("courseName", courseName);
        body.put("duration", duration);      // 时长
        body.put("difficulty", 3);     // 难度
        body.put("color", "#AACF53");
        body.put("calorie", "0"); // 卡路里
        body.put("coverId", "0");
        body.put("timeUnit", "MINUTE");
        body.put("operatorId", dataApi.getLoginInfo().getAdminId());
        body.put("sourceId", dataApi.getLoginInfo().getStudioId());  // 来源：场馆、品牌
        body.put("sourceType", "STUDIO");  // 来源类型：场馆、品牌
        body.put("brandId", dataApi.getLoginInfo().getBrandId());  // 品牌
        body.put("studioId", dataApi.getLoginInfo().getStudioId());
        body.put("brandCourseId", courseId);
        body.put("singleReserve", singleReserve); // 是否支持单次约课
        if (singleReserve){
            body.put("reservePrice", reservePrice); // 单次预约的价格

        }
        dataApi.doRequest(RequestType.JSON, dataparams, body.toString(), dataheadrs).assetsSuccess(true);
    }

}
