IFiannce_ServiceDeliveyBillRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Dapper;
  5. using XYY.Model.Standard.Finance;
  6. using XYY.Core.Standard.Data.Infrastructure;
  7. using System.Threading.Tasks;
  8. using XYY.Model.Standard;
  9. namespace XYY.Data.Standard.Finance
  10. {
  11. public interface IFiannce_ServiceDeliveyBillRepository : IBaseRepository<Finance_ServiceDeliveryBill>
  12. {
  13. Task<AddDeliveryBillResult> AddDeliveryBill(Finance_ServiceDeliveryBill bill, List<Finance_ServiceDeliveryBillDetailInput> details);
  14. Task InputSupplementOrDeduction(int billId, List<Finance_ServiceDeliveryBillDetailInput> details, int type);
  15. Task UpdateConfirmed(int billId, List<Finance_ServiceDeliveryBillDetailInput> details);
  16. Task<List<Finance_ServiceDeliveryBillDetail>> GetDetail(int billId, int type, bool? confirmed);
  17. Task<List<Finance_ServiceDeliveryBillDto>> GetDataAsStatus(int status, string billName);
  18. Task<Dictionary<int, int>> GetGroupCounts(string billName);
  19. Task Pass(int billId);
  20. Task Rereject(int billId);
  21. Task<List<Finance_Logistics_LadingBillLogDto>> GetPaymentLog(int billId);
  22. Task DeleteBill(int billId);
  23. Task FreightRecalculation(int channelId, DateTime start, DateTime end);
  24. }
  25. public class Fiannce_ServiceDeliveyBillRepository : BaseRepository<Finance_ServiceDeliveryBill>, IFiannce_ServiceDeliveyBillRepository
  26. {
  27. public Fiannce_ServiceDeliveyBillRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
  28. {
  29. }
  30. public async Task DeleteBill(int billId)
  31. {
  32. await DeleteByIdAsync(billId);
  33. await _unitOfWork.Connection.ExecuteAsync("delete Finance_ServiceDeliveryBillDetail where BillId=" + billId, null, _unitOfWork.Transaction);
  34. }
  35. public async Task<List<Finance_Logistics_LadingBillLogDto>> GetPaymentLog(int billId)
  36. {
  37. var task = await _unitOfWork.QueryAsync<Finance_ServiceDeliveryBillLog>(x => x.Id == billId);
  38. var list = task.ToList();
  39. List<Finance_Logistics_LadingBillLogDto> dtos = new List<Finance_Logistics_LadingBillLogDto>();
  40. foreach (var item in list)
  41. {
  42. dtos.Add(new Finance_Logistics_LadingBillLogDto
  43. {
  44. OldPayment = item.OldValue,
  45. NewPayment = item.NewValue,
  46. Payment = (decimal.Parse(item.NewValue) - decimal.Parse(item.OldValue)).ToString(),
  47. CreateTime = item.CreateTime.Value
  48. });
  49. }
  50. return dtos;
  51. }
  52. public async Task UpdateConfirmed(int billId, List<Finance_ServiceDeliveryBillDetailInput> details)
  53. {
  54. await _unitOfWork.ExecuteScalarAsync<Finance_ServiceDeliveryBillDetailInput>("delete Finance_ServiceDeliveryBillDetailInput where BillId=" + billId);
  55. details.ForEach(x => { x.BillId = billId; x.CreateTime = DateTime.Now; });
  56. await _unitOfWork.BulkToDBAsync(details);
  57. await _unitOfWork.ExecuteStoredProcedureAsync("UpdateConfirmed", new
  58. {
  59. billId = billId
  60. });
  61. await _unitOfWork.ExecuteStoredProcedureAsync("updateBill", new { billId = billId });
  62. }
  63. public async Task<List<Finance_ServiceDeliveryBillDetail>> GetDetail(int billId, int type, bool? confirmed)
  64. {
  65. string sql = @" select * from Finance_ServiceDeliveryBillDetail where [FeeType]=" + type + " and billId= " + billId;
  66. if (confirmed.HasValue)
  67. sql += " and confirmed = " + (confirmed.Value ? 1 : 0);
  68. var t = await _unitOfWork.QueryBySqlAsync<Finance_ServiceDeliveryBillDetail>(sql);
  69. return t.ToList();
  70. }
  71. public async Task<List<Finance_ServiceDeliveryBillDto>> GetDataAsStatus(int status, string billName)
  72. {
  73. string sql = $@"select * from Finance_ServiceDeliveryBill where Status=" + status;
  74. if (!string.IsNullOrEmpty(billName))
  75. {
  76. sql += " and Name like '%" + billName + "%' ";
  77. }
  78. var t = await _unitOfWork.QueryBySqlAsync<Finance_ServiceDeliveryBillDto>(sql);
  79. return t.ToList();
  80. }
  81. public async Task<Dictionary<int, int>> GetGroupCounts(string billName)
  82. {
  83. string where = "";
  84. if (!string.IsNullOrEmpty(billName))
  85. {
  86. where += " where Name like '%" + billName + "%' ";
  87. }
  88. string sql = $@"select Status as [Key],count(0) as [Value] from Finance_ServiceDeliveryBill {where} group by Status";
  89. var t = await _unitOfWork.QueryBySqlAsync<dynamic>(sql);
  90. return t.ToDictionary(x => (int)x.Key, x => (int)x.Value);
  91. }
  92. public async Task InputSupplementOrDeduction(int billId, List<Finance_ServiceDeliveryBillDetailInput> details, int type)
  93. {
  94. await _unitOfWork.ExecuteScalarAsync<Finance_ServiceDeliveryBillDetailInput>("delete Finance_ServiceDeliveryBillDetailInput where BillId=" + billId);
  95. details.ForEach(x => { x.BillId = billId; x.CreateTime = DateTime.Now; });
  96. await _unitOfWork.BulkToDBAsync(details);
  97. await _unitOfWork.ExecuteStoredProcedureAsync("InputSupplementOrDeduction", new
  98. {
  99. billId = billId,
  100. type = type
  101. });
  102. await _unitOfWork.ExecuteStoredProcedureAsync("updateBill", new { billId = billId });
  103. }
  104. public async Task<AddDeliveryBillResult> AddDeliveryBill(Finance_ServiceDeliveryBill bill, List<Finance_ServiceDeliveryBillDetailInput> details)
  105. {
  106. System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
  107. var repeatData = details.Where(x => !string.IsNullOrEmpty(x.TrackingNumber)).GroupBy(x => x.TrackingNumber).Where(x => x.Count() > 1);
  108. if (repeatData.Count() > 0)
  109. throw new Exception("导入帐单失败,帐单内有重复数据");
  110. //写入帐单主体完成
  111. int billId = (int)(await _unitOfWork.InsertAsync(bill, 1200));
  112. stringBuilder.AppendLine("写入帐单主体完成");
  113. //将数据导入到数据库内分析
  114. await _unitOfWork.ExecuteScalarAsync<Finance_ServiceDeliveryBillDetailInput>("delete Finance_ServiceDeliveryBillDetailInput where BillId=" + billId, 1200);
  115. stringBuilder.AppendLine("清理旧的导入数据完成");
  116. details.ForEach(x => { x.BillId = billId; x.CreateTime = DateTime.Now; });
  117. await _unitOfWork.BulkToDBAsync(details);
  118. stringBuilder.AppendLine("写入本次导入数据完成");
  119. //已计费的帐单不处理
  120. string sql = @"select * from Finance_ServiceDeliveryBillDetailInput(nolock) a where BillId = " + billId + "" +
  121. " and exists (select 1 from Finance_ServiceDeliveryBillDetail(nolock) b where BillId<>" + billId
  122. + " and a.TrackingNumber=b.TrackingNumber and FeeType=0 and DeliveryBillType = " + bill.DeliveryBillType.GetValue() + ") ";
  123. AddDeliveryBillResult result = new AddDeliveryBillResult();
  124. var t = await _unitOfWork.QueryBySqlAsync<Finance_ServiceDeliveryBillDetailInput>(sql, 1200);
  125. stringBuilder.AppendLine("查找已计费帐单完成");
  126. result.RepeatTrackingNumber = t.ToList();
  127. //没有下单的帐单不处理
  128. sql = @"select * from Finance_ServiceDeliveryBillDetailInput(nolock) a where BillId = " + billId + "" +
  129. " and not exists (select 1 from Order_Order(nolock) b where a.TrackingNumber=b.TrackingNumber) ";
  130. var t2 = await _unitOfWork.QueryBySqlAsync<Finance_ServiceDeliveryBillDetailInput>(sql, 1200);
  131. stringBuilder.AppendLine("查找没有下单订单完成");
  132. result.NotDeliveryTrackingNumber = t2.ToList();
  133. result.ErrorList = new List<Finance_ServiceDeliveryBillDetailInput>();
  134. result.RepeatTrackingNumber.ForEach(x => x.Remark = "已导入过数据");
  135. result.NotDeliveryTrackingNumber.ForEach(x => x.Remark = "未称重");
  136. result.ErrorList.AddRange(result.RepeatTrackingNumber);
  137. result.ErrorList.AddRange(result.NotDeliveryTrackingNumber);
  138. if (result.ErrorList.Count > 0)
  139. {
  140. result.ErrorQty = result.ErrorList.Count;
  141. }
  142. if (result.ErrorQty < details.Count)
  143. {
  144. await _unitOfWork.ExecuteStoredProcedureAsync("inputBill", new { billId = billId }, 1200);
  145. stringBuilder.AppendLine("导入帐单完成");
  146. await _unitOfWork.ExecuteStoredProcedureAsync("updateBill", new { billId = billId }, 1200);
  147. stringBuilder.AppendLine("更新主帐单完成");
  148. }
  149. else
  150. {
  151. await _unitOfWork.DeleteByIdAsync<Finance_ServiceDeliveryBill>(billId);
  152. }
  153. return result;
  154. }
  155. public async Task Pass(int billId)
  156. {
  157. var model = await GetAsync(billId);
  158. if (model.Status == FiannceServiceBillStatus.结算中)
  159. throw new Exception("帐单已完结,无须再审核");
  160. if (model.Status == FiannceServiceBillStatus.最终确认)
  161. {
  162. //最终确认通过时,更新应付款金额
  163. model.Receivables = model.ConfirmedFreigh + model.Supplement - Math.Abs(model.Deduction);
  164. }
  165. if (model.Status == FiannceServiceBillStatus.待对帐)
  166. {
  167. model.Status = FiannceServiceBillStatus.最终确认;
  168. }
  169. else
  170. {
  171. model.Status = (FiannceServiceBillStatus)(model.Status.GetValue() + 1);
  172. }
  173. await _unitOfWork.UpdateAsync(model);
  174. }
  175. public async Task Rereject(int billId)
  176. {
  177. var model = await GetAsync(billId);
  178. if (model.Status == FiannceServiceBillStatus.最终确认)
  179. model.Status = FiannceServiceBillStatus.财务确认;
  180. if (model.Status == FiannceServiceBillStatus.结算中)
  181. throw new Exception("帐单已完结,无须再审核");
  182. if (model.Status == FiannceServiceBillStatus.待对帐)
  183. throw new Exception("帐单刚开始对帐,无法驳回");
  184. model.Status = (FiannceServiceBillStatus)(model.Status.GetValue() - 1);
  185. await _unitOfWork.UpdateAsync(model);
  186. }
  187. public async Task FreightRecalculation(int channelId, DateTime start, DateTime end)
  188. {
  189. string sql = @"update b set b.OPSFreight =0 from order_order(nolock) a join order_fee(nolock)b on a.Id=b.OrderId where a.ChannelId=@channelId and ReceiveTime between @start and @end";
  190. await _unitOfWork.Connection.ExecuteAsync(sql, new { channelId, start, end }, _unitOfWork.Transaction);
  191. }
  192. }
  193. }