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.CloudClassCardDto;
import com.cpop.api.cloudDb.core.dto.CloudClassCardRecordDto;
import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.entity.ExtendPage;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.base.exception.UtilException;
import com.mybatisflex.core.paginate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;

/**
 * 云课卡工具类
 * @author DB
 * @version 1.0.0
 * @since 2024-01-10 11:08
 */
@Component
public class CloudClassCardHandler {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取课卡分页
     * @author DB
     * @since 2024/1/9
     * @param pageNum 分页参数
     * @param pageSize 分页参数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param storeId 云校区id
     * @param customerId 客户id
     * @param cardId 课卡id
     * @param cardType 课卡类型
     * @param cardStatus 课卡状态
     * @return Page<CloudCustomerDto>
     */
    public Page<CloudClassCardDto> getClassCardPage(Integer pageNum, Integer pageSize, String startDate, String endDate, String storeId, String customerId, String cardId, String cardType, Integer cardStatus) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("_type", "period");
        //分页参数
        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(customerId)) {
            jsonBody.put("customerId", customerId);
        }
        if (StringUtils.isNotBlank(cardId)) {
            jsonBody.put("cardId", cardId);
        }
        if (StringUtils.isNotBlank(cardType)) {
            jsonBody.put("cardType", cardType);
        }
        if (cardStatus != null) {
            jsonBody.put("cardStatus", cardStatus);
        }
        //校区id
        jsonBody.put("storeId", storeId);
        JSONObject jsonObject = restTemplate.postForObject(CloudDbUrl.DATA_CLOUD, jsonBody, JSONObject.class);
        ExtendPage<CloudClassCardDto> page = ExtendPage.of(pageNum, pageSize);
        if (jsonObject != null) {
            if (jsonObject.getBoolean("success")) {
                List<CloudClassCardDto> courseDtoList = jsonObject.getJSONArray("list").toJavaList(CloudClassCardDto.class);
                page.setRecords(courseDtoList);
                page.setTotalPage(jsonObject.getInteger("total"));
                page.setTotalRow(jsonObject.getInteger("total"));
                HashMap<String, Object> extendParams = new HashMap<>();
                //总课时
                extendParams.put("totalClassHours",jsonObject.getString("allNumber"));
                //总金额
                extendParams.put("totalAmount",jsonObject.getString("allAmount"));
                page.setExtendParams(extendParams);
                return page;
            } else {
                throw new UtilException(jsonObject.getString("error"));
            }
        } else {
            return page;
        }
    }

    /**
     * 获取课卡记录分页
     * @author DB
     * @since 2024/1/9
     * @param pageNum 分页参数
     * @param pageSize 分页参数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param storeId 云校区id
     * @param customerId 客户id
     * @param cardId 课卡id
     * @return Page<CloudCustomerDto>
     */
    public Page<CloudClassCardRecordDto> getClassCardRecordPage(Integer pageNum, Integer pageSize, String startDate, String endDate, String storeId, String customerId, String cardId) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("_type", "periodLog");
        //分页参数
        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(customerId)) {
            jsonBody.put("customerId", customerId);
        }
        if (StringUtils.isNotBlank(cardId)) {
            jsonBody.put("cardId", cardId);
        }
        //校区id
        jsonBody.put("storeId", storeId);
        JSONObject jsonObject = restTemplate.postForObject(CloudDbUrl.DATA_CLOUD, jsonBody, JSONObject.class);
        Page<CloudClassCardRecordDto> page = Page.of(pageNum, pageSize);
        if (jsonObject != null) {
            if (jsonObject.getBoolean("success")) {
                List<CloudClassCardRecordDto> courseDtoList = jsonObject.getJSONArray("list").toJavaList(CloudClassCardRecordDto.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/20
     * @param dto 课卡中间表
     * @return String
     */
    public String insertCloudClassCard(CloudClassCardDto dto){
        JSONObject jsonBody = JSONObject.parseObject(JSONObject.toJSONString(dto));
        jsonBody.put("_type", "addPeriod");
        JSONObject result = restTemplate.postForObject(CloudDbUrl.COMMON_USE_URL, jsonBody, JSONObject.class);
        if (result != null) {
            if (result.getBoolean("success")) {
                return result.getString("data");
            } else {
                throw new UtilException(result.getString("error"));
            }
        } else {
            throw new ServiceException("办卡失败!");
        }
    }


    /**
     * 通知退款退卡
     * @author DB
     * @since 2024/1/22
     * @param dto 请求参数
     */
    public void refundCloudClassCard(CloudClassCardDto dto){
        JSONObject jsonBody = JSONObject.parseObject(JSONObject.toJSONString(dto));
        jsonBody.put("_type", "periodRefund");
        JSONObject result = restTemplate.postForObject(CloudDbUrl.COMMON_USE_URL, jsonBody, JSONObject.class);
        if (result != null) {
            if (!result.getBoolean("success")) {
                throw new UtilException(result.getString("error"));
            }
        } else {
            throw new ServiceException("操作失败!");
        }
    }
}
