Spaces:
Sleeping
Sleeping
File size: 8,851 Bytes
13240d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | using BakeryErp.Infrastructure.Database;
namespace BakeryErp.Infrastructure.Repositories;
public sealed class ItemRepository
{
private readonly DatabaseOptions _options;
public ItemRepository(DatabaseOptions options)
{
_options = options;
}
public IReadOnlyList<LocalItem> List() => LocalDataStoreFile.Load(_options).Items
.OrderByDescending(static item => item.IsActive)
.ThenBy(static item => item.Sku)
.ToList();
public LocalItem? Find(Guid id) => LocalDataStoreFile.Load(_options).Items.FirstOrDefault(item => item.Id == id);
public SaveMasterDataResult Save(LocalItem item)
{
var store = LocalDataStoreFile.Load(_options);
var sku = item.Sku?.Trim().ToUpperInvariant() ?? string.Empty;
var name = item.Name?.Trim() ?? string.Empty;
var categoryName = item.CategoryName?.Trim() ?? string.Empty;
var baseUnit = item.BaseUnit?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(sku) || string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(baseUnit))
{
return SaveMasterDataResult.ValidationError("品項代號、名稱與基本單位為必填。");
}
if (store.Items.Any(existing => existing.Id != item.Id && string.Equals(existing.Sku, sku, StringComparison.OrdinalIgnoreCase)))
{
return SaveMasterDataResult.ValidationError("品項代號已存在。");
}
var existingItem = store.Items.FirstOrDefault(existing => existing.Id == item.Id);
if (existingItem is null)
{
item.Id = item.Id == Guid.Empty ? Guid.NewGuid() : item.Id;
item.Sku = sku;
item.Name = name;
item.CategoryName = categoryName;
item.BaseUnit = baseUnit;
item.CreatedAt = DateTime.UtcNow;
item.IsActive = true;
store.Items.Add(item);
}
else
{
existingItem.Sku = sku;
existingItem.Name = name;
existingItem.ItemType = item.ItemType;
existingItem.CategoryName = categoryName;
existingItem.BaseUnit = baseUnit;
existingItem.SafetyStockQuantity = item.SafetyStockQuantity;
existingItem.ShelfLifeDays = item.ShelfLifeDays;
existingItem.UpdatedAt = DateTime.UtcNow;
}
LocalDataStoreFile.Save(_options, store);
return SaveMasterDataResult.Success();
}
public void SetActive(Guid id, bool isActive)
{
var store = LocalDataStoreFile.Load(_options);
var item = store.Items.FirstOrDefault(existing => existing.Id == id);
if (item is null)
{
return;
}
item.IsActive = isActive;
item.UpdatedAt = DateTime.UtcNow;
LocalDataStoreFile.Save(_options, store);
}
}
public sealed class StoreRepository
{
private readonly DatabaseOptions _options;
public StoreRepository(DatabaseOptions options)
{
_options = options;
}
public IReadOnlyList<LocalStore> List() => LocalDataStoreFile.Load(_options).Stores
.OrderByDescending(static store => store.IsActive)
.ThenBy(static store => store.Code)
.ToList();
public LocalStore? Find(Guid id) => LocalDataStoreFile.Load(_options).Stores.FirstOrDefault(store => store.Id == id);
public SaveMasterDataResult Save(LocalStore storeInfo)
{
var store = LocalDataStoreFile.Load(_options);
var code = storeInfo.Code?.Trim().ToUpperInvariant() ?? string.Empty;
var name = storeInfo.Name?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(name))
{
return SaveMasterDataResult.ValidationError("門市代號與名稱為必填。");
}
if (store.Stores.Any(existing => existing.Id != storeInfo.Id && string.Equals(existing.Code, code, StringComparison.OrdinalIgnoreCase)))
{
return SaveMasterDataResult.ValidationError("門市代號已存在。");
}
var existingStore = store.Stores.FirstOrDefault(existing => existing.Id == storeInfo.Id);
if (existingStore is null)
{
storeInfo.Id = storeInfo.Id == Guid.Empty ? Guid.NewGuid() : storeInfo.Id;
storeInfo.Code = code;
storeInfo.Name = name;
storeInfo.Phone = BlankToNull(storeInfo.Phone);
storeInfo.Address = BlankToNull(storeInfo.Address);
storeInfo.CreatedAt = DateTime.UtcNow;
storeInfo.IsActive = true;
store.Stores.Add(storeInfo);
}
else
{
existingStore.Code = code;
existingStore.Name = name;
existingStore.StoreType = storeInfo.StoreType;
existingStore.Phone = BlankToNull(storeInfo.Phone);
existingStore.Address = BlankToNull(storeInfo.Address);
existingStore.UpdatedAt = DateTime.UtcNow;
}
LocalDataStoreFile.Save(_options, store);
return SaveMasterDataResult.Success();
}
public void SetActive(Guid id, bool isActive)
{
var store = LocalDataStoreFile.Load(_options);
var storeInfo = store.Stores.FirstOrDefault(existing => existing.Id == id);
if (storeInfo is null)
{
return;
}
storeInfo.IsActive = isActive;
storeInfo.UpdatedAt = DateTime.UtcNow;
LocalDataStoreFile.Save(_options, store);
}
private static string? BlankToNull(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}
public sealed class WarehouseRepository
{
private readonly DatabaseOptions _options;
public WarehouseRepository(DatabaseOptions options)
{
_options = options;
}
public IReadOnlyList<LocalWarehouse> List() => LocalDataStoreFile.Load(_options).Warehouses
.OrderByDescending(static warehouse => warehouse.IsActive)
.ThenBy(static warehouse => warehouse.Code)
.ToList();
public LocalWarehouse? Find(Guid id) => LocalDataStoreFile.Load(_options).Warehouses.FirstOrDefault(warehouse => warehouse.Id == id);
public IReadOnlyList<string> ActiveStoreNames() => LocalDataStoreFile.Load(_options).Stores
.Where(static store => store.IsActive)
.Select(static store => store.Name)
.ToList();
public SaveMasterDataResult Save(LocalWarehouse warehouse)
{
var store = LocalDataStoreFile.Load(_options);
var code = warehouse.Code?.Trim().ToUpperInvariant() ?? string.Empty;
var name = warehouse.Name?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(name))
{
return SaveMasterDataResult.ValidationError("倉庫代號與名稱為必填。");
}
if (store.Warehouses.Any(existing => existing.Id != warehouse.Id && string.Equals(existing.Code, code, StringComparison.OrdinalIgnoreCase)))
{
return SaveMasterDataResult.ValidationError("倉庫代號已存在。");
}
var existingWarehouse = store.Warehouses.FirstOrDefault(existing => existing.Id == warehouse.Id);
if (existingWarehouse is null)
{
warehouse.Id = warehouse.Id == Guid.Empty ? Guid.NewGuid() : warehouse.Id;
warehouse.Code = code;
warehouse.Name = name;
warehouse.StoreName = BlankToNull(warehouse.StoreName);
warehouse.CreatedAt = DateTime.UtcNow;
warehouse.IsActive = true;
store.Warehouses.Add(warehouse);
}
else
{
existingWarehouse.Code = code;
existingWarehouse.Name = name;
existingWarehouse.StoreName = BlankToNull(warehouse.StoreName);
existingWarehouse.WarehouseType = warehouse.WarehouseType;
existingWarehouse.UpdatedAt = DateTime.UtcNow;
}
LocalDataStoreFile.Save(_options, store);
return SaveMasterDataResult.Success();
}
public void SetActive(Guid id, bool isActive)
{
var store = LocalDataStoreFile.Load(_options);
var warehouse = store.Warehouses.FirstOrDefault(existing => existing.Id == id);
if (warehouse is null)
{
return;
}
warehouse.IsActive = isActive;
warehouse.UpdatedAt = DateTime.UtcNow;
LocalDataStoreFile.Save(_options, store);
}
private static string? BlankToNull(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}
public sealed record SaveMasterDataResult(bool IsSuccess, string? ErrorMessage)
{
public static SaveMasterDataResult Success() => new(true, null);
public static SaveMasterDataResult ValidationError(string message) => new(false, message);
}
|