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.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) }); /// /// 查询生产订单 /// /// /// /// [HttpPost] [Route("api/U8/MO/MO21/Load")] public async Task> MO21_Load(string cAccId,[FromBody] Dictionary input) { var workId = SnowFlakeSingle.Instance.NextId(); try { //var userToken = Request.Headers.GetValues("userToken").First(); //if (userToken!= "04fafa004ad8851485d9026229964a488f2080907248680178bae68a236b33bc086aa234c94927e4099f21209bea98640ad4e9b1b5319cd5f55144606d82f3dbeff44ec66575ca2438144df9fa3f3e1babd80a140d2170975a031fca1ade326234b4d9a61386") //{ // throw new Exception("调用信息不正确,请确认参数"); //} _logInfo.Debug($"作业Id:{workId}-----单据:MO21,操作:查询,对接账套:{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 db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConfigId = cAccId, ConnectionString = baseLogin.UFDataConnstringForNet }); var mo21Txt = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data/MO21.json")); var mo21s = mo21Txt.ToObject>(); var query = db.Queryable().AS("v_mom_order_wf", "v_mom_order_wf") .AddJoinInfo("v_mom_orderdetail_wf", "v_mom_orderdetail_wf", "v_mom_order_wf.MoId=v_mom_orderdetail_wf.MoId"); if (db.DbMaintenance.IsAnyTable("mom_order_extradefine", false)) { query.AddJoinInfo("mom_order_extradefine", "mom_order_extradefine", "v_mom_order_wf.MoId=mom_order_extradefine.MoId"); } if (db.DbMaintenance.IsAnyTable("mom_orderdetail_extradefine", false)) { query.AddJoinInfo("mom_orderdetail_extradefine", "mom_order_extradefine", "v_mom_orderdetail_wf.MoDId=mom_orderdetail_extradefine.MoDId"); } if (input != null) { var conModels = new List(); foreach (var o in input) { if (o.Key == "MoId" && input.ContainsKey($"{o.Key}__2")) { conModels.Add(new ConditionalModel { FieldName = "v_mom_order_wf.MoId", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = $"{o.Value}" }); } else if (o.Key == "MoId") { conModels.Add(new ConditionalModel { FieldName = "v_mom_order_wf.MoId", ConditionalType = ConditionalType.Equal, FieldValue = $"{o.Value}" }); } else if (o.Key == "MoId__2") { conModels.Add(new ConditionalModel { FieldName = "v_mom_order_wf.MoId", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = $"{o.Value}" }); }else if (o.Key == "MoDId" && input.ContainsKey($"{o.Key}__2")) { conModels.Add(new ConditionalModel { FieldName = "v_mom_orderdetail_wf.MoDId", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = $"{o.Value}" }); } else if (o.Key == "MoDId") { conModels.Add(new ConditionalModel { FieldName = "v_mom_orderdetail_wf.MoDId", ConditionalType = ConditionalType.Equal, FieldValue = $"{o.Value}" }); } else if (o.Key == "MoDId__2") { conModels.Add(new ConditionalModel { FieldName = "v_mom_orderdetail_wf.MoDId", ConditionalType = ConditionalType.LessThanOrEqual, 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); } if (mo21s != null && mo21s.Count > 0) { var selector = new List(); foreach (var mo21 in mo21s) { var model = new SelectModel() { FieldName = $"{mo21.TableName}.{mo21.FieldName}" }; if (mo21.AsName != null && !string.IsNullOrWhiteSpace(mo21.AsName)) { model.AsName = mo21.AsName; } selector.Add(model); } query.Select(selector); } return new BaseResult { bSuccess = true, message = "查询成功", result = await query.ToListAsync() }; } catch (Exception ex) { _logInfo.Error($"作业Id:{workId}-----错误信息:{ex.Message}", ex); await Task.Delay(1); return new BaseResult { bSuccess = false, message = ex.Message, result = ex.StackTrace }; } } /// /// 查询生产订单子件用料表 /// /// /// /// [HttpPost] [Route("api/U8/MO/MO2101/Load")] public async Task> MO2101_Load(string cAccId, [FromBody] Dictionary input) { var workId = SnowFlakeSingle.Instance.NextId(); try { //var userToken = Request.Headers.GetValues("userToken").First(); //if (userToken != "04fafa004ad8851485d9026229964a488f2080907248680178bae68a236b33bc086aa234c94927e4099f21209bea98640ad4e9b1b5319cd5f55144606d82f3dbeff44ec66575ca2438144df9fa3f3e1babd80a140d2170975a031fca1ade326234b4d9a61386") //{ // throw new Exception("调用信息不正确,请确认参数"); //} _logInfo.Debug($"作业Id:{workId}-----单据:MO2101,操作:查询,对接账套:{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 db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.SqlServer, ConfigId = cAccId, ConnectionString = baseLogin.UFDataConnstringForNet }); var mo2101Txt = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data/MO2101.json")); var mo2101s = mo2101Txt.ToObject>(); var query = db.Queryable().AS("mom_moallocate", "mom_moallocate"); if (db.DbMaintenance.IsAnyTable("mom_moallocate_extradefine", false)) { query.AddJoinInfo("mom_moallocate_extradefine", "mom_moallocate_extradefinemom_moallocate_extradefine", "om_moallocate.AllocateId=mom_moallocate_extradefine.AllocateId"); } if (input != null) { var conModels = new List(); foreach (var o in input) { if (o.Key == "AllocateId" && input.ContainsKey($"{o.Key}__2")) { conModels.Add(new ConditionalModel { FieldName = "mom_moallocate.AllocateId", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = $"{o.Value}" }); } else if (o.Key == "AllocateId") { conModels.Add(new ConditionalModel { FieldName = "mom_moallocate.AllocateId", ConditionalType = ConditionalType.Equal, FieldValue = $"{o.Value}" }); } else if (o.Key == "AllocateId__2") { conModels.Add(new ConditionalModel { FieldName = "mom_moallocate.AllocateId", ConditionalType = ConditionalType.LessThanOrEqual, 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); } if (mo2101s != null && mo2101s.Count > 0) { var selector = new List(); foreach (var mo2101 in mo2101s) { var model = new SelectModel() { FieldName = $"{mo2101.TableName}.{mo2101.FieldName}" }; if (mo2101.AsName != null && !string.IsNullOrWhiteSpace(mo2101.AsName)) { model.AsName = mo2101.AsName; } selector.Add(model); } query.Select(selector); } return new BaseResult { bSuccess = true, message = "查询成功", result = await query.ToListAsync() }; } catch (Exception ex) { _logInfo.Error($"作业Id:{workId}-----错误信息:{ex.Message}", ex); await Task.Delay(1); return new BaseResult { bSuccess = false, message = ex.Message, result = ex.StackTrace }; } } } }