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)
{
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(),
};
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("Edit")]
public async Task EditAsync([FromBody] FeeCustTemplate model)
{
if (model == null)
return DataResult.FailedWithDesc(MultiLanguageConst.IllegalRequest);
return await _invokeService.EditAsync(model);
}
///
/// 详情
///
///
///
[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);
}
///
/// 根据ID删除明细
///
///
///
[HttpPost, Route("DeleteDetails")]
public async Task DeleteAsync([FromServices] IFeeCustTemplateDetailService detailService, [FromBody] IdModel model)
{
if (!ModelState.IsValid)
return DataResult.Failed(ModelState.GetErrorMessage(), MultiLanguageConst.IllegalRequest);
return await detailService.DeleteAsync(model);
}
}
}