public HttpResponseMessage getStockMovements(DTOGetStockMovementRequest request) { var user = KOCAuthorizeAttribute.getCurrentUser(); using (var db = new KOCSAMADLSEntities()) { if (request.fromobject != null && request.fromobject.value == null && request.toobject.value == null) { request.fromobject.value = ""; } var filter = request.getFilter(); var querySql = filter.getPagingSQL(request.pageNo, request.rowsPerPage); var countSql = filter.getCountSQL(); if (querySql.Contains("fromobject") || querySql.Contains("toobject")) { var sqlPartitions = querySql.Split(new string[] { "paging as (", ") SELECT *" }, StringSplitOptions.RemoveEmptyEntries); var pagingWhereClauses = sqlPartitions[1].Split(new string[] { "stockmovement WHERE", ") AND" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var subTablesClauses = pagingWhereClauses.Where(sc => sc.Contains("stockmovement.fromobject") || sc.Contains("stockmovement.toobject")).ToList(); var newClauses = new Dictionary <int, List <string> >(); newClauses[0] = new List <string>(); newClauses[1] = new List <string>(); for (int i = 0; i < subTablesClauses.Count(); i++) { var clause = subTablesClauses[i]; if (clause.Contains("fromobject1")) { newClauses[0].Add(clause.Replace("))", ")").Replace("stockmovement.fromobject1", "stockmovement.fromobject") + ")"); } else if (clause.Contains("toobject1")) { newClauses[1].Add(clause.Replace("))", ")").Replace("stockmovement.toobject1", "stockmovement.toobject") + ")"); } else { newClauses[clause.Contains("fromobject") ? 0 : 1].Add(clause + ")"); } pagingWhereClauses.Remove(clause); } var whereClauses = new List <string>(); if (pagingWhereClauses.Skip(1).Any()) { whereClauses.Add(string.Join(") AND", pagingWhereClauses.Skip(1)) + ")"); } if (newClauses[0].Any()) { whereClauses.Add($"({string.Join(" OR ", newClauses[0])})"); } if (newClauses[1].Any()) { whereClauses.Add($"({string.Join(" OR ", newClauses[1])})"); } if (user.userRole == 2147483647 || user.hasRole(KOCUserTypes.StockRoomStuff)) { } else if (user.hasRole(KOCUserTypes.TeamLeader)) { var rolelist = Enum.GetValues(typeof(KOCUserTypes)).OfType <KOCUserTypes>().Where(r => user.hasRole(r)).Select(r => (int)r).ToList(); rolelist.Add(user.userRole); whereClauses.Add($"(fromobjecttype in ({string.Join(",", rolelist)}) or toobjecttype in ({string.Join(",", rolelist)}))"); } else { whereClauses.Add($"((fromobject = {user.userId} and fromobjecttype!= 16777217) or (toobject = {user.userId} and toobjecttype!= 16777217))"); // aynı id'ye sahip personel ve müşterilerde müşterinin hareketini personelinmiş gibi göstermesin diye type eklendi (16777217 -> müşteri type) } var whereClause = string.Join(" AND ", whereClauses); querySql = $"{sqlPartitions[0]}paging as ({pagingWhereClauses[0]}stockmovement WHERE{whereClause}) SELECT *{sqlPartitions[2]} "; countSql = $"{sqlPartitions[0]}paging as ({pagingWhereClauses[0]}stockmovement WHERE{whereClause}) SELECT COUNT(*) FROM _paging"; } var performance = new DTOQueryPerformance(); var perf = Stopwatch.StartNew(); var rowCount = db.Database.SqlQuery <int>(countSql).First(); performance.CountSQLDuration = perf.Elapsed; perf.Restart(); var res = db.stockmovement.SqlQuery(querySql).ToList(); performance.QuerSQLyDuration = perf.Elapsed; perf.Restart(); var fromPerObjectIds = res.Where(t => t.fromobjecttype != (int)KOCUserTypes.ADSLCustomer).Select(s => s.fromobject).Distinct().ToList(); var fromCusObjectIds = res.Where(t => t.fromobjecttype == (int)KOCUserTypes.ADSLCustomer).Select(s => s.fromobject).Distinct().ToList(); var fromPersonels = db.personel.Where(p => fromPerObjectIds.Contains(p.personelid)).ToList(); var fromCustomers = db.customer.Where(c => fromCusObjectIds.Contains(c.customerid)).ToList(); var toPerObjectIds = res.Where(t => t.toobjecttype != (int)KOCUserTypes.ADSLCustomer).Select(s => s.toobject).Distinct().ToList(); var toCusObjectIds = res.Where(t => t.toobjecttype == (int)KOCUserTypes.ADSLCustomer).Select(s => s.toobject).Distinct().ToList(); var toPersonels = db.personel.Where(p => toPerObjectIds.Contains(p.personelid)).ToList(); var toCustomers = db.customer.Where(c => toCusObjectIds.Contains(c.customerid)).ToList(); var stockcardids = res.Select(s => s.stockcardid).Distinct().ToList(); var stockcards = db.stockcard.Where(s => stockcardids.Contains(s.stockid)).ToList(); res.ForEach(r => { r.frompersonel = fromPersonels.Where(p => p.personelid == r.fromobject).FirstOrDefault(); if (r.frompersonel == null) { r.fromcustomer = fromCustomers.Where(c => c.customerid == r.fromobject).FirstOrDefault(); } r.topersonel = toPersonels.Where(p => p.personelid == r.toobject).FirstOrDefault(); if (r.topersonel == null) { r.tocustomer = toCustomers.Where(c => c.customerid == r.toobject).FirstOrDefault(); } r.stockcard = stockcards.Where(s => s.stockid == r.stockcardid).FirstOrDefault(); }); performance.LookupDuration = perf.Elapsed; DTOResponsePagingInfo paginginfo = new DTOResponsePagingInfo { pageCount = (int)Math.Ceiling(rowCount * 1.0 / request.rowsPerPage), pageNo = request.pageNo, rowsPerPage = request.rowsPerPage, totalRowCount = rowCount }; return(Request.CreateResponse(HttpStatusCode.OK, new DTOPagedResponse(DTOResponseError.NoError(), res.Where(r => r.deleted == false).Select(s => s.toDTO()).ToList(), paginginfo, querySql, performance) , "application/json")); } }
public HttpResponseMessage getStock(DTOGetStockMovementRequest request) { // getStockMovements --> fromobject (id) ve toobject (id) olarak geldiğinde isim kontrolü ile çakışma olduğu için oluşturuldu var user = KOCAuthorizeAttribute.getCurrentUser(); using (var db = new KOCSAMADLSEntities()) { if (request.fromobject != null && request.fromobject.value == null && request.toobject.value == null) { request.fromobject.value = ""; } var filter = request.getFilter(); var querySql = filter.getPagingSQL(request.pageNo, request.rowsPerPage); var countSql = filter.getCountSQL(); var performance = new DTOQueryPerformance(); var perf = Stopwatch.StartNew(); var rowCount = db.Database.SqlQuery <int>(countSql).First(); performance.CountSQLDuration = perf.Elapsed; perf.Restart(); var res = db.stockmovement.SqlQuery(querySql).ToList(); performance.QuerSQLyDuration = perf.Elapsed; perf.Restart(); var fromObjectIds = res.Select(s => s.fromobject).Distinct().ToList(); var fromPersonels = db.personel.Where(p => fromObjectIds.Contains(p.personelid)).ToList(); var fromCustomers = db.customer.Where(c => fromObjectIds.Contains(c.customerid)).ToList(); var toObjectIds = res.Select(s => s.toobject).Distinct().ToList(); var toPersonels = db.personel.Where(p => toObjectIds.Contains(p.personelid)).ToList(); var toCustomers = db.customer.Where(c => toObjectIds.Contains(c.customerid)).ToList(); var stockcardids = res.Select(s => s.stockcardid).Distinct().ToList(); var stockcards = db.stockcard.Where(s => stockcardids.Contains(s.stockid)).ToList(); res.ForEach(r => { r.frompersonel = fromPersonels.Where(p => p.personelid == r.fromobject).FirstOrDefault(); if (r.frompersonel == null) { r.fromcustomer = fromCustomers.Where(c => c.customerid == r.fromobject).FirstOrDefault(); } r.topersonel = toPersonels.Where(p => p.personelid == r.toobject).FirstOrDefault(); if (r.topersonel == null) { r.tocustomer = toCustomers.Where(c => c.customerid == r.toobject).FirstOrDefault(); } r.stockcard = stockcards.Where(s => s.stockid == r.stockcardid).FirstOrDefault(); }); performance.LookupDuration = perf.Elapsed; DTOResponsePagingInfo paginginfo = new DTOResponsePagingInfo { pageCount = (int)Math.Ceiling(rowCount * 1.0 / request.rowsPerPage), pageNo = request.pageNo, rowsPerPage = request.rowsPerPage, totalRowCount = rowCount }; return(Request.CreateResponse(HttpStatusCode.OK, new DTOPagedResponse(DTOResponseError.NoError(), res.Where(r => r.deleted == false).Select(s => s.toDTO()).ToList(), paginginfo, querySql, performance) , "application/json")); } }
public HttpResponseMessage getStockMovements(DTOGetStockMovementRequest request) { var userID = 12; using (var db = new CRMEntities()) { var filter = request.getFilter(); var querySql = filter.getPagingSQL(request.pageNo, request.rowsPerPage); var countSql = filter.getCountSQL(); if (querySql.Contains("fromobject") || querySql.Contains("toobject")) { var sqlPartitions = querySql.Split(new string[] { "paging as (", ") SELECT *" }, StringSplitOptions.RemoveEmptyEntries); var pagingWhereClauses = sqlPartitions[1].Split(new string[] { "stockmovement WHERE", ") AND" }, StringSplitOptions.RemoveEmptyEntries).ToList(); var subTablesClauses = pagingWhereClauses.Where(sc => sc.Contains("stockmovement.fromobject") || sc.Contains("stockmovement.toobject")).ToList(); var newClauses = new Dictionary <int, List <string> >(); newClauses[0] = new List <string>(); newClauses[1] = new List <string>(); for (int i = 0; i < subTablesClauses.Count(); i++) { var clause = subTablesClauses[i]; if (clause.Contains("fromobject1")) { newClauses[0].Add(clause.Replace("))", ")").Replace("stockmovement.fromobject1", "stockmovement.fromobject") + " AND fromobjecttype = 3000)"); } else if (clause.Contains("fromobject2")) { newClauses[0].Add(clause.Replace("))", ")").Replace("stockmovement.fromobject2", "stockmovement.fromobject") + " AND fromobjecttype = 2000)"); } else if (clause.Contains("fromobject3")) { newClauses[0].Add(clause.Replace("))", ")").Replace("stockmovement.fromobject3", "stockmovement.fromobject") + " AND fromobjecttype = 1000)"); } else if (clause.Contains("toobject1")) { newClauses[1].Add(clause.Replace("))", ")").Replace("stockmovement.toobject1", "stockmovement.toobject") + " AND toobjecttype = 3000)"); } else if (clause.Contains("toobject2")) { newClauses[1].Add(clause.Replace("))", ")").Replace("stockmovement.toobject2", "stockmovement.toobject") + " AND toobjecttype = 2000)"); } else if (clause.Contains("toobject3")) { newClauses[1].Add(clause.Replace("))", ")").Replace("stockmovement.toobject3", "stockmovement.toobject") + " AND toobjecttype = 1000)"); } else { newClauses[clause.Contains("fromobject") ? 0 : 1].Add(clause + ")"); } pagingWhereClauses.Remove(clause); } var whereClauses = new List <string>(); if (pagingWhereClauses.Skip(1).Any()) { whereClauses.Add(string.Join(") AND", pagingWhereClauses.Skip(1)) + ")"); } if (newClauses[0].Any()) { whereClauses.Add($"({string.Join(" OR ", newClauses[0])})"); } if (newClauses[1].Any()) { whereClauses.Add($"({string.Join(" OR ", newClauses[1])})"); } whereClauses.Add($"(fromobject = {userID} or toobject = {userID})"); var whereClause = string.Join(" AND ", whereClauses); querySql = $"{sqlPartitions[0]}paging as ({pagingWhereClauses[0]}stockmovement WHERE{whereClause}) SELECT *{sqlPartitions[2]} "; countSql = $"{sqlPartitions[0]}paging as ({pagingWhereClauses[0]}stockmovement WHERE{whereClause}) SELECT COUNT(*) FROM _paging"; } var performance = new DTOQueryPerformance(); var perf = Stopwatch.StartNew(); var rowCount = db.Database.SqlQuery <int>(countSql).First(); performance.CountSQLDuration = perf.Elapsed; perf.Restart(); var res = db.stockmovement.SqlQuery(querySql).ToList(); performance.QuerSQLyDuration = perf.Elapsed; perf.Restart(); var fromObjectIds = res.Select(s => s.fromobject).Distinct().ToList(); var fromPersonels = db.personel.Where(p => fromObjectIds.Contains(p.personelid)).ToList(); var fromCustomers = db.customer.Where(c => fromObjectIds.Contains(c.customerid)).ToList(); var fromSites = db.site.Where(s => fromObjectIds.Contains(s.siteid)).ToList(); var fromBlocks = db.block.Where(b => fromObjectIds.Contains(b.blockid)).ToList(); var toObjectIds = res.Select(s => s.toobject).Distinct().ToList(); var toPersonels = db.personel.Where(p => toObjectIds.Contains(p.personelid)).ToList(); var toCustomers = db.customer.Where(c => toObjectIds.Contains(c.customerid)).ToList(); var toSites = db.site.Where(s => toObjectIds.Contains(s.siteid)).ToList(); var toBlocks = db.block.Where(b => toObjectIds.Contains(b.blockid)).ToList(); var stockcardids = res.Select(s => s.stockcardid).Distinct().ToList(); var stockcards = db.stockcard.Where(s => stockcardids.Contains(s.stockid)).ToList(); res.ForEach(r => { r.frompersonel = fromPersonels.Where(p => p.personelid == r.fromobject).FirstOrDefault(); if (r.frompersonel == null) { r.fromcustomer = fromCustomers.Where(c => c.customerid == r.fromobject).FirstOrDefault(); } r.topersonel = toPersonels.Where(p => p.personelid == r.toobject).FirstOrDefault(); if (r.topersonel == null) { r.tocustomer = toCustomers.Where(c => c.customerid == r.toobject).FirstOrDefault(); } r.stockcard = stockcards.Where(s => s.stockid == r.stockcardid).FirstOrDefault(); }); performance.LookupDuration = perf.Elapsed; DTOResponsePagingInfo paginginfo = new DTOResponsePagingInfo { pageCount = (int)Math.Ceiling(rowCount * 1.0 / request.rowsPerPage), pageNo = request.pageNo, rowsPerPage = request.rowsPerPage, totalRowCount = rowCount }; return(Request.CreateResponse(HttpStatusCode.OK, new DTOPagedResponse(DTOResponseError.NoError(), res.Where(r => r.deleted == false).Select(s => s.toDTO()).ToList(), paginginfo, querySql, performance) , "application/json")); } }