package com.zcloud.basic.info.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.gbscommon.utils.Sm2Util;
import org.slf4j.MDC;

import java.util.Collections;

/**
 * @author zhangyue
 * @date 2026/4/23 11:39
 */
public class SyncPageResponse extends Response {
    private static final long serialVersionUID = 1L;
    private int totalCount = 0;
    private int pageSize = 1;
    private int pageIndex = 1;
    private Object data;

    public SyncPageResponse() {
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return this.pageSize < 1 ? 1 : this.pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            this.pageSize = 1;
        } else {
            this.pageSize = pageSize;
        }

    }

    public int getPageIndex() {
        return this.pageIndex < 1 ? 1 : this.pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        if (pageIndex < 1) {
            this.pageIndex = 1;
        } else {
            this.pageIndex = pageIndex;
        }

    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public int getTotalPages() {
        return this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1;
    }

    public boolean isEmpty() {
        return ObjectUtil.isEmpty(this.data);
    }

    public boolean isNotEmpty() {
        return !this.isEmpty();
    }

    public static SyncPageResponse buildSuccess() {
        SyncPageResponse response = new SyncPageResponse();
        response.setSuccess(true);
        response.setTraceId(MDC.get("tlogTraceId"));
        return response;
    }

    public static SyncPageResponse buildFailure(String errCode, String errMessage) {
        SyncPageResponse response = new SyncPageResponse();
        response.setSuccess(false);
        response.setErrCode(errCode);
        response.setErrMessage(errMessage);
        response.setTraceId(MDC.get("tlogTraceId"));
        return response;
    }

    public static <T> SyncPageResponse of(int pageSize, int pageIndex) {
        SyncPageResponse response = new SyncPageResponse();
        response.setSuccess(true);
        response.setData(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(pageSize);
        response.setPageIndex(pageIndex);
        response.setTraceId(MDC.get("tlogTraceId"));
        return response;
    }

    public static <T> SyncPageResponse of(Object data, int totalCount, int pageSize, int pageIndex) {
        SyncPageResponse response = new SyncPageResponse();
        response.setSuccess(true);
        response.setData(data);
        response.setTotalCount(totalCount);
        response.setPageSize(pageSize);
        response.setPageIndex(pageIndex);
        response.setTraceId(MDC.get("tlogTraceId"));
        return response;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("corpinfoId", 2590800828822460065L);
        jsonObject.put("timestamp", 1777339041444L);
        JSONObject  jsonbody = new JSONObject();
        jsonbody.put("corpinfoId", 2590800828822460065L);
        jsonbody.put("pageIndex", 1);
        jsonbody.put("pageSize", 10);
        jsonObject.put("body", jsonbody);
        String str = jsonObject.toJSONString();
        System.out.println(str);
    }
}
