using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.Core.Helpers; using DS.Module.UserModule; using DS.WMS.Common.Enum; using DS.WMS.Core.App.Dtos; using DS.WMS.Core.App.Interface; using DS.WMS.Core.BaseInfo.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 Mapster; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using SqlSugar; using OP_WMS_STOREHOUSE = DS.WMS.Core.App.Entity.OP_WMS_STOREHOUSE; using OP_WMS_STOREHOUSE_AREA = DS.WMS.Core.App.Entity.OP_WMS_STOREHOUSE_AREA; namespace DS.WMS.Core.App.Method; /// /// /// public class AppService : IAppService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; /// /// /// /// public AppService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); } /// /// App用户登录 /// /// /// public DataResult AppUserLogin(AppLoginModel model) { var user = db.Queryable().First(x => x.UserCode == model.UserCode); if (user == null) { return DataResult.Failed("账号不存在,请检查!"); } if (user.Password != model.Password) { return DataResult.Failed("密码错误!"); } var data = db.Queryable() .Where(a => a.Id == user.Id) .Select(a => new CurrentUserViewModel { UserId = a.Id.ToString(), UserCode = a.UserCode, UserName = a.UserName, CompanyId = a.CompanyId.ToString(), // CompanyName = a.c }).First(); var tokenModel = new JwtHelper.JwtTokenModel { Uid = data.UserId.ToString(), CompanyId = data.CompanyId, GID = user.GID, TenantId = user.TenantId, }; data.Token = JwtHelper.Encrypt(tokenModel); // data.Token = JwtHelper.GetJWT(data.ObjToString()); return DataResult.Success("登录成功!", data); } public DataResult> GetStockInPlanList(StockInPlanQueryModel model) { var trucklist = db.Queryable().Select(a => new { a.WMSPLANID, a.TRUCKNO }) .Where(a => a.TRUCKNO != "" && a.TRUCKNO != null).Distinct(); var data = db.Queryable() .LeftJoin(trucklist, (a, b) => a.WMSPLANID == b.WMSPLANID) .LeftJoin((a, b, c) => a.STOREHOUSE == c.AREACODE) .Where((a, b, c) => a.AuditStatus == AuditStatus.Approve.ToEnumInt() && a.ISEND == false) .Select((a, b, c) => new StockInPlanListViewModel() { STOREHOUSENAME = c.AREANAME //手动指定一列在自动映射 }, true) .WhereIF(!model.MBLNO.IsNullOrEmpty(), a => a.MBLNO.Contains(model.MBLNO)) .WhereIF(!model.TRUCKNO.IsNullOrEmpty(), a => a.TRUCKNO.Contains(model.TRUCKNO)) .ToList(); // .Select() if (data.Count() == 0) { return DataResult>.Failed("未检索到数据!"); } return DataResult>.Success("获取数据成功", data); } public DataResult GetStockInSummaryInfo(StockInPlanQueryModel model) { var data = new StockInPlanViewModel(); var info = db.Queryable().First(x => x.WMSPLANID == model.WMSPLANID); var store = db.Queryable().First(x => x.AREACODE == info.STOREHOUSE); data.WMSPLANID = model.WMSPLANID; data.TRUCKNO = model.TRUCKNO; data.MBLNO = info.MBLNO; data.STOREHOUSENAME = store.AREANAME; // var goods = db.Queryable().Where(a=>a.WMSPLANID == model.WMSPLANID) // .LeftJoin( // db.SqlQueryable( // "select WMSPLANDETAILID, sum(PKGS) AS PKGS,SUM(STORAGEUNITCOUNT) AS STORAGEUNITCOUNT from OP_WMS_IN_DO_GOODS WHERE WMSPLANDETAILID is not null GROUP BY WMSPLANDETAILID"), // (a, b) => a.GID == b.WMSPLANDETAILID) // .Select((a, b) => new StockInPlanGoodsViewModel() // { // PKGS = a.PKGS, // STORAGEUNITCOUNT = a.STORAGEUNITCOUNT, // DOSTORAGEUNITCOUNT = b.STORAGEUNITCOUNT, // DOPKGS = b.PKGS //手动指定一列在自动映射 // }, // true) // // .Select() // // .Where(a => a.STORAGEUNITCOUNT > b.DOSTORAGEUNITCOUNT) // .ToList(); // // data.GoodsList = goods.Where(x => x.STORAGEUNITCOUNT > x.DOSTORAGEUNITCOUNT).ToList(); // data.GoodsList = goods.ToList(); data.AreaList = db.Queryable() .LeftJoin((a, b) => a.Pid == b.Id) .Where((a, b) => b.AREACODE == info.STOREHOUSE) .Select(a => new AreaListViewModel { text = a.AREANAME, value = a.AREACODE }) .ToList(); return DataResult.Successed("获取数据成功", data); } public DataResult GetStockInGoodsInfo(StockGoodsQueryModel model) { var goods = db.Queryable().Where(a => a.WMSPLANID == model.WMSPLANID) .LeftJoin((a, b) => a.GoodsId == b.GID && b.ISSTOP ==false) .Where((a, b) => b.PinYinCode.Contains(model.Code)) .LeftJoin( db.SqlQueryable( "select WMSPLANDETAILID, sum(PKGS) AS PKGS,SUM(STORAGEUNITCOUNT) AS STORAGEUNITCOUNT from OP_WMS_IN_DO_GOODS WHERE WMSPLANDETAILID is not null GROUP BY WMSPLANDETAILID"), (a, b, StockInDoGoods) => a.GID == StockInDoGoods.WMSPLANDETAILID) .Select((a, b, StockInDoGoods) => new StockInPlanGoodsViewModel() { PKGS = a.PKGS, STORAGEUNITCOUNT = a.STORAGEUNITCOUNT, DOSTORAGEUNITCOUNT = StockInDoGoods.STORAGEUNITCOUNT, DOPKGS = StockInDoGoods.PKGS //手动指定一列在自动映射 }, true) // .Select() // .Where(a => a.STORAGEUNITCOUNT > b.DOSTORAGEUNITCOUNT) .First(); return DataResult.Successed("获取数据成功", goods); } public DataResult StockInBatch(StockInPlanGoodsBatchInput model) { var userId = user.UserId; if (model.Goods.Count == 0) { return DataResult.Failed("执行明细不能为空!"); } var WMSPLANID = model.WMSPLANID; //检测入库计划状态 var plan = db.Queryable().Where(s => s.WMSPLANID == WMSPLANID).First(); if (plan == null) { return DataResult.Failed("入库计划不存在!"); } if (plan.ISEND == true) { return DataResult.Failed("入库计划已完成!"); } try { //开启事务 db.Ado.BeginTran(); var doId = Guid.NewGuid(); //var stockDo = plan.MapTo(); var stockDo = plan.Adapt(); stockDo.BSNO = NumUtil.GetBillNo(db, "WM02"); stockDo.BILLSTATUS = "1000"; stockDo.BSSTATUS = false; stockDo.TRUCKNO = model.TRUCKNO; stockDo.MBLNO = model.MBLNO; stockDo.WMSDOID = doId; stockDo.GOODSOWNER = stockDo.CUSTOMERNAME; stockDo.APCLIENTWMSOUT = stockDo.CUSTOMERNAME; stockDo.ARCLIENTWMSOUT = stockDo.CUSTOMERNAME; stockDo.DODATE = DateTime.Now; stockDo.FEESTARTDATE = DateTime.Now; stockDo.CORPID = user.GetCompanyId(); // stockDo.CreateID = Guid.Parse(userId); // stockDo.Creator = userinfo.UserTrueName; // stockDo.CreateDate = DateTime.Now; //写入OP_WMS_IN_DO db.Insertable(stockDo).ExecuteCommand(); // TODO 入库执行费率 var doGoodsList = new List(); foreach (var item in model.Goods) { var planGood = db.Queryable().Where(s => s.GID == item.GID).First(); var doGoods = planGood.Adapt(); doGoods.WMSDODETAILID = Guid.NewGuid(); doGoods.WMSDOID = doId; doGoods.WMSPLANID = WMSPLANID; doGoods.WMSPLANDETAILID = item.GID; doGoods.MBLNO = plan.MBLNO; doGoods.PKGS = item.PKGS; doGoods.ACCEPT_PKGS = item.PKGS; doGoods.ACCEPT_KGS = item.KGS; doGoods.ACCEPT_CBM = item.CBM; doGoods.ACCEPT_NETWEIGHT = item.NETWEIGHT; doGoods.ACCEPT_STORAGEUNITCOUNT = item.STORAGEUNITCOUNT; // doGoods.ACCEPT_RULEUNITCOUNT = item.RULEUNITCOUNT; doGoods.STOREHOUSE = stockDo.STOREHOUSE; doGoods.ACCEPT_STOREHOUSE = stockDo.STOREHOUSE; //更新库位信息 doGoods.AREACODE = item.AREACODE; //更新原箱号 doGoods.OLD_CNTRNO = item.OLD_CNTRNO; // doGoods.CNTRNO = item.CNTRNO; //添加到List doGoodsList.Add(doGoods); //最后处理 计划明细 ISEXECUTE 是否执行入库执行标识 // planGood.ISEXECUTE = true; // db.Updateable(planGood).ExecuteCommand(); } //写入 OP_WMS_IN_DO_GOODS db.Insertable(doGoodsList).ExecuteCommand(); db.Ado.CommitTran(); return DataResult.Successed("入库确认成功!"); } catch (Exception ex) { db.Ado.RollbackTran(); return DataResult.Failed("入库确认失败!" + ",请联系管理员!"); } } public DataResult GetStockOutPlanList(StockOutPlanQueryModel model) { var data = db.Queryable() .Where(a => a.AuditStatus == AuditStatus.Approve.ToEnumInt() && a.ISEND == false) .LeftJoin((a, b) => a.STOREHOUSE == b.AREACODE) .LeftJoin( db.SqlQueryable( "select DISTINCT TRUCKNO, WMSPLANID from OP_WMS_OUT_PLAN_DETAIL WHERE TRUCKNO is not null"), (a, b, c) => a.WMSPLANID == c.WMSPLANID) .Select((a, b, c) => new StockOutPlanListViewModel() { MBLNO = a.MBLNO, STOREHOUSENAME = b.AREANAME, //手动指定一列在自动映射 TRUCKNO = c.TRUCKNO }, true) .WhereIF(!model.MBLNO.IsNullOrEmpty(), a => a.MBLNO.Contains(model.MBLNO)) .WhereIF(!model.TRUCKNO.IsNullOrEmpty(), a => a.TRUCKNO.Contains(model.TRUCKNO)) .ToList(); if (data.Count() == 0) { return DataResult.Failed("未检索到数据!"); } return DataResult.Successed("获取数据成功", data); } public DataResult GetStockOutSummaryInfo(StockOutPlanQueryModel model) { var data = new StockOutPlanViewModel(); var info = db.Queryable().First(x => x.WMSPLANID == model.WMSPLANID); var store = db.Queryable().First(x => x.AREACODE == info.STOREHOUSE); data.WMSPLANID = model.WMSPLANID; data.TRUCKNO = model.TRUCKNO; data.MBLNO = info.MBLNO; data.STOREHOUSENAME = store.AREANAME; // data.AreaList = db.Queryable() // .LeftJoin((a, b) => a.Pid == b.Id) // .Where((a, b) => b.AREACODE == info.STOREHOUSE) // .Select(a => new AreaListViewModel // { // text = a.AREANAME, // value = a.AREACODE // }) // .ToList(); return DataResult.Successed("获取数据成功", data); } public DataResult GetStockOutGoodsInfo(StockGoodsQueryModel model) { var goods = db.Queryable().Where(a => a.WMSPLANID == model.WMSPLANID) .LeftJoin((a, b) => a.GoodsId == b.GID && b.ISSTOP ==false) .Where((a, b) => b.PinYinCode.Contains(model.Code)) .LeftJoin( db.SqlQueryable( "select WMSPLANDETAILID, sum(PKGS) AS PKGS,SUM(STORAGEUNITCOUNT) AS STORAGEUNITCOUNT from OP_WMS_OUT_DO_GOODS WHERE WMSPLANDETAILID is not null GROUP BY WMSPLANDETAILID"), (a, b, StockDoGoods) => a.WMSOUTDETAILID == StockDoGoods.WMSPLANDETAILID) .LeftJoin((a, b, StockDoGoods, d) => a.AREACODE == d.AREACODE) .Select((a, b, StockDoGoods, d) => new StockOutPlanGoodsViewModel() { PKGS = a.PKGS, STORAGEUNITCOUNT = a.STORAGEUNITCOUNT, DOSTORAGEUNITCOUNT = StockDoGoods.STORAGEUNITCOUNT, DOPKGS = StockDoGoods.PKGS, //手动指定一列在自动映射 AREANAME = d.AREANAME }, true) .First(); if (goods.IsNull()) { return DataResult.Failed("未检索到数据!"); } return DataResult.Successed("获取数据成功", goods); } public DataResult StockOutBatch(StockOutPlanGoodsBatchInput model) { var userId = user.UserId; if (model.Goods.Count == 0) { return DataResult.Failed("执行明细不能为空!"); } var WMSPLANID = model.WMSPLANID; //检测出库计划状态 var plan = db.Queryable().Where(s => s.WMSPLANID == WMSPLANID).First(); if (plan == null) { return DataResult.Failed("出库计划不存在!"); } if (plan.ISEND == true) { return DataResult.Failed("出库计划已完成!"); } try { //开启事务 db.Ado.BeginTran(); var doId = Guid.NewGuid(); //var stockDo = plan.MapTo(); var stockDo = plan.Adapt(); stockDo.WMSNO = NumUtil.GetBillNo(db, "WM04"); stockDo.BILLSTATUS = "1010"; stockDo.StockOutType = 0;//正常出库 stockDo.BSSTATUS = false; stockDo.TRUCKNO = model.TRUCKNO; stockDo.MBLNO = model.MBLNO; stockDo.ISEND = false; stockDo.FEESTATUS = false; stockDo.BSSTATUS = false; stockDo.WMSDOID = doId; var now = DateTime.Now; stockDo.DODATE = new DateTime(now.Year, now.Month, now.Day); stockDo.CORPID = user.GetCompanyId(); stockDo.CUSTOMNO_IMPORT = plan.CUSTOMNO; //写入OP_WMS_OUT_DO db.Insertable(stockDo).ExecuteCommand(); // TODO 出库执行费率 var doGoodsList = new List(); foreach (var item in model.Goods) { var planGood = db.Queryable() .Where(s => s.WMSOUTDETAILID == item.WMSOUTDETAILID).First(); var doGoods = planGood.Adapt(); doGoods.WMSDODETAILID = Guid.NewGuid(); doGoods.WMSDOID = doId; doGoods.WMSPLANDETAILID = item.WMSOUTDETAILID; // doGoods.PKGS = item.PKGS; doGoods.STORAGEUNITCOUNT = item.STORAGEUNITCOUNT; doGoods.STOREHOUSE = stockDo.STOREHOUSE; doGoodsList.Add(doGoods); } //写入 OP_WMS_OUT_DO_GOODS db.Insertable(doGoodsList).ExecuteCommand(); db.Ado.CommitTran(); return DataResult.Successed("出库确认成功!"); } catch (Exception ex) { db.Ado.RollbackTran(); return DataResult.Failed("出库确认失败!" + ",请联系管理员!"); } } public DataResult GetClearancePlanList(ClearancePlanQueryModel model) { var data = db.Queryable() .Where(a => a.AuditStatus == AuditStatus.Approve.ToEnumInt() && a.ISEND == false) .LeftJoin((a, b) => a.STOREHOUSE == b.AREACODE) .LeftJoin( db.SqlQueryable( "select DISTINCT TRUCKNO, WMSPLANID from OP_WMS_CLEARANCE_DETAIL WHERE TRUCKNO is not null"), (a, b, c) => a.WMSPLANID == c.WMSPLANID) .Select((a, b, c) => new ClearancePlanListViewModel() { MBLNO = a.MBLNO, STOREHOUSENAME = b.AREANAME, //手动指定一列在自动映射 TRUCKNO = c.TRUCKNO }, true) .WhereIF(model.Type == 0, a => a.IsClearStore == false) .WhereIF(model.Type == 1, a => a.IsClearStore == true) .WhereIF(!model.MBLNO.IsNullOrEmpty(), a => a.MBLNO.Contains(model.MBLNO)) .WhereIF(!model.TRUCKNO.IsNullOrEmpty(), a => a.TRUCKNO.Contains(model.TRUCKNO)) .Mapper(it => { it.MoveStoreHouseName = db.Queryable() .First(x => x.AREACODE == it.MoveStoreHouse)?.AREANAME; }) .ToList(); if (data.Count() == 0) { return DataResult.Failed("未检索到数据!"); } return DataResult.Successed("获取数据成功", data); } public DataResult GetClearanceSummaryInfo(ClearancePlanQueryModel model) { var data = new WmsSummaryInfo(); var info = db.Queryable().First(x => x.WMSPLANID == model.WMSPLANID); var store = db.Queryable().First(x => x.AREACODE == info.STOREHOUSE); data.WMSPLANID = model.WMSPLANID; data.TRUCKNO = model.TRUCKNO; data.MBLNO = info.MBLNO; data.STOREHOUSENAME = store.AREANAME; data.AreaList = db.Queryable() .LeftJoin((a, b) => a.Pid == b.Id) .Where((a, b) => b.AREACODE == info.MoveStoreHouse) .Select(a => new AreaListViewModel { text = a.AREANAME, value = a.AREACODE }) .ToList(); return DataResult.Successed("获取数据成功", data); } public DataResult GetClearanceGoodsInfo(StockGoodsQueryModel model) { var goods = db.Queryable().Where(a => a.WMSPLANID == model.WMSPLANID) .LeftJoin((a, b) => a.GoodsId == b.GID && b.ISSTOP ==false) .Where((a, b) => b.PinYinCode.Contains(model.Code)) .LeftJoin( db.SqlQueryable( "select WMSPLANDETAILID, sum(PKGS) AS PKGS,SUM(STORAGEUNITCOUNT) AS STORAGEUNITCOUNT from OP_WMS_OUT_DO_GOODS WHERE WMSPLANDETAILID is not null GROUP BY WMSPLANDETAILID"), (a, b, StockDoGoods) => a.WMSCLEARANCEDETAILID == StockDoGoods.WMSPLANDETAILID) .LeftJoin((a, b, StockDoGoods, d) => a.AREACODE == d.AREACODE) .Select((a, b, StockDoGoods, d) => new ClearanceOutPlanGoodsViewModel() { PKGS = a.PKGS, STORAGEUNITCOUNT = a.STORAGEUNITCOUNT, DOSTORAGEUNITCOUNT = StockDoGoods.STORAGEUNITCOUNT, DOPKGS = StockDoGoods.PKGS, //手动指定一列在自动映射 AREANAME = d.AREANAME }, true) .First(); if (goods.IsNull()) { return DataResult.Failed("未检索到数据!"); } return DataResult.Successed("获取数据成功", goods); } public DataResult ClearanceOutBatch(ClearanceOutPlanGoodsBatchInput model) { var userId = user.UserId; if (model.Goods.Count == 0) { return DataResult.Failed("执行明细不能为空!"); } var WMSPLANID = model.WMSPLANID; //检测清关出库计划状态 var plan = db.Queryable().Where(s => s.WMSPLANID == WMSPLANID).First(); if (plan == null) { return DataResult.Failed("清关出库计划不存在!"); } if (plan.ISEND == true) { return DataResult.Failed("清关出库计划已完成!"); } try { //开启事务 db.Ado.BeginTran(); var doId = Guid.NewGuid(); //var stockDo = plan.MapTo(); var stockDo = plan.Adapt(); stockDo.WMSNO = NumUtil.GetBillNo(db, "WM04"); stockDo.StockOutType = 1;//清关出库 stockDo.BILLSTATUS = "1010"; stockDo.BSSTATUS = false; stockDo.TRUCKNO = model.TRUCKNO; stockDo.MBLNO = model.MBLNO; stockDo.ISEND = false; stockDo.FEESTATUS = false; stockDo.BSSTATUS = false; stockDo.WMSDOID = doId; var now = DateTime.Now; stockDo.DODATE = new DateTime(now.Year, now.Month, now.Day); stockDo.CORPID = user.GetCompanyId(); stockDo.CUSTOMNO_IMPORT = plan.CUSTOMNO; //写入OP_WMS_OUT_DO db.Insertable(stockDo).ExecuteCommand(); // TODO 出库执行费率 var doGoodsList = new List(); foreach (var item in model.Goods) { var planGood = db.Queryable() .Where(s => s.WMSCLEARANCEDETAILID == item.WMSCLEARANCEDETAILID).First(); var doGoods = planGood.Adapt(); doGoods.WMSDODETAILID = Guid.NewGuid(); doGoods.WMSDOID = doId; doGoods.WMSPLANDETAILID = item.WMSCLEARANCEDETAILID; // doGoods.PKGS = item.PKGS; doGoods.STORAGEUNITCOUNT = item.STORAGEUNITCOUNT; doGoods.STOREHOUSE = stockDo.STOREHOUSE; doGoodsList.Add(doGoods); } //写入 OP_WMS_OUT_DO_GOODS db.Insertable(doGoodsList).ExecuteCommand(); db.Ado.CommitTran(); return DataResult.Successed("清关出库确认成功!"); } catch (Exception ex) { db.Ado.RollbackTran(); return DataResult.Failed("清关出库确认失败!" + ",请联系管理员!"); } } public DataResult ClearanceMoveBatch(ClearanceMovePlanGoodsBatchInput model) { var userId = user.UserId; if (model.Goods.Count == 0) { return DataResult.Failed("执行明细不能为空!"); } var WMSPLANID = model.WMSPLANID; //检测清关出库计划状态 var plan = db.Queryable().Where(s => s.WMSPLANID == WMSPLANID).First(); if (plan == null) { return DataResult.Failed("清关移库计划不存在!"); } if (plan.ISEND == true) { return DataResult.Failed("清关移库计划已完成!"); } var stockids = model.Goods.Select(x => x.WMSPHYSICSID).ToList(); var stocklist = db.Queryable().Where(x => stockids.Contains(x.WMSPHYSICSID)).ToList(); // foreach (var item in model.Goods) // { // var stockinfo = stocklist.Where(x => x.WMSPHYSICSID == item.WMSPHYSICSID).First(); // // if (item.STORAGEUNITCOUNT > stockinfo.STORAGEUNITCOUNT) // { // return DataResult.Failed("清关计划明细商品[" + item.GOODSNAME +"]计费数量:" + stockinfo.STORAGEUNITCOUNT +" 不足,不能审批!"); // } // } try { //开启事务 db.Ado.BeginTran(); #region 处理锁库数量 foreach (var item in model.Goods) { var wms = db.Queryable().First(x => x.WMSID == item.WMSID); wms.BILLTYPE = "0"; //改为非保税 db.Updateable(wms).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); var stocking = stocklist.Where(x => x.WMSPHYSICSID == item.WMSPHYSICSID).First(); stocking.STOREHOUSE = plan.MoveStoreHouse; stocking.AREACODE = item.MOVEAREACODE; stocking.STORAGEUNITCOUNT += item.STORAGEUNITCOUNT; stocking.LOCKSTORAGEUNITCOUNT -= item.STORAGEUNITCOUNT; db.Updateable(stocking).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); //处理执行标识 var detail = db.Queryable() .First(x => x.WMSCLEARANCEDETAILID == item.WMSCLEARANCEDETAILID); detail.ISEXECUTE = true; db.Updateable(detail).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); } #endregion if (!db.Queryable().Any(x=>x.WMSPLANID == WMSPLANID && x.ISEXECUTE == false)) { plan.ISEND = true; db.Updateable(plan).ExecuteCommand(); } db.Ado.CommitTran(); return DataResult.Successed("清关移库确认成功!"); } catch (Exception ex) { db.Ado.RollbackTran(); return DataResult.Failed("清关移库确认失败!" + ",请联系管理员!"); } } }