using Common.Request;
using Common.Request.Label;
using Utils;
using Common;
using Newtonsoft.Json.Linq;
/// <summary>
/// 商家寄件（官方快递）
/// </summary>
public static class LabelV2
{

    /// <summary>
    /// 下单
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
     public static string order(BaseReq<OrderParam> param){
        
        param.method = ApiInfoConstant.ORDER;
        var request = ObjectToDictionaryUtils.ObjectToMap(param);
        
        if(request == null){
            return null;
        }
        var result = HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL,request);
        return result;
    }

    /// <summary>
    /// 复打
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
     public static string repeatPrint(BaseReq<RepeatPrintParam> param){
        
        param.method = ApiInfoConstant.CLOUD_PRINT_OLD_METHOD;
        var request = ObjectToDictionaryUtils.ObjectToMap(param);
        
        if(request == null){
            return null;
        }
        var result = HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL,request);
        return result;
    }

    /// <summary>
    /// 自定义打印
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
     public static string customPrint(BaseReq<CustomPrintParam> param){
        
        param.method = ApiInfoConstant.CUSTOM;
        var request = ObjectToDictionaryUtils.ObjectToMap(param);
        
        if(request == null){
            return null;
        }
        var result = HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL,request);
        return result;
    }

    public static string callOrder(BaseReq<OrderParam> param)
    {
        int code = 0;
        string msg = "";
        string data = "";
        string errorStr = "";
        try
        {
            param.method = ApiInfoConstant.ORDER;
            var request = ObjectToDictionaryUtils.ObjectToMap(param);

            if (request == null)
            {
                msg = "body参数错误";
            }
            if (HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL, request, out data, out errorStr))
            {
                code = 1;
                msg = "操作成功";
            }
            else
            {
                msg = errorStr;
            }
        }
        catch (System.Exception ex)
        {
            msg = "操作失败:" + ex.ToString();
        }

        JObject result = new JObject();
        result.Add("code", code);
        result.Add("msg", msg);
        result.Add("data", Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(data));

        return result.ToString();
    }
}