using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using XYY.Common.Standard;
using XYY.Core.Standard.Mvc;
using XYY.Model.Standard.Finance;
using XYY.Service.Standard.Finance;

namespace XYY.Api.Finance.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Finance_ServiceSurchargeController : ApiControllerBase
    {
        IFinance_ServiceSurchargeService serviceSurchargeService;
        XYY.Core.Standard.AliYun.IAliYunPostFileSerivce aliOSS;
        public Finance_ServiceSurchargeController(IFinance_ServiceSurchargeService serviceSurchargeService, Core.Standard.AliYun.IAliYunPostFileSerivce aliOSS)
        {
            this.serviceSurchargeService = serviceSurchargeService;
            this.aliOSS = aliOSS;
        }

        #region wms调用
      
        public async Task<IActionResult> AddServiceSurchargeService([FromForm] Finance_ServiceSurcharge surcharge)
        {
            var data = new byte[surcharge.Stream.Length];
            surcharge.FileUrl = aliOSS.PostHttpFile(surcharge.Stream.OpenReadStream(), surcharge.Stream.FileName);
            await this.serviceSurchargeService.AddServiceSurchargeService(surcharge);
            return Ok();
        }

        public async Task<IActionResult> UpdateServiceSurchargeService([FromForm] Finance_ServiceSurcharge surcharge)
        {
            if (surcharge.Stream != null)
            {
                var data = new byte[surcharge.Stream.Length];
                surcharge.FileUrl = aliOSS.PostHttpFile(surcharge.Stream.OpenReadStream(), Guid.NewGuid().ToString()); 
            }
            await this.serviceSurchargeService.UpdateServiceSurchargeService(surcharge);
            return Ok();
        }

        public async Task<IActionResult> GetLogs([FromQuery] int id)
        {
            var data = await serviceSurchargeService.GetLogs(id);
            return new JsonResult(new
            {
                total = data.Count,
                rows = data
            });
        }

        public async Task<IActionResult> QueryServiceSurchargeService([FromQuery] int expressId)
        {
            var list = await serviceSurchargeService.QueryServiceSurchargeService(expressId);
            PageResult<Finance_ServiceSurcharge> pr
                = new PageResult<Finance_ServiceSurcharge>() { Result = list, PageIndex = 1, PageSize = 1000, TotalCount = list.Count };
            return new JsonResult(new
            {
                total = pr.TotalCount,
                rows = pr.Result
            });
        }

        public async Task<IActionResult> DownloadServiceSurchargeService(int expresssId)
        {
            var data = serviceSurchargeService.DownloadServiceSurchargeService(expresssId);
            return File(DateTime.Now.ToString_yyyyMMdd() + ".xlsx", data);
        }
        #endregion

        #region 附加费调价处理
        public async Task<IActionResult> AddServiceSurcharge(int ExpressId)
        {
            var filelist = (await Request.ReadFormAsync()).Files;
            if (filelist != null && filelist.Count > 0)
            {
                var file = filelist[0];
                using (var ms = new System.IO.MemoryStream())
                {
                    file.CopyTo(ms);
                    var byteData=ms.ToArray();
                    string url = aliOSS.PostHttpFile(new MemoryStream(byteData), Guid.NewGuid().ToString() + ".xlsx");
                    var result =await serviceSurchargeService.ConcertToQuotation(ExpressId, byteData);
                    return Ok(new { url = url, detail = result });
                }
            }
            else
            {
                return Error("请上传一个文件");
            }

        }
       
        #endregion
    }
}
