package com.chaozhanggui.common.system.config;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * 统一API响应结果封装
 * @author DJH
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private int code;
    private String message;
    private T data;
    private String respCode;
    
    private T list;
    
    private String totalCount;

    private String timestamp;

    public Result<T> setCode(ResultCode resultCode) {
        this.code = resultCode.code();
        return this;
    }

    public Result(ResultCode resultCode, String message) {
        this(resultCode, message, null);
    }

    public Result(ResultCode resultCode, String message, T data) {
        this.code = resultCode.code();
        this.message = message;
        this.data = data;
        timestamp = DateUtil.format(new Date(), "yyyyMMddHHmmss");
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        timestamp = DateUtil.format(new Date(), "yyyyMMddHHmmss");
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
        timestamp = DateUtil.format(new Date(), "yyyyMMddHHmmss");
    }

	@Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
