using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Core.Entity;
using Myshipping.Application.Entity;
using Furion.FriendlyException;
using System.Collections.Generic;
using System;
using System.Collections;
namespace Myshipping.Core.Service
{
///
/// 船期手工维护模块
///
[ApiDescriptionSettings(Name = "DjyVesselInfo", Order = 1)]
public class DjyVesselInfoService : IDjyVesselInfoService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly ISysCacheService _sysCacheService;
public DjyVesselInfoService(SqlSugarRepository rep, ISysCacheService sysCacheService)
{
_sysCacheService = sysCacheService;
_rep = rep;
}
/////
///// 获取列表
/////
/////
//[HttpGet("/DjyVesselInfoService/GetListPage")]
//public async Task GetListPage([FromQuery] string CARRIER, string Vessel, string VoynoInside, string Voyno, DateTime? StartETD, DateTime? EndETD)
//{
// return await _rep.AsQueryable().Filter(null, true).Where(x => x.TenantId == UserManager.TENANT_ID && x.IsDeleted == false)
// .WhereIF(!string.IsNullOrWhiteSpace(CARRIER), x => x.CARRIER.Contains(CARRIER))
// .WhereIF(!string.IsNullOrWhiteSpace(Vessel), x => x.Vessel.Contains(Vessel))
// .WhereIF(!string.IsNullOrWhiteSpace(VoynoInside), x => x.VoynoInside.Contains(VoynoInside))
// .WhereIF(!string.IsNullOrWhiteSpace(Voyno), x => x.Voyno.Contains(Voyno))
// .WhereIF(StartETD != null, x => x.ETD >= StartETD)
// .WhereIF(EndETD != null, x => x.ETD <= EndETD)
// .ToListAsync();
//}
///
/// 获取列表分页
///
///
///
[HttpGet("/DjyVesselInfoService/page")]
public async Task GetListPage([FromQuery] QueryDjyVesselInfoInput input)
{
var query = _rep.AsQueryable().Filter(null, true).Where(x => x.TenantId == UserManager.TENANT_ID && x.IsDeleted == false)
.WhereIF(!string.IsNullOrWhiteSpace(input.CARRIER), x => x.CARRIER.Contains(input.CARRIER))
.WhereIF(!string.IsNullOrWhiteSpace(input.Vessel), x => x.Vessel.Contains(input.Vessel))
.WhereIF(!string.IsNullOrWhiteSpace(input.VoynoInside), x => x.VoynoInside.Contains(input.VoynoInside))
.WhereIF(!string.IsNullOrWhiteSpace(input.Voyno), x => x.Voyno.Contains(input.Voyno))
.WhereIF(input.StartETD != null, x => x.ETD >= input.StartETD)
.WhereIF(input.EndETD != null, x => x.ETD <= input.EndETD);
if (!string.IsNullOrEmpty(input.SortField) || input.MultiSort == null || input.MultiSort.Count == 0)
{
query = query.OrderBy(PageInputOrder.OrderBuilder(input.SortField, input.DescSort));
}
else
{
query = query.OrderBy(PageInputOrder.MultiOrderBuilder(input.MultiSort));
}
return await query.ToPagedListAsync(input.PageNo, input.PageSize);
}
///
/// 删除
///
///
///
[HttpGet("/DjyVesselInfoService/Delete")]
public async Task Delete(string Ids)
{
var arr = Ids.Split(",");
if (arr.Count() > 0)
{
foreach (var ar in arr)
{
long Id = Convert.ToInt64(ar);
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == Id);
await _rep.DeleteAsync(entity);
}
}
}
///
/// 新增编辑
///
///
///
[HttpPost("/DjyVesselInfoService/AddOrUpdate")]
public async Task AddOrUpdate(DjyVesselInfoDto dto)
{
if (dto == null)
{
throw Oops.Bah("请传入数据!");
}
if (dto.Id == 0)
{
var entity = dto.Adapt();
var e = _rep.AsQueryable().Filter(null, true).Where(x => x.TenantId == UserManager.TENANT_ID && x.IsDeleted == false && x.CARRIERID == entity.CARRIERID && x.Vessel == entity.Vessel && x.Voyno == entity.Voyno && x.VoynoInside == entity.VoynoInside).First();
if (e == null)
{
await _rep.InsertAsync(entity);
}
else
{
entity.Id = e.Id;
if (e.ETA != null)
{
entity.ETA = e.ETA;
}
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
}
else
{
var entity = dto.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
return dto.Id;
}
///
/// 下拉列表
///
///
[HttpGet("/DjyVesselInfoService/GetList")]
public async Task GetList([FromQuery] string CarrierID,
DateTime? ETD = null,
string PortDischargeId = null,
string PortLoadingId = null,
string KeyWord = "",
DateTime? etdStart = null,
DateTime? etdEnd = null,
string sortField = null,
bool descSort = false,
int limit = 20)
{
var query = _rep.AsQueryable().Filter(null, true).
Where(x => x.TenantId == UserManager.TENANT_ID && x.IsDeleted == false && x.CARRIERID != null && x.CARRIERID != "").
WhereIF(!string.IsNullOrWhiteSpace(KeyWord), x => x.Vessel.StartsWith(KeyWord.ToUpper())).
WhereIF(!string.IsNullOrEmpty(PortDischargeId), x => x.PortDischargeId == PortDischargeId).
WhereIF(!string.IsNullOrEmpty(PortLoadingId), x => x.PortLoadingId == PortLoadingId).
WhereIF(ETD != null, x => Convert.ToDateTime(x.ETD).ToString("yyyy-MM-dd") == Convert.ToDateTime(ETD).ToString("yyyy-MM-dd")).//船期关联船名
WhereIF(ETD == null && etdStart == null && etdEnd == null, x => x.ETD > DateTime.Now.AddDays(-7)).//领导需求当前日期7天之前
WhereIF(etdStart.HasValue, x => x.ETD >= etdStart).
WhereIF(etdEnd.HasValue, x => x.ETD <= etdEnd).
WhereIF(!string.IsNullOrEmpty(CarrierID), x => x.CARRIERID == CarrierID || x.CARRIERID == null || x.CARRIERID == "").
Select(x => new
{
Voyno = x.Voyno,
VoynoInside = x.VoynoInside,
Vessel = x.Vessel,
ETD = x.ETD == null ? "" : Convert.ToDateTime(x.ETD).ToString("yyyy-MM-dd"),
//日期和时间都要
ATD = x.ATD == null ? "" : Convert.ToDateTime(x.ATD).ToString("yyyy-MM-dd HH:mm:ss"),
PortDischargeId = x.PortDischargeId,
PortDischarge = x.PortDischarge,
ClosingDate = x.ClosingDate == null ? "" : Convert.ToDateTime(x.ClosingDate).ToString("yyyy-MM-dd HH:mm:ss"),
CloseDocTime = x.CloseDocTime == null ? "" : Convert.ToDateTime(x.CloseDocTime).ToString("yyyy-MM-dd HH:mm:ss"),
});
if (!string.IsNullOrEmpty(sortField))
{
query = query.OrderBy(PageInputOrder.OrderBuilder(sortField, descSort));
}
var tlist = await query
.Take(limit)
.ToListAsync();
if (_sysCacheService.GetAllTenantParam().Result.Where(x => x.TenantId == UserManager.TENANT_ID && x.ParaCode == "VESSEL_FROM_CONFIG_ONLY").Select(x => x.ItemCode).FirstOrDefault() == "YES"
)
{
return tlist;
}
List commonList = await _sysCacheService.GetAllCodeVessel();
if (string.IsNullOrEmpty(KeyWord))
{
foreach (var item in commonList)
{
if (tlist.Count >= 10)
{
return tlist;
}
if (tlist.Any(x => x.Vessel == item.Name))
{
continue;
}
tlist.Add(new
{
Voyno = "",
VoynoInside = "",
Vessel = item.Name,
ETD = "",
ATD = "",
PortDischargeId = "",
PortDischarge = "",
ClosingDate = "",
CloseDocTime = ""
});
}
return tlist;
}
else
{
commonList = commonList.Where(x => x.Name.StartsWith(KeyWord.ToUpper())).ToList();
foreach (var item in tlist)
{
commonList.RemoveAll(x => x.Name == item.Vessel);
}
var temp = commonList.Select(x => new
{
Voyno = "",
VoynoInside = "",
Vessel = x.Name,
ETD = "",
ATD = "",
PortDischargeId = "",
PortDischarge = "",
ClosingDate = "",
CloseDocTime = ""
});
return tlist.Union(temp);
}
}
}
}