using System;
using System.Linq.Expressions;
using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.ContainerManagement.Info.Dtos;
using DS.WMS.ContainerManagement.Info.Entity;
using DS.WMS.Core.Application.Dtos;
using DS.WMS.Core.Application.Entity;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Fee.Dtos;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core.Info.Dtos;
using DS.WMS.Core.Op.Entity;
using LanguageExt.Pipes;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Ocsp;
using SqlSugar;
namespace DS.WMS.ContainerManagement.Info.Method
{
///
/// 费用服务基类
///
public abstract class CMServiceBase
{
///
/// 人民币代码
///
public const string RMB_CODE = "CNY";
///
/// 美元代码
///
public const string USD_CODE = "USD";
///
/// 获取用户相关信息
///
protected IUser User { get; private set; }
///
/// 获取主库访问对象
///
protected ISqlSugarClient Db { get; private set; }
///
/// 获取业务库访问对象
///
protected ISaasDbService SaasService { get; private set; }
SqlSugarScopeProvider? _tenantDb;
///
/// 获取业务库访问对象
///
protected SqlSugarScopeProvider TenantDb
{
get
{
if (_tenantDb == null)
_tenantDb = SaasService.GetBizDbScopeById(User.TenantId);
return _tenantDb;
}
}
///
/// 初始化
///
/// 服务提供程序
protected CMServiceBase(IServiceProvider serviceProvider)
{
User = serviceProvider.GetRequiredService();
Db = serviceProvider.GetRequiredService();
SaasService = serviceProvider.GetRequiredService();
}
///
/// 检查业务是否已费用锁定或已业务锁定
///
/// 业务ID
/// 业务类型
/// 锁定范围
///
internal async Task IsLockedAsync(long bizid, BusinessType businessType)
{
bool? isFeeLocking = null;
isFeeLocking = await TenantDb.Queryable().Where(
x => x.BusinessId == bizid && x.BusinessType == businessType).Select(x => x.IsFeeLocking).FirstAsync();
var IsBusinessLocking = await TenantDb.Queryable().Where(
x => x.BusinessId == bizid && x.BusinessType == businessType).Select(x => x.IsBusinessLocking).FirstAsync();
return (isFeeLocking.GetValueOrDefault()|| IsBusinessLocking.GetValueOrDefault());
}
///
/// 是否业务锁定
///
///
///
///
internal async Task IsBusinessLocking(long bizid, BusinessType businessType)
{
var IsBusinessLocking = await TenantDb.Queryable().Where(
x => x.BusinessId == bizid && x.BusinessType == businessType).Select(x => x.IsBusinessLocking).FirstAsync();
return IsBusinessLocking.GetValueOrDefault();
}
///
/// 是否费用锁定
///
///
///
///
internal async Task IsFeeLockedAsync(long bizid, BusinessType businessType)
{
bool? isFeeLocking = null;
isFeeLocking = await TenantDb.Queryable().Where(
x => x.BusinessId == bizid && x.BusinessType == businessType).Select(x => x.IsFeeLocking).FirstAsync();
return isFeeLocking.GetValueOrDefault() ;
}
///
/// 业务是否包含费用
///
///
///
///
internal async Task IsHaveFee(long bizid, BusinessType businessType)
{
bool? isFeeLocking = null;
isFeeLocking = await TenantDb.Queryable().Where(
x => x.BusinessId == bizid && x.BusinessType == businessType).Select(x => x.IsFeeLocking).FirstAsync();
return isFeeLocking.GetValueOrDefault();
}
///
/// 返回箱当前状态与箱基础信息联合的查询对象
///
/// 关联条件1
/// 查询对象
public ISugarQueryable CreateCurrentStateQuery()
{
var query1 = TenantDb.Queryable()
.LeftJoin((c, b) => c.CtnBaseinfoId == b.Id)
//.WhereIF(expr1 != null, expr1)
.Select((c,b) => new CM_CurrentStateRes
{
Id = c.Id,
OrgId=c.OrgId,
CtnBaseinfoId = c.CtnBaseinfoId,
Cntrno= b.Cntrno,
CtnCode = b.CtnCode,
Ctnall = b.Ctnall,
UsedState = c.UsedState,
CtnOwnerId = c.CtnOwnerId,
CtnOwner = c.CtnOwner,
CtnSourceId = c.CtnSourceId,
//CtnSource = c.CtnSource
CtnBizStateId = c.CtnBizStateId,
Billno = c.Billno,
CtnReleaseNo = c.CtnReleaseNo,
CtnStateId = c.CtnStateId,
CtnBreakStateId = c.CtnBreakStateId,
IsOnlineId = c.IsOnlineId,
IsHeavy=c.IsHeavy,
Portid = c.Portid,
Port = c.Port,
Depot = c.Depot,
VesselVoyno = c.VesselVoyno,
Mblno = c.Mblno,
CustomerId = c.CustomerId,
CustomerName = c.CustomerName,
ETD = c.ETD,
ETA = c.ETA,
StateTime = c.StateTime,
CtnWeight = b.CtnWeight,
CtnValue_Base = b.CtnValue_Base,
CreateTime= c.CreateTime,
ProductionDate= b.ProductionDate,
});
return query1;
//return TenantDb.UnionAll(new List> { query1 });
}
///
/// 初始化费用状态表
///
public async void InitBusiness(long Id)
{
#region 初始化费用状态表
var feeStatus = BusinessFeeStatus.Init(Id);
TenantDb.Insertable(feeStatus).ExecuteCommand();
#endregion
}
public class CtnTotalHelper
{
public Dictionary CtnTotal { get; set; } = new Dictionary();
public CtnTotalHelper() {
CtnTotal = new Dictionary();
}
public void Add(string key, int value=1) {
if (CtnTotal.ContainsKey(key))
{
CtnTotal[key] += value;
}
else {
CtnTotal.Add(key, value);
}
}
public string GetStr() {
var result = "";
if (CtnTotal.Count > 0) {
foreach (var item in CtnTotal) {
if (result != "")
{
result += ",";
}
result+= $"{item.Key}*{item.Value}";
}
}
return result;
}
}
public class CtnTotalHelper2
{
List bodyList { get; set; }=new List();
public CtnTotalHelper2() {
}
public CtnTotalHelper2 ( T _bodyList)
{
if (_bodyList != null) {
bodyList = _bodyList.Adapt>();
}
}
/*
public CtnTotalHelper2(CM_RentIn _head,List _bodyList)
{
head = _head.Adapt();
head._bodyList = _bodyList.Adapt>();
}
public CtnTotalHelper2(CM_RentOut _head, List _bodyList)
{
head = _head.Adapt();
head._bodyList = _bodyList.Adapt>();
}
public CtnTotalHelper2(CM_RentOneWay _head, List _bodyList)
{
head = _head.Adapt();
head._bodyList = _bodyList.Adapt>();
}
public CtnTotalHelper2(CM_BuyCtn _head, List _bodyList)
{
head = _head.Adapt();
head._bodyList = _bodyList.Adapt>();
}
public CtnTotalHelper2(CM_SellCtn _head, List _bodyList)
{
head = _head.Adapt();
head._bodyList = _bodyList.Adapt>();
}
public CtnTotalHelper2(CM_CtnScrap _head, List _bodyList)
{
head = _head.Adapt();
head._bodyList = _bodyList.Adapt>();
}*/
public string GetCtnTotalStr()
{
var result = "";
var ch = new CtnTotalHelper();
foreach (var item in bodyList)
{
ch.Add(item.Ctnall);
}
result= ch.GetStr();
return result;
}
public string GetCntrTotalStr()
{
var result = "";
result=string.Join(",", bodyList.Select(s => s.Cntrno).Distinct().ToList());
return result;
}
public string Get已提箱Str()
{
var result = "";
var ch = new CtnTotalHelper();
foreach (var item in bodyList)
{
if(item.PickupDate!= null)
ch.Add(item.Ctnall);
}
result = ch.GetStr();
return result;
}
public string Get未提箱Str()
{
var result = "";
var ch = new CtnTotalHelper();
foreach (var item in bodyList)
{
if (item.PickupDate == null)
ch.Add(item.Ctnall);
}
result = ch.GetStr();
return result;
}
public string Get已还箱Str()
{
var result = "";
var ch = new CtnTotalHelper();
foreach (var item in bodyList)
{
if (item.DropoffDate != null)
ch.Add(item.Ctnall);
}
result = ch.GetStr();
return result;
}
public string Get未还箱Str()
{
var result = "";
var ch = new CtnTotalHelper();
foreach (var item in bodyList)
{
if (item.DropoffDate == null)
ch.Add(item.Ctnall);
}
result = ch.GetStr();
return result;
}
}
}
}