using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Service.Standard.Finance.Charging.Express;
using XYY.TaskTrack.Standard;
using RestSharp;
using Microsoft.AspNetCore.Mvc;
using XYY.Service.Standard.Finance.Charging.Base;
using XYY.Core.Standard.Mvc;

namespace XYY.Service.Standard.Finance.Charging.CustomerOrderFee
{
    public interface IFinanceChargeConsumer : IBaseConsumer<ExpressFeechargesModel>
    {

    }

    public class FinanceChargeConsumer : IFinanceChargeConsumer
    {
        string host = "http://120.24.149.148:9503";
        string token = "132A7468DE079C6CEB59F383A661E612";

        public FinanceChargeConsumer()
        {
        }

        public async Task Consume(ExpressFeechargesModel message)
        {
            try
            {
                RestClient client = new RestClient(host);
                RestRequest request = new RestRequest("/api/ExpressFee/ChargesForBranchCompany", Method.POST);
                request.AddHeader("Authorization", $"token {token}");
                request.AddJsonBody(message.Copy<ExpressFeechargesModelCopy>());
                var response = await client.ExecuteAsync<ApiJsonModel>(request);
                ApiJsonModel result = response.Data;
                //Console.WriteLine(response.Content);
                if(result==null || !result.success)
                {
                    string errMsg = "";
                    if (result == null)
                        errMsg = "分公司扣费结果为空";
                    else
                        errMsg = result.error ?? result.message;
                    string msg = errMsg ?? "分公司扣费错误";
                    Console.WriteLine(msg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
