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.CloudOrderDto;
import com.cpop.common.utils.StringUtils;
import com.cpop.core.base.entity.ExtendPage;
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;
import java.util.Map;

/**
 * 云账单工具类
 * @author DB
 * @version 1.0.0
 * @since 2024-01-10 10:32
 */
@Component
public class CloudOrderHandler {

    @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 orderId 云订单id
     * @return Page<CloudCustomerDto>
     */
    public Page<CloudOrderDto> getOrderPage(Integer pageNum, Integer pageSize, String startDate, String endDate, String storeId, String customerId, String cardId, String orderId) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("_type", "daybook");
        //分页参数
        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(orderId)) {
            jsonBody.put("ticketId", orderId);
        }
        //校区id
        jsonBody.put("storeId", storeId);
        JSONObject jsonObject = restTemplate.postForObject(CloudDbUrl.DATA_CLOUD, jsonBody, JSONObject.class);
        ExtendPage<CloudOrderDto> page = ExtendPage.of(pageNum, pageSize);
        if (jsonObject != null) {
            if (jsonObject.getBoolean("success")) {
                List<CloudOrderDto> courseDtoList = jsonObject.getJSONArray("list").toJavaList(CloudOrderDto.class);
                page.setRecords(courseDtoList);
                page.setTotalPage(jsonObject.getInteger("total"));
                page.setTotalRow(jsonObject.getInteger("total"));
                Map<String, Object> extendParams = new HashMap<>();
                extendParams.put("totalRevenue", jsonObject.getString("totalRevenue"));
                extendParams.put("totalExpenditure", jsonObject.getString("totalExpenditure"));
                page.setExtendParams(extendParams);
                return page;
            } else {
                throw new UtilException(jsonObject.getString("error"));
            }
        } else {
            return page;
        }
    }
}
