PerformanceItemController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using wispro.sp.entity;
  10. using wispro.sp.share;
  11. namespace wispro.sp.api.Controllers
  12. {
  13. [Route("api/[controller]/[action]")]
  14. [ApiController]
  15. //[Authorize]
  16. public class PerformanceItemController : ControllerBase
  17. {
  18. spDbContext Context;
  19. public PerformanceItemController(spDbContext context)
  20. {
  21. Context = context;
  22. }
  23. public ApiSaveResponse New(PerformanceItem item)
  24. {
  25. ApiSaveResponse ret = new ApiSaveResponse();
  26. ret.Success = true;
  27. using (Context.Database.BeginTransaction())
  28. {
  29. try
  30. {
  31. if(item.CalMonth != null)
  32. {
  33. var calMonth = Context.CalMonths.Where<CalMonth>(c => c.Year == item.CalMonth.Year && c.Month == item.CalMonth.Month).FirstOrDefault();
  34. if(calMonth == null)
  35. {
  36. Context.CalMonths.Add(item.CalMonth);
  37. Context.SaveChanges();
  38. }
  39. else
  40. {
  41. item.CalMonth = calMonth;
  42. }
  43. item.CalMonthId = item.CalMonth.Id;
  44. item.CalMonth = null;
  45. }
  46. if (!string.IsNullOrEmpty(item.Customer.Name))
  47. {
  48. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == item.Customer.Name).FirstOrDefault();
  49. if (temCustomer == null)
  50. {
  51. Context.Customers.Add(item.Customer);
  52. Context.SaveChanges();
  53. }
  54. else
  55. {
  56. item.Customer = temCustomer;
  57. }
  58. item.CustomerId = item.Customer.Id;
  59. item.Customer = null;
  60. }
  61. else
  62. {
  63. item.Customer = null;
  64. }
  65. List<PerformanceItem> items = Context.PerformanceItems.Where<PerformanceItem>(x =>
  66. x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告").ToList();
  67. if (items.Count > 0)
  68. {
  69. ret.Success = false;
  70. ret.ErrorMessage = $"案件【{item.CaseNo}】处理事项【{item.DoItem }】已存在!";
  71. }
  72. else
  73. {
  74. var ItemStaffs = item.ItemStaffs;
  75. item.ItemStaffs = null;
  76. Context.PerformanceItems.Add(item);
  77. Context.SaveChanges();
  78. foreach (ItemStaff itemStaff in ItemStaffs)
  79. {
  80. itemStaff.ItemId = item.Id;
  81. itemStaff.Item = null;
  82. if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null)
  83. {
  84. var temStaff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == itemStaff.DoPerson.Name);
  85. if (temStaff != null)
  86. {
  87. itemStaff.DoPersonId = temStaff.Id;
  88. itemStaff.DoPerson = null;
  89. }
  90. else
  91. {
  92. Context.Staffs.Add(itemStaff.DoPerson);
  93. Context.SaveChanges();
  94. itemStaff.DoPersonId = itemStaff.DoPerson.Id;
  95. itemStaff.DoPerson = null;
  96. }
  97. }
  98. }
  99. Context.ItemStaffs.AddRange(ItemStaffs);
  100. Context.SaveChanges();
  101. }
  102. Context.Database.CommitTransaction();
  103. }
  104. catch (Exception ex)
  105. {
  106. ret.Success = false;
  107. ret.ErrorMessage = ex.Message;
  108. Context.Database.RollbackTransaction();
  109. }
  110. }
  111. return ret;
  112. }
  113. public ListApiResponse<PerformanceItem> Query(int pageIndex,int pageSize)
  114. {
  115. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  116. var results = Context.PerformanceItems
  117. .Where<PerformanceItem>(s =>
  118. (s.ItemStaffs.Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == User.Identity.Name).Count() > 0 || s.Reviewer.Name == User.Identity.Name)
  119. && s.CalMonth.Month == 7 && s.CalMonth.Year == 2021);
  120. ret.TotalCount = results.Count();
  121. List<PerformanceItem> retList = results
  122. .Include(pi=>pi.ItemStaffs).ThenInclude(iStaff=>iStaff.DoPerson)
  123. .Include(pi=>pi.Reviewer)
  124. .Include(pi=>pi.Customer)
  125. .OrderByDescending(o=>o.Id)
  126. .Skip<PerformanceItem>((pageIndex - 1) * pageSize).Take(pageSize).ToList<PerformanceItem>();
  127. ret.Results = retList;
  128. return ret;
  129. }
  130. public ListApiResponse<PerformanceItem> QueryFilter(QueryFilter queryFilter)
  131. {
  132. ListApiResponse<PerformanceItem> ret = new ListApiResponse<PerformanceItem>();
  133. ret.TotalCount = Context.PerformanceItems.Count<PerformanceItem>();
  134. List<PerformanceItem> retList = Context.PerformanceItems
  135. .Where<PerformanceItem>(s => (s.ItemStaffs
  136. .Where<ItemStaff>(iStaff => iStaff.DoPerson.Name == queryFilter.PersonName) .Count() > 0 || s.Reviewer.Name == queryFilter.PersonName) && s.CalMonth.Month == queryFilter.Month && s.CalMonth.Year == queryFilter.Year)
  137. .Include(pi => pi.ItemStaffs).ThenInclude(iStaff => iStaff.DoPerson)
  138. .Include(pi => pi.Reviewer)
  139. .Include(pi => pi.Customer)
  140. .OrderByDescending(o => o.Id)
  141. .Skip<PerformanceItem>((queryFilter.pageIndex - 1) * queryFilter.pageSize).Take(queryFilter.pageSize).ToList<PerformanceItem>();
  142. //Context.PerformanceItems.Where<PerformanceItem>(p=>p.ItemStaffs.Where<ItemStaff>)
  143. ret.Results = retList;
  144. return ret;
  145. }
  146. }
  147. }