using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Opsoft.Data; using Opsoft.NetCore.O3App.Domain.Interface; using Opsoft.NetCore.O3App.Domain.UnitOfWork.Public.IRepositories; using Opsoft.NetCore.O3App.Shared; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Opsoft.NetCore.O3App.Repositories.Public { [MapTo(typeof(IPublicRepository), ServiceLifetime.Transient)] public class PublicRepository : RepositoryBase, IPublicRepository { public async Task OpenWin(JObject request) { using APPServerContext context = new APPServerContext(LoginInfo.DBAias, LoginInfo.UserID); DataTable SYS_BILLTEMPLATE3 = context.Database.SqlQuery(string.Format("SELECT T01.* FROM SYS_BILLTEMPLATEENTRY2 T01 LEFT JOIN SYS_BILLTEMPLATE T02 ON T01.FINTERID=T02.FINTERID WHERE T02.FNUMBER='{0}'AND FLOOKUPCTLTYPE=3 ORDER BY T01.FSHOWINDEX", request["PrgKey"].ToStringEx())); switch (request["PrgKey"].ToStringEx()) { default: { switch (request["openWinNumber"].ToStringEx()) { case "FPROCESSNUMBER": { string openWinPrgKey = "SFC_PROCESS"; string winSetVar = $@"FPROCESSID=FINTERID;FPROCESSNUMBER=FNUMBER;FPROCESSNAME=FNAME"; return OpenWin(openWinPrgKey, context, request["openWinNumber"].ToStringEx(), request["filterValue"].ToStringEx().IsNullOrEmpty() ? null : request["filterValue"].ToStringEx(), winSetVar); } case "FCOMPANYNAME": { string openWinPrgKey = "ORG_Organizations"; string winSetVar = $@"FCOMPANYID=FINTERID;FCOMPANYNAME=FNAME"; return OpenWin(openWinPrgKey, context, request["openWinNumber"].ToStringEx(), request["filterValue"].ToStringEx().IsNullOrEmpty() ? null : request["filterValue"].ToStringEx(), winSetVar); } case "FRESOURCENUMBER": { string openWinPrgKey = "ENG_Resource"; string winSetVar = $@"FRESOURCEID=FINTERID;FRESOURCENUMBER=FNUMBER;FRESOURCENAME=FNAME"; return OpenWin(openWinPrgKey, context, request["openWinNumber"].ToStringEx(), request["filterValue"].ToStringEx().IsNullOrEmpty() ? null : request["filterValue"].ToStringEx(), winSetVar); } default: { DataRow para = SYS_BILLTEMPLATE3.Rows.Cast().FirstOrDefault(o => o["FFIELDNAMEAS"].ToStringEx().Equals(request["openWinNumber"].ToStringEx())); if (para == null) { throw new Exception("开窗参数不存在"); } string openWinPrgKey = para["FLOOKUPCLASSID"].ToStringEx(); return OpenWin(openWinPrgKey, context, request["openWinNumber"].ToStringEx(), request["filterValue"].ToStringEx().IsNullOrEmpty() ? null : request["filterValue"].ToStringEx(), para["FWINSETVAR"].ToStringEx()); } } break; } } throw new Exception("开窗失败"); } private JObject OpenWin(string openWinPrgKey, APPServerContext context, string openWinNumber, string filterValue = null, string winSetVar = null) { JObject jobject = new JObject(); DataRow row = context.Database.SqlQuery($@"SELECT T01.* FROM SYS_BILLTEMPLATEENTRY T01 LEFT JOIN SYS_BILLTEMPLATE T02 ON T01.FINTERID=T02.FINTERID WHERE T02.FNUMBER='{openWinPrgKey}' AND FSTYPE=2 ;") .Rows.Cast().FirstOrDefault(); if (row == null) { throw new Exception($@"表单{openWinPrgKey}不存在"); } DataTable fieldTable = context.Database.SqlQuery($@"SELECT T01.* FROM SYS_BILLTEMPLATEENTRY2 T01 LEFT JOIN SYS_BILLTEMPLATE T02 ON T01.FINTERID=T02.FINTERID WHERE T01.FBODYID='{row["FDETAILID"].ToStringEx()}' AND T01.FISVISIBLE=1 ORDER BY FSHOWINDEX;"); if (fieldTable != null && fieldTable.Rows.Count > 0) { JArray jRelation = new JArray(); jobject.Add("MappingRelation", jRelation); foreach (DataRow field in fieldTable.Rows) { JObject ob = new JObject(); ob.Add(field["FHEADCAPTION"].ToStringEx(), field["FFIELDNAMEAS"].ToStringEx()); jRelation.Add(ob); } string orderBy = row["FORDERKEY"].ToStringEx().IsNullOrEmpty() ? string.Empty : $@"order by {row["FORDERKEY"].ToStringEx()}"; string where = GetWhere(openWinNumber, filterValue, winSetVar); DataTable dataTable = context.Database.SqlQuery($@"SELECT * FROM ({row["FSQL"].ToStringEx()})T {where} {orderBy}"); jobject.Add("table", JArray.Parse(JsonConvert.SerializeObject(dataTable))); jobject.Add("setValue", SetValue(winSetVar)); return jobject; } else { throw new Exception($@"开窗字段列表不存在"); } } private JArray SetValue(string winSetVar) { string[] splitStrings = winSetVar.Split(';'); JArray jArray = new JArray(); foreach (string st in splitStrings) { string[] keyAndValue = st.Split('='); if (keyAndValue.Length == 2) { JObject jobj = new JObject(); jArray.Add(jobj); jobj.Add("billObject", keyAndValue[0]); jobj.Add("tableObject", keyAndValue[1]); } } return jArray; } private string GetWhere(string openWinNumber, string value, string winSetVar) { string where = $@"WHERE 1=1"; if (value != null) { string[] splitStrings = winSetVar.Split(';'); Dictionary dictionary = new Dictionary(); foreach (string st in splitStrings) { string[] keyAndValue = st.Split('='); if (keyAndValue.Length == 2) { dictionary.Add(keyAndValue[0], keyAndValue[1]); } } where = $@"{where} AND {dictionary[openWinNumber]}='{value}'"; } return where; } } }