package com.izouma.nineth.domain;

import com.izouma.nineth.enums.SystemCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    //返回码
    private int code;
    //提示信息
    private String msg;
    //当前时候毫秒值
    private String time;
    // 返回对象
    private List<T> data;


    public static Result OK() {
        return new Result(SystemCode.SUCCESS);
    }


    public static Result NO() {
        return new Result(SystemCode.ERROR);
    }


    public void setMsgIsNUll(String msg) {
        if (StringUtils.isBlank(getMsg())) {
            setMsg(msg);
        }
    }


    public void setCodeIsNUll(String code) {
        if (ObjectUtils.isEmpty(getCode())) {
            setMsg(code);
        }
    }


    public void setSysCodeIsNUll(SystemCode sysCode) {
        if (ObjectUtils.isEmpty(getCode())) {
            setCode(sysCode.getCode());
        }
        if (StringUtils.isBlank(getMsg())) {
            setMsg(sysCode.getMsg());
        }
    }

    public Result(SystemCode sysCode) {
        this.code = sysCode.getCode();
        this.msg = sysCode.getMsg();
    }

    public void setSystemCode(SystemCode sysCode) {
        this.code = sysCode.getCode();
        this.msg = sysCode.getMsg();
    }

    public  Result<T> success(List<T> data){
        return new Result<T>(SystemCode.SUCCESS.getCode(), SystemCode.SUCCESS.getMsg(),String.valueOf(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))),data);
    }
}
