using System.Linq;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using XYY.Api.Order.Model.Order;
using XYY.Core.Standard.Mvc;
using XYY.Service.Standard.Order;
using XYY.TaskTrack.Standard;
using XYY.Model.Standard.MpsOrder;
using XYY.Authentication.Standard;
namespace XYY.Api.Order.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InputMoreThanOneTicketOrdersController : ControllerBase
    {
        IMoreThanOneTicketService orderService;
        private IMQManager _MQManager;
        public InputMoreThanOneTicketOrdersController(IMoreThanOneTicketService orderService, IMQManager mQManager)
        {
            this.orderService = orderService;
            _MQManager = mQManager;
        }

        [HttpPost]
        public async Task<ApiJsonModel> Post([FromBody] PostInputMoreThanOneTicketOrdersRequest postInputMoreThanOneTicketOrdersRequest)
        {
            var u = User.GetUserContent();
            int customerId = User.GetUserContent().CustomerId;
            if (customerId == 0)
                throw new System.Exception("未传入客户编号");
            var list = await this.orderService.LoadData(postInputMoreThanOneTicketOrdersRequest.FileUrl, customerId);
            var errorList = list.Where(x => !string.IsNullOrEmpty(x.Remark));
            if (errorList.Count() > 0)
                throw new System.Exception(string.Join(",", errorList.Select(x => x.Remark)));


            await this.orderService.SaveMpsData(list, postInputMoreThanOneTicketOrdersRequest.FileUrl);
            await this.orderService.AddDraftOrderData(list);
            foreach (var item in list)
            { 
                await _MQManager.Publish(new OrderMpsOrderSubModel { Order = item });
            }
            return ApiJsonModel.OK();
        }
    }
}
