using DS.Module.Core;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using DS.Module.Core.Extensions;
using Mapster;
using Org.BouncyCastle.Ocsp;
using DS.WMS.Core.Code.Entity;
using DS.Module.Core.Data;
using DS.WMS.Core.Sys.Dtos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using DS.WMS.Core.Check.Dtos;
using MiniExcelLibs;
namespace DS.WMS.Core.Op.Method
{
///
///
///
public class BookingGoodsStatusConfigService : IBookingGoodsStatusConfigService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
///
///
///
///
public BookingGoodsStatusConfigService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
}
public DataResult EditBookingGoodsStatusConfig(BookingGoodsStatusConfigReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id == 0)
{
if (tenantDb.Queryable().Where(x => x.SystemCode == req.SystemCode.Trim()).Any())
{
return DataResult.Failed("订舱货物状态已存在!", MultiLanguageConst.BookingGoodsStatusConfigExist);
}
var data = req.Adapt();
var entity = tenantDb.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = tenantDb.Queryable().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
public DataResult GetBookingGoodsStatusConfigInfo(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var data = tenantDb.Queryable()
.Where(a => a.Id == long.Parse(id))
.Select()
.First();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
public DataResult> GetListByPage(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable()
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
///
///
///
///
///
public DataResult BatchDelBookingStatusConfig(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var list = tenantDb.Queryable().Where(x => req.Ids.Contains(x.Id)).ToList();
if (list.Count > 0)
{
tenantDb.Deleteable(list).ExecuteCommand();
}
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
///
/// 按Excel导入订舱货物状态
///
///
///
public async Task> ImportBookingStatusConfigByExcel(IFormFile file)
{
//未上传文件
if (file == null || file.Length == 0)
{
return await Task.FromResult(DataResult.Failed("请上传Excel!"));
}
var formFile = file;
//DataResult api_Result = new Api_Result();
//获取文件名
var fileName = formFile.FileName;
// 获取文件后缀
var fileExtension = Path.GetExtension(fileName);
// 判断后缀是否是xlsx或者xls
if (fileExtension != ".xlsx" && fileExtension != ".xls")
{
return DataResult.Failed("文件格式错误");
}
var length = formFile.Length;
if (length > 1024 * 1024 * 10)
{
return DataResult.Failed("文件大小不能超过10M");
}
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
// MemoryStream 内存流 在内存当中创建一个流(开辟空间)
using (var stream = new MemoryStream())
{
//将文件写入内存流
formFile.CopyTo(stream);
//将流的位置归零
stream.Position = 0;
//将内存流转成List集合
var list = await stream.QueryAsync();
var configs = new List();
foreach (var item in list)
{
var config = new BookingGoodsStatusConfig()
{
SystemCode = item.SystemCode,
StatusName = item.StatusName,
StatusColor = item.StatusColor,
Note = item.Note,
OrderNo = item.OrderNo,
};
configs.Add(config);
}
var codes = configs.Select(c => c.SystemCode).ToList();
if (tenantDb.Queryable().Where(x=> codes.Contains(x.SystemCode)).Any()) {
return DataResult.Failed("存在已有的系统编码,请检查");
}
//var configs = list.Adapt();
await tenantDb.Insertable(configs).ExecuteCommandAsync();
return await Task.FromResult(DataResult.Failed("导入成功!", MultiLanguageConst.DataImportSuccess));
}
}
}
}