SitePriceHistoryService.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Claims;
  6. using System.Text;
  7. using System.Transactions;
  8. using FMSAdmin.Data;
  9. using FMSAdmin.Helpers;
  10. using FMSAdmin.Entities;
  11. using Microsoft.EntityFrameworkCore;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using Microsoft.IdentityModel.Tokens;
  15. using FMSAdmin.Models;
  16. namespace FMSAdmin.Services {
  17. public class SitePriceHistoryService {
  18. private readonly ILogger<SitePriceHistoryService> _logger;
  19. private readonly FMSContext _context;
  20. public SitePriceHistoryService(
  21. ILogger<SitePriceHistoryService> logger,
  22. FMSContext context) {
  23. _logger = logger;
  24. _context = context;
  25. }
  26. public void Save(BemsSitePriceHistory[] data) {
  27. using (var trans = new TransactionScope()) {
  28. foreach (BemsSitePriceHistory row in data) {
  29. var persist = _context.BemsSitePriceHistory
  30. .Where(x => x.SiteId == row.SiteId && x.PayDate == row.PayDate && x.FuelTypeId == row.FuelTypeId && x.PriceTypeId == row.PriceTypeId && x.PriceCode == row.PriceCode).FirstOrDefault();
  31. var persist2 = _context.BemsPriceCode.Where(x => x.FuelTypeId == row.FuelTypeId && x.PriceCode == row.PriceCode).FirstOrDefault();
  32. if (!persist2.Unit.Equals("성명") && !persist2.Unit.Equals("날짜")) {
  33. try {
  34. Convert.ToDecimal(Util.IsNullOrEmpty(row.PriceValue) ? "0" : row.PriceValue);
  35. } catch {
  36. throw new ServiceException("잘못된 형식입니다.(ex>형식 1,000.00)");
  37. }
  38. }
  39. if (persist == null) {
  40. _context.BemsSitePriceHistory.Add(row);
  41. _context.SaveChanges();
  42. } else {
  43. persist.PriceValue = row.PriceValue;
  44. //
  45. _context.SaveChanges();
  46. }
  47. }
  48. trans.Complete();
  49. }
  50. }
  51. public IQueryable<BemsSitePriceHistory> GetAll() {
  52. var query = _context.BemsSitePriceHistory/*.AsNoTracking()*/;
  53. return query;
  54. }
  55. public void Delete(int siteId, int fuelTypeId, int priceTypeId) {
  56. using (var trans = new TransactionScope()) {
  57. var data = _context.BemsSitePriceHistory.First(x => x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.PriceTypeId == priceTypeId);
  58. //data.IsUse = false;
  59. _context.BemsSitePriceHistory.Remove(data);
  60. _context.SaveChanges();
  61. trans.Complete();
  62. }
  63. }
  64. public IQueryable<BemsFuelType> GetFuelType(int? siteId) {
  65. siteId = siteId == null ? 0 : siteId;
  66. //var data = _context.BemsFuelType.Where(x => x.FuelTypeId != 0);
  67. var data = _context.BemsSitePrice.Where(x => x.SiteId == siteId).Select(x => x.FuelType);
  68. return data;
  69. }
  70. public IQueryable<BemsSitePrice> GetPriceCode(int siteId, int fuelTypeId, int priceTypeId) {
  71. var data = _context.BemsSitePrice.Where(x => x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.PriceTypeId == priceTypeId && x.UseYn == "Y" && !x.PriceCodeNavigation.Unit.Contains("성명") && !x.PriceCodeNavigation.Unit.Contains("날짜"));
  72. return data;
  73. }
  74. }
  75. }