)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » ProductPriceSpot (Ia.Statistics.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

    1: using System;
    2: using System.ComponentModel.DataAnnotations.Schema;
    3: using System.ComponentModel.DataAnnotations;
    4: using System.Collections.Generic;
    5: using System.Linq;
    6: using Microsoft.EntityFrameworkCore;
    7:  
    8: namespace Ia.Statistics.Cl.Models
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     ///
   14:     /// </summary>
   15:     /// 
   16:     /// <remarks> 
   17:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   18:     ///
   19:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   20:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   21:     ///
   22:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   23:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   24:     /// 
   25:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   26:     /// 
   27:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   28:     /// </remarks> 
   29:     public class ProductPriceSpot
   30:     {
   31:         private static List<Ia.Statistics.Cl.Models.ProductPriceSpot> productPriceSpotList;
   32:         private static Dictionary<int, Dictionary<string, List<decimal>>> siteIdToProductIdToPriceListDictionaryDictionary;
   33:  
   34:         private static readonly object objectLock = new object();
   35:  
   36:         /// <summary/>
   37:         [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
   38:         public string Id { get; set; }
   39:  
   40:         /// <summary/>
   41:         public decimal Price { get; set; }
   42:  
   43:         /// <summary/>
   44:         public DateTime Created { get; set; }
   45:  
   46:         /// <summary/>
   47:         [ForeignKey("Product_Id")]
   48:         public virtual Product Product { get; set; }
   49:  
   50:         ////////////////////////////////////////////////////////////////////////////
   51:  
   52:         /// <summary>
   53:         ///
   54:         /// </summary>
   55:         public static string ProductPriceSpotId(Ia.Statistics.Cl.Models.Product product)
   56:         {
   57:             string id;
   58:  
   59:             if (product != null)
   60:             {
   61:                 id = product.Id + ":" + DateTime.UtcNow.AddHours(3).ToString("yyyyMMddHHmmss");
   62:             }
   63:             else id = string.Empty;
   64:  
   65:             return id;
   66:         }
   67:  
   68:         ////////////////////////////////////////////////////////////////////////////
   69:  
   70:         /// <summary>
   71:         ///
   72:         /// </summary>
   73:         public static List<Ia.Statistics.Cl.Models.ProductPriceSpot> List
   74:         {
   75:             get
   76:             {
   77:                 if (productPriceSpotList == null || productPriceSpotList.Count == 0)
   78:                 {
   79:                     lock (objectLock)
   80:                     {
   81:                         using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
   82:                         {
   83:                             productPriceSpotList = (from pss in db.ProductPriceSpots
   84:                                                     select pss).Include(p => p.Product).AsNoTracking().ToList();
   85:                         }
   86:                     }
   87:                 }
   88:  
   89:                 return productPriceSpotList;
   90:             }
   91:         }
   92:  
   93:         ////////////////////////////////////////////////////////////////////////////
   94:  
   95:         /// <summary>
   96:         ///
   97:         /// </summary>
   98:         public static List<Ia.Statistics.Cl.Models.ProductPriceSpot> ListByProductId(string productId)
   99:         {
  100:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  101:             {
  102:                 var list = (from pps in db.ProductPriceSpots
  103:                             where pps.Product.Id == productId
  104:                             select pps).AsNoTracking().ToList();
  105:  
  106:                 return list;
  107:             }
  108:         }
  109:  
  110:         ////////////////////////////////////////////////////////////////////////////
  111:  
  112:         /// <summary>
  113:         ///
  114:         /// </summary>
  115:         public static Dictionary<string, List<decimal>> ProductIdToPriceListDictionaryBySiteId(int siteId)
  116:         {
  117:             if (siteIdToProductIdToPriceListDictionaryDictionary == null || siteIdToProductIdToPriceListDictionaryDictionary.Count == 0)
  118:             {
  119:                 siteIdToProductIdToPriceListDictionaryDictionary = new Dictionary<int, Dictionary<string, List<decimal>>>();
  120:             }
  121:  
  122:             if (!siteIdToProductIdToPriceListDictionaryDictionary.ContainsKey(siteId) || siteIdToProductIdToPriceListDictionaryDictionary[siteId] == null)
  123:             {
  124:                 siteIdToProductIdToPriceListDictionaryDictionary[siteId] = new Dictionary<string, List<decimal>>();
  125:  
  126:                 lock (objectLock)
  127:                 {
  128:                     var siteShortName = Ia.Statistics.Cl.Models.Site.SiteShortNameFromId(siteId);
  129:  
  130:                     using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  131:                     {
  132:                         var productIdPriceForSiteList = (from pps in db.ProductPriceSpots
  133:                                                          where pps.Id.StartsWith(siteShortName)
  134:                                                          orderby pps.Created ascending
  135:                                                          select new
  136:                                                          {
  137:                                                              ProductId = pps.Product.Id,
  138:                                                              pps.Price,
  139:                                                          }).ToList();
  140:  
  141:                         foreach (var productIdPrice in productIdPriceForSiteList)
  142:                         {
  143:                             if (!siteIdToProductIdToPriceListDictionaryDictionary[siteId].ContainsKey(productIdPrice.ProductId))
  144:                             {
  145:                                 siteIdToProductIdToPriceListDictionaryDictionary[siteId][productIdPrice.ProductId] = new List<decimal>();
  146:                             }
  147:  
  148:                             siteIdToProductIdToPriceListDictionaryDictionary[siteId][productIdPrice.ProductId].Add(productIdPrice.Price);
  149:                         }
  150:                     }
  151:                 }
  152:             }
  153:  
  154:             return siteIdToProductIdToPriceListDictionaryDictionary[siteId];
  155:         }
  156:  
  157:         ////////////////////////////////////////////////////////////////////////////
  158:  
  159:         /// <summary>
  160:         ///
  161:         /// </summary>
  162:         public static void EarliestAndLatestRecordDateTimeBySiteId(int siteId, out DateTime? earliestRecordDateTime, out DateTime? latestRecordDateTime)
  163:         {
  164:             var siteShortName = Ia.Statistics.Cl.Models.Site.SiteShortNameFromId(siteId);
  165:  
  166:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  167:             {
  168:                 var list = (from pps in db.ProductPriceSpots
  169:                             where pps.Id.StartsWith(siteShortName)
  170:                             select pps);
  171:  
  172:                 earliestRecordDateTime = list.Count() > 0 ? list.Min(u => u.Created) : null;
  173:  
  174:  
  175:                 list = (from pps in db.ProductPriceSpots
  176:                         where pps.Id.StartsWith(siteShortName)
  177:                         select pps);
  178:  
  179:                 latestRecordDateTime = list.Count() > 0 ? list.Max(u => u.Created) : null;
  180:             }
  181:         }
  182:  
  183:         ////////////////////////////////////////////////////////////////////////////
  184:  
  185:         /// <summary>
  186:         ///
  187:         /// </summary>
  188:         public static void Create(Ia.Statistics.Cl.Models.Product product, decimal price)
  189:         {
  190:             var productPriceSpot = new Ia.Statistics.Cl.Models.ProductPriceSpot();
  191:  
  192:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  193:             {
  194:                 productPriceSpot.Id = Ia.Statistics.Cl.Models.ProductPriceSpot.ProductPriceSpotId(product);
  195:                 productPriceSpot.Price = price;
  196:                 productPriceSpot.Product = (from p in db.Products where p.Id == product.Id select p).SingleOrDefault();
  197:                 productPriceSpot.Created = DateTime.UtcNow.AddHours(3);
  198:  
  199:                 db.ProductPriceSpots.Add(productPriceSpot);
  200:                 db.SaveChanges();
  201:             }
  202:         }
  203:  
  204:         ////////////////////////////////////////////////////////////////////////////
  205:  
  206:         /// <summary>
  207:         ///
  208:         /// </summary>
  209:         public static void Create(Ia.Statistics.Cl.Models.ProductPriceSpot productPriceSpot)
  210:         {
  211:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  212:             {
  213:                 db.ProductPriceSpots.Add(productPriceSpot);
  214:                 db.SaveChanges();
  215:             }
  216:         }
  217:  
  218:         ////////////////////////////////////////////////////////////////////////////
  219:         ////////////////////////////////////////////////////////////////////////////
  220:     }
  221:  
  222:     ////////////////////////////////////////////////////////////////////////////
  223:     ////////////////////////////////////////////////////////////////////////////
  224: }