using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using DS.WMS.Common.Enum;
using DS.WMS.Core.App.Entity;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.WmsModule.Dtos;
using DS.WMS.Core.WmsModule.Entity;
using DS.WMS.Core.WmsModule.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.WmsModule.Method;
public class WmsInDoService:IWmsInDoService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly IWmsCommonService _wmsCommonService;
///
///
///
///
public WmsInDoService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
_wmsCommonService = _serviceProvider.GetRequiredService();
}
public DataResult> GetListByPage(PageRequest request)
{
var companyId = user.GetCompanyId();
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.LeftJoin((a, b) => a.STOREHOUSE == b.AREACODE)
.Select((a, b) => new WmsInDoListPageModel()
{
STOREHOUSENAME = b.AREANAME //手动指定一列在自动映射
},
true)
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
public DataResult GetWmsInDoInfo(string id)
{
var gid = Guid.Parse(id);
var data = db.Queryable()
.LeftJoin((a, b) => a.STOREHOUSE == b.AREACODE)
.Where(a => a.WMSDOID == gid)
.Select((a, b) => new WmsInDoInfoViewModel()
{
STOREHOUSENAME = b.AREANAME //手动指定一列在自动映射
},
true)
.First();
return DataResult.Success(data);
}
public DataResult EditWmsInDoInfo(WmsInDoInput model)
{
// var companyId = user.GetCompanyId();
if (model.WMSDOID.ToString().IsNullOrEmpty())
{
// var exitInfo = db.Queryable().Where(x => x.MBLNO == model.MBLNO && x.CONTRACTNO == model.CONTRACTNO && x.ClientId == companyId).First();
// if (exitInfo.IsNotNull())
// {
// return DataResult.Failed("存在同一提单号合同号的预约单!");
// }
try
{
//开启事务
db.Ado.BeginTran();
var gid = Guid.NewGuid();
model.WMSDOID = gid;
var wmsInDo = model.Adapt();
wmsInDo.BSNO = NumUtil.GetBillNo(db,"WM02");
// wmsInDo.ClientId = companyId;
wmsInDo.BSSTATUS = false;
wmsInDo.BILLSTATUS = "1000";
wmsInDo.FEESTATUS = false;
// wmsInDo.CORPID = "Comcab2d43f60454327af30a131fc1d3abd";
if (model.List.Count()>0)
{
var record = model.List.First();
wmsInDo.GOODSNAME = record.GOODSNAME;
wmsInDo.GOODSCODE = record.GOODSCODE;
// wmsInDo.GOODSMODEL = record.GOODSMODEL;
wmsInDo.TRUCKNO = record.TRUCKNO;
}
db.Insertable(wmsInDo).ExecuteCommand();
#region 处理计划明细
if (model.List.Count()>0)
{
foreach (var item in model.List)
{
item.WMSDODETAILID = Guid.NewGuid();
item.WMSDOID = gid;
// item.WMSPLANID = gid;
var detail = item.Adapt();
db.Insertable(detail).ExecuteCommand();
}
}
#endregion
db.Ado.CommitTran();
return DataResult.Successed("添加成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("添加失败!" + ex);
}
}
else
{
try
{
var gid = (Guid)model.WMSDOID;
var exitInfo = db.Queryable().First(x => x.WMSDOID == gid);
if (exitInfo.BILLSTATUS == "1002" )
{
return DataResult.Failed("已审核通过,不能修改!");
}
if (exitInfo.BILLSTATUS == "1003" )
{
return DataResult.Failed("已执行入库,不能修改!");
}
// if (exitInfo.BSSTATUS == true )
// {
// return DataResult.Failed("已审核通过,不能修改!");
// }
//开启事务
db.Ado.BeginTran();
var doInfo = model.Adapt(exitInfo);
if (model.List.Count()>0)
{
var record = model.List.First();
doInfo.GOODSNAME = record.GOODSNAME;
doInfo.GOODSCODE = record.GOODSCODE;
// wmsInDo.GOODSMODEL = record.GOODSMODEL;
doInfo.TRUCKNO = record.TRUCKNO;
}
db.Updateable(doInfo).IgnoreColumns(ignoreAllNullColumns:true).IgnoreColumns(x=> new {x.ClientId,x.CORPID}).ExecuteCommand();
#region 处理入库确认明细
if (model.List.Count()>0)
{
//先删除 计划明细
var existGoods = db.Queryable().Where(x => x.WMSDOID == gid).Select(n=>n.WMSDODETAILID.ToString()).ToArray();
var inGoods = model.List.Select(x => x.WMSDODETAILID.ToString().ToUpper()).ToArray();
IEnumerable delGoods = existGoods.AsQueryable().Except(inGoods);
if (delGoods.Count()>0)
{
foreach (var item in delGoods)
{
var existInfo = db.Queryable()
.Where(x => x.WMSDOID == gid && x.WMSDODETAILID ==Guid.Parse(item)).First();
db.Deleteable(existInfo).ExecuteCommand();
}
}
//更新 计划明细
var updateList = model.List.Where(x => x.WMSDODETAILID != null && !delGoods.Contains(x.WMSDODETAILID.ToString())).ToList();
foreach (var item in updateList)
{
var detail = item.Adapt();
db.Updateable(detail).ExecuteCommand();
}
//追加 计划明细
var addList = model.List.Where(x => x.WMSDODETAILID == null).ToList();
foreach (var item in addList)
{
item.WMSDODETAILID = Guid.NewGuid();
item.WMSDOID = gid;
var detail = item.Adapt();
db.Insertable(detail).ExecuteCommand();
}
}
#endregion
db.Ado.CommitTran();
return DataResult.Successed("更新成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("更新失败!" + ex);
}
}
}
public DataResult GetWmsInDoGoodsList(string id)
{
var data = db.Queryable()
.LeftJoin((a, b) => a.AREACODE == b.AREACODE)
.Select()
.Mapper(it =>
{
//只能写在Select后面
it.EDITABLE = true;
})
.Where(a => a.WMSDOID == Guid.Parse(id)).ToList();
return DataResult.Successed("查询成功!", data);
}
public DataResult DelWmsInDo(string id)
{
var gid = Guid.Parse(id);
var wmsInDo = db.Queryable().First(x => x.WMSDOID == gid);
if (wmsInDo.BILLSTATUS == "1002" )
{
return DataResult.Failed("已审核通过,不能删除!");
}
if (wmsInDo.BILLSTATUS == "1003" )
{
return DataResult.Failed("已执行入库!");
}
try
{
//开启事务
db.Ado.BeginTran();
db.Deleteable(wmsInDo).ExecuteCommand();
var detail = db.Queryable().Where(x => x.WMSDOID == gid).ToList();
if (detail.Count()>0)
{
db.Deleteable(detail).ExecuteCommand();
}
db.Ado.CommitTran();
return DataResult.Successed("删除成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("删除失败!" + ex);
}
}
public DataResult AuditWmsInDo(string id)
{
var gid = Guid.Parse(id);
var exitInfo = db.Queryable().First(x => x.WMSDOID == gid);
if (exitInfo.BILLSTATUS == "1002" )
{
return DataResult.Failed("已审核通过,不能修改!");
}
if (exitInfo.BILLSTATUS == "1003" )
{
return DataResult.Failed("已执行入库!");
}
var detail = db.Queryable().Where(x => x.WMSDOID == gid);
if (detail.Count()==0)
{
return DataResult.Failed("执行明细不能为空!");
}
exitInfo.BILLSTATUS = "1002";
db.Updateable(exitInfo).ExecuteCommand();
return DataResult.Successed("审核成功");
}
public DataResult CancelAuditWmsInDo(string id)
{
var gid = Guid.Parse(id);
var exitInfo = db.Queryable().First(x => x.WMSDOID == gid);
if (exitInfo.BILLSTATUS == "1003" )
{
return DataResult.Failed("已执行入库!");
}
exitInfo.BILLSTATUS = "1000";
db.Updateable(exitInfo).ExecuteCommand();
return DataResult.Successed("取消审核成功");
}
public DataResult AccountWmsInDo(string id)
{
var gid = Guid.Parse(id);
var wmsInDo = db.Queryable().First(x => x.WMSDOID == gid);
if (wmsInDo.BILLSTATUS == "1000" )
{
return DataResult.Failed("未审核,不能执行!");
}
if (wmsInDo.BILLSTATUS == "1003" )
{
return DataResult.Failed("已执行入库!");
}
var detail = db.Queryable().Where(x => x.WMSDOID == gid).ToList();
if (detail.Count()==0)
{
return DataResult.Failed("执行明细不能为空!");
}
try
{
//开启事务
db.Ado.BeginTran();
Guid? WMSPLANID = Guid.Empty;
WMSPLANID = detail.First().WMSPLANID;
foreach (var item in detail)
{
var plandetail = db.Queryable().First(x => x.GID == item.WMSPLANDETAILID);
if (plandetail != null)
{
// var doPkgs = db.Queryable().Where(x => x.WMSPLANDETAILID == item.WMSPLANDETAILID)
// .Sum(x => x.PKGS);
var doPkgs= db.Queryable().Where(x => x.WMSPLANDETAILID == item.WMSPLANDETAILID)
.Sum(x => x.PKGS);
if (plandetail.PKGS == doPkgs)
{
plandetail.ISEXECUTE = true;
db.Updateable(plandetail).ExecuteCommand();
}
}
#region 初始化相关信息
// var comomService = new WmsCommonService();
var wms = _wmsCommonService.InitOpWms(wmsInDo,item).Data;
db.Insertable(wms).ExecuteCommand();
var wmsBase = _wmsCommonService.InitOpWmsBase(wms,item,plandetail).Data;
db.Insertable(wmsBase).ExecuteCommand();
var wmsPhysics = _wmsCommonService.InitOpWmsPhysics(wms,wmsInDo,item).Data;
db.Insertable(wmsPhysics).ExecuteCommand();
var wmsChange = _wmsCommonService.InitOpWmsChange(wms,wmsInDo,item).Data;
db.Insertable(wmsChange).ExecuteCommand();
var wmsPhysicsChange = _wmsCommonService.InitOpWmsPhysicsChange(wms.WMSID,wmsPhysics.WMSPHYSICSID,wmsInDo.DODATE,item).Data;
db.Insertable(wmsPhysicsChange).ExecuteCommand();
#endregion
}
#region 处理计划结束标识
if (WMSPLANID.IsNotNull())
{
var plan = db.Queryable().First(x => x.WMSPLANID == WMSPLANID);
if (!db.Queryable().Any(x=>x.WMSPLANID == WMSPLANID && x.ISEXECUTE == false))
{
plan.ISEND = true;
db.Updateable(plan).ExecuteCommand();
}
}
#endregion
wmsInDo.BILLSTATUS = "1003";
db.Updateable(wmsInDo).ExecuteCommand();
db.Ado.CommitTran();
return DataResult.Successed("执行成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("执行失败!" + ex);
}
}
}