123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using WMS.Core._02Entity;
- using WMS.Info;
- using WMS.Info.Models;
- using WMS.Util;
- namespace WMS.Core
- {
- public class AppCoreBLL
- {
- private bool IsSkip;
- public string token;
- public AppCoreBLL()
- {
- //token = (string)System.Runtime.Remoting.Messaging.CallContext.GetData("token");
- }
- public AppCoreBLL(bool skipOauth)
- {
- IsSkip = skipOauth;
- // token = (string)System.Runtime.Remoting.Messaging.CallContext.GetData("token");
- }
- private LoginUserInfo _LoginUser;
- public LoginUserInfo LoginUser
- {
- get
- {
- if (_LoginUser != null) return _LoginUser;
- if (IsSkip)
- {
- _LoginUser = new LoginUserInfo { UserNo = "wcs" };
- return _LoginUser;
- }
- if (string.IsNullOrWhiteSpace(token))
- {
- #if DEBUG
- _LoginUser = new LoginUserInfo { UserNo = "TEST" };
- # else
- throw SysExCore.ThrowToken();
- #endif
- }
- else
- {
- _LoginUser= LoginBLLCore.GetLoginUser(token);
- }
- return _LoginUser;
- }
- }
- private HttpHelper _httpHelper;
- public HttpHelper httpClinet
- {
- get
- {
- if (_httpHelper == null)
- _httpHelper = new HttpHelper(SysSetCore.GetSysSet().WCSApiUrl);
- return _httpHelper;
- }
- }
- /// <summary>
- /// 获取列表数据
- /// <summary>
- /// <returns></returns>
- protected List<FX_sod_det> GetList(string trayNo)
- {
- try
- {
- return SysDbCore.GetDbCtx().Queryable<FX_sod_det>().Where(it => it.sod_part == trayNo).ToList();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据点位获取类型
- /// <param name="keyValue">主键</param>
- /// <summary>
- /// <returns></returns>
- protected BASE_POINT GetPointInfo(string pointNo)
- {
- try
- {
- return SysDbCore.GetDbCtx().Queryable<BASE_POINT>().Where(it => it.F_isDelete == 0 && it.F_no == pointNo).First();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据类型获取空置点位
- /// <param name="keyValue">主键</param>
- /// <summary>
- /// <returns></returns>
- public BASE_POINT GetFreePointInfoByType(int type)
- {
- var point = SysDbCore.GetDbCtx().Queryable<BASE_POINT>().Where(it => it.F_isDelete == 0 && it.F_type == type && it.F_status == (int)EPointSatus.Idle).First();
- if (point == null)
- throw SysExCore.ThrowFailException("没有空闲的点位!");
- //point.F_status = (int)EPointSatus.Occupied;
- //SysDbCore.GetDbCtx().Updateable(point).UpdateColumns(it => new { it.F_status }).WhereColumns(it => it.F_no == point.F_no).ExecuteCommand();
- return point;
- }
- /// <summary>
- /// 根据类型获取空置货位
- /// <param name="keyValue">主键</param>
- /// <summary>
- /// <returns></returns>
- public BASE_LOCATION GetFreeLocationByType(int type)
- {
- var locEntity = SysDbCore.GetDbCtx().Queryable<BASE_LOCATION>().Where(it => it.F_isDelete == 0 && it.F_type == type && it.F_status == (int)EWareCellState.Empty).First();
- if (locEntity == null)
- throw SysExCore.ThrowFailException("没有闲置的货位!");
- return locEntity;
- }
- public ResInfo LocationGenerate(GenerateLocationRequest request)
- {
- SqlSugarClient ctx = SysDbCore.GetDbCtx();
- try
- {
- ctx.BeginTran();
- for (int x = request.StartLine.HasValue ? request.StartLine.Value : 1; x <= request.Line; x++)
- {
- for (int y = request.StartCell.HasValue ? request.StartCell.Value : 1; y <= request.Cell; y++)
- {
- if (request.SkinLineCell.Any(a => a.Key == x && a.Value == y))
- continue;
- var location = new BASE_LOCATION
- {
- F_addTime = DateTime.Now,
- F_addUserNo = "admin",
- F_editTime = DateTime.Now,
- F_cell = y,
- F_depth = 1,
- F_isDelete = 0,
- F_layer = request.layer,
- F_line = x,
- F_name = "货位",
- F_no = $"{x.ToString().PadLeft(2, '0') }-{y.ToString().PadLeft(2, '0')}-{request.layer.ToString().PadLeft(2, '0')}",
- F_status = 1,
- F_type = 1,
- F_warehouseNo = SysSetCore.GetSysSet().DefaultWarehouseNo
- };
- ctx.Insertable(location).ExecuteCommand();
- }
- }
- ctx.CommitTran();
- }
- catch (Exception ex)
- {
- ctx.RollbackTran();
- throw ex;
- }
- finally
- {
- ctx.Dispose();
- }
- return SysExCore.GetResSucc();
- }
- /// <summary>
- /// 获取出库单类型
- /// <summary>
- /// <returns></returns>
- public ResInfo GetStockOutType()
- {
- try
- {
- var data = SysDbCore.GetDbCtx().Queryable<SYS_DATA>().Where(it => it.F_PNO == "StockOutOrderType").Select<dynamic>("f_no,f_name,f_num").ToList();
- return SysExCore.GetResSucc(data: data);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 转仓类型
- /// <summary>
- /// <returns></returns>
- public ResInfo GetTransferType()
- {
- try
- {
- var data = SysDbCore.GetDbCtx().Queryable<SYS_DATA>().Where(it => it.F_PNO == "ETransferType").Select<dynamic>("f_no,f_name,f_num").ToList();
- return SysExCore.GetResSucc(data: data);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 更新库存及添加库存事务
- /// </summary>
- /// <param name="list"></param>
- /// <param name="db"></param>
- public void UpdateInventory(List<BILL_INVENTORY> inv, SqlSugarClient db)
- {
- if (!inv.Any()) return;
- var strSql = new StringBuilder();
- db.Ado.ExecuteCommand(" SELECT * INTO #INVENTORY FROM dbo.BILL_INVENTORY WHERE 1=2");
- var data = new List<SugarParameter>();
- foreach (var item in inv)
- {
- var param = GetParameter(item);
- db.Ado.ExecuteCommand(@" INSERT INTO #INVENTORY
- (
- F_warehouseNo,
- F_projectNo,
- F_matNo,
- F_matName,
- F_matType,
- F_quantity,
- F_lockQty,
- F_trayNo,
- F_batchNo,
- F_memo,
- F_isBonded,
- F_UID,
- F_addUserNo,
- F_addTime,
- F_editUserNo,
- F_editTime,
- F_boxNo,
- F_productDate
- )
- VALUES
- ( @F_warehouseNo,
- @F_projectNo,
- @F_matNo,
- @F_matName,
- @F_matType,
- @F_quantity,
- @F_lockQty,
- @F_trayNo,
- @F_batchNo,
- @F_memo,
- @F_isBonded,
- @F_UID,
- @F_addUserNo,
- @F_addTime,
- @F_editUserNo,
- @F_editTime,
- @F_boxNo,
- @F_productDate
- )", param);
- }
- strSql.Append($@"
-
-
- MERGE INTO dbo.BILL_INVENTORY A
- USING #INVENTORY C
- ON (
- A.F_trayNo = C.F_trayNo
- AND A.F_matNo = C.F_matNo
- AND A.F_matType = C.F_matType
- AND isnull(A.F_boxNo,'') = isnull(C.F_boxNo,'')
- AND isnull(A.F_UID,'') =isnull(C.F_UID,'')
- )
- WHEN MATCHED THEN
- UPDATE SET A.F_quantity = IIF(C.F_quantity>0 AND C.F_matType=1,C.F_quantity,A.F_quantity + C.F_quantity),
-
- A.F_editTime = GETDATE(),
- A.F_editUserNo = C.F_editUserNo
- WHEN NOT MATCHED THEN
- INSERT
- (
- F_warehouseNo,
- F_projectNo,
- F_matNo,
- F_matName,
- F_matType,
- F_quantity,
- F_lockQty,
- F_trayNo,
- F_batchNo,
- F_memo,
- F_isBonded,
- F_UID,
- F_addUserNo,
- F_addTime,
- F_productDate,
- F_boxNo
- )
- VALUES
- (C.F_warehouseNo, C.F_projectNo, C.F_matNo, C.F_matName, C.F_matType, C.F_quantity, 0, C.F_trayNo, C.F_batchNo,
- C.F_memo, C.F_isBonded, C.F_UID, C.F_addUserNo, GETDATE(),C.F_productDate,C.F_boxNo)
- OUTPUT Inserted.F_no,
- Inserted.F_warehouseNo,
- Inserted.F_projectNo,
- C.F_matNo,
- C.F_matName,
- C.F_matType,
- ISNULL(DELETED.F_quantity, 0),
- ISNULL(DELETED.F_lockQty, 0),
- ISNULL(Inserted.F_quantity, 0),
- ISNULL(Inserted.F_lockQty, 0),
- DELETED.F_trayNo,
- C.F_trayNo,
- C.F_batchNo,
- C.F_memo,
- C.F_isBonded,
- C.F_UID,
- C.F_addUserNo,
- C.F_addTime,
- C.F_boxNo,
- C.F_productDate
- INTO dbo.BILL_INVENTORYTRANSACTION
- (
- [F_inventoryNo],
- [F_warehouseNo],
- [F_projectNo],
- [F_matNo],
- [F_matName],
- [F_matType],
- [F_sourceQuantity],
- [F_sourceLockQty],
- [F_targetQuantity],
- [F_targetLockQty],
- [F_sourceTrayNo],
- [F_targetTrayNo],
- [F_batchNo],
- [F_memo],
- [F_isBonded],
- [F_UID],
- [F_addUserNo],
- [F_addTime],
- F_boxNo,
- F_productDate
- );
- DELETE dbo.BILL_INVENTORY WHERE EXISTS(SELECT 1 FROM #INVENTORY tmp WHERE tmp.F_matNo=BILL_INVENTORY.F_matNo AND tmp.F_trayNo=BILL_INVENTORY.F_trayNo AND tmp.F_boxNo=BILL_INVENTORY.F_boxNo) AND BILL_INVENTORY.F_quantity=0
- DROP TABLE #INVENTORY;");
- db.Ado.ExecuteCommand(strSql.ToString());
- }
- /// <summary>
- /// 返回基于该对象的SugarParameters的数组
- /// </summary>
- /// <param name=""></param>
- /// <returns></returns>
- public static SugarParameter[] GetParameter<T>(T Info) where T : class
- {
- Type type = typeof(T);
- object obj = Activator.CreateInstance(type);
- // 获取所有属性。
- PropertyInfo[] properties = type.GetProperties();
- SugarParameter[] arParms = new SugarParameter[properties.Length];
- for (int i = 0; i < properties.Length; i++)
- {
- arParms[i] = new SugarParameter($"@{properties[i].Name}", properties[i].GetValue(Info));
- }
- return arParms;
- }
- /// <summary>
- /// 出库下一个点位分配
- /// </summary>
- /// <param name="ctx"></param>
- /// <returns></returns>
- public string StockOutNextPointAllot(SqlSugarClient ctx)
- {
- var point = ctx.Queryable<BASE_POINT>().First(c => c.F_type == (int)EPointType.CKXYGDZ);
- var pointTask = ctx.Queryable<WMS_TASK>().Where(c => ((c.F_posidNext == point.F_no || c.F_posidNext == point.F_cachePoint) && c.F_taskStatus == (int)ETaskStatus.NotIssued || c.F_taskStatus == (int)ETaskStatus.NotExecute || c.F_taskStatus == (int)ETaskStatus.Executing));
- if (pointTask.Any())
- {
- if (pointTask.Any(a => a.F_posidNext == point.F_no))
- {
- if (!pointTask.Any(a => a.F_posidNext == point.F_cachePoint))
- {
- return point.F_cachePoint;
- }
- }
-
- }
- return point.F_no;
- }
- }
- }
|