123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using Dapper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using XYY.Common.Standard;
- using XYY.Core.Standard;
- using XYY.Core.Standard.Data.Infrastructure;
- using XYY.Model.Standard.Finance;
- using XYY.Model.Standard.Order;
- namespace XYY.Data.Standard.Finance
- {
- public interface IFinance_Customer_SupplementRepository : IBaseRepository<Finance_Customer_Supplement>
- {
- Task<List<Finance_Customer_Supplement>> GetInputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list, List<SMPDetailType> detailTypes);
- Task<List<Finance_Customer_Supplement>> InputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list);
- Task<PageResult<ViewFinance_Customer_Supplement>> GetPageResult(QueryModel queryModel);
- }
- public class Finance_Customer_SupplementRepository : BaseRepository<Finance_Customer_Supplement>, IFinance_Customer_SupplementRepository
- {
- public Finance_Customer_SupplementRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
- {
- }
- /// <summary>
- /// 快递资费信息表
- /// </summary>
- [Table("Logistics_Fee_Other")]
- public class Logistics_Fee_Other : BaseEntity
- {
- /// <summary>
- /// 快递编号
- /// </summary>
- public int ExpressId { get; set; }
- /// <summary>
- /// 快递资费类型
- /// </summary>
- public int FeeType { get; set; }
- /// <summary>
- /// 费用
- /// </summary>
- public decimal Price { get; set; }
- /// <summary>
- /// 生效时间
- /// </summary>
- public DateTime EffectiveDate { get; set; }
- /// <summary>
- /// 是否生效
- /// </summary>
- public bool IsEffective { get; set; }
- /// <summary>
- /// COD代收手继费是否为百分比
- /// </summary>
- public bool IsPercentByCodHandCost { get; set; }
- public int CustomerId { get; set; }
- }
- public async Task<List<Finance_Customer_Supplement>> GetInputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list,
- List<SMPDetailType> detailTypes)
- {
- string sql = @"select Id,CustomerId,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where SystemNo in @SystemNo
- union select Id,CustomerId,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where TrackingNumber in @SystemNo ";
- var systemNos = list.Select(x => x.SystemNo).ToArray();
- var orders = await _unitOfWork.QueryBySqlAsync<Order_Order>(sql, param: new { SystemNo = systemNos });
- Dictionary<string, string> errorList = new Dictionary<string, string>();
- foreach (var item in list)
- {
- if (item.FinanceCustomerSupplementType != FinanceCustomerSupplementOrDeductionType.补_Sku处理费)
- {
- try
- {
- var o = orders.Where(x => x.SystemNo == item.SystemNo.Trim() || x.TrackingNumber == item.SystemNo.Trim()).FirstOrDefault();
- if (o == null)
- errorList.Add(item.SystemNo, "找不到相关的订单信息");
- else
- {
- item.CurrenyRate = "RMB";
- item.OrderId = o.Id;
- item.SystemNo = o.SystemNo;
- item.TrackingNumber = o.TrackingNumber;
- item.CustomerOrderNo = o.CustomerOrderNo;
- item.CustomerId = o.CustomerId;
- item.CreateOrderChannelId = o.CreateOrderChannelId;
- if (string.IsNullOrEmpty(item.SMPDetailType))
- {
- errorList.Add(item.SystemNo, "费用类型必须填写");
- }
- else
- {
- var type = detailTypes.FirstOrDefault(x => x.ItemName == item.SMPDetailType);
- if (type == null)
- {
- errorList.Add(item.SystemNo, $"未找到费用类型{item.SMPDetailType},请核对费用类型表后再找联系技术员");
- }
- else
- {
- item.SMPDetailTypeId = type.Id;
- if (item.FeeString.Contains("+燃油"))
- {
- //如果包含燃油费用的话,我们对费用进行一次计算
- }
- else
- {
- string regex = @"^(\d+(\.\d+)?)([A-Za-z]{3})$";
- if (Regex.IsMatch(item.FeeString, regex))
- {
- //币种
- var maths = Regex.Match(item.FeeString, regex, RegexOptions.IgnoreCase);
- if (maths.Groups.Count > 0)
- {
- item.CurrenyRate = maths.Groups[2].Value;
- item.Fee = decimal.Parse(maths.Groups[1].Value);
- }
- }
- else
- {
- string r = @"^(\d+(\.\d+)?)$";
- if (Regex.IsMatch(item.FeeString, r))
- {
- item.Fee = decimal.Parse(Regex.Match(item.FeeString, r, RegexOptions.IgnoreCase).Value);
- }
- else
- {
- errorList.Add(item.SystemNo, "无法识别该费用");
- }
- }
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- errorList.Add(item.SystemNo, ex.Message);
- }
- }
- else
- {
- var type = detailTypes.FirstOrDefault(x => x.ItemName == item.SMPDetailType);
- if (type == null)
- {
- errorList.Add(item.SystemNo, $"未找到费用类型{item.SMPDetailType},请核对费用类型表后再找联系技术员");
- }
- else
- {
- item.SMPDetailTypeId = type.Id;
- }
- }
- }
- if (errorList.Count > 0)
- {
- StringBuilder stringBuilder = new StringBuilder();
- foreach (var item in errorList)
- {
- stringBuilder.AppendLine(item.Key + "," + item.Value);
- }
- throw new Exception(stringBuilder.ToString());
- }
- else
- {
- list.ForEach(x => { x.CreateTime = DateTime.Now; x.CreateUserName = _unitOfWork.CurrentName; });
- return list;
- }
- }
- public async Task<List<Logistics_Fee_Other>> GetOthers()
- {
- var data = (await _unitOfWork.QueryBySqlAsync<Logistics_Fee_Other>($"select * from Logistics_Fee_Other(nolock) where FeeType=2 and EffectiveDate<='{DateTime.Now.ToString_yyyyMMddHHmmss()}'")).ToList();
- return data;
- }
- public async Task<List<Finance_Customer_Supplement>> InputFinanceCustomerSupplements(List<Finance_Customer_Supplement> list)
- {
- var fjf = await GetOthers();
- string sql = @"select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where SystemNo in @SystemNo
- union select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where TrackingNumber in @SystemNo
- union select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where TransferNumber in @SystemNo
- union select Id,CustomerId,ServicesOrderNo,TransferNumber,SystemNo,TrackingNumber,CustomerOrderNo,CreateOrderChannelId from Order_Order(nolock) where ServicesOrderNo in @SystemNo";
- var systemNos = list.Select(x => x.SystemNo).ToArray();
- var orders = await _unitOfWork.QueryBySqlAsync<Order_Order>(sql, param: new { SystemNo = systemNos });
- Dictionary<string, string> errorList = new Dictionary<string, string>();
- foreach (var item in list)
- {
- if (item.FinanceCustomerSupplementType != FinanceCustomerSupplementOrDeductionType.补_Sku处理费)
- {
- try
- {
- var o = orders.Where(x => x.SystemNo == item.SystemNo.Trim() || x.TrackingNumber == item.SystemNo.Trim()
- || x.ServicesOrderNo == item.SystemNo.Trim() || x.TransferNumber == item.SystemNo.Trim()).FirstOrDefault();
- if (o == null)
- errorList.Add(item.SystemNo, "找不到相关的订单信息");
- else
- {
- item.CurrenyRate = "RMB";
- item.OrderId = o.Id;
- item.SystemNo = o.SystemNo;
- item.TrackingNumber = o.TrackingNumber;
- item.CustomerOrderNo = o.CustomerOrderNo;
- item.CustomerId = o.CustomerId;
- item.CreateOrderChannelId = o.CreateOrderChannelId;
- if (item.FeeString.Contains("+燃油"))
- {
- //如果包含燃油费用的话,我们对费用进行一次计算
- item.Fee = decimal.Parse(item.FeeString.Replace("+燃油", ""));
- int year = int.Parse(item.Month.Split('-')[0]);
- int month = int.Parse(item.Month.Split('-')[1]);
- int days = DateTime.DaysInMonth(year, month);
- DateTime monthLastDay = Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-" + days.ToString());
- //取客户,渠道,最后一份生效时间的燃油
- var c = fjf.Where(x => x.CustomerId == item.CustomerId && x.IsEffective && x.ExpressId == o.CreateOrderChannelId && x.EffectiveDate <= monthLastDay).OrderByDescending(x => x.EffectiveDate).FirstOrDefault();
- if (c != null)
- {
- item.Fee *= (1 + c.Price);
- }
- }
- else
- {
- string regex = @"^(\d+(\.\d+)?)([A-Za-z]{3})$";
- if (Regex.IsMatch(item.FeeString, regex))
- {
- //币种
- var maths = Regex.Match(item.FeeString, regex, RegexOptions.IgnoreCase);
- if (maths.Groups.Count > 0)
- {
- item.CurrenyRate = maths.Groups[2].Value;
- item.Fee = decimal.Parse(maths.Groups[1].Value);
- }
- }
- else
- {
- string r = @"^(\d+(\.\d+)?)$";
- if (Regex.IsMatch(item.FeeString, r))
- {
- item.Fee = decimal.Parse(Regex.Match(item.FeeString, r, RegexOptions.IgnoreCase).Value);
- }
- else
- {
- errorList.Add(item.SystemNo, "无法识别该费用");
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- errorList.Add(item.SystemNo, ex.Message);
- }
- }
- else
- {
- if (await IsExistsAsync(x => x.SystemNo == item.SystemNo && x.FinanceCustomerSupplementType
- == FinanceCustomerSupplementOrDeductionType.补_Sku处理费))
- {
- errorList.Add(item.SystemNo, $"已存在SKU处理费{item.SystemNo}");
- }
- }
- }
- if (errorList.Count > 0)
- {
- StringBuilder stringBuilder = new StringBuilder();
- foreach (var item in errorList)
- {
- stringBuilder.AppendLine(item.Key + "," + item.Value);
- }
- throw new Exception(stringBuilder.ToString());
- }
- else
- {
- list.ForEach(x => { x.CreateTime = DateTime.Now; x.CreateUserName = _unitOfWork.CurrentName; });
- await _unitOfWork.BulkToDBAsync(list);
- return list;
- }
- }
- public async Task<PageResult<ViewFinance_Customer_Supplement>> GetPageResult(QueryModel queryModel)
- {
- string sql = @"select a.*,b.CompanyName as CustomerName,d.Name as PublicName from Finance_Customer_Supplement(nolock) a
- join User_Customer(nolock)b on a.CustomerId = b.Id
- join Logistics_Channel(nolock) c on c.Id = a.CreateOrderChannelId
- join Logistics_Public(nolock) d on d.Code = c.PublicCode";
- return await _unitOfWork.GetPagingListAsync<ViewFinance_Customer_Supplement>(queryModel, sql);
- }
- }
- }
|