You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.4 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Code.Dtos;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Fee.Interface;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using MiniExcelLibs;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
/// <summary>
///
/// </summary>
public class FeeCodeService: IFeeCodeService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public FeeCodeService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<FeeCodeRes>> GetListByPage(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable<FeeCode>()
.Where(whereList)
.Select<FeeCodeRes>().ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditFeeCode(FeeCodeReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id == 0)
{
if (tenantDb.Queryable<FeeCode>()
.Where(x => x.Code == req.Code).Any())
{
return DataResult.Failed("费用代码已存在", MultiLanguageConst.FeeCodeExist);
}
var data = req.Adapt<FeeCode>();
var entity = tenantDb.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = tenantDb.Queryable<FeeCode>().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<FeeCodeRes> GetFeeCodeInfo(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var data = tenantDb.Queryable<FeeCode>()
.Where(x => x.Id == long.Parse(id))
.Select<FeeCodeRes>()
.First();
return DataResult<FeeCodeRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 按Excel导入费用信息
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public async Task<DataResult<string>> ImportFeeCodeByExcel(IFormFile file)
{
//未上传文件
if (file == null || file.Length == 0)
{
return await Task.FromResult(DataResult<string>.Failed("请上传Excel!"));
}
var formFile = file;
//DataResult<string> api_Result = new Api_Result<string>();
//获取文件名
var fileName = formFile.FileName;
// 获取文件后缀
var fileExtension = Path.GetExtension(fileName);
// 判断后缀是否是xlsx或者xls
if (fileExtension != ".xlsx" && fileExtension != ".xls")
{
return DataResult<string>.Failed("文件格式错误");
}
var length = formFile.Length;
if (length > 1024 * 1024 * 10)
{
return DataResult<string>.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<FeeCodeExcelImportReq>();
var fees = new List<FeeCode>();
foreach (var item in list)
{
var unit = string.Empty;
var unitName = string.Empty;
if (item.DefaultUnit == "TEU")
{
unit = "TEU";
unitName = "箱型";
}
else if (item.DefaultUnit == "BILL")
{
unit = "P";
unitName = "单票";
}
var fee = new FeeCode()
{
Name = item.Name,
EnName = item.EnName,
Code = item.Code,
DefaultCurrency = item.DefaultCurrency.IsNull() ? "CNY": item.DefaultCurrency,
IsSea = true,
DefaultUnit = unit,
DefaultUnitName = unitName,
};
fees.Add(fee);
}
await tenantDb.Insertable(fees).ExecuteCommandAsync();
return await Task.FromResult(DataResult<string>.Failed("导入成功!", MultiLanguageConst.DataImportSuccess));
}
}
}
}