CommonBusRepository.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using OASystem.Domain.Common;
  2. using OASystem.Domain.Dtos.Business;
  3. using OASystem.Domain.Entities.Business;
  4. using OASystem.Domain.ViewModels.Business;
  5. using OASystem.Domain.ViewModels.Resource;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace OASystem.Infrastructure.Repositories.Business
  12. {
  13. public class CommonBusRepository : BaseRepository<EntityBase, ViewBase>
  14. {
  15. public CommonBusRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
  16. {
  17. }
  18. #region 可选物料集合
  19. /// <summary>
  20. /// 获取可选物料集合
  21. /// </summary>
  22. /// <returns></returns>
  23. public async Task<List<OptionalBusRangeView>> GetViewList_OptionalItem()
  24. {
  25. List<OptionalBusRangeView> result = new List<OptionalBusRangeView>();
  26. //获取业务范围
  27. string sqlRange = string.Format(@" Select * From Sys_SetData With(Nolock) Where STid = {0} And IsDel=0 ", SetDataTypeDefaultParam.Media);
  28. List<Sys_SetData> entitySetDataList = _sqlSugar.SqlQueryable<Sys_SetData>(sqlRange).ToList();
  29. //获取物料信息
  30. List<Res_ItemInfoView> itemSource = new List<Res_ItemInfoView>();
  31. string sqlItemList = string.Format(@" Select
  32. d.ItemName,d.Id as ItemId,s.Id as ItemTypeSetDataId,d.VendorId,v.FullName as VendorFullName,
  33. v.ShortName as VendorShortName,t.Id as ItemTypeId,t.TypeName as ItemTypeName,
  34. d.MinRate,d.MaxRate,d.CurrRate,d.Remark
  35. From Res_ItemDetail as d With(Nolock)
  36. Inner Join Res_ItemType as t With(Nolock) On d.ItemTypeId = t.Id
  37. Inner Join Res_ItemVendor as v With(Nolock) On d.VendorId=v.Id
  38. Inner Join Sys_SetData as s With(Nolock) On t.SdId=s.Id ");
  39. itemSource = _sqlSugar.SqlQueryable<Res_ItemInfoView>(sqlItemList).ToList();
  40. if (itemSource.Count > 0)
  41. {
  42. foreach (var item in entitySetDataList)
  43. {
  44. List<Res_ItemInfoView> tempList = itemSource.Where(s => s.ItemTypeSetDataId == item.Id).ToList();
  45. if (tempList.Count > 0)
  46. {
  47. OptionalBusRangeView _tempView = new OptionalBusRangeView()
  48. {
  49. SdId = item.Id,
  50. SdName = item.Name
  51. };
  52. List<OptionalItemTypeView> optionalItemTypeViewList = new List<OptionalItemTypeView>();
  53. var itemGroups = tempList.GroupBy(p => p.ItemTypeId);
  54. foreach (var group in itemGroups)
  55. {
  56. OptionalItemTypeView tempOptional = new OptionalItemTypeView();
  57. tempOptional.ItemTypeId = group.Key;
  58. tempOptional.ItemTypeName = group.First().ItemTypeName;
  59. tempOptional.ItemList = new List<Res_ItemInfoView>(group.ToList());
  60. optionalItemTypeViewList.Add(tempOptional);
  61. }
  62. _tempView.ItemTypeList = new List<OptionalItemTypeView>(optionalItemTypeViewList);
  63. result.Add(_tempView);
  64. }
  65. }
  66. }
  67. return result;
  68. }
  69. #endregion
  70. #region 会务物料采购清单
  71. /// <summary>
  72. /// 新增会务物料采购清单和具体采购物料信息
  73. /// </summary>
  74. /// <param name="_dto"></param>
  75. /// <returns></returns>
  76. public async Task<int> Insert_ConfItemList(Edit_OptionalItemListDto _dto)
  77. {
  78. Bus_ConfItemListInfo _check1 = await Query<Bus_ConfItemListInfo>(s => s.Diid == _dto.DiId).FirstAsync();
  79. if (_check1 != null)
  80. {
  81. return -2;//已存在数据,无法新增,请检查BusController中非空验证
  82. }
  83. Bus_ConfItemListInfo _insert = new Bus_ConfItemListInfo();
  84. _insert.Diid = _dto.DiId;
  85. _insert.TotalCost = _dto.ConfItemList.Sum(s => (s.CurrCost * s.Count));
  86. BeginTran();
  87. try
  88. {
  89. int result = await _sqlSugar.Insertable(_insert).ExecuteReturnIdentityAsync();
  90. if (result > 0)
  91. {
  92. List<Bus_ConfItemInfo> insertList = new List<Bus_ConfItemInfo>();
  93. foreach (var item in _dto.ConfItemList)
  94. {
  95. Bus_ConfItemInfo temp = new Bus_ConfItemInfo();
  96. temp.ConfListId = result;
  97. temp.Count = item.Count;
  98. temp.CurrCost = item.CurrCost;
  99. temp.ItemId = item.ItemId;
  100. temp.OpRemark = item.OpRemark;
  101. insertList.Add(temp);
  102. }
  103. _sqlSugar.Insertable(insertList).UseParameter().ExecuteCommand();
  104. return 0;
  105. }
  106. else
  107. {
  108. return -3;//保存清单主体数据失败
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. RollbackTran();
  114. return -4;
  115. }
  116. return -1;
  117. }
  118. /// <summary>
  119. /// 修改会务物料采购清单和具体采购物料信息(未存在删除、暂时)
  120. /// </summary>
  121. /// <param name="_dto"></param>
  122. /// <returns></returns>
  123. public async Task<int> Edit_ConfItemList(Edit_OptionalItemListDto _dto)
  124. {
  125. Bus_ConfItemListInfo _eidt = await Query<Bus_ConfItemListInfo>(s => s.Id == _dto.ConfItemListId).FirstAsync();
  126. if (_eidt == null)
  127. {
  128. return -2;//不存在数据,无法修改,请检查BusController中非空验证
  129. }
  130. _eidt.TotalCost = _dto.ConfItemList.Sum(s => (s.CurrCost * s.Count));
  131. BeginTran();
  132. try
  133. {
  134. int result = await _sqlSugar.Updateable<Bus_ConfItemListInfo>()
  135. .SetColumns(it => it.TotalCost == _eidt.TotalCost)
  136. .Where(s => s.Id == _dto.ConfItemListId)
  137. .ExecuteCommandAsync();
  138. _sqlSugar.Deleteable<Bus_ConfItemInfo>().Where(it => it.ConfListId == _dto.ConfItemListId).ExecuteCommand();
  139. if (result > 0)
  140. {
  141. List<Bus_ConfItemInfo> insertList = new List<Bus_ConfItemInfo>();
  142. foreach (var item in _dto.ConfItemList)
  143. {
  144. Bus_ConfItemInfo temp = new Bus_ConfItemInfo();
  145. temp.ConfListId = result;
  146. temp.Count = item.Count;
  147. temp.CurrCost = item.CurrCost;
  148. temp.ItemId = item.ItemId;
  149. temp.OpRemark = item.OpRemark;
  150. insertList.Add(temp);
  151. }
  152. _sqlSugar.Insertable(insertList).UseParameter().ExecuteCommand();
  153. return 0;
  154. }
  155. else
  156. {
  157. return -3;//保存清单主体数据失败
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. RollbackTran();
  163. return -4;
  164. }
  165. return -1;
  166. }
  167. #endregion
  168. }
  169. }