// データリスト初期化メソッド private ObservableCollection <PaymentLog> GetData() { DatabaseAccess db = new DatabaseAccess(); var all_Logs = db.Get_AllPayment(); // 売上商品の集計 foreach (var buf in all_Logs) { if (buf._type == "購入") { if (sellingList.ContainsKey(buf._janCode)) { // 既にリストに存在する場合、個数を加算 sellingList[buf._janCode] += buf._num; // 件数の記録 sellingScore[buf._janCode]++; } else { sellingList.Add(buf._janCode, buf._num); // リストに含まれない場合、新規追加 sellingScore.Add(buf._janCode, 1); } } } // 売れ筋評価 foreach (var item in sellingList) { // 売上日時リスト DateTime[] sellingDate = db.Get_AllSellingDate(item.Key); // 売上個数リスト int[] sellingHistory = new int[0]; // 仕入れ日時リスト DateTime[] stockingDate = db.Get_AllStockingDate(item.Key); // 仕入れインデックス int stockingidx = 0; // itemの売上件数 int total_record = sellingScore[item.Key]; // 初期化(ここまでは売上件数の保持につかっていたが、ここからはスコアを保存する) sellingScore[item.Key] = 0; // 仕入れ個数履歴 int[] history = db.Get_AllStockingHistory(item.Key); // 仕入れ履歴インデックス int historyidx = 0, total_sell = 0; // 売上商品の集計 foreach (var buf in all_Logs) { if (buf._type == "購入") { if (item.Key == buf._janCode) { // リストの追加 Array.Resize(ref sellingHistory, sellingHistory.Length + 1); sellingHistory[sellingHistory.Length - 1] = buf._num; } else { } } } // スコア計算 for (int i = 0; i < sellingDate.Length; i++) { total_sell += sellingHistory[i]; for (; ;) { if (stockingidx == stockingDate.Length - 1) { // 現在参照している仕入れ日時が最終仕入れの場合 break; } // 現在参照している仕入れ日時よりも後に仕入れがあった場合 if (stockingDate[stockingidx + 1] > sellingDate[i]) { // 現在参照している仕入れ日時と後の仕入れ日時の間の売上日時の場合 break; } else { // 現在参照している仕入れ日時の後の仕入れ日時以降の売上日時の場合 for (; ;) { if (history[historyidx] - total_sell >= 0) { break; } else { } if (total_sell >= history[historyidx]) { total_sell = total_sell - history[historyidx]; historyidx++; stockingidx++; continue; } } break; } } // 売上日時と、直前の仕入れ日時の差を分換算にてスコア算出(大きいほど売れていない) TimeSpan ts = new TimeSpan(); if (i == 0) { // 初売上の場合 ts = sellingDate[i] - stockingDate[stockingidx]; } else if (sellingDate[i - 1] > stockingDate[stockingidx]) { // 直前のデータが売上日時の場合 ts = sellingDate[i] - sellingDate[i - 1]; } else { // 直前のデータが仕入れ日時の場合 ts = sellingDate[i] - stockingDate[stockingidx]; } sellingScore[item.Key] += (int)ts.TotalMinutes; } sellingScore[item.Key] /= item.Value; // (売上件数/売上個数)のレートでスコアを減らす手法(一度に多く買っている場合の考慮) float r = ((float)total_record / (float)item.Value); sellingScore[item.Key] = (int)(sellingScore[item.Key] * r); } foreach (var log in sellingScore) { PaymentLog pl = new PaymentLog(); if (log.Key != "") { Item item = db.Get_Item(log.Key); pl._price = log.Value; pl._type = CheckFunction.Get_categoryName(item._categoryId); pl._itemName = item._itemName; pl._num = item._num; } Data.Add(pl); } List <Item> itemlist = db.Get_AllItem(); foreach (var item in itemlist) { if (!sellingList.ContainsKey(item._janCode)) { PaymentLog pl = new PaymentLog(); pl._price = 99999; pl._type = CheckFunction.Get_categoryName(item._categoryId); pl._itemName = item._itemName; pl._num = item._num; Data.Add(pl); } } Data = new ObservableCollection <PaymentLog>(from i in Data orderby i._price ascending select i); return(Data); }
/// <summary> /// all_logデータベースから全ての購入・チャージレコードを取得 /// </summary> /// <returns> /// List<PaymentLog> : PaymentLogクラスを参照 /// </returns> public List <PaymentLog> Get_AllPayment() { SqliteConnection db = this.OpenDB(); SqliteCommand command = new SqliteCommand("SELECT * from all_log where operation_type = '購入' OR operation_type = 'チャージ' OR operation_type = '仕入れ'", db); List <PaymentLog> ret = new List <PaymentLog>(); using (SqliteDataReader query = command.ExecuteReader()) { while (query.Read()) { PaymentLog pl = new PaymentLog(); pl._id = query.GetInt32(0); pl._date = query.GetString(1); pl._mid = query.GetString(2); pl._type = query.GetString(3); ret.Add(pl); } } foreach (PaymentLog pl in ret) { // user_name 取得 if (Search_UserInformation(pl._mid)) { command = new SqliteCommand("SELECT * from users_information where @mid = mid", db); command.Parameters.AddWithValue("@mid", pl._mid); using (SqliteDataReader query = command.ExecuteReader()) { query.Read(); pl._user_name = query.GetString(2); } } else { pl._user_name = "削除ユーザー"; } if (pl._type == "購入") { command = new SqliteCommand("SELECT * from purchases_log where @id = operation_id", db); command.Parameters.AddWithValue("@id", pl._id); using (SqliteDataReader query = command.ExecuteReader()) { query.Read(); pl._janCode = query.GetString(1); pl._num = query.GetInt32(2); pl._price = query.GetInt32(3); pl._total_price = pl._num * pl._price; } if (Search_Item(pl._janCode)) { command = new SqliteCommand("SELECT * from Items_information where @janCode = jan_code", db); command.Parameters.AddWithValue("@janCode", pl._janCode); using (SqliteDataReader query = command.ExecuteReader()) { query.Read(); pl._itemName = query.GetString(1); } } else { pl._itemName = "削除商品"; } } else if (pl._type == "チャージ" || pl._type == "仕入れ") { command = new SqliteCommand("SELECT * from charges_log where @id = operation_id", db); command.Parameters.AddWithValue("@id", pl._id); using (SqliteDataReader query = command.ExecuteReader()) { query.Read(); pl._total_price = query.GetInt32(1); } } } db.Close(); return(ret); }