using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XYY.Authentication.Standard;
using XYY.Common.Standard;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard.Dto.MpsOrder;
using XYY.Model.Standard.MpsOrder;
using XYY.Service.Standard.Order;

namespace XYY.Api.Order.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GetMoreThanOneTicketMpsController : ApiControllerBase
    {
        IMoreThanOneTicketService orderService;

        public GetMoreThanOneTicketMpsController(IMoreThanOneTicketService orderService)
        {
            this.orderService = orderService;
        }
        [HttpGet]
        public async Task<ApiJsonModel<Order_MpsOrder>> Get([FromQuery] int orderId, [FromQuery] int draftOrderId)
        {
            var result = await this.orderService.Get(orderId, draftOrderId);
            return ApiJsonModel<Order_MpsOrder>.OK(result);
        }
        [HttpPost]
        public async Task<IActionResult> ConfirmCustomerOrderReceipt(ConfirmCustomerOrderReceiptInput data)
        {
            var result = await orderService.ConfirmCustomerOrderReceipt(data);
            return Ok(result);
        }
        [HttpPost]
        public async Task<IActionResult> ModifyCreateOrderChannelId(MpsModifyCreateOrderChannelId  mpsModify)
        {
            var u = User.GetUserContent();
             await orderService.ModifyCreateOrderChannelId(mpsModify, u);
            return Ok();
        }
        public async Task<IActionResult> CustomerOrderOut(string InputNumber, int opStatus)
        {
            var result = await orderService.CustomerOrderOut(InputNumber, opStatus);
            return Ok(result);
        }

        public async Task<IActionResult> BoxStatusOp(int boxId, BoxStatus boxStatus)
        {
            await orderService.BoxStatusOp(boxId, boxStatus);
            return Ok();
        }

        public class OutBoxDataInfo
        {
            public string orderIds
            {
                get; set;
            }
        }

        [AllowAnonymous]
        public async Task<IActionResult> OutBoxData(OutBoxDataInfo dataInfo)
        {
            if (string.IsNullOrEmpty(dataInfo.orderIds))
                throw new Exception("请传入需要下单的订单ID");

            var u = User.GetUserContent();
            byte[] data = await orderService.OutBoxInfo(dataInfo.orderIds.SplitToArray<int>(), u.Roles.Contains("FBA末端重量查看"));
            return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }
        [HttpPost]
        public async Task<IActionResult> UpdateMpsOrder(UpdateMpsOrderInfo updateMpsOrderInfo)
        {
            await orderService.UpdateMpsOrder(updateMpsOrderInfo);
            return Ok();
        }
        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> OutMpsOrderData(OutBoxDataInfo dataInfo)
        {
            if (string.IsNullOrEmpty(dataInfo.orderIds))
                throw new Exception("请传入需要下单的订单ID");

            byte[] data = await orderService.OutMpsOrderData(dataInfo.orderIds.SplitToArray<int>());
            return File(data, "application/x-zip-compressed");
        }
        /// <summary>
        /// 导入箱子信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ImportMpsBoxInfo()
        {
            var filelist = (await Request.ReadFormAsync()).Files;
            if (filelist != null && filelist.Count > 0)
            {
                var u = User.GetUserContent();
                string userName = u.NiceName;
                if (!u.Roles.Contains("FBA末端重量修改")) { throw new Exception("抱歉，您无权限进行此操作！"); }
                var file = filelist[0];
                var result = await orderService.ImportMpsBoxInfo(file.OpenReadStream(), userName);
                return Ok(result);
            }
            else
            {
                return Error("请上传一个文件");
            }

        }
        /// <summary>
        /// 导入末端订单信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ImportFBAChannelOrder()
        {
            var filelist = (await Request.ReadFormAsync()).Files;
            if (filelist != null && filelist.Count > 0)
            {
                var u = User.GetUserContent();
                string userName = u.NiceName;
                if (!u.Roles.Contains("FBA末端重量修改")) { throw new Exception("抱歉，您无权限进行此操作！"); }
                var file = filelist[0];
                var result = await orderService.ImportFBAChannelOrder(file.OpenReadStream());
                return Ok(result);
            }
            else
            {
                return Error("请上传一个文件");
            }

        }

        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> Recalculate663Billing()
        {
            await orderService.RecalculateBilling();
            return Ok();
        }
        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> RecalculateCustomerFee()
        {
            await orderService.RecalculateCustomerFee();
            return Ok();
        }
    }
}
