using Microsoft.AspNetCore.Mvc; 
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard;
using XYY.Model.Standard.Order;
using XYY.Service.Standard.ChannelApi.Base;
using XYY.Service.Standard.Order;

namespace XYY.Api.Order.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LadingTrunkLineController : ApiControllerBase
    {
        private IEnumerable<ITrunkLineApi> _trunkLineApis;
        private readonly ILadingTrunkLineService _ladingTrunkLineService;
        public LadingTrunkLineController(IEnumerable<ITrunkLineApi> trunkLineApis, ILadingTrunkLineService ladingTrunkLineService)
        {
            _trunkLineApis = trunkLineApis;
            _ladingTrunkLineService = ladingTrunkLineService;
        }
        [HttpPost]
        public async Task<IActionResult> EditTrunkLinePager(Logistics_TrunkLine model)
        {
            await _ladingTrunkLineService.EditTrunkLinePager(model);
            return Ok();
        }
        [HttpPost]
        public async Task<IActionResult> GetTrunkLinePager(QueryModel queryModel)
        {
            var result = await _ladingTrunkLineService.GetTrunkLinePager(queryModel);
            return Ok(result);
        }
        [HttpGet]
        public async Task<IActionResult> GetTrunkLineInfo(int id)
        {
            var result = await _ladingTrunkLineService.GetTrunkLineInfo(id);
            return Ok(result);
        }
        [HttpGet]
        public async Task<IActionResult> DeleteTrunkLineInfo(int id)
        {
            var result = await _ladingTrunkLineService.DeleteTrunkLineInfo(id);
            return Ok(result);
        }
        #region 菜鸟干线推送信息
        [HttpGet]
        public async Task<IActionResult> CNDeliveryPush(int id)
        {
            List<int> channelIds = new List<int>() { 540,539,538,537,536,549,550};
            var data = await _ladingTrunkLineService.GetTrunkLinePushInfo(id, channelIds);
            var baseApi = _trunkLineApis.Where(x => x.ServiceName == data.Channel.ServiceCode).FirstOrDefault();
            try
            {
                var resultData = await baseApi.DeliveryPush(data);
                if (resultData.IsSuccess)
                {
                    data.TrunkLine.DeliveryPushStatus = PushStatusEnum.Success;
                    data.TrunkLine.ErrorMessage = "";
                }
                else
                {
                    data.TrunkLine.DeliveryPushStatus = PushStatusEnum.Fail;
                    data.TrunkLine.ErrorMessage = resultData.ErrorMsg;
                }
            }
            catch (System.Exception ex)
            {
                data.TrunkLine.DeliveryPushStatus = PushStatusEnum.Fail;
                data.TrunkLine.ErrorMessage = ex.Message;
            }
            await _ladingTrunkLineService.EditTrunkLinePager(data.TrunkLine);
            return Ok();
        }
        [HttpGet]
        public async Task<IActionResult> CNArrivePush(int id)
        {
            List<int> channelIds = new List<int>() { 540, 539, 538, 537, 536, 549, 550 };
            var data = await _ladingTrunkLineService.GetTrunkLinePushInfo(id, channelIds);
            var baseApi = _trunkLineApis.Where(x => x.ServiceName == data.Channel.ServiceCode).FirstOrDefault();
            try
            {
                var resultData = await baseApi.ArrivePush(data);
                if (resultData.IsSuccess)
                {
                    data.TrunkLine.ArrivePushStatus = PushStatusEnum.Success;
                    data.TrunkLine.ErrorMessage = "";
                }
                else
                {
                    data.TrunkLine.ArrivePushStatus = PushStatusEnum.Fail;
                    data.TrunkLine.ErrorMessage = resultData.ErrorMsg;
                }
            }
            catch (System.Exception ex)
            {
                data.TrunkLine.ArrivePushStatus = PushStatusEnum.Fail;
                data.TrunkLine.ErrorMessage = ex.Message;
            }
            await _ladingTrunkLineService.EditTrunkLinePager(data.TrunkLine);
            return Ok();
        }
        #endregion

    }
}
