721 lines
33 KiB
C#
721 lines
33 KiB
C#
using Hg.Core.Domain.Dto.OrderRefund;
|
||
using StackExchange.Redis;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
|
||
namespace Hg.Core.Domain
|
||
{
|
||
internal class OrderRefundApplyDomain : IOrderRefundApplyDomain
|
||
{
|
||
private readonly IBaseRepository<ZxdDbContext> _zxdRepository;
|
||
private readonly IRedisManager _redisManager;
|
||
private readonly IConfiguration _configuration;
|
||
private readonly IHttpClient _httpClient;
|
||
private readonly IMapper _mapper;
|
||
private readonly SystemConfig _systemConfig;
|
||
private readonly IInneruserDomain _inneruserDomain;
|
||
private readonly ICacheDomain _cacheDomain;
|
||
|
||
public OrderRefundApplyDomain(IBaseRepository<ZxdDbContext> zxdRepository,
|
||
IRedisManager redisManager,
|
||
IConfiguration configuration,
|
||
IMapper mapper,
|
||
IHttpClient httpClient,
|
||
IInneruserDomain inneruserDomain,
|
||
ICacheDomain cacheDomain)
|
||
{
|
||
_zxdRepository = zxdRepository;
|
||
_redisManager = redisManager;
|
||
_mapper = mapper;
|
||
_httpClient = httpClient;
|
||
_configuration = configuration;
|
||
_inneruserDomain = inneruserDomain;
|
||
_cacheDomain = cacheDomain;
|
||
_systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
||
}
|
||
|
||
public async Task<OrderRefundDto> GetOrderRefund(int id)
|
||
{
|
||
var query = from a in _zxdRepository.GetRepository<OrderRefundApply>().Query()
|
||
select new OrderRefundDto
|
||
{
|
||
Id = a.Id,
|
||
Deptid = a.Deptid,
|
||
ApplyStatus = a.ApplyStatus,
|
||
RefundDate = a.RefundDate,
|
||
RefundNo = a.RefundNo,
|
||
RefundName = a.RefundName,
|
||
RefundPrice = a.RefundPrice,
|
||
Cname = a.Cname,
|
||
ComplaintId = a.ComplaintId,
|
||
RefundRemark = a.RefundRemark,
|
||
CreateTime = a.CreateTime,
|
||
Creator = a.Creator,
|
||
Eid = a.Eid,
|
||
Orderid = a.Orderid,
|
||
PayStatus = a.PayStatus,
|
||
RefundStatus = a.RefundStatus,
|
||
BusinessStatus = a.BusinessStatus,
|
||
RefundType = a.RefundType,
|
||
Resid = a.Resid,
|
||
Updater = a.Updater,
|
||
UpdateTime = a.UpdateTime,
|
||
CreateEid = a.CreateEid,
|
||
UpdateEid = a.UpdateEid,
|
||
ComplianceResult = a.ComplianceResult,
|
||
ComplianceRemark = a.ComplianceRemark,
|
||
Attachment = a.Attachment,
|
||
ComplianceAttachment = a.ComplianceAttachment,
|
||
};
|
||
|
||
var result = await query.FirstOrDefaultAsync(x => x.Id == id);
|
||
if (result == null) throw new ApiException("外诉数据不存在或已删除!");
|
||
return result;
|
||
}
|
||
|
||
public async Task<PageResult<OrderRefundDto>> GetOrderRefundPage(SearchOrderRefundDto dto)
|
||
{
|
||
var eids = new List<int>();
|
||
|
||
#region MUID转RESID
|
||
|
||
if (!string.IsNullOrEmpty(dto.UMID) && string.IsNullOrEmpty(dto.Resid))
|
||
{
|
||
var UMIDMain = await _zxdRepository.GetRepository<RES_CUSTOMER>().Query().FirstOrDefaultAsync(m => m.UMID == dto.UMID);
|
||
if (UMIDMain != null)
|
||
{
|
||
dto.Resid = UMIDMain.RESID;
|
||
}
|
||
else
|
||
{
|
||
dto.Resid = "NULL_RESID";
|
||
}
|
||
}
|
||
|
||
#endregion MUID转RESID
|
||
|
||
if (!string.IsNullOrWhiteSpace(dto.Eids))
|
||
eids = dto.Eids.Split(",").Select(x => int.Parse(x)).ToList();
|
||
|
||
var deptList = await _cacheDomain.GetDeptments();
|
||
var departIds = new List<int>();
|
||
if (!string.IsNullOrWhiteSpace(dto.Channel))
|
||
{
|
||
var arr0 = dto.Channel.Split('|');
|
||
|
||
foreach (var item0 in arr0)
|
||
{
|
||
var arr1 = item0.Split(',');
|
||
|
||
foreach (var item in arr1)
|
||
{
|
||
//var dept = deptList.Where(y => y.DeptmentCampains.Any(a => "2500-2900" == $"{a.StartCampainId}-{a.EndCampainId}" || "3000-3900" == $"{a.StartCampainId}-{a.EndCampainId}"));
|
||
var a = item.Split(';');
|
||
var min = Convert.ToInt32(a[0]);
|
||
var max = Convert.ToInt32(a[1]);
|
||
|
||
var ids = deptList.Where(x => x.DeptmentCampains.Any(y => y.StartCampainId >= min && y.EndCampainId <= max)).Select(x => x.Id).ToList();
|
||
departIds.AddRange(ids);
|
||
}
|
||
}
|
||
}
|
||
|
||
var showDeptIds = new List<int>();
|
||
if (!string.IsNullOrWhiteSpace(dto.ShowDeptIds))
|
||
showDeptIds = dto.ShowDeptIds.Split(',').Select(x => int.Parse(x)).ToList();
|
||
|
||
var query = from a in _zxdRepository.GetRepository<OrderRefundApply>().Query()
|
||
// join b in _zxdRepository.GetRepository<RES_CUSTOMER>().Query() on a.Resid equals b.RESID into ruc
|
||
// from ru in ruc.DefaultIfEmpty()
|
||
select new OrderRefundDto
|
||
{
|
||
Id = a.Id,
|
||
Deptid = a.Deptid,
|
||
ApplyStatus = a.ApplyStatus,
|
||
RefundDate = a.RefundDate,
|
||
RefundNo = a.RefundNo,
|
||
RefundName = a.RefundName,
|
||
RefundPrice = a.RefundPrice,
|
||
Cname = a.Cname,
|
||
ComplaintId = a.ComplaintId,
|
||
RefundRemark = a.RefundRemark,
|
||
CreateTime = a.CreateTime,
|
||
Creator = a.Creator,
|
||
Eid = a.Eid,
|
||
Orderid = a.Orderid,
|
||
PayStatus = a.PayStatus,
|
||
RefundStatus = a.RefundStatus,
|
||
BusinessStatus = a.BusinessStatus,
|
||
RefundType = a.RefundType,
|
||
Resid = a.Resid,
|
||
// UMID=ru.UMID,
|
||
Updater = a.Updater,
|
||
UpdateTime = a.UpdateTime,
|
||
CreateEid = a.CreateEid,
|
||
UpdateEid = a.UpdateEid,
|
||
Attachment = a.Attachment,
|
||
ComplianceAttachment = a.ComplianceAttachment,
|
||
HasComplianceRemark = !string.IsNullOrEmpty(a.ComplianceRemark),
|
||
HasAttachment = !string.IsNullOrEmpty(a.Attachment),
|
||
ComplianceTime = a.ComplianceTime,
|
||
ComplianceOperator = a.ComplianceOperator,
|
||
ComplianceOperatorEid = a.ComplianceOperatorEid
|
||
};
|
||
|
||
if (!string.IsNullOrWhiteSpace(dto.Eid))
|
||
{
|
||
query = from a in query
|
||
join b in _zxdRepository.GetRepository<BAS_INNERUSER>().Query() on a.Eid equals b.EID
|
||
where (a.Eid.ToString() == dto.Eid || b.UNAME == dto.Eid)
|
||
select a;
|
||
}
|
||
|
||
query = query.If(dto.Deptid != null, x => x.Where(x => x.Deptid == dto.Deptid))
|
||
.If(departIds.Any(), x => x.Where(x => x.Deptid.HasValue && departIds.Contains(x.Deptid.Value)))
|
||
.If(showDeptIds.Any(), x => x.Where(x => x.Deptid.HasValue && showDeptIds.Contains(x.Deptid.Value)))
|
||
.If(dto.ApplyStatus != null, x => x.Where(x => x.ApplyStatus == dto.ApplyStatus))
|
||
.If(dto.BusinessStatus != null, x => x.Where(x => x.BusinessStatus == dto.BusinessStatus))
|
||
.If(dto.RefundStatus != null, x => x.Where(x => x.RefundStatus == dto.RefundStatus))
|
||
.If(dto.RefundType != null, x => x.Where(x => x.RefundType == dto.RefundType))
|
||
.If(dto.Orderid.HasValue, x => x.Where(x => dto.Orderid == x.Orderid))
|
||
.If(dto.PayStatus != null, x => x.Where(x => dto.PayStatus == x.PayStatus))
|
||
.If(eids.Any(), x => x.Where(x => eids.Contains(x.Eid.Value)))
|
||
.If(!string.IsNullOrEmpty(dto.Resid), x => x.Where(x => x.Resid == dto.Resid))
|
||
.If(!string.IsNullOrEmpty(dto.Cname), x => x.Where(x => x.Cname == dto.Cname))
|
||
.If(!string.IsNullOrEmpty(dto.ComplaintId), x => x.Where(x => x.ComplaintId == dto.ComplaintId))
|
||
.If(!string.IsNullOrEmpty(dto.CreaetOrUpdateUser), x => x.Where(x => x.Creator == dto.CreaetOrUpdateUser || x.Updater == dto.CreaetOrUpdateUser))
|
||
.If(dto.DateType == InComplaintDateType.创建日期 && dto.DateFrom != null, x => x.Where(x => x.CreateTime >= dto.DateFrom.Value))
|
||
.If(dto.DateType == InComplaintDateType.更新日期 && dto.DateFrom != null, x => x.Where(x => x.UpdateTime >= dto.DateFrom.Value))
|
||
.If(dto.DateType == InComplaintDateType.审核日期 && dto.DateFrom != null, x => x.Where(x => x.ComplianceTime >= dto.DateFrom.Value))
|
||
.If(dto.DateType == InComplaintDateType.创建日期 && dto.DateTo != null, x => x.Where(x => x.CreateTime < dto.DateTo.Value.AddDays(1).AddMilliseconds(-1)))
|
||
.If(dto.DateType == InComplaintDateType.更新日期 && dto.DateTo != null, x => x.Where(x => x.UpdateTime < dto.DateTo.Value.AddDays(1).AddMilliseconds(-1)))
|
||
.If(dto.DateType == InComplaintDateType.审核日期 && dto.DateTo != null, x => x.Where(x => x.ComplianceTime < dto.DateTo.Value.AddDays(1).AddMilliseconds(-1)))
|
||
.OrderByDescending(x => x.CreateTime)
|
||
.ThenByDescending(x => x.UpdateTime);
|
||
|
||
var total = await query.CountAsync();
|
||
|
||
var data = await query
|
||
.Skip((dto.PageIndex - 1) * dto.PageSize)
|
||
.Take(dto.PageSize)
|
||
.ToListAsync();
|
||
|
||
var innerUsers = await _zxdRepository.GetRepository<BAS_INNERUSER>().Query().Where(x => data.Select(x => x.Eid).Contains(x.EID)).ToListAsync();//在坐席中对应的关系
|
||
|
||
var now = DateTime.Now;
|
||
var rcu = await _zxdRepository.GetRepository<RES_CUSTOMER>().Query().Where(x => data.Select(x => x.Resid).Contains(x.RESID)).ToListAsync();//在坐席中对应的关系
|
||
// _zxdRepository.GetRepository<RES_CUSTOMER>().Query();
|
||
foreach (var item in data)
|
||
{
|
||
var company = deptList.FirstOrDefault(y => y.Id == item.Deptid);
|
||
item.Deptname = company?.Title;
|
||
|
||
var innerUser = innerUsers.FirstOrDefault(x => x.EID == item.Eid);
|
||
item.Euser = item.Eid.HasValue && innerUser != null ? $"{item.Eid}-{innerUser?.UNAME}-{(innerUser?.ISDISMISS == 1 ? "离职" : "在职")}" : item.Eid.ToString();
|
||
var cur = rcu.FirstOrDefault(m => m.RESID == item.Resid);
|
||
|
||
item.UMID = cur.UMID;
|
||
}
|
||
return new PageResult<OrderRefundDto>(dto.PageIndex, dto.PageSize, total, data);
|
||
}
|
||
|
||
public async Task<bool> CreateOrderRefund(CreateOrderRefundDto dto)
|
||
{
|
||
var date = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
|
||
|
||
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query()
|
||
.FirstOrDefaultAsync(x => x.ORDERID == dto.Orderid);
|
||
|
||
if (order == null)
|
||
throw new Exception($"订单编号:{dto.Orderid}不存在");
|
||
|
||
var data = new OrderRefundApply
|
||
{
|
||
Deptid = dto.Deptid,
|
||
RefundType = dto.RefundType,
|
||
RefundDate = dto.RefundDate,
|
||
Cname = dto.Cname,
|
||
ComplaintId = dto.ComplaintId,
|
||
ApplyStatus = OrderRefundApplyStatus.未通过,
|
||
Orderid = dto.Orderid,
|
||
BusinessStatus = OrderRefundBusinessStatus.待确认,
|
||
RefundName = dto.RefundName,
|
||
CreateTime = DateTime.Now,
|
||
Creator = dto.Operator,
|
||
RefundNo = dto.RefundNo,
|
||
RefundPrice = dto.RefundPrice,
|
||
RefundStatus = OrderRefundStatus.未退款,
|
||
RefundRemark = dto.RefundRemark,
|
||
PayStatus = dto.PayStatus,
|
||
Resid = dto.Resid,
|
||
Eid = order.eid,
|
||
CreateEid = dto.OperatorEid,
|
||
Attachment = dto.Attachment
|
||
};
|
||
|
||
var transaction = await _zxdRepository.BeginTransactionAsync();
|
||
try
|
||
{
|
||
await _zxdRepository.GetRepository<OrderRefundApply>().InsertAsync(data);
|
||
|
||
//发送消息
|
||
var sendMessage = new
|
||
{
|
||
Message = $"合规提交了{data.Cname}({data.Resid})的退款申请,退款订单号是{data.Orderid},请及时查看。",
|
||
Eid = order.eid,
|
||
Deptid = dto.Deptid,
|
||
Method = "HgRefundMessage",
|
||
IsDepartment = true,
|
||
};
|
||
await _cacheDomain.SendCrmMessage(sendMessage);
|
||
|
||
await transaction.CommitAsync();
|
||
|
||
return true;
|
||
}
|
||
catch
|
||
{
|
||
await transaction.RollbackAsync();
|
||
await transaction.DisposeAsync();
|
||
throw;
|
||
}
|
||
}
|
||
|
||
public async Task<OrderRefundRestoreDto> GetOrderRefundRestore(int? id)
|
||
{
|
||
var query = from a in _zxdRepository.GetRepository<OrderRefundApply>().Query()
|
||
where a.Id == id
|
||
select new OrderRefundRestoreDto
|
||
{
|
||
Id = a.Id,
|
||
Orderid = a.Orderid,
|
||
Cname = a.Cname,
|
||
RefundPrice = a.RefundPrice,
|
||
Resid = a.Resid,
|
||
RefundType = a.RefundType,
|
||
ComplianceRemark = a.ComplianceRemark,
|
||
ComplianceResult = a.ComplianceResult,
|
||
Operator = a.ComplianceOperator,
|
||
OperatorEid = a.ComplianceOperatorEid,
|
||
Attachment = a.Attachment,
|
||
};
|
||
var data = await query.FirstOrDefaultAsync();
|
||
if (data == null) throw new ApiException("退款申请数据不存在或已删除!");
|
||
return data;
|
||
}
|
||
|
||
public async Task<bool> CreateOrderRefundRestore(CreateOrderRefundRestoreDto dto)
|
||
{
|
||
var data = await _zxdRepository.GetRepository<OrderRefundApply>().Query()
|
||
.Where(a => a.Id == dto.Id)
|
||
.FirstOrDefaultAsync();
|
||
|
||
if (data == null) throw new ApiException("退款申请数据不存在或已删除!");
|
||
|
||
if (data.ApplyStatus != OrderRefundApplyStatus.需COO和合规处理)
|
||
throw new ApiException("只有需COO和合规处理的申请才需要提交处理意见!");
|
||
|
||
data.ComplianceRemark = dto.ComplianceRemark;
|
||
data.ComplianceResult = dto.ComplianceResult;
|
||
data.ComplianceAttachment = dto.Attachment;
|
||
|
||
data.UpdateEid = dto.OperatorEid;
|
||
data.Updater = dto.Operator;
|
||
data.UpdateTime = DateTime.Now;
|
||
if (dto.ComplianceResult == OrderRefundComplianceResult.申请通过)
|
||
{
|
||
data.ApplyStatus = OrderRefundApplyStatus.已通过;
|
||
|
||
try
|
||
{
|
||
//自动创建退款申请
|
||
var refundDto = new OrderRefundAutoDto
|
||
{
|
||
orderid = data.Orderid.Value,
|
||
refundtype = (int)data.PayStatus,
|
||
account = data.RefundNo,
|
||
username = data.Cname,
|
||
refundprice = data.RefundPrice.Value,
|
||
refunddate = DateTime.Now.Date,
|
||
remark = data.RefundRemark,
|
||
creator = data.Eid,
|
||
creatorname = data.Creator,
|
||
};
|
||
int refundId = await CreateOrderRefund(refundDto);
|
||
data.RefundId = refundId;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ApiException("自动创建退款申请失败。原因:" + ex.ToString());
|
||
}
|
||
}
|
||
else if (dto.ComplianceResult == OrderRefundComplianceResult.申请未通过)
|
||
{
|
||
data.ApplyStatus = OrderRefundApplyStatus.未通过;
|
||
}
|
||
await _zxdRepository.GetRepository<OrderRefundApply>().UpdateAsync(data, x => new
|
||
{
|
||
x.ComplianceRemark,
|
||
x.ComplianceResult,
|
||
x.ComplianceOperator,
|
||
x.ComplianceOperatorEid,
|
||
x.ApplyStatus,
|
||
x.ComplianceAttachment,
|
||
x.RefundId
|
||
});
|
||
return true;
|
||
}
|
||
|
||
public async Task<bool> UpdateOrderRefundStatus(UpdateOrderRefundStatusDto dto)
|
||
{
|
||
var data = await _zxdRepository.GetRepository<OrderRefundApply>().Query().FirstOrDefaultAsync(x => x.Id == dto.Id);
|
||
if (data == null) throw new ApiException("退款数据不存在或已删除!");
|
||
|
||
if (data.BusinessStatus != OrderRefundBusinessStatus.待确认)
|
||
throw new ApiException("已经审核确认过,无法重复审核!");
|
||
|
||
data.ComplianceOperatorEid = dto.OperatorEid;
|
||
data.ComplianceOperator = dto.Operator;
|
||
data.ComplianceTime = DateTime.Now;
|
||
data.BusinessStatus = dto.BusinessStatus;
|
||
if (dto.BusinessStatus == OrderRefundBusinessStatus.已确认)
|
||
{
|
||
data.ApplyStatus = OrderRefundApplyStatus.已通过;
|
||
|
||
try
|
||
{
|
||
//自动创建退款申请
|
||
var refundDto = new OrderRefundAutoDto
|
||
{
|
||
orderid = data.Orderid.Value,
|
||
refundtype = (int)data.PayStatus,
|
||
account = data.RefundNo,
|
||
username = data.Cname,
|
||
refundprice = data.RefundPrice.Value,
|
||
refunddate = DateTime.Now.Date,
|
||
remark = data.RefundRemark,
|
||
creator = data.Eid,
|
||
creatorname = data.Creator,
|
||
};
|
||
var refundId = await CreateOrderRefund(refundDto);
|
||
data.RefundId = refundId;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ApiException("自动创建退款申请失败。原因:" + ex.ToString());
|
||
}
|
||
}
|
||
else if (dto.BusinessStatus == OrderRefundBusinessStatus.已拒绝)
|
||
{
|
||
data.ApplyStatus = OrderRefundApplyStatus.需COO和合规处理;
|
||
}
|
||
await _zxdRepository.GetRepository<OrderRefundApply>().UpdateAsync(data, x => new
|
||
{
|
||
x.UpdateEid,
|
||
x.Updater,
|
||
x.UpdateTime,
|
||
x.ApplyStatus,
|
||
x.BusinessStatus,
|
||
x.RefundId
|
||
});
|
||
return true;
|
||
}
|
||
|
||
public async Task<OrderRefundCustomerInfoDto> GetOrderRefundCustomerInfo(string? resid)
|
||
{
|
||
var orderStatus = new List<string>() { "200", "220", "205", "80", "90" };
|
||
|
||
var data = new OrderRefundCustomerInfoDto
|
||
{
|
||
Orders = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query()
|
||
.Where(x => x.RESID == resid && orderStatus.Contains(x.ORDERSTATUS))
|
||
.OrderByDescending(x => x.CTIME)
|
||
.Select(x => new OrderRefundCustomerOrderDto
|
||
{
|
||
Orderid = x.ORDERID,
|
||
Price = x.NEEDPAY,
|
||
Channel = x.CHANNEL,
|
||
}).ToListAsync(),
|
||
Cname = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query()
|
||
.Where(x => x.RESID == resid && orderStatus.Contains(x.ORDERSTATUS))
|
||
.Select(x => x.CNAME)
|
||
.FirstOrDefaultAsync(),
|
||
Complaints = new List<OrderRefundCustomerComplaintDto>()
|
||
};
|
||
|
||
var outComplaints = await _zxdRepository.GetRepository<OutComplaint>().Query()
|
||
.Where(x => x.Resid == resid)
|
||
.OrderByDescending(x => x.CreateTime)
|
||
.Select(x => new OrderRefundCustomerComplaintDto
|
||
{
|
||
ComplaintId = x.ComplaintId,
|
||
ComplaintType = OrderRefundType.外诉,
|
||
}).ToListAsync();
|
||
var inComplaints = await _zxdRepository.GetRepository<InComplaint>().Query()
|
||
.Where(x => x.Resid == resid)
|
||
.OrderByDescending(x => x.CreateTime)
|
||
.Select(x => new OrderRefundCustomerComplaintDto
|
||
{
|
||
ComplaintId = x.ComplaintId,
|
||
ComplaintType = OrderRefundType.内诉,
|
||
}).ToListAsync();
|
||
data.Complaints.AddRange(outComplaints);
|
||
data.Complaints.AddRange(inComplaints);
|
||
|
||
var deptList = await _cacheDomain.GetDeptments();
|
||
if (data != null && data.Orders != null && data.Orders.Any())
|
||
{
|
||
data.Orders.ForEach(x =>
|
||
{
|
||
var dept = deptList.FirstOrDefault(y => y.DeptmentCampains.Any(a => x.Channel >= a.StartCampainId && x.Channel <= a.EndCampainId));
|
||
x.DeptId = dept?.Id;
|
||
});
|
||
}
|
||
return data;
|
||
}
|
||
|
||
public async Task<bool> UpdateRefundStatus(int id)
|
||
{
|
||
var refundApply = await _zxdRepository.GetRepository<OrderRefundApply>().Query()
|
||
.FirstOrDefaultAsync(x => x.RefundId == id);
|
||
|
||
if (refundApply != null)
|
||
{
|
||
refundApply.RefundStatus = OrderRefundStatus.已退款;
|
||
await _zxdRepository.GetRepository<OrderRefundApply>().UpdateAsync(refundApply, x => new
|
||
{
|
||
x.RefundStatus
|
||
});
|
||
|
||
//创建内外诉关联订单
|
||
if (refundApply.RefundType == OrderRefundType.外诉)
|
||
{
|
||
var outComplaintOrder = new OutComplaintOrder
|
||
{
|
||
ComplaintId = refundApply.ComplaintId,
|
||
Orderid = refundApply.Orderid,
|
||
Price = refundApply.RefundPrice
|
||
};
|
||
await _zxdRepository.GetRepository<OutComplaintOrder>().InsertAsync(outComplaintOrder);
|
||
}
|
||
else if (refundApply.RefundType == OrderRefundType.内诉)
|
||
{
|
||
var inComplaintOrder = new InComplaintOrder
|
||
{
|
||
ComplaintId = refundApply.ComplaintId,
|
||
Orderid = refundApply.Orderid,
|
||
Price = refundApply.RefundPrice
|
||
};
|
||
await _zxdRepository.GetRepository<InComplaintOrder>().InsertAsync(inComplaintOrder);
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public async Task SyncOrderRefundApplyStatus()
|
||
{
|
||
var now = DateTime.Now;
|
||
var data = await _zxdRepository.GetRepository<OrderRefundApply>().Query()
|
||
.Where(x => x.BusinessStatus == OrderRefundBusinessStatus.待确认 && x.ApplyStatus == OrderRefundApplyStatus.未通过
|
||
&& x.CreateTime.AddHours(24) <= now)
|
||
.ToListAsync();
|
||
|
||
foreach (var item in data)
|
||
{
|
||
//自动确认日期避开节假日
|
||
var expireTime = await _cacheDomain.AddWorkDays(item.CreateTime, 1);
|
||
if (now < expireTime)
|
||
continue;
|
||
|
||
Log.Error($"到期未确认自动创建退款申请开始。对象:{item.ToJson()}");
|
||
|
||
item.ApplyStatus = OrderRefundApplyStatus.已通过;
|
||
item.ComplianceTime = DateTime.Now;
|
||
item.ComplianceOperator = "admin";
|
||
item.ComplianceOperatorEid = 0;
|
||
|
||
try
|
||
{
|
||
//自动创建退款申请
|
||
var refundDto = new OrderRefundAutoDto
|
||
{
|
||
orderid = item.Orderid.Value,
|
||
refundtype = (int)item.PayStatus,
|
||
account = item.RefundNo,
|
||
username = item.Cname,
|
||
refundprice = item.RefundPrice.Value,
|
||
refunddate = DateTime.Now.Date,
|
||
remark = item.RefundRemark,
|
||
creator = item.Eid,
|
||
creatorname = item.Creator,
|
||
};
|
||
var refundId = await CreateOrderRefund(refundDto);
|
||
item.RefundId = refundId;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error($"到期未确认自动创建退款申请失败。对象:{item.ToJson()}。原因:{ex.ToString()}");
|
||
}
|
||
}
|
||
|
||
if (data.Any())
|
||
{
|
||
await _zxdRepository.GetRepository<OrderRefundApply>().BatchUpdateAsync(data, x => new { x.ApplyStatus, x.ComplianceTime, x.ComplianceOperator, x.ComplianceOperatorEid, x.RefundId });
|
||
}
|
||
}
|
||
|
||
private async Task<int> CreateOrderRefund(OrderRefundAutoDto dto)
|
||
{
|
||
try
|
||
{
|
||
int refundId = 0;
|
||
|
||
dto.isacturalrefund = 1;//默认先都是实际退款
|
||
|
||
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().FirstOrDefaultAsync(x => x.ORDERID == dto.orderid);
|
||
if (order == null)
|
||
throw new Exception("请输入正确的订单号!");
|
||
if (dto.refundprice <= 0)
|
||
{
|
||
throw new Exception("退款金额必须大于零!");
|
||
}
|
||
if (order.ARRIVALPAY == null || order.ARRIVALPAY.Value <= 0)
|
||
{
|
||
throw new Exception("无可退金额!");
|
||
}
|
||
|
||
//dto.creator = dto.eid;
|
||
//dto.creatorname = dto.username;
|
||
dto.applytype = 1;//合规申请
|
||
|
||
var url = await _cacheDomain.GetValueParameter("Core_ZxdService_OrderRefund");
|
||
|
||
if (string.IsNullOrWhiteSpace(url))
|
||
throw new Exception("中心点退款路径不正确!");
|
||
|
||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||
request.Method = "POST";
|
||
//request.ContentType = "application/x-www-form-urlencoded";
|
||
// request.ContentType = "application/json";
|
||
//dto.companycode = companyCode;
|
||
dto.companycode = order.companycode;
|
||
//dto.creator = Convert.ToInt32(dto.eid);
|
||
//dto.creatorname = UserName;
|
||
|
||
Log.Information("自动创建退款信息:合规退款申请列表自动退款:" + dto.ToJson());
|
||
var accessKey = _systemConfig.GetAccessKey("UPWEBSITE");
|
||
|
||
var jiami = SecurityHelper.EncyptData(dto.ToJson(), accessKey);
|
||
var content = HttpUtility.UrlEncode(jiami);
|
||
var sing = HttpUtility.UrlEncode(SecurityHelper.SignData(jiami, accessKey));
|
||
|
||
request.Headers.Add("content", content);
|
||
request.Headers.Add("clientid", "UPWEBSITE");
|
||
request.Headers.Add("sign", sing);
|
||
|
||
//没有参数的时候必须加上这句,这个是HTTP POST要求必须带参数,不然就需要说明长度是0
|
||
request.ContentLength = 0;
|
||
|
||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||
{
|
||
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
||
string result = reader.ReadToEnd();
|
||
Log.Information(result);
|
||
|
||
//var retmsg = SecurityHelper.DecyptData(SecurityHelper.OrderClientIdKey, result);
|
||
var retmsg = SecurityHelper.DecyptData(result, accessKey);
|
||
var retmessage = JsonHelper.FromJson<retMsg>(retmsg);
|
||
if (retmessage.result)
|
||
{
|
||
#region 数据同步给坐席
|
||
|
||
try
|
||
{
|
||
int refid = Convert.ToInt32(retmessage.retmsg);//返回的退款ID
|
||
refundId = refid;
|
||
var refinfo = await _zxdRepository.GetRepository<WX_SzzyOrderRefund>().Query().FirstOrDefaultAsync(x => x.id == refid);// _orderRefund.Get(m => m.id == refid);
|
||
//同步数据给坐席
|
||
var host = _systemConfig.DataSyncApiUrl;
|
||
var syncurl = host + "/api/DataSync";
|
||
var para = new SYNC_PUSH_DTO()
|
||
{
|
||
bidatatype = "Client_RefundInfo",
|
||
deptcode = order.companycode,
|
||
jsontext = refinfo.ToJson()
|
||
};
|
||
//var rep = Utility.PostAjaxData(syncurl, para.ToJson(), Encoding.UTF8);
|
||
var ret = await _httpClient.PostAsync<retMsg>(syncurl, para);
|
||
//var ret = Utility.JSONToObject<retMsg>(rep);
|
||
}
|
||
catch (Exception xx)
|
||
{
|
||
Log.Error("自动创建退款信息:同步给坐席出现问题:" + xx.ToString());
|
||
throw new Exception("自动创建退款信息:同步给坐席出现问题:" + xx.ToString());
|
||
}
|
||
|
||
#endregion 数据同步给坐席
|
||
}
|
||
else
|
||
{
|
||
Log.Error("自动创建退款信息:提交失败," + retmessage.retmsg);
|
||
throw new Exception("自动创建退款信息:提交失败," + retmessage.retmsg);
|
||
}
|
||
|
||
return refundId;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error("自动创建退款信息:提交失败," + ex.ToString());
|
||
throw new Exception("自动创建退款信息:提交失败," + ex.ToString());
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取退款合同信息
|
||
/// </summary>
|
||
/// <param name="orderid"></param>
|
||
/// <returns></returns>
|
||
public async Task<OrderRefundContractDto> GetRefundInfo(int orderid)
|
||
{
|
||
OrderRefundContractDto res = new OrderRefundContractDto();
|
||
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().FirstOrDefaultAsync(x => x.ORDERID == orderid);// _orderRefund.Get(m => m.id == refid);
|
||
res.RefundIdCardRemark = order?.RefundIdCardRemark;
|
||
res.RefundIdCardStatus = order?.RefundIdCardStatus;
|
||
return res;
|
||
}
|
||
|
||
public async Task<bool> RefundIdCardAudit(IdCardAuditDto dto)
|
||
{
|
||
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().FirstOrDefaultAsync(x => x.ORDERID == dto.OrderId);// _orderRefund.Get(m => m.id == refid);
|
||
var contractInfo = await _zxdRepository.GetRepository<BasParameter>().Query().FirstOrDefaultAsync(x => x.PARAKEY == "NewContractInfoUrl"); //
|
||
var url = $"{contractInfo?.PARAVALUE}/v1/api/h5/setOrderRefundIdCardState";
|
||
var nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||
var ctObj = new
|
||
{
|
||
uid = order.SOFTUSERNAME,
|
||
orderId = order.SZZYORDERID.Value.ToString(),
|
||
hqrTime = nowtime,
|
||
orderIdCardState = dto.Status == -1 ? 0 : dto.Status
|
||
};
|
||
var hqr = BlowFish.Encode(ctObj.ToJson());
|
||
var para = new { hqr };
|
||
var ss = para.ToJson();
|
||
var response = StockHttpHelper.PostAjaxData($"{url}", para.ToJson(), "", true);
|
||
var ret = JsonHelper.FromJson<IdCardApiResult>(response);
|
||
if (ret.ret == 0)
|
||
{
|
||
order.RefundIdCardStatus = dto.Status;
|
||
order.RefundIdCardRemark = dto.Remark;
|
||
await _zxdRepository.GetRepository<WX_SZZYORDER>().UpdateAsync(order);
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
} |