//using Furion.DatabaseAccessor;
//using Furion.DatabaseAccessor.Extensions;
//using Furion.DependencyInjection;
//using Furion.DynamicApiController;
//using Furion.FriendlyException;
//using Admin.NET.Core;
//using Mapster;
//using Microsoft.AspNetCore.Mvc;
//using Microsoft.EntityFrameworkCore;
//using System.Linq.Dynamic.Core;
//using Microsoft.AspNetCore.Http;
//using System.Text;
//using System.Web;
//namespace Admin.NET.Application
//{
// ///
// /// 托盘信息服务
// ///
// [ApiDescriptionSettings("自己的业务", Name = "WmsContainer", Order = 100)]
// [Route("api/[Controller]")]
// public class WmsContainerService : IWmsContainerService, IDynamicApiController, ITransient
// {
// private readonly IRepository _wmsContainerRep;
// private readonly IRepository _sysDictTypeRep;
// private readonly IRepository _sysDictDataRep;
// private readonly ISysExcelTemplateService _sysExcelTemplateService;
// private readonly static object _lock = new();
// ///
// /// 构造函数
// ///
// public WmsContainerService(
// IRepository wmsContainerRep
// ,IRepository sysDictTypeRep
// ,IRepository sysDictDataRep
// ,ISysExcelTemplateService sysExcelTemplateService
// )
// {
// _wmsContainerRep = wmsContainerRep;
// _sysDictTypeRep = sysDictTypeRep;
// _sysDictDataRep = sysDictDataRep;
// _sysExcelTemplateService = sysExcelTemplateService;
// }
// ///
// /// 分页查询托盘信息
// ///
// ///
// ///
// [HttpGet("page")]
// public async Task> Page([FromQuery] WmsContainerSearch input)
// {
// var wmsContainers = await _wmsContainerRep.DetachedEntities
// .Where(!string.IsNullOrEmpty(input.ContainerCode), u => EF.Functions.Like(u.ContainerCode, $"%{input.ContainerCode.Trim()}%"))
// .Where(input.ContainerType != null, u => u.ContainerType == input.ContainerType)
// .Where(input.ContainerStatus != null, u => u.ContainerStatus == input.ContainerStatus)
// .Where(input.SpecLength != null, u => u.SpecLength == input.SpecLength)
// .Where(input.SpecWidth != null, u => u.SpecWidth == input.SpecWidth)
// .Where(input.ContainerCategory != null, u => u.ContainerCategory == input.ContainerCategory)
// .Where(input.WorkShopType != null, u => u.WorkShopType == input.WorkShopType)
// .OrderBy(PageInputOrder.OrderBuilder(input))
// .ProjectToType()
// .ToADPagedListAsync(input.PageNo, input.PageSize);
// return wmsContainers;
// }
// ///
// /// 不分页查询托盘信息列表
// ///
// /// 托盘信息查询参数
// /// (托盘信息)实例列表
// [HttpGet("listNonPage")]
// public async Task> ListNonPageAsync([FromQuery] WmsContainerSearchNonPage input)
// {
// var pContainerCode = input.ContainerCode?.Trim() ?? "";
// var pContainerType = input.ContainerType;
// var pContainerStatus = input.ContainerStatus;
// var pSpecLength = input.SpecLength;
// var pSpecWidth = input.SpecWidth;
// var pContainerCategory = input.ContainerCategory;
// var pWorkShopType = input.WorkShopType;
// var wmsContainers = await _wmsContainerRep.DetachedEntities
// .Where(!string.IsNullOrEmpty(pContainerCode), u => EF.Functions.Like(u.ContainerCode, $"%{pContainerCode}%"))
// .Where(pContainerType != null, u => u.ContainerType == pContainerType)
// .Where(pContainerStatus != null, u => u.ContainerStatus == pContainerStatus)
// .Where(pSpecLength != null, u => u.SpecLength == pSpecLength)
// .Where(pSpecWidth != null, u => u.SpecWidth == pSpecWidth)
// .Where(pContainerCategory != null, u => u.ContainerCategory == pContainerCategory)
// .Where(pWorkShopType != null, u => u.WorkShopType == pWorkShopType)
// .OrderBy(PageInputOrder.OrderNonPageBuilder(input))
// .ProjectToType()
// .ToListAsync();
// return wmsContainers;
// }
// ///
// /// 增加托盘信息
// ///
// ///
// ///
// [HttpPost("add")]
// public async Task Add(AddWmsContainerInput input)
// {
// var isExist = await _wmsContainerRep.AnyAsync(u => u.ContainerCode == input.ContainerCode, false);
// if (isExist) throw Oops.Oh($"当前托盘已存在,新增失败!");
// var ContainerInfo = input.Adapt();
// ContainerInfo.ContainerStatus = ContainerStatus.KOUXIAN;
// ContainerInfo.AssetNo = "N/A";
// ContainerInfo.ContainerCategory = ContainerCategory.GZP;
// ContainerInfo.ErpNo = "N/A";
// ContainerInfo.IsVirtually = YesOrNot.N;
// ContainerInfo.WorkShopType = LesWorkShopType.FAPAOCHEJIAN;
// await _wmsContainerRep.InsertAsync(ContainerInfo);
// //string ContainerCode = "ZHONGTONG";
// //if (input.SpecLength == 1250)
// //{
// // ContainerCode = "YKA";
// //}
// //else if (input.SpecLength == 1050)
// //{
// // ContainerCode = "YKB";
// //}
// //else if (input.SpecLength == 800)
// //{
// // ContainerCode = "YKC";
// //}
// //var wmsContainerModal = await _wmsContainerRep.DetachedEntities.Where(u => u.ContainerCode.Contains(input.ContainerCode))
// // .ProjectToType()
// // .OrderByDescending(u => u.ContainerCode)
// // .FirstOrDefaultAsync();
// //int a = 0;
// //if (wmsContainerModal != null)
// //{
// // a = Convert.ToInt32(wmsContainerModal.ContainerCode.Substring(wmsContainerModal.ContainerCode.Length - 5));
// //}
// //for (int i = a + 1; i <= a + input.Quantity; i++)
// //{
// // var wmsContainer = input.Adapt();
// // wmsContainer.AssetNo = "N/A";
// // wmsContainer.ErpNo = "N/A";
// // wmsContainer.IsVirtually = YesOrNot.N;
// // wmsContainer.IsDeleted = false;;
// // wmsContainer.ContainerCode = ContainerCode + i.ToString("00000");
// // await _wmsContainerRep.InsertAsync(wmsContainer);
// //}
// }
// ///
// /// 删除托盘信息
// ///
// ///
// ///
// [HttpPost("delete")]
// public async Task Delete(DeleteWmsContainerInput input)
// {
// var wmsContainer = await _wmsContainerRep.FirstOrDefaultAsync(u => u.Id == input.Id);
// await _wmsContainerRep.DeleteAsync(wmsContainer);
// }
// ///
// /// 更新托盘信息
// ///
// ///
// ///
// [HttpPost("edit")]
// public async Task Update(UpdateWmsContainerInput input)
// {
// var isExist = await _wmsContainerRep.AnyAsync(u => u.Id == input.Id, false);
// if (!isExist) throw Oops.Oh(ErrorCode.D3000);
// var wmsContainer = input.Adapt();
// await _wmsContainerRep.UpdateAsync(wmsContainer,ignoreNullValues:true);
// }
// ///
// /// 获取托盘信息
// ///
// ///
// ///
// [HttpGet("detail")]
// public async Task Get([FromQuery] QueryeWmsContainerInput input)
// {
// return (await _wmsContainerRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt();
// }
// ///
// /// 获取托盘信息列表
// ///
// ///
// ///
// [HttpGet("list")]
// public async Task> List([FromQuery] WmsContainerInput input)
// {
// return await _wmsContainerRep.DetachedEntities.ProjectToType().ToListAsync();
// }
// ///
// /// Excel模板导入托盘信息功能
// ///
// /// Excel模板文件
// /// Excel导入方式
// /// 导入的记录数
// [HttpPost("fromExcel")]
// public async Task FromExcelAsync(IFormFile file, [FromQuery] ImportExcelType importExcelType)
// {
// int size = 200;
// var excelTemplate = await _sysExcelTemplateService.GetByAppNameAndClassNameAndVersionAsync("WmsContainer", "v2");
// if (excelTemplate == null) throw Oops.Oh(ErrorCode.Excel002);
// var keys = excelTemplate.UnionUniqueFields.Split(",") ?? Array.Empty();
// for (var i = 0; i < keys.Length; i++)
// {
// keys[i] = keys[i]?.Trim() ?? string.Empty;
// }
// ExcelUtil.FromExcel(file, excelTemplate.HeadStartLine, excelTemplate.DataStartLine, out List headers, out List> data, out string sheetName);
// List wmsContainerList = DataConvertUtil.ToObjectList(headers, data, sheetName, keys, excelTemplate?.DataStartLine ?? 2, out Dictionary dict);
// List> uniqueKeyValueDictList = wmsContainerList.ParseUniqueKeyValueDictList(keys.ToList(), excelTemplate?.DataStartLine ?? 2, sheetName);
// var filters = DataConvertUtil.GetExpressionListByUniqueDict(keys.ToList(), uniqueKeyValueDictList, size);
// var selectKeys = keys.ToList();
// if(!selectKeys.Contains("Id")) selectKeys.Add("Id");
// var selector = DataConvertUtil.GetSelectExpressionListByUniqueDict(selectKeys);
// List updates = new();
// List adds = new();
// lock (_lock)
// {
// foreach (var filter in filters)
// {
// var wmsContainerExistSubList = _wmsContainerRep.Where(filter).Select(selector).ToList();
// wmsContainerExistSubList.ForEach(x =>
// {
// var k = DataConvertUtil.GetKey(x, keys);
// if (dict.ContainsKey(k)) dict[k].Id = x.Id;
// });
// }
// foreach (var wmsContainer in wmsContainerList)
// {
// if (wmsContainer.Id > 0)
// {
// if (importExcelType == ImportExcelType.ADD_AND_UPDATE) updates.Add(wmsContainer.Adapt());
// }
// else
// {
// adds.Add(wmsContainer.Adapt());
// }
// }
// if (importExcelType == ImportExcelType.ADD_AND_UPDATE) updates.ForEach(x => _wmsContainerRep.Update(x));
// var maxId = _wmsContainerRep.DetachedEntities.OrderByDescending(x => x.Id).Select(x => x.Id).FirstOrDefault();
// adds.ForEach(x => x.Id = ++maxId);
// Db.GetDbContext().Set().AddRange(adds);
// Db.GetDbContext().SaveChanges();
// }
// await Task.CompletedTask;
// return adds.Count;
// }
// ///
// /// 根据版本下载托盘信息的Excel导入模板
// ///
// /// 模板版本
// /// 下载的模板文件
// [HttpGet("downloadExcelTemplate")]
// public async Task DownloadExcelTemplate([FromQuery] string version)
// {
// var excelTemplate = await _sysExcelTemplateService.GetByAppNameAndClassNameAndVersionAsync("WmsContainer", version);
// if (excelTemplate == null) throw Oops.Oh(ErrorCode.Excel002);
// var path = Path.Combine(@"\", excelTemplate.TemplateFileName);
// Stream ms = FileUtil.Download(path, excelTemplate.TemplateFileName);
// var fileName = HttpUtility.UrlEncode($"{excelTemplate.Name}导入模板.xlsx", Encoding.GetEncoding("UTF-8"));
// return new FileStreamResult(ms, "application/octet-stream") { FileDownloadName = fileName };
// }
// ///
// /// 根据托盘信息查询参数导出Excel
// ///
// /// 托盘信息查询参数
// /// 导出的Excel文件
// [HttpGet("toExcel")]
// public async Task ToExcelAsync([FromQuery] WmsContainerSearchNonPage input)
// {
// var wmsContainerList = await ListNonPageAsync(input);
// MemoryStream ms = new();
// DataConvertUtil.ToExcelData(wmsContainerList, _sysDictTypeRep, _sysDictDataRep, out List headers,
// out List> data, out string sheetName);
// var excelTemplate = await _sysExcelTemplateService.GetByAppNameAndClassNameAndVersionAsync("WmsContainer", "v1");
// if (excelTemplate != null)
// {
// ExcelUtil.ToExcel(excelTemplate.TemplateFileName, headers, data, sheetName, excelTemplate.HeadStartLine, excelTemplate.DataStartLine, ms);
// }
// else
// {
// ExcelUtil.ToExcel(headers, data, sheetName, ms);
// }
// ms.Position = 0;
// var fileName = HttpUtility.UrlEncode($"{sheetName}[{DateTimeOffset.Now:yyyy-MM-dd}].xlsx", Encoding.GetEncoding("UTF-8"));
// return new FileStreamResult(ms, "application/octet-stream") { FileDownloadName = fileName };
// }
// }
//}