using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using WCS.BLL.DbModels;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.BatchBindMatDetail;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.PDAMatBind;
using WCS.Model.ApiModel.StoreInfo;
using WCS.Model.ApiModel.User;
using WCS.Model.WebSocketModel;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// PDA物料绑定相关接口
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class BatchBindMatDetailController : ControllerBase
    {
        public IBatchBindMatDetailService _batchBindMatDetailService { get; set; }

        public BatchBindMatDetailController(IBatchBindMatDetailService batchBindMatDetailService)
        {
            _batchBindMatDetailService = batchBindMatDetailService;
        }

        [Route("getOrderTypes")]
        [HttpPost(Name = "getOrderTypes")]
        public async Task<ResponseBase> getOrderTypes(RequestBase request)
        {
            try
            {
                //直接获取当前所有货架类型并返回
                var OrderTypes = DbHelp.db.Queryable<OrderTypeInfo>()
                                          .Select(t => new OrderTypeModel()
                                          {
                                              Id = t.Id,
                                              OrderTypeName = t.OrderTypeName
                                          })
                                          .ToList();
                return new PageQueryResponse<OrderTypeModel>()
                {
                    Code = 200,
                    Message = "success",
                    Data = new PageQueryResponseData<OrderTypeModel>
                    {
                        Lists = OrderTypes,
                        Count = OrderTypes.Count
                    }
                };
            }
            catch (Exception ex)
            {
                return new PageQueryResponse<OrderTypeModel>()
                {
                    Code = 300,
                    Message = $"查询失败:{ex.Message}",
                };
            }
        }

        [Route("getMatDetailCurrentInfosByStationCode")]
        [HttpPost(Name = "getMatDetailCurrentInfosByStationCode")]
        public async Task<ResponseBase> GetMatDetailCurrentInfosByStationCode(GetMatDetailCurrentInfosByStationCodeRequest request)
        {

            ////判断参数
            //if (string.IsNullOrEmpty(request.StationCode))
            //{
            //    return new ResponseCommon()
            //    {
            //        Code = 201,
            //        Message = "工位编码为空,请联系相关人员进行配置！",
            //        Data = null,
            //    };
            //}
            ////判断工位是否存在
            //var isExsistLocation = await DbHelp.db.Queryable<LocationInfo>()
            //                            .Where(t => t.LocationCode == request.StationCode)
            //                            .AnyAsync();
            ////不存在工位
            //if (!isExsistLocation)
            //{
            //    return new ResponseCommon()
            //    {
            //        Code = 201,
            //        Message = $"工位[{request.StationCode}]不存在，暂时无法使用此功能！",
            //        Data = null,
            //    };
            //}
            return await _batchBindMatDetailService.GetMatDetailCurrentInfosByStationCode(request);
        }

        [HttpPost("updateMatDetailCurrentInfoForBind")]
        public async Task<ResponseCommon<object>> updateMatDetailCurrentInfoForBind(AddLocaionInfoRequest<MatDetailCurrentInfo> request)
        {
            return await _batchBindMatDetailService.updateMatDetailCurrentInfo(request);
        }

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

        [HttpPost("importMatDetailCurrentInfos")]
        public async Task<ResponseBase> importMatDetailCurrentInfos([FromForm] IFormFile excelFile, [FromForm] string userName
                                                                  , [FromForm] string deviceType, [FromForm] string stationCode)
        {
            //文件校验
            if (excelFile == null || excelFile.Length == 0)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = "导入失败:文件无有效内容！"
                };
            }
            //输入数据校验
            if (string.IsNullOrEmpty(userName))
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = "导入失败:参数[用户名]无效，请登录后重试！"
                };
            }
            if (string.IsNullOrEmpty(deviceType))
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = "导入失败:参数[设备类型]无效！"
                };
            }
            if (string.IsNullOrEmpty(stationCode))
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = "导入失败:参数[工位]无效，请联系相关人员维护工位编码！"
                };
            }

            using (var stream = new MemoryStream())
            {
                await excelFile.CopyToAsync(stream);
                stream.Position = 0;
                var list = MiniExcelLibs.MiniExcel.Query<MatDetailCurrentInfoImportModel>(stream, "物料绑定", ExcelType.XLSX).ToList();
                //去除空白行
                //list.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料编码));
                if (list == null || list.Count == 0)
                {
                    return new ResponseCommon()
                    {
                        Code = 201,
                        Message = "导入失败：上传的文件中不存在有效的数据！"
                    };
                }
                return await _batchBindMatDetailService.importMatDetailCurrentInfo(list, userName, deviceType, stationCode);
            }
        }

    }
}
