using System.Net;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Fee.Interface;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
namespace DS.WMS.FeeApi.Controllers
{
///
/// 自动费用模板API
///
public class FeeCustTemplateController : ApiController
{
readonly IFeeCustTemplateService _invokeService;
///
/// 构造函数
///
///
public FeeCustTemplateController(IFeeCustTemplateService invokeService)
{
_invokeService = invokeService;
}
///
/// 生成订单费用
///
/// 开船日期
///
[HttpPost, Route("GenerateFees")]
public async Task GenerateFeesAsync(DateTime? etd)
{
HttpStatusCode httpStatusCode;
try
{
await _invokeService.GenerateFeesAsync(etd ?? DateTime.Now.Date);
httpStatusCode = HttpStatusCode.NoContent;
}
catch
{
httpStatusCode = HttpStatusCode.InternalServerError;
throw;
}
return new StatusCodeResult((int)httpStatusCode);
}
///
/// 导入费用模板
///
///
///
///
[HttpPost, Route("Import")]
public async Task ImportAsync(IFormFile file, string? sourceCode)
{
if (file == null)
return BadRequest("请求未包含文件流");
var stream = file.OpenReadStream();
var query = await MiniExcel.QueryAsync(stream, excelType: ExcelType.XLSX, startCell: "A4");
List list = [];
try
{
foreach (IDictionary item in query)
{
var model = new FeeCustTemplateModel
{
StatusText = item["A"]?.ToString(),
FeeName = item["B"].ToString(),
CarrierName = item["C"]?.ToString(),
POL = item["D"]?.ToString(),
POD = item["E"]?.ToString(),
LaneName = item["F"]?.ToString(),
PaymentType = item["G"]?.ToString(),
Currency = item["L"]?.ToString(),
CustomerName = item["M"]?.ToString(),
SourceCode = sourceCode
};
if (!string.IsNullOrEmpty(model.Currency))
model.Currency = model.Currency.Replace("CNY", FeeCurrency.RMB_CODE);
if (int.TryParse(item["H"]?.ToString(), out int hVal))
model.GP20 = hVal;
if (int.TryParse(item["I"]?.ToString(), out int iVal))
model.GP40 = iVal;
if (int.TryParse(item["J"]?.ToString(), out int jVal))
model.HQ40 = jVal;
if (int.TryParse(item["K"]?.ToString(), out int kVal))
model.UnitPrice = kVal;
if (DateTime.TryParse(item["N"]?.ToString(), out DateTime nVal))
model.StartTime = nVal;
if (DateTime.TryParse(item["O"]?.ToString(), out DateTime oVal))
model.EndTime = oVal;
list.Add(model);
}
}
catch (Exception ex)
{
return Ok(DataResult.Failed("读取文件失败:" + ex.Message));
}
var result = await _invokeService.ImportAsync(list);
return Ok(result);
}
///
/// 列表
///
///
///
[HttpPost, Route("GetList")]
public async Task>> GetListAsync([FromBody] PageRequest request)
{
return await _invokeService.GetListAsync(request);
}
///
/// 视图列表
///
///
///
[HttpPost, Route("GetListView")]
public async Task>> GetListViewAsync([FromBody] PageRequest request)
{
return await _invokeService.GetListViewAsync(request);
}
///
/// 编辑
///
///
///
[HttpPost, Route("Edit")]
public async Task EditAsync([FromBody] FeeCustTemplate model)
{
if (model == null)
return DataResult.FailedWithDesc(MultiLanguageConst.IllegalRequest);
return await _invokeService.EditAsync(model);
}
///
/// 批量编辑模板
///
///
///
[HttpPost, Route("BulkEdit")]
public async Task BulkEditAsync([FromBody] List list)
{
if (list == null || list.Count == 0)
return DataResult.FailedWithDesc(MultiLanguageConst.IllegalRequest);
return await _invokeService.BulkEditAsync(list);
}
///
/// 批量编辑模板明细
///
///
///
[HttpPost, Route("BulkEditDetails")]
public async Task BulkEditDetailsAsync([FromBody] List details)
{
if (details == null || details.Count == 0)
return DataResult.FailedWithDesc(MultiLanguageConst.IllegalRequest);
return await _invokeService.BulkEditDetailsAsync(details);
}
///
/// 复制模板
///
///
///
[HttpPost, Route("Copy")]
public async Task CopyAsync([FromBody] IdModel model)
{
if (!ModelState.IsValid)
return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest);
return await _invokeService.CopyAsync(model.Ids);
}
///
/// 详情
///
///
///
[HttpGet, Route("Edit")]
public async Task> GetAsync([FromQuery] long id)
{
return await _invokeService.GetAsync(id);
}
///
/// 根据ID删除
///
///
///
[HttpPost, Route("Delete")]
public async Task DeleteAsync([FromBody] IdModel model)
{
if (!ModelState.IsValid)
return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest);
return await _invokeService.DeleteAsync(model.Ids);
}
///
/// 根据ID删除明细
///
///
///
///
[HttpPost, Route("DeleteDetails")]
public async Task DeleteDetailsAsync([FromServices] IFeeCustTemplateDetailService detailService, [FromBody] IdModel model)
{
if (!ModelState.IsValid)
return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest);
return await detailService.DeleteAsync(model);
}
}
}