package com.awspaas.user.apps.donenow_crm.controller;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

/**
 * @Description
 * @Author fengf
 * @Date 2023å¹?06æœ?26æ—? 14:58
 * @Version 1.0
 */
public class BaseController {
    private static final int SUCCESS = 0;

    private static final int FAIL = 1;

    private static final String SUCCESS_MSG = "success";

    private static final String FAIL_MSG = "fail";

    protected String success(Object data, Integer code, String msg) {
        Map<String, Object> returnData = new HashMap<>();
        returnData.put("code", Integer.valueOf((code == null) ? SUCCESS : code.intValue()));
        returnData.put("data", (data == null) ? new HashMap<>() : data);
        returnData.put("msg", (msg == null) ? SUCCESS_MSG : msg);
        return JSON.toJSONString(returnData);
    }

    protected String fail(Integer code, String msg) {
        Map<String, Object> returnData = new HashMap<>();
        returnData.put("code", Integer.valueOf((code == null) ? FAIL : code.intValue()));
        returnData.put("data", new HashMap<>());
        returnData.put("msg", (msg == null) ? FAIL_MSG : msg);
        return JSON.toJSONString(returnData);
    }

    protected String success(Object data) {
        return success(data, SUCCESS, SUCCESS_MSG);
    }
    
    protected String fail(String msg) {
        return fail(FAIL, msg);
    }
}
