using Microsoft.AspNetCore.Mvc;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.Model;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.Stocktaking;
using WCS.Model.ApiModel.StoreInfo;
using WCS.WebApi.Helper;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// PDA库存盘点
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class PDAStocktakingController : ControllerBase
    {
        public IStockTakingService _stockTakingService { get; set; }
        public PDAStocktakingController(IStockTakingService stockTakingService)
        {
            _stockTakingService = stockTakingService;
        }

        [Route("stockTakingById")]
        [HttpPost(Name = "stockTakingById")]
        public async Task<ResponseCommon> stockTakingById(StockTakingByIdRequest request)
        {
            try
            {
                #region 参数校验
                //判断参数  //数量可以为空 数量为空盘点确认 这边删除对应数据即可
                if (request.MatDetailCurrentInfoId == 0)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"操作失败:参数传入错误(Id为0)!",
                        Data = null,
                    };
                }

                if (request.StocktakingQty < 0)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = $"操作失败:数量应大于等于0!",
                        Data = null,
                    };
                }
                #endregion

                return await _stockTakingService.stockTakingById(request);
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = ex.Message,
                    Data = null,
                };
            }
        }

        [Route("getStocktakingInfos")]
        [HttpPost(Name = "getStocktakingInfos")]
        public async Task<ResponseCommon> getStocktakingInfos(GetStocktakingInfosRequest request)
        {
            try
            {
                return await _stockTakingService.getStocktakingInfos(request);
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = ex.Message,
                    Data = null,
                };
            }
        }

        [Route("exportStocktakingInfos")]
        [HttpPost(Name = "exportStocktakingInfos")]
        public async Task<IActionResult> exportStocktakingInfos(GetStocktakingInfosRequest request)
        {
            var result = await _stockTakingService.exportStocktakingInfos
                (request);
            var data = result.Data?.Lists;
            var columns = new[]
            {
                new ExportableColumn("序号","RowNumber"),
                new ExportableColumn("物料编码","MatCode"),
                new ExportableColumn("物料名称","MatName"),
                new ExportableColumn("物料批次","MatBatch"),
                new ExportableColumn("物料规格","MatSpec"),
                new ExportableColumn("货架编号","ShelfCode"),
                new ExportableColumn("货架区域","ShelfArea"),
                new ExportableColumn("原数量","MatQty"),
                new ExportableColumn("盘点数量","StocktakingQty"),
                new ExportableColumn("盘点人", "StocktakingUser"),
                new ExportableColumn("盘点时间", "StocktakingTime"),
                new ExportableColumn("盘点状态", "StocktakingStatus"),
            };
            if (data == null)
            {
                return NotFound();
            }
            else
                return ExportExcelHelper.Export("导出数据", columns, data);
        }

        [Route("getStocktakingInfosByShelfCode")]
        [HttpPost(Name = "getStocktakingInfosByShelfCode")]
        public async Task<ResponseCommon> getStocktakingInfosByShelfCode(GetStocktakingInfosByShelfCodeRequest request)
        {
            try
            {
                return await _stockTakingService.getStocktakingInfosByShelfCode(request);
            }
            catch (Exception ex)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = ex.Message,
                    Data = null,
                };
            }
        }

        [HttpPost("updateStocktakingInfos")]
        public async Task<ResponseCommon<object>> updateStocktakingInfos(UpdateStocktakingInfoRequest request)
        {
            return await _stockTakingService.updateStocktakingInfo(request);
        }

        [HttpPost("deleteStocktakingInfos")]
        public async Task<ResponseCommon<object>> deleteStocktakingInfos(DeleteInfosRequest request)
        {
            //校验
            if (request.needDeleteIds == null || request.needDeleteIds.Count == 0)
            {
                return new ResponseCommon<object>()
                {
                    Code = 201,
                    Message = "操作失败：参数校验失败(ID)."
                };
            }
            return await _stockTakingService.deleteStocktakingInfos(request);
        }

        [HttpPost("commitStocktakingInfos")]
        public async Task<ResponseCommon<object>> commitStocktakingInfos(DeleteInfosRequest request)
        {
            //校验
            if (request.needDeleteIds == null || request.needDeleteIds.Count == 0)
            {
                return new ResponseCommon<object>()
                {
                    Code = 201,
                    Message = "操作失败：参数校验失败(ID)."
                };
            }
            return await _stockTakingService.commitStocktakingInfos(request);
        }
    }
}
