using Microsoft.AspNetCore.Mvc;
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.Finance;
using XYY.Model.Standard.MpsOrder;
using XYY.Model.Standard.MpsOrder.dto;
using XYY.Model.Standard.MpsOrder.views;
using XYY.Service.Standard.Order.FBA;

namespace XYY.Api.Order.Controllers.FBA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FBAOrderController : ApiControllerBase
    {
        IFBAOrderService orderService;

        public FBAOrderController(IFBAOrderService orderService)
        {
            this.orderService = orderService;
        }
        #region 空派收件

        public async Task<IActionResult> GetReceivingInfo(QueryModel queryModel)
        {
            if (queryModel?.QueryParamer != null && queryModel.QueryParamer.Any(x => x.Filed == "IsGoodsNames"))
            {
                var tempParamer = queryModel.QueryParamer.Where(x => x.Filed == "IsGoodsNames").FirstOrDefault();
                if (tempParamer.Value == "1") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "GoodsNames", Method = "IsNotNl", Value = "" }); }
                else if (tempParamer.Value == "2") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "GoodsNames", Method = "IsNl", Value = "" }); }
                queryModel.QueryParamer.Remove(tempParamer);
            }
            var data = await this.orderService.GetReceivingInfo(queryModel);
            return Ok(data);
        }
        public async Task<IActionResult> GetReceivingBoxs(int Id)
        {
            var data = await this.orderService.GetReceivingBoxs(new List<int>() { Id });
            return Ok(data);
        }
        public async Task<IActionResult> GetReceivingBoxsByIds(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GetReceivingBoxs(Ids);
            return Ok(data);
        }

        public async Task<IActionResult> UpdateReceivingBoxs(List<Order_MpsOrderBox> boxs)
        {
            var data = await this.orderService.UpdateReceivingBoxs(boxs);
            return Ok(data);
        }
        public async Task<IActionResult> GoToPrint(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GoToPrint(Ids);
            return Ok(data);
        }
        #endregion
        //public async Task<IActionResult> GetOrderReceivingInfo(int orderId)
        //{
        //    var data = await this.orderService.GetOrderReceivingInfo(orderId);
        //    return Ok(data);
        //}

        //public async Task<IActionResult> SaveOrderRceiveingInfo(View_Order_ReceivingInfo View_Order_ReceivingInfo)
        //{
        //    await this.orderService.SaveOrderRceiveingInfo(View_Order_ReceivingInfo);
        //    return Ok();
        //}

        #region 空派打单
        public async Task<IActionResult> GetPrintList(QueryModel queryModel)
        {
            if (queryModel?.QueryParamer != null && queryModel.QueryParamer.Any(x => x.Filed == "IsXYY"))
            {
                var tempParamer = queryModel.QueryParamer.Where(x => x.Filed == "IsXYY").FirstOrDefault();
                if (tempParamer.Value == "1") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "LEFT(TrackingNumber,3)", Method = "Eq", Value = "XYY" }); }
                else if (tempParamer.Value == "2") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "LEFT(TrackingNumber,3)", Method = "NotEq", Value = "XYY" }); }
                queryModel.QueryParamer.Remove(tempParamer);
            }

            var data = await this.orderService.GetPrintList(queryModel);
            return Ok(data);
        }
        public async Task<IActionResult> GetPrint(int orderId)
        {
            var data = await this.orderService.GetPrint(orderId);
            return Ok(data);
        }
        public async Task<IActionResult> GetReceivingBoxsGoods(int Id)
        {
            var data = await this.orderService.GetReceivingGoods(Id);
            return Ok(data);

        }
        public async Task<IActionResult> GetReceivingAddress(int Id)
        {
            var data = await this.orderService.GetReceivingAddress(Id);
            return Ok(data);
        }
        public async Task<IActionResult> UpdatePrintBoxs(MpsPrint boxs)
        {
            var data = await this.orderService.UpdatePrintBoxs(boxs);
            return Ok(data);
        }
        public async Task<IActionResult> GoToIncome(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GoToIncome(Ids);
            return Ok(data);
        }
        #endregion


        //public async Task<IActionResult> SavePrintInfos(View_Order_Print View_Order_ReceivingInfo)
        //{
        //    await this.orderService.SavePrintInfos(View_Order_ReceivingInfo);
        //    return Ok();
        //}


        #region 空派收入

        public async Task<IActionResult> GetCustomerConfirmList(QueryModel queryModel)
        {
            if (queryModel?.QueryParamer != null && queryModel.QueryParamer.Any(x => x.Filed == "IsConfim"))
            {
                var tempParamer = queryModel.QueryParamer.Where(x => x.Filed == "IsConfim").FirstOrDefault();
                if (tempParamer.Value == "1") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "isnull(CustomerConfirmBillWeight,0)", Method = "Gr", Value = "0" }); }
                else if (tempParamer.Value == "2") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "isnull(CustomerConfirmBillWeight,0)", Method = "LeE", Value = "0" }); }
                queryModel.QueryParamer.Remove(tempParamer);
            }
            var data = await this.orderService.GetCustomerConfirmList(queryModel);
            return Ok(data);
        }
        public async Task<IActionResult> GetCustomerConfirm(int orderId)
        {
            var data = await this.orderService.GetCustomerConfirm(orderId);
            return Ok(data);
        }
        public async Task<IActionResult> UpdateCustomerConfirm(MpsCustomerConfirm dto)
        {
            var data = await this.orderService.UpdateCustomerConfirm(dto);
            return Ok(data);
        }
        public async Task<IActionResult> GetBatchConfirmWeight(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GetBatchConfirmWeight(Ids);
            return Ok(data);
        }
        public async Task<IActionResult> UpdateBatchConfirmWeight(List<ConfirmWeight> confirmWeights)
        {
            var data = await this.orderService.UpdateBatchConfirmWeight(confirmWeights);
            return Ok();
        }
        public async Task<IActionResult> GetBatchConfirmIncidenta(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GetBatchIncidenta(Ids, XYY.Model.Standard.Finance.FBAIncidentalCalculateTarget.客户);
            data = data.OrderBy(x => x.OrderId).ToList();
            return Ok(data);
        }
        public async Task<IActionResult> UpdateBatchConfirmIncidenta(List<FBA_Incidental> incidentals)
        {
            var data = await this.orderService.UpdateBatchConfirmIncidenta(incidentals);
            return Ok();
        }
        public async Task<IActionResult> GoToSettlement(IEnumerable<int> Ids)
        {
            var u = User.GetUserContent();
            var data = await this.orderService.GoToSettlement(Ids, u.NiceName);
            return Ok(data);
        }
        #endregion

        #region 单价获取
        public async Task<IActionResult> GetUnitPrice(int OrderId)
        {
            var data = await this.orderService.GetUnitPrice(OrderId);
            return Ok(data);
        }
        #endregion

        #region 结算
        public async Task<IActionResult> GetSettlementConfirmList(QueryModel queryModel)
        {
            if (queryModel?.QueryParamer != null && queryModel.QueryParamer.Any(x => x.Filed == "IsConfim"))
            {
                var tempParamer = queryModel.QueryParamer.Where(x => x.Filed == "IsConfim").FirstOrDefault();
                if (tempParamer.Value == "1") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "isnull(IsSettlementConfirm,0)", Method = "Gr", Value = "0" }); }
                else if (tempParamer.Value == "2") { queryModel.QueryParamer.Add(new QueryParamer() { Filed = "isnull(IsSettlementConfirm,0)", Method = "LeE", Value = "0" }); }
                queryModel.QueryParamer.Remove(tempParamer);
            }
            var data = await this.orderService.GetSettlementConfirmList(queryModel);
            return Ok(data);
        }
        public async Task<IActionResult> GetSettlementConfirm(int orderId)
        {
            var data = await this.orderService.GetSettlementConfirm(orderId);
            return Ok(data);
        }
        public async Task<IActionResult> GetBatchSettlementWeight(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GetBatchSettlementWeight(Ids);
            return Ok(data);
        }
        public async Task<IActionResult> UpdateBatchSettlementWeight(List<ConfirmWeight> confirmWeights)
        {
            var data = await this.orderService.UpdateBatchSettlementWeight(confirmWeights);
            return Ok();
        }
        public async Task<IActionResult> UpdateSettlementConfirm(MpsSettlementConfirm dto)
        {
            var data = await this.orderService.UpdateSettlementConfirm(dto);
            return Ok(data);
        }
        public async Task<IActionResult> GetBatchSettlementIncidenta(IEnumerable<int> Ids)
        {
            var data = await this.orderService.GetBatchIncidenta(Ids, XYY.Model.Standard.Finance.FBAIncidentalCalculateTarget.结算价);
            data = data.OrderBy(x => x.OrderId).ToList();
            return Ok(data);
        }
        public async Task<IActionResult> UpdateBatchSettlementIncidenta(List<FBA_Incidental> incidentals)
        {
            var data = await this.orderService.UpdateBatchSettlementIncidenta(incidentals);
            return Ok();
        }
        public async Task<IActionResult> UpdateSettlementConfirmForTable(MpsSettlementConfirmTableData dto)
        {
            var data = await this.orderService.UpdateSettlementConfirmForTable(dto);
            return Ok(data);
        }
        public async Task<IActionResult> SettlementConfirmOperation(int orderId)
        {
            var u = User.GetUserContent();
            var data = await this.orderService.SettlementConfirmOperation(orderId, u.NiceName);
            return Ok(data);
        }

        public async Task<IActionResult> GoToEnd(IEnumerable<int> Ids)
        {
            var u = User.GetUserContent();
            var data = await this.orderService.GoToEnd(Ids, u.NiceName);
            return Ok(data);
        }
        #endregion

        public async Task<IActionResult> SaveCustomerConfirm(View_Order_CustomerConfirm View_Order_CustomerConfirm)
        {
            await this.orderService.SaveCustomerConfirm(View_Order_CustomerConfirm);
            return Ok();
        }



        public async Task<IActionResult> SaveSettlementConfirm(View_Order_SettlementConfirm View_Order_SettlementConfirm)
        {
            await this.orderService.SaveSettlementConfirm(View_Order_SettlementConfirm);
            return Ok();
        }

        #region 合计报表
        public async Task<IActionResult> GetGrossProfitByCustomer(QueryModel queryModel)
        {
            var data = await orderService.GetGrossProfitByCustomer(queryModel);
            return Ok(data);
        }
        public async Task<IActionResult> GetGrossProfitByOrder(QueryModel queryModel)
        {
            var data = await orderService.GetGrossProfitByOrder(queryModel);
            return Ok(data);
        }
        #endregion
    }
}
