using log4net; using Microsoft.Extensions.Caching.Memory; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Http; using UFIDA.U8.Framework.Api.ControlObject.AA; using UFIDA.U8.Framework.Api.ControlObject.AA.U8PzInsert; 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.Unit; // ReSharper disable InconsistentNaming namespace UFIDA.U8.Framework.Api.Controllers.U8 { /// /// 基础资料 /// public class AAController : 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/AA/Inventory/Load")] public async Task> Inventory_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "Inventory", "" }, { "Inventory_Sub", "Inventory_Sub.cInvSubCode=Inventory.cInvCode" }, }; return await BaseLoad(cAccId, "Inventory", dict, input); } /// /// 查询计量单位档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/ComputationUnit/Load")] public async Task> ComputationUnit_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "ComputationUnit", "" }, }; return await BaseLoad(cAccId, "ComputationUnit", dict, input); } /// /// 查询计量单位组档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/ComputationGroup/Load")] public async Task> ComputationGroup_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "ComputationGroup", "" }, }; return await BaseLoad(cAccId, "ComputationGroup", dict, input); } /// /// 查询部门档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/Department/Load")] public async Task> Department_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "Department", "" }, }; return await BaseLoad(cAccId, "Department", dict, input); } /// /// 查询仓库档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/Warehouse/Load")] public async Task> Warehouse_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "Warehouse", "" }, }; return await BaseLoad(cAccId, "Warehouse", dict, input); } /// /// 查询存货分类档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/InventoryClass/Load")] public async Task> InventoryClass_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "InventoryClass", "" }, }; return await BaseLoad(cAccId, "InventoryClass", dict, input); } /// /// 查询供应商档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/Vendor/Load")] public async Task> Vendor_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "Vendor", "" }, //{ "VendorClass", "VendorClass.cVCCode = Vendor.cVCCode" }, //{ "DistrictClass", "DistrictClass.cDCCode = Vendor.cDCCode" }, //{ "Person", "Person.cPersonCode = dbo.Vendor.cVenPPerson" }, }; return await BaseLoad(cAccId, "Vendor", dict, input); } /// /// 查询供应商分类档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/VendorClass/Load")] public async Task> VendorClass_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "VendorClass", "" }, }; return await BaseLoad(cAccId, "VendorClass", dict, input); } /// /// 查询客户档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/Customer/Load")] public async Task> Customer_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "Customer", "" }, //{ "CustomerClass", "Customer.cCCCode = CustomerClass.cCCCode" }, //{ "DistrictClass", "DistrictClass.cDCCode = Customer.cDCCode" }, //{ "DistrictClass", "DistrictClass.cDCCode = Customer.cDCCode" }, //{ "Person", "Person.cPersonCode = Customer.cCusPerson" }, //{ "Warehouse", "Warehouse.cWhCode = Customer.cCusWhCode" }, }; return await BaseLoad(cAccId, "Customer", dict, input); } /// /// 查询客户分类档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/CustomerClass/Load")] public async Task> CustomerClass_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "CustomerClass", "" }, }; return await BaseLoad(cAccId, "CustomerClass", dict, input); } /// /// 查询人员档案 /// /// /// /// [HttpPost] [Route("api/U8/AA/Person/Load")] public async Task> Person_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "hr_hi_person", "" }, //{ "Department", "Department.cDepCode = hr_hi_person.cDept_num" } }; return await BaseLoad(cAccId, "Person", dict, input); } /// /// 查询标准工序资料 /// /// /// /// [HttpPost] [Route("api/U8/AA/Operation/Load")] public async Task> Operation_Load(string cAccId, [FromBody] Dictionary input) { var dict = new Dictionary() { { "sfc_operation", "" } }; return await BaseLoad(cAccId, "Operation", dict, input); } /// /// 基础档案查询 /// /// [NonAction] public async Task> BaseLoad(string cAccId, string tableName, Dictionary tables, [FromBody] Dictionary input = null) { var workId = SnowFlakeSingle.Instance.NextId(); try { _logInfo.Debug($"作业Id:{workId}-----档案:{tableName},操作:查询,对接账套:{cAccId},条件:{input.ToJson()}"); if (tables == null || tables.Count == 0) { throw new Exception("未指定档案表"); } 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, IsAutoCloseConnection = true, MoreSettings = new ConnMoreSettings() { IsWithNoLockQuery = true } }); var colList = new Dictionary(); var query = db.Queryable(); var bFirst = true; foreach (var table in tables) { if (!db.DbMaintenance.IsAnyTable(table.Key)) { throw new Exception($"表:{table.Key}不存在"); } var cols = db.DbMaintenance.GetColumnInfosByTableName(table.Key); if (cols != null && cols.Count > 0) { foreach (var col in cols.Where(col => !colList.Any(a => string.Equals(a.Key, col.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))) { colList.Add(col.DbColumnName, $"{table.Key}.[{col.DbColumnName}]"); } } if (bFirst) { query.AS(table.Key, table.Key); bFirst = false; } else { query.AddJoinInfo(table.Key, table.Key, table.Value); } } if (input != null) { var conModels = new List(); foreach (var o in input) { if ($"{o.Value}".EndsWith("%") || $"{o.Value}".StartsWith("%")) { if (colList.Any(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase))) { var col = colList.First(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase)); conModels.Add(new ConditionalModel { FieldName = $"{col.Value}", ConditionalType = ConditionalType.Like, FieldValue = $"{o.Value}" }); } } else if (o.Key.EndsWith("__2")) { if (colList.Any(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase))) { var col = colList.First(a => string.Equals(a.Key, o.Key.Replace("__2", ""), StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key.Replace("__2", ""), StringComparison.OrdinalIgnoreCase)); conModels.Add(new ConditionalModel { FieldName = $"{col.Value}", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = $"{o.Value}" }); } } else if (input.ContainsKey($"{o.Key}__2")) { if (colList.Any(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase))) { var col = colList.First(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase)); conModels.Add(new ConditionalModel { FieldName = $"{col.Value}", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = $"{o.Value}" }); } } else if (colList.Any(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase))) { var col = colList.First(a => string.Equals(a.Key, o.Key, StringComparison.OrdinalIgnoreCase) || string.Equals(a.Value, o.Key, StringComparison.OrdinalIgnoreCase)); conModels.Add(new ConditionalModel { FieldName = col.Value, ConditionalType = ConditionalType.Equal, FieldValue = $"{o.Value}" }); } } query.Where(conModels); } if (colList != null && colList.Keys.Count > 0) { var selector = colList .Select(col => new SelectModel { FieldName = $"{col.Value}", AsName = col.Key }).ToList(); query.Select(selector); } _logInfo.Debug($"作业Id:{workId}-----输出查询语句:{query.ToSqlString()}"); return new BaseResult { bSuccess = true, message = "查询成功", result = await query.ToListAsync() }; } catch (Exception ex) { _logInfo.Error($"作业Id:{workId}-----错误信息:{ex.Message}", ex); return new BaseResult { bSuccess = false, message = ex.Message, result = ex.StackTrace }; } } /// /// 新增项目大类 /// /// /// /// /// [HttpPost] [Route("api/U8/AA/{cCardNum}/Add")] public async Task> Fitem_Add(string cAccId, string cCardNum, [FromBody] Dictionary input) { try { var db = CommUnit.GetDbClient(); var workId = SnowFlakeSingle.Instance.NextId(); _logInfo.Debug($"作业Id:{workId}-----单据:{cCardNum},操作:新增,对接账套:{cAccId},数据:{input.ToJson()}"); //LoginParamter param; 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 co = new IFitem(); var res = await co.Transact(baseLogin, "", null); return res; } catch (Exception ex) { _logInfo.Error(ex.Message, ex); return new BaseResult { bSuccess = false, message = ex.Message, result = ex.StackTrace }; } } } }