IFinance_Customer_SupplementRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using Dapper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using XYY.Common.Standard;
  9. using XYY.Core.Standard;
  10. using XYY.Core.Standard.Data.Infrastructure;
  11. using XYY.Model.Standard.Finance;
  12. using XYY.Model.Standard.Order;
  13. namespace XYY.Data.Standard.Finance
  14. {
  15. public interface IFinance_Customer_SupplementRepository : IBaseRepository<Finance_Customer_Supplement>
  16. {
  17. Task<List<Finance_Customer_Supplement>> GetInputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list, List<SMPDetailType> detailTypes);
  18. Task<List<Finance_Customer_Supplement>> InputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list);
  19. Task<PageResult<ViewFinance_Customer_Supplement>> GetPageResult(QueryModel queryModel);
  20. }
  21. public class Finance_Customer_SupplementRepository : BaseRepository<Finance_Customer_Supplement>, IFinance_Customer_SupplementRepository
  22. {
  23. public Finance_Customer_SupplementRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
  24. {
  25. }
  26. /// <summary>
  27. /// 快递资费信息表
  28. /// </summary>
  29. [Table("Logistics_Fee_Other")]
  30. public class Logistics_Fee_Other : BaseEntity
  31. {
  32. /// <summary>
  33. /// 快递编号
  34. /// </summary>
  35. public int ExpressId { get; set; }
  36. /// <summary>
  37. /// 快递资费类型
  38. /// </summary>
  39. public int FeeType { get; set; }
  40. /// <summary>
  41. /// 费用
  42. /// </summary>
  43. public decimal Price { get; set; }
  44. /// <summary>
  45. /// 生效时间
  46. /// </summary>
  47. public DateTime EffectiveDate { get; set; }
  48. /// <summary>
  49. /// 是否生效
  50. /// </summary>
  51. public bool IsEffective { get; set; }
  52. /// <summary>
  53. /// COD代收手继费是否为百分比
  54. /// </summary>
  55. public bool IsPercentByCodHandCost { get; set; }
  56. public int CustomerId { get; set; }
  57. }
  58. public async Task<List<Finance_Customer_Supplement>> GetInputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list,
  59. List<SMPDetailType> detailTypes)
  60. {
  61. string sql = @"select Id,CustomerId,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where SystemNo in @SystemNo
  62. union select Id,CustomerId,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where TrackingNumber in @SystemNo ";
  63. var systemNos = list.Select(x => x.SystemNo).ToArray();
  64. var orders = await _unitOfWork.QueryBySqlAsync<Order_Order>(sql, param: new { SystemNo = systemNos });
  65. Dictionary<string, string> errorList = new Dictionary<string, string>();
  66. foreach (var item in list)
  67. {
  68. if (item.FinanceCustomerSupplementType != FinanceCustomerSupplementOrDeductionType.补_Sku处理费)
  69. {
  70. try
  71. {
  72. var o = orders.Where(x => x.SystemNo == item.SystemNo.Trim() || x.TrackingNumber == item.SystemNo.Trim()).FirstOrDefault();
  73. if (o == null)
  74. errorList.Add(item.SystemNo, "找不到相关的订单信息");
  75. else
  76. {
  77. item.CurrenyRate = "RMB";
  78. item.OrderId = o.Id;
  79. item.SystemNo = o.SystemNo;
  80. item.TrackingNumber = o.TrackingNumber;
  81. item.CustomerOrderNo = o.CustomerOrderNo;
  82. item.CustomerId = o.CustomerId;
  83. item.CreateOrderChannelId = o.CreateOrderChannelId;
  84. if (string.IsNullOrEmpty(item.SMPDetailType))
  85. {
  86. errorList.Add(item.SystemNo, "费用类型必须填写");
  87. }
  88. else
  89. {
  90. var type = detailTypes.FirstOrDefault(x => x.ItemName == item.SMPDetailType);
  91. if (type == null)
  92. {
  93. errorList.Add(item.SystemNo, $"未找到费用类型{item.SMPDetailType},请核对费用类型表后再找联系技术员");
  94. }
  95. else
  96. {
  97. item.SMPDetailTypeId = type.Id;
  98. if (item.FeeString.Contains("+燃油"))
  99. {
  100. //如果包含燃油费用的话,我们对费用进行一次计算
  101. }
  102. else
  103. {
  104. string regex = @"^(\d+(\.\d+)?)([A-Za-z]{3})$";
  105. if (Regex.IsMatch(item.FeeString, regex))
  106. {
  107. //币种
  108. var maths = Regex.Match(item.FeeString, regex, RegexOptions.IgnoreCase);
  109. if (maths.Groups.Count > 0)
  110. {
  111. item.CurrenyRate = maths.Groups[2].Value;
  112. item.Fee = decimal.Parse(maths.Groups[1].Value);
  113. }
  114. }
  115. else
  116. {
  117. string r = @"^(\d+(\.\d+)?)$";
  118. if (Regex.IsMatch(item.FeeString, r))
  119. {
  120. item.Fee = decimal.Parse(Regex.Match(item.FeeString, r, RegexOptions.IgnoreCase).Value);
  121. }
  122. else
  123. {
  124. errorList.Add(item.SystemNo, "无法识别该费用");
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. errorList.Add(item.SystemNo, ex.Message);
  135. }
  136. }
  137. else
  138. {
  139. var type = detailTypes.FirstOrDefault(x => x.ItemName == item.SMPDetailType);
  140. if (type == null)
  141. {
  142. errorList.Add(item.SystemNo, $"未找到费用类型{item.SMPDetailType},请核对费用类型表后再找联系技术员");
  143. }
  144. else
  145. {
  146. item.SMPDetailTypeId = type.Id;
  147. }
  148. }
  149. }
  150. if (errorList.Count > 0)
  151. {
  152. StringBuilder stringBuilder = new StringBuilder();
  153. foreach (var item in errorList)
  154. {
  155. stringBuilder.AppendLine(item.Key + "," + item.Value);
  156. }
  157. throw new Exception(stringBuilder.ToString());
  158. }
  159. else
  160. {
  161. list.ForEach(x => { x.CreateTime = DateTime.Now; x.CreateUserName = _unitOfWork.CurrentName; });
  162. return list;
  163. }
  164. }
  165. public async Task<List<Logistics_Fee_Other>> GetOthers()
  166. {
  167. var data = (await _unitOfWork.QueryBySqlAsync<Logistics_Fee_Other>($"select * from Logistics_Fee_Other(nolock) where FeeType=2 and EffectiveDate<='{DateTime.Now.ToString_yyyyMMddHHmmss()}'")).ToList();
  168. return data;
  169. }
  170. public async Task<List<Finance_Customer_Supplement>> InputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list)
  171. {
  172. var fjf = await GetOthers();
  173. string sql = @"select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where SystemNo in @SystemNo
  174. union select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where TrackingNumber in @SystemNo
  175. union select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where TransferNumber in @SystemNo
  176. union select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where ServicesOrderNo in @SystemNo";
  177. var systemNos = list.Select(x => x.SystemNo).ToArray();
  178. var orders = await _unitOfWork.QueryBySqlAsync<Order_Order>(sql, param: new { SystemNo = systemNos });
  179. Dictionary<string, string> errorList = new Dictionary<string, string>();
  180. foreach (var item in list)
  181. {
  182. if (item.FinanceCustomerSupplementType != FinanceCustomerSupplementOrDeductionType.补_Sku处理费)
  183. {
  184. try
  185. {
  186. var o = orders.Where(x => x.SystemNo == item.SystemNo.Trim() || x.TrackingNumber == item.SystemNo.Trim()
  187. || x.ServicesOrderNo == item.SystemNo.Trim() || x.TransferNumber == item.SystemNo.Trim()).FirstOrDefault();
  188. if (o == null)
  189. errorList.Add(item.SystemNo, "找不到相关的订单信息");
  190. else
  191. {
  192. item.CurrenyRate = "RMB";
  193. item.OrderId = o.Id;
  194. item.SystemNo = o.SystemNo;
  195. item.TrackingNumber = o.TrackingNumber;
  196. item.CustomerOrderNo = o.CustomerOrderNo;
  197. item.CustomerId = o.CustomerId;
  198. item.CreateOrderChannelId = o.CreateOrderChannelId;
  199. if (item.FeeString.Contains("+燃油"))
  200. {
  201. //如果包含燃油费用的话,我们对费用进行一次计算
  202. item.Fee = decimal.Parse(item.FeeString.Replace("+燃油", ""));
  203. int year = int.Parse(item.Month.Split('-')[0]);
  204. int month = int.Parse(item.Month.Split('-')[1]);
  205. int days = DateTime.DaysInMonth(year, month);
  206. DateTime monthLastDay = Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-" + days.ToString());
  207. //取客户,渠道,最后一份生效时间的燃油
  208. var c = fjf.Where(x => x.CustomerId == item.CustomerId && x.IsEffective && x.ExpressId == o.CreateOrderChannelId && x.EffectiveDate <= monthLastDay).OrderByDescending(x => x.EffectiveDate).FirstOrDefault();
  209. if (c != null)
  210. {
  211. item.Fee *= (1 + c.Price);
  212. }
  213. }
  214. else
  215. {
  216. string regex = @"^(\d+(\.\d+)?)([A-Za-z]{3})$";
  217. if (Regex.IsMatch(item.FeeString, regex))
  218. {
  219. //币种
  220. var maths = Regex.Match(item.FeeString, regex, RegexOptions.IgnoreCase);
  221. if (maths.Groups.Count > 0)
  222. {
  223. item.CurrenyRate = maths.Groups[2].Value;
  224. item.Fee = decimal.Parse(maths.Groups[1].Value);
  225. }
  226. }
  227. else
  228. {
  229. string r = @"^(\d+(\.\d+)?)$";
  230. if (Regex.IsMatch(item.FeeString, r))
  231. {
  232. item.Fee = decimal.Parse(Regex.Match(item.FeeString, r, RegexOptions.IgnoreCase).Value);
  233. }
  234. else
  235. {
  236. errorList.Add(item.SystemNo, "无法识别该费用");
  237. }
  238. }
  239. }
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. errorList.Add(item.SystemNo, ex.Message);
  245. }
  246. }
  247. else
  248. {
  249. if (await IsExistsAsync(x => x.SystemNo == item.SystemNo && x.FinanceCustomerSupplementType
  250. == FinanceCustomerSupplementOrDeductionType.补_Sku处理费))
  251. {
  252. errorList.Add(item.SystemNo, $"已存在SKU处理费{item.SystemNo}");
  253. }
  254. }
  255. }
  256. if (errorList.Count > 0)
  257. {
  258. StringBuilder stringBuilder = new StringBuilder();
  259. foreach (var item in errorList)
  260. {
  261. stringBuilder.AppendLine(item.Key + "," + item.Value);
  262. }
  263. throw new Exception(stringBuilder.ToString());
  264. }
  265. else
  266. {
  267. list.ForEach(x => { x.CreateTime = DateTime.Now; x.CreateUserName = _unitOfWork.CurrentName; });
  268. await _unitOfWork.BulkToDBAsync(list);
  269. return list;
  270. }
  271. }
  272. public async Task<PageResult<ViewFinance_Customer_Supplement>> GetPageResult(QueryModel queryModel)
  273. {
  274. string sql = @"select a.*,b.CompanyName as CustomerName,d.Name as PublicName from Finance_Customer_Supplement(nolock) a
  275. join User_Customer(nolock)b on a.CustomerId = b.Id
  276. join Logistics_Channel(nolock) c on c.Id = a.CreateOrderChannelId
  277. join Logistics_Public(nolock) d on d.Code = c.PublicCode";
  278. return await _unitOfWork.GetPagingListAsync<ViewFinance_Customer_Supplement>(queryModel, sql);
  279. }
  280. }
  281. }