using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using WCS.BLL.HardWare;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.Model;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.OutStore;

namespace WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OutstoreController : ControllerBase
    {
        private readonly IOutstoreService _outstoreService;
        private readonly IGenerateService _generateService;

        public OutstoreController(IOutstoreService outstoreService, IGenerateService generateService)
        {
            _outstoreService = outstoreService;
            _generateService = generateService;
        }


        /// <summary>
        /// 导入模板进行数据校验和比对
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("importMat")]
        [HttpPost(Name = "importMat")]
        public async Task<ResponseBase> importMat([FromForm] IFormFile excelFile, [FromForm] string userName, [FromForm] string deviceType)
        {
            try
            {
                //文件校验
                if (excelFile == null || excelFile.Length == 0)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = "导入失败:文件无有效内容！"
                    };
                }
                //获取库存中的数据
                using (var stream = new MemoryStream())
                {
                    await excelFile.CopyToAsync(stream);
                    stream.Position = 0;
                    var list = MiniExcelLibs.MiniExcel.Query<OutImportMatModel>(stream, "出库导入模板", ExcelType.XLSX).ToList();
                    return await _outstoreService.importMat(list);
                }
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 201,
                    Message = "导入失败：" + ex.Message,
                };
            }
        }



        /// <summary>
        /// 按物料编码同步数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("sysOutOrderByMatCode")]
        [HttpPost(Name = "sysOutOrderByMatCode")]
        public async Task<ResponseBase> sysOutOrderByMatCode(SysOutOrderByMatCodeRequest request)
        {
            try
            {
                if (string.IsNullOrEmpty(request.OrderNumber))
                {
                    request.OrderNumber = await _generateService.generateOutOrderNumber();
                }
                return await _outstoreService.SysOutOrderByMatCode(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "同步单据失败：" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 按物料明细同步数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("sysOutOrderByMatSn")]
        [HttpPost(Name = "sysOutOrderByMatSn")]
        public async Task<ResponseBase> sysOutOrderByMatSn(SysOutOrderByMatSnRequest request)
        {
            try
            {
                if (string.IsNullOrEmpty(request.OrderNumber))
                {
                    if (request.IsMXPD)
                    {
                         request.OrderNumber = await _generateService.generateMXPDOutOrderNumber();
                    }
                    else
                        request.OrderNumber = await _generateService.generateOutOrderNumber();
                }
                return await _outstoreService.SysOutOrderByMatSn(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "同步单据失败：" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 查询、返回当前出库单列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("getOutOrderList")]
        [HttpPost(Name = "getOutOrderList")]
        public async Task<ResponseBase> getOutOrderList(GetOutOrderListRequest request)
        {
            try
            {
                return await _outstoreService.GetOutOrderList(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 按状态查询出库单号
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("getOutOrderListByStatus")]
        [HttpPost(Name = "getOutOrderListByStatus")]
        public async Task<ResponseBase> getOutOrderListByStatus(GetOutOrderListByStatusRequest request)
        {
            try
            {
                return await _outstoreService.GetOutOrderListByStatus(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 获取出库单的出库明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("getOutOrderDetail")]
        [HttpPost(Name = "getOutOrderDetail")]
        public async Task<ResponseBase> getOutOrderDetail(GetOutOrderDetailRequest request)
        {
            try
            {
                return await _outstoreService.GetOutOrderDetail(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }

        [Route("getOutOrderDetailSingleLight")]
        [HttpPost(Name = "getOutOrderDetailSingleLight")]
        public async Task<ResponseBase> getOutOrderDetailSingleLight(GetOutOrderDetailRequest request)
        {
            try
            {
                return await _outstoreService.GetOutOrderDetailSingleLight(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 获取出库单出库物料明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("getOutOrderMatDetail")]
        [HttpPost(Name = "getOutOrderMatDetail")]
        public async Task<ResponseBase> getOutOrderMatDetail(GetOutOrderDetailRequest request)
        {
            try
            {
                return await _outstoreService.GetOutOrderMatDetail(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 通过出库单据号开始出库
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("goInOutstore")]
        [HttpPost(Name = "goInOutstore")]
        public async Task<ResponseBase> goInOutstore(GetOutOrderDetailRequest request)
        {
            try
            {
                return await _outstoreService.GoInOutstore(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "进入出库模式失败：" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 通过出库单据号退出/结束出库
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("goOutOutstore")]
        [HttpPost(Name = "goOutOutstore")]
        public async Task<ResponseBase> goOutOutstore(GetOutOrderDetailRequest request)
        {
            try
            {
                return await _outstoreService.GoOutOutstore(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }

        /// <summary>
        /// 单灯确认出库
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("singleLightConfirmOutstore")]
        [HttpPost(Name = "singleLightConfirmOutstore")]
        public async Task<ResponseBase> singleLightConfirmOutstore(OutOrderMatDetailModelSingle request)
        {
            try
            {
                return await _outstoreService.SingleLightConfirmOutstore(request);
            }
            catch (Exception ex)
            {
                return new ResponseBase()
                {
                    Code = 300,
                    Message = "查询失败：" + ex.Message,
                };
            }
        }

    }
}
