package com.cpop.api.cloudDb.handler;

import com.alibaba.fastjson.JSONObject;
import com.cpop.api.cloudDb.core.constant.CloudDbUrl;
import com.cpop.api.cloudDb.core.dto.CloudCourseDto;
import com.cpop.api.cloudDb.core.dto.CloudCourseStudentDto;
import com.cpop.api.cloudDb.core.dto.CloudCourseStudentListDto;
import com.cpop.api.cloudDb.core.dto.CloudCustomerDto;
import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.exception.UtilException;
import com.mybatisflex.core.paginate.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 云课程工具类
 * @author DB
 * @version 1.0.0
 * @since 2024-01-09 15:29
 */
@Component
@Slf4j
public class CloudCourseHandler {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取课程分页
     * @author DB
     * @since 2024/1/9
     * @param pageNum 分页参数
     * @param pageSize 分页参数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param storeId 云校区id
     * @param courseId 云课程id
     * @param classId 云课程id
     * @return Page<CloudCustomerDto>
     */
    public Page<CloudCourseDto> getCoursePage(Integer pageNum, Integer pageSize, String startDate, String endDate, String storeId, String courseId,String classId) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("_type", "courseContent");
        //分页参数
        jsonBody.put("page", pageNum);
        jsonBody.put("limit", pageSize);
        //开始日期结束日期
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            jsonBody.put("startTime", startDate);
            jsonBody.put("endTime", endDate);
        }
        if (StringUtils.isNotBlank(courseId)){
            jsonBody.put("courseId", courseId);
        }
        if (StringUtils.isNotBlank(classId)) {
            jsonBody.put("classId", classId);
        }
        //校区id
        jsonBody.put("storeId", storeId);
        JSONObject jsonObject = restTemplate.postForObject(CloudDbUrl.DATA_CLOUD, jsonBody, JSONObject.class);
        Page<CloudCourseDto> page = Page.of(pageNum, pageSize);
        if (jsonObject != null) {
            if (jsonObject.getBoolean("success")) {
                List<CloudCourseDto> courseDtoList = jsonObject.getJSONArray("list").toJavaList(CloudCourseDto.class);
                page.setRecords(courseDtoList);
                page.setTotalPage(jsonObject.getInteger("total"));
                page.setTotalRow(jsonObject.getInteger("total"));
                return page;
            } else {
                throw new UtilException(jsonObject.getString("error"));
            }
        } else {
            return page;
        }
    }

    /**
     * 上课情况
     * @author DB
     * @since 2024/1/9
     * @param customerId 客户id
     * @param studentId 学员id
     * @param cardId 课卡id
     * @param classId 班级id
     * @param startDate 开始时间
     * @param endDate 接受时间
     * @param storeId 校区id
     * @param courseId 课程id
     * @return List<CloudCourseStudentDto>
     */
    public CloudCourseStudentListDto getCourseStudentList(String customerId, String studentId, String cardId, String classId, String startDate, String endDate, String storeId, String courseId) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("_type", "courseStudent");
        //开始日期结束日期
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            jsonBody.put("startTime", startDate);
            jsonBody.put("endTime", endDate);
        }
        if (StringUtils.isNotBlank(courseId)){
            jsonBody.put("courseId", courseId);
        }
        if (StringUtils.isNotBlank(customerId)){
            jsonBody.put("customerId", customerId);
        }
        if (StringUtils.isNotBlank(studentId)){
            jsonBody.put("studentId", studentId);
        }
        if (StringUtils.isNotBlank(cardId)){
            jsonBody.put("cardId", cardId);
        }
        if (StringUtils.isNotBlank(classId)){
            jsonBody.put("classId", classId);
        }
        //校区id
        jsonBody.put("storeId", storeId);
        JSONObject jsonObject = restTemplate.postForObject(CloudDbUrl.DATA_CLOUD, jsonBody, JSONObject.class);
        if (jsonObject != null) {
            if (jsonObject.getBoolean("success")) {
                return jsonObject.toJavaObject(CloudCourseStudentListDto.class);
            } else {
                throw new UtilException(jsonObject.getString("error"));
            }
        } else {
            return new CloudCourseStudentListDto();
        }
    }
}
