using log4net; using Microsoft.Extensions.Caching.Memory; using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Web.Http; using UFIDA.U8.Framework.Api.ControlObject.Common; using UFIDA.U8.Framework.Api.ControlObject.Entity; using UFIDA.U8.Framework.Api.ControlObject.Json; using UFIDA.U8.Framework.Api.ControlObject.U8M; using UFIDA.U8.Framework.Api.Entity; using UFIDA.U8.Framework.Api.Unit; namespace UFIDA.U8.Framework.Api.Controllers.U8 { /// /// 生产订单 /// public class MOController : ApiController { /// /// 日志组件 /// private readonly ILog _logInfo = LogManager.GetLogger("logInfo"); private static readonly MemoryCache _cache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(10) }); /// /// 查询特定单据部分 /// /// 系统号 /// 单据类型编码 /// 查询条件 /// 查询所属区域 H:表头,B:表体 /// /// [HttpPost] [Route("api/U8/MO/{cCardNum}/Load")] [Route("api/U8/MO/{cCardNum}/Load/{cCardPostion}")] public async Task> Load(string cAccId, string cCardNum, [FromBody] Dictionary input, string cCardPostion = "H") { var workId = SnowFlakeSingle.Instance.NextId(); try { _logInfo.Debug($"作业Id:{workId}-----单据:{cCardNum},操作:查询,对接账套:{cAccId},条件:{input.ToJson()}"); if (!_cache.TryGetValue($"U8_{cAccId}", out BaseLogin baseLogin)) { _logInfo.Debug($"作业Id:{workId}-----未获取到缓存的登录信息,正在重新构建"); baseLogin = new BaseLogin(); var param = await CommUnit.GetLoginParamter(cAccId); if (param == null) { throw new Exception("未能加载指定系统号的登录信息"); } baseLogin.Init(param, false); _cache.Clear(); _cache.Set($"U8_{cAccId}", baseLogin); } var vouchers = CommonHelper.GetVouchers(baseLogin.Connection, cCardNum); if (vouchers == null) { throw new Exception("未能获取到单据配置信息"); } var db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConfigId = cAccId, ConnectionString = baseLogin.UFDataConnstringForNet, IsAutoCloseConnection = true, MoreSettings = new ConnMoreSettings() { IsWithNoLockQuery = true } }); var cBTTblName = vouchers.cBTTblName; var cBWTblName = vouchers.cBWTblName; var voucherIdFieldName = vouchers.voucherIdFieldName; var vouchersIdFieldName = ""; var tableName = cBTTblName; var colsHeadList = new List(); var colsBodyList = new List(); var bExtHead = false; var bExtBody = false; if (cCardPostion == "H") { tableName = cBTTblName; } if (cCardPostion == "B") { tableName = cBWTblName; } if (cCardPostion == "L") { tableName = cBWTblName; } var cDefWhere = vouchers.cDefWhere; var query = db.Queryable().AS(cBTTblName, cBTTblName); if (!string.IsNullOrWhiteSpace(cBWTblName)) { query.AddJoinInfo(cBWTblName, cBWTblName, $"{cBTTblName}.[{voucherIdFieldName}]={cBWTblName}.[{voucherIdFieldName}]"); var bodyCols = db.DbMaintenance.GetColumnInfosByTableName(cBWTblName); if (bodyCols != null && bodyCols.Count > 0) { colsBodyList.AddRange(from col in bodyCols where !col.DbColumnName.ToLower().Contains("ufts") && col.DbColumnName.ToLower() != voucherIdFieldName.ToLower() select $"{cBWTblName}.[{col.DbColumnName.ToLower()}]"); if (cCardPostion=="B" && !colsBodyList.Contains(voucherIdFieldName.ToLower())) { colsBodyList.Add($"{cBWTblName}.[{voucherIdFieldName.ToLower()}]"); } } } var headCols = db.DbMaintenance.GetColumnInfosByTableName(cBTTblName); if (headCols != null && headCols.Count > 0) { colsHeadList.AddRange(from col in headCols where !col.DbColumnName.ToLower().Contains("ufts") select $"{cBTTblName}.[{col.DbColumnName}]"); } if (db.DbMaintenance.IsAnyTable($"{cBTTblName}_extradefine", false)) { bExtHead = true; var cols = db.DbMaintenance.GetColumnInfosByTableName($"{cBTTblName}_extradefine"); if (cols != null && cols.Count > 0) { foreach (var col in cols) { if (col.DbColumnName.ToLower().StartsWith("chdefine")) { colsHeadList.Add($"{cBTTblName}_extradefine.[{col.DbColumnName.ToLower()}]"); } else { vouchersIdFieldName = col.DbColumnName; } } } query.AddJoinInfo($"{cBTTblName}_extradefine", $"{cBTTblName}_extradefine", $"{cBTTblName}.[{voucherIdFieldName}]={cBTTblName}_extradefine.[{voucherIdFieldName}]"); } if (db.DbMaintenance.IsAnyTable($"{cBWTblName}_extradefine", false)) { bExtBody = true; var cols = db.DbMaintenance.GetColumnInfosByTableName($"{cBWTblName}_extradefine"); if (cols != null && cols.Count > 0) { foreach (var col in cols) { if (col.DbColumnName.ToLower().StartsWith("cbdefine")) { colsBodyList.Add($"{cBWTblName}_extradefine.[{col.DbColumnName.ToLower()}]"); } else { vouchersIdFieldName = col.DbColumnName; } } } query.AddJoinInfo($"{cBWTblName}_extradefine", $"{cBWTblName}_extradefine", $"{cBWTblName}.[{vouchersIdFieldName}]={cBWTblName}_extradefine.[{vouchersIdFieldName}]"); } if (input != null) { var conModels = new List(); foreach (var o in input) { if (o.Key == "dModifyDate") { conModels.Add(new ConditionalModel { FieldName = "RelsTime", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = $"{o.Value}" }); } else if (o.Key == "dModifyDate__2") { conModels.Add(new ConditionalModel { FieldName = "RelsTime", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = $"{o.Value}" }); } else if ($"{o.Value}".EndsWith("%") || $"{o.Value}".StartsWith("%")) { conModels.Add(new ConditionalModel { FieldName = $"{o.Key}", ConditionalType = ConditionalType.Like, FieldValue = $"{o.Value}" }); } else if (o.Key.EndsWith("__2")) { conModels.Add(new ConditionalModel { FieldName = $"{o.Key.Replace("__2", "")}", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = $"{o.Value}" }); } else if (input.ContainsKey($"{o.Key}__2")) { conModels.Add(new ConditionalModel { FieldName = $"{o.Key}", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = $"{o.Value}" }); } else { conModels.Add(new ConditionalModel { FieldName = o.Key, ConditionalType = ConditionalType.Equal, FieldValue = $"{o.Value}" }); } } query.Where(conModels); } query.Where(cDefWhere); if (cCardPostion == "H") { if (bExtHead) { query.Select($"{string.Join(",", colsHeadList)}").Distinct(); } else { query.Select($"{string.Join(",", colsHeadList)}").Distinct(); } } if (cCardPostion == "B") { if (bExtBody) { query.Select($"{string.Join(",", colsBodyList)}").Distinct(); } else { query.Select($"{string.Join(",", colsBodyList)}").Distinct(); } } if (cCardPostion == "L") { colsBodyList.AddRange(colsHeadList); if (bExtBody) { query.Select($"{string.Join(",", colsBodyList)}").Distinct(); } else { query.Select($"{string.Join(",", colsBodyList)}").Distinct(); } } _logInfo.Debug(query.ToSqlString()); return new BaseResult() { bSuccess = true, message = "查询成功", result = await query.ToListAsync() }; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = ex.Message, result = ex.StackTrace }; } } } }