c52d74c59650e578c75d036f166f7cff7d211fb3.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Data;
  6. using System.Data.Entity;
  7. using System.Data.Entity.Infrastructure;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Web;
  12. using System.Web.Http;
  13. using System.Web.Http.Description;
  14. using System.Web.Http.OData;
  15. using iBemsDataService;
  16. using iBemsDataService.Model;
  17. namespace iBemsDataService.Controllers
  18. {
  19. public class FmsBudgetCodeClassExController : ODataController
  20. {
  21. private iBemsEntities db = new iBemsEntities();
  22. // GET: odata/FmsBudgetCodeClassEx
  23. [Queryable]
  24. public List<FmsBudgetCodeClassEx> GetFmsBudgetCodeClassEx()
  25. {
  26. List<FmsBudgetCodeClassEx> flatList = null;
  27. // 2019.07.12
  28. Uri uri = Request.RequestUri;
  29. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  30. int siteId;
  31. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false)
  32. {
  33. throw new Exception("Not Found Parameters.");
  34. }
  35. var codeList = db.FmsBudgetCodeClass.ToList();
  36. flatList = (
  37. from c in codeList
  38. where c.SiteId == siteId &&
  39. c.Depth == 2
  40. select new FmsBudgetCodeClassEx
  41. {
  42. SiteId = c.SiteId,
  43. ThirdBudgetClassId = c.BudgetClassId,
  44. ThirdBudgetName = c.Name,
  45. YearlyBudget = 0,
  46. MonthlyBudget = 0,
  47. //Depth = c.Depth,
  48. //ParentBudgetClassId = c.ParentBudgetClassId
  49. }).ToList();
  50. foreach (var item in flatList)
  51. {
  52. // Todo : 방어코드 혹은 exception 필요
  53. //catch (DbUpdateException e) { throw e; }
  54. KeyValuePair<int, string> secondBudgetValue = GetParent(item.ThirdBudgetClassId, codeList);
  55. KeyValuePair<int, string> firstBudgetValue = GetParent(secondBudgetValue.Key, codeList);
  56. if (secondBudgetValue.Key == 0 || firstBudgetValue.Key == 0)
  57. {
  58. throw new Exception("Budget Code 가 잘못설정되었습니다.");
  59. }
  60. item.SecondBudgetClassId = secondBudgetValue.Key;
  61. item.SecondBudgetName = secondBudgetValue.Value;
  62. item.SecondBudgetNameWithSum = secondBudgetValue.Value; ;
  63. item.FirstBudgetClassId = firstBudgetValue.Key;
  64. item.FirstBudgetName = firstBudgetValue.Value;
  65. item.FirstBudgetNameWithSum = firstBudgetValue.Value; ;
  66. }
  67. return flatList;
  68. }
  69. private KeyValuePair<int, string> GetParent(int budgetId, List<FmsBudgetCodeClass> codeList)
  70. {
  71. // 2019.07.12
  72. Uri uri = Request.RequestUri;
  73. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  74. int siteId;
  75. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false)
  76. {
  77. throw new Exception("Not Found Parameters.");
  78. }
  79. var tempCodeClass = codeList.Where(c => c.SiteId == siteId && c.BudgetClassId == budgetId).FirstOrDefault();
  80. var tempParent = codeList.Where(c => c.SiteId == siteId && c.BudgetClassId == tempCodeClass.ParentBudgetClassId).FirstOrDefault();
  81. if (tempParent == null) { return new KeyValuePair<int, string>(0, ""); }
  82. return new KeyValuePair<int, string>(tempParent.BudgetClassId, tempParent.Name);
  83. }
  84. protected override void Dispose(bool disposing)
  85. {
  86. if (disposing)
  87. {
  88. db.Dispose();
  89. }
  90. base.Dispose(disposing);
  91. }
  92. }
  93. }