package com.sv.netty.netty;


import org.springframework.util.Assert;

import java.util.LinkedHashMap;

public class ResponseDTO extends LinkedHashMap<String, Object> {
    public static final String ERR_CODE = "err_code";
    public static final String ERR_MSG = "err_msg";
    public static final String TIMESTAMP = "timestamp";
    private static final long serialVersionUID = 8410965932046471023L;

    public ResponseDTO() {
        this(0, "OK");
    }

    public ResponseDTO(Integer errCode, String errMsg) {
        this(errCode, errMsg, System.currentTimeMillis() / 1000L);
    }

    public ResponseDTO(Integer errCode, String errMsg, Long timestamp) {
        this.addAttribute("err_code", errCode);
        this.addAttribute("err_msg", errMsg);
        this.addAttribute("timestamp", timestamp);
    }



    public ResponseDTO(Integer errCode, String errMsg, String attrName, Object attrValue) {
        this(errCode, errMsg);
        this.addAttribute(attrName, attrValue);
    }

    public static ResponseDTO ok() {
        return new ResponseDTO();
    }

    public static ResponseDTO ok(String msg) {
        ResponseDTO ret = ok();
        ret.setErrorMsg(msg);
        return ret;
    }

    public ResponseDTO addAttribute(String attrName, Object attrValue) {
        Assert.notNull(attrName, "属性名称不能为空");
        this.put(attrName, attrValue);
        return this;
    }



    public void setErrorCode(Integer errCode) {
        this.addAttribute("err_code", errCode);
    }

    public void setErrorMsg(String errMsg) {
        this.addAttribute("err_msg", errMsg);
    }
}
