using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Service.Standard.Finance.Charging.Base;
using XYY.Service.Standard.Finance.Charging.Express;
using XYY.Core.Standard.Mvc;
using StackExchange.Redis;
using XYY.Core.Standard.Data.Redis;
using System.Net.NetworkInformation;

namespace XYY.Service.Standard.Finance.Charging.CustomerOrderFee
{
    public interface ICustomerOrderRecalculationService : IChargesService<ExpressFeechargesModel>
    {
        Task<ApiJsonModel> ChargeCustomerAndBranchCompany(ExpressFeechargesModel t);
    }

    public class CustomerOrderRecalculationService : ICustomerOrderRecalculationService
    {
        ICustomerOrderFeeOffsetService _orderFeeOffsetService;
        ICustomerOrderFeeService _orderFeeService;
        IDatabase db;

        public CustomerOrderRecalculationService(ICustomerOrderFeeOffsetService orderFeeOffsetService, RedisHelper redis, ICustomerOrderFeeService orderFeeService)
        {
            _orderFeeOffsetService = orderFeeOffsetService;
            _orderFeeService = orderFeeService;
            db = redis.GetDatabase();
        }

        public async Task<ApiJsonModel> ChargeCustomerAndBranchCompany(ExpressFeechargesModel t)
        {
            string key = t.BillNo;
            bool flag = db.StringSet(t.BillNo
                , "1", TimeSpan.FromSeconds(3), When.NotExists, StackExchange.Redis.CommandFlags.None);
            if (flag)
            {
                //先克隆一个，以免计费过程中t的值已被修改
                var tForBranchCompany = t.Copy<ExpressFeechargesModel>();

                var customerResult = await Charges(t);
                if (customerResult.success && t.BranchCompanyCustomerId.HasValue&& t.BranchCompanyCustomerId>0)
                {
                    tForBranchCompany.CustomerId = t.BranchCompanyCustomerId.Value;
                    tForBranchCompany.IsChargeBranchCompany = true;
                    var branchCompanyResult = await Charges(tForBranchCompany);
                    if (!branchCompanyResult.success)
                    {
                        branchCompanyResult.error = $"结算计费失败:cid-{t.CustomerId},eid-{t.ExpressId},c-{t.CountryCode},{branchCompanyResult.error ?? customerResult.message}";
                    }
                    //清除客户运费
                    //await _orderFeeOffsetService.Charges(t);
                    if (t.PublicExpressId != 49)
                        return branchCompanyResult;
                }
                if (!customerResult.success)
                { 
                    customerResult.error = $"客户计费失败:cid-{t.CustomerId},eid-{t.ExpressId},c-{t.CountryCode},{customerResult.error??customerResult.message}";
                }
                return customerResult;
            }
            else
            {
                return ApiJsonModel.OK();
            }
        }

        public async Task<ApiJsonModel> Charges(ExpressFeechargesModel t)
        {
            //这里调了两次回退接口，优化下。
            var josn =  await _orderFeeOffsetService.Charges(t);
            if (josn.success)
            {
                return await _orderFeeService.Charges(t);
            }
            else
                return josn;
        }
    }
}
