ComplianceServer/code/Hg.Core.Domain/OutComplaintDomain.cs

903 lines
45 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Core;
using Hg.Core.Domain.Dto.InComplaint;
using Hg.Core.Domain.Dto.OrderRefund;
using Hg.Core.Domain.Dto.OutComplaint;
using Org.BouncyCastle.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hg.Core.Domain
{
internal class OutComplaintDomain : IOutComplaintDomain
{
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 OutComplaintDomain(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<OutComplaintDto> GetOutComplaint(int id)
{
var query = from a in _zxdRepository.GetRepository<OutComplaint>().Query()
select new OutComplaintDto
{
Id = a.Id,
Deptid = a.Deptid,
DueStatus = a.DueStatus,
BusinessDeadline = a.BusinessDeadline,
ComplaintDeadline = a.ComplaintDeadline,
OutComplaintDate = a.OutComplaintDate,
BusinessStatus = a.BusinessStatus,
Cname = a.Cname,
ComplaintId = a.ComplaintId,
Content = a.Content,
CreateTime = a.CreateTime,
Creator = a.Creator,
Eid = a.Eid,
Follow = a.Follow,
OutComplaintChannel = a.OutComplaintChannel,
OutComplaintStatus = a.OutComplaintStatus,
Reason = a.Reason,
Remark = a.Remark,
Resid = a.Resid,
Updater = a.Updater,
UpdateTime = a.UpdateTime,
RiskSituation = a.RiskSituation,
};
var result = await query.FirstOrDefaultAsync(x => x.Id == id);
if (result == null) throw new ApiException("外诉数据不存在或已删除!");
return result;
}
public async Task<PageResult<OutComplaintDto>> GetOutComplaintPage(SearchOutComplaintDto dto)
{
var eids = new List<int>();
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>();
#region UMID转RESID
if (!string.IsNullOrEmpty(dto.UMID) && string.IsNullOrEmpty(dto.Resid)) {
var UMIDMain = _zxdRepository.GetRepository<RES_CUSTOMER>().Query().FirstOrDefault(m => dto.UMID == m.UMID);
if (UMIDMain != null)
{
dto.Resid = UMIDMain.RESID;
}
else {
dto.Resid = "NULL_RESID";
}
}
#endregion
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<OutComplaint>().Query()
// join b in _zxdRepository.GetRepository<RES_CUSTOMER>().Query() on a.Resid equals b.RESID into ab
// from b in ab.DefaultIfEmpty()
select new OutComplaintDto
{
Id = a.Id,
Deptid = a.Deptid,
DueStatus = dto.Type.HasValue ? a.ComplaintDueStatus : a.DueStatus,
BusinessDeadline = a.BusinessDeadline,
ComplaintDeadline = a.ComplaintDeadline,
OutComplaintDate = a.OutComplaintDate,
BusinessStatus = a.BusinessStatus,
Cname = a.Cname,
ComplaintId = a.ComplaintId,
Content = a.Content,
CreateTime = a.CreateTime,
Creator = a.Creator,
Eid = a.Eid,
Follow = a.Follow,
OutComplaintChannel = a.OutComplaintChannel,
OutComplaintStatus = a.OutComplaintStatus,
Reason = a.Reason,
Remark = a.Remark,
Resid = a.Resid,
//UMID= b.UMID,
Updater = a.Updater,
UpdateTime = a.UpdateTime,
//Restorer = b.FirstCreator,
//RestoreTime = b.FirstCreateTime,
//RefundStatus = _zxdRepository.GetRepository<OrderRefundApply>().Query().OrderByDescending(x=>x.CreateTime).FirstOrDefault(x=>x.ComplaintId == a.ComplaintId).RefundStatus,
Restorer = _zxdRepository.GetRepository<OutComplaintRestore>().Query().OrderByDescending(x => x.CreateTime).FirstOrDefault(x => x.ComplaintId == a.ComplaintId && x.RestoreType == RestoreType.).Creator,
RestoreTime = _zxdRepository.GetRepository<OutComplaintRestore>().Query().OrderByDescending(x => x.CreateTime).FirstOrDefault(x => x.ComplaintId == a.ComplaintId && x.RestoreType == RestoreType.).CreateTime,
HGRestorer = _zxdRepository.GetRepository<OutComplaintRestore>().Query().OrderByDescending(x => x.CreateTime).FirstOrDefault(x => x.ComplaintId == a.ComplaintId && x.RestoreType == RestoreType.).Creator,
HGRestoreTime = _zxdRepository.GetRepository<OutComplaintRestore>().Query().OrderByDescending(x => x.CreateTime).FirstOrDefault(x => x.ComplaintId == a.ComplaintId && x.RestoreType == RestoreType.).CreateTime,
//OrderList = _zxdRepository.GetRepository<OutComplaintOrder>().Query().Where(x => x.ComplaintId == a.ComplaintId).Select(x => x.Orderid).ToList(),
//PriceList = _zxdRepository.GetRepository<OutComplaintOrder>().Query().Where(x => x.ComplaintId == a.ComplaintId).Select(x => x.Price).ToList(),
RestoreCount = _zxdRepository.GetRepository<OutComplaintRestore>().Query().Count(x => x.ComplaintId == a.ComplaintId),
FollowUpCount = _zxdRepository.GetRepository<OutComplaintFollowUp>().Query().Count(x => x.ComplaintId == a.ComplaintId),
NeedRestoreCount = _zxdRepository.GetRepository<OutComplaintRestore>().Query().Count(x => x.ComplaintId == a.ComplaintId),
TimeSort = a.UpdateTime.HasValue && a.CreateTime < a.UpdateTime ? a.UpdateTime.Value : a.CreateTime,
IsInComplaint = _zxdRepository.GetRepository<InComplaint>().Query().Count(x=>x.Resid == a.Resid) > 0 ? "是":"否",
Verifyier = a.Verifyier,
VerifyTime = a.VerifyTime,
RiskSituation = a.RiskSituation
};
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;
}
if (dto.Orderid.HasValue)
{
query = from a in query
join b in _zxdRepository.GetRepository<WX_SZZYORDER>().Query() on a.Resid equals b.RESID
join c in _zxdRepository.GetRepository<WX_SzzyOrderRefund>().Query() on b.ORDERID equals c.orderid
where c.orderid == dto.Orderid && c.isdelete == 0 && c.auditstatus == 1
select a;
}
if (dto.RefundStatus.HasValue)
{
if (dto.RefundStatus == OrderRefundStatus.退)
{
query = from a in query
join b in _zxdRepository.GetRepository<WX_SZZYORDER>().Query() on a.Resid equals b.RESID
join c in _zxdRepository.GetRepository<WX_SzzyOrderRefund>().Query() on b.ORDERID equals c.orderid
where c.orderid == 0 && c.isdelete == 0 && c.auditstatus == 1
select a;
}
else if (dto.RefundStatus == OrderRefundStatus.退)
{
query = from a in query
join b in _zxdRepository.GetRepository<WX_SZZYORDER>().Query() on a.Resid equals b.RESID
join c in _zxdRepository.GetRepository<WX_SzzyOrderRefund>().Query() on b.ORDERID equals c.orderid
where c.orderid > 0 && c.isdelete == 0 && c.auditstatus == 1
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.DueStatus != null, x => x.Where(x => x.DueStatus == dto.DueStatus))
.If(dto.BusinessStatus != null, x => x.Where(x => x.BusinessStatus == dto.BusinessStatus))
.If(dto.OutComplaintChannel != null, x => x.Where(x => x.OutComplaintChannel == dto.OutComplaintChannel))
.If(dto.OutComplaintStatus != null, x => x.Where(x => x.OutComplaintStatus == dto.OutComplaintStatus))
//.If(dto.RefundStatus != null, x => x.Where(x => x.RefundStatus == dto.RefundStatus))
.If(dto.Reason != null, x => x.Where(x => ((int)dto.Reason & x.Reason.Value) > 0))
.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) && dto.PersonType == PersonTypeEnum., x => x.Where(x => x.Creator == dto.CreaetOrUpdateUser))
.If(!string.IsNullOrEmpty(dto.CreaetOrUpdateUser) && dto.PersonType == PersonTypeEnum., x => x.Where(x => x.Updater == dto.CreaetOrUpdateUser))
.If(!string.IsNullOrEmpty(dto.CreaetOrUpdateUser) && dto.PersonType == PersonTypeEnum., x => x.Where(x => x.Verifyier == dto.CreaetOrUpdateUser))
.If(!string.IsNullOrEmpty(dto.CreaetOrUpdateUser) && dto.PersonType == PersonTypeEnum., x => x.Where(x => x.Restorer == dto.CreaetOrUpdateUser))
.If(!string.IsNullOrEmpty(dto.CreaetOrUpdateUser) && dto.PersonType == PersonTypeEnum., x => x.Where(x => x.HGRestorer == dto.CreaetOrUpdateUser))
//.If(dto.Orderid != null, x => x.Where(x => x.OrderList.Contains(dto.Orderid)))
.If(dto.DateType == OutComplaintDateType. && dto.DateFrom != null, x => x.Where(x => x.OutComplaintDate >= dto.DateFrom.Value))
.If(dto.DateType == OutComplaintDateType. && dto.DateFrom != null, x => x.Where(x => x.RestoreTime >= dto.DateFrom.Value))
.If(dto.DateType == OutComplaintDateType. && dto.DateFrom != null, x => x.Where(x => x.HGRestoreTime >= dto.DateFrom.Value))
.If(dto.DateType == OutComplaintDateType. && dto.DateFrom != null, x => x.Where(x => x.CreateTime >= dto.DateFrom.Value))
.If(dto.DateType == OutComplaintDateType. && dto.DateFrom != null, x => x.Where(x => x.UpdateTime >= dto.DateFrom.Value))
.If(dto.DateType == OutComplaintDateType. && dto.DateFrom != null, x => x.Where(x => x.VerifyTime >= dto.DateFrom.Value))
.If(dto.DateType == OutComplaintDateType. && dto.DateTo != null, x => x.Where(x => x.OutComplaintDate <= dto.DateTo.Value.AddDays(1).AddSeconds(-1)))
.If(dto.DateType == OutComplaintDateType. && dto.DateTo != null, x => x.Where(x => x.RestoreTime <= dto.DateTo.Value.AddDays(1).AddSeconds(-1)))
.If(dto.DateType == OutComplaintDateType. && dto.DateTo != null, x => x.Where(x => x.HGRestoreTime <= dto.DateTo.Value.AddDays(1).AddSeconds(-1)))
.If(dto.DateType == OutComplaintDateType. && dto.DateTo != null, x => x.Where(x => x.CreateTime <= dto.DateTo.Value.AddDays(1).AddSeconds(-1)))
.If(dto.DateType == OutComplaintDateType. && dto.DateTo != null, x => x.Where(x => x.UpdateTime <= dto.DateTo.Value.AddDays(1).AddSeconds(-1)))
.If(dto.DateType == OutComplaintDateType. && dto.DateTo != null, x => x.Where(x => x.VerifyTime <= dto.DateTo.Value.AddDays(1).AddSeconds(-1)))
.OrderByDescending(x => x.TimeSort);
var total = await query.CountAsync();
var data = await query
.Skip((dto.PageIndex - 1) * dto.PageSize)
.Take(dto.PageSize)
.ToListAsync();
var now = DateTime.Now;
if (data.Any())
{
var eidStr = string.Join(",", data.Where(x => x.Eid != null).Select(x => x.Eid).ToList());
var innerUsers = await _inneruserDomain.GetInnerusers(eidStr);
var orderStatus = new List<string> { "220", "205", "80", "90" };
var orders = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().Where(x => data.Select(x => x.Resid).Contains(x.RESID) && orderStatus.Contains(x.ORDERSTATUS)).OrderByDescending(x => x.OTIME).ToListAsync();
var resIds = data.Select(x => x.Resid);
var refundOrders = await (from a in _zxdRepository.GetRepository<WX_SzzyOrderRefund>().Query()
join b in _zxdRepository.GetRepository<WX_SZZYORDER>().Query() on a.orderid equals b.ORDERID
where resIds.Contains(b.RESID) && a.isdelete == 0 && a.auditstatus == 1
select new { a.refundprice, a.orderid, b.RESID }).ToListAsync();
var rcu = _zxdRepository.GetRepository<RES_CUSTOMER>().Query().Where(m => resIds.Contains(m.RESID)).ToList();
foreach (var item in data)
{
var company = deptList.FirstOrDefault(y => y.Id == item.Deptid);
var itemOrders = orders.Where(x => x.RESID == item.Resid);
if (item.Resid == "224284360094995449")//该客户测试单号过多
itemOrders = itemOrders.Take(4);
var idCard = itemOrders.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.idcard))?.idcard;
var minOTime = itemOrders.Min(x => x.OTIME);
item.Age = _cacheDomain.GetAge(idCard);
//item.OrderInfo = itemOrders.Select(x => $"产品:{x.SUBPRODUCTNAME} 订单号:{x.ORDERID} 开通时间:{x.OTIME} 到账金额:{x.ARRIVALPAY} 权限到期状态:{(now > x.ARRIVALTIME ? "到期" : "未到期")}").ToList();
item.Products = itemOrders.Select(x => x.SUBPRODUCTNAME).ToList();
item.OrderIds = itemOrders.Select(x => x.ORDERID).ToList();
item.OTimes = itemOrders.Select(x => x.OTIME).ToList();
item.ArrivalPays = itemOrders.Select(x => x.ARRIVALPAY).ToList();
item.ArrivalStatus = itemOrders.Select(x => (x.OTIME.HasValue && x.OPENDAYS.HasValue && now < x.OTIME.Value.AddDays(x.OPENDAYS.Value) ? "未到期" : "到期")).ToList();
item.ComplaintCycle = minOTime.HasValue ? item.OutComplaintDate.Value.Subtract(minOTime.Value).Days : null;
item.Deptname = company?.Title;
item.BusinessDeadlineSpan = $"{await _cacheDomain.GetWorkDays(now, item.BusinessDeadline)}个工作日";
item.ComplaintDeadlineSpan = $"{await _cacheDomain.GetWorkDays(now, item.ComplaintDeadline)}个工作日";
var innerUser = innerUsers.FirstOrDefault(x => x.EId == item.Eid);
item.Euser = item.Eid.HasValue && innerUser != null ? $"{item.Eid}-{innerUser?.Name}-{(innerUser?.Status == 2 ? "" : "")}" : item.Eid.ToString();
item.OrderList = refundOrders.Where(x => x.RESID == item.Resid).Select(x => x.orderid).ToList();
item.PriceList = refundOrders.Where(x => x.RESID == item.Resid).Select(x => x.refundprice).ToList();
item.RefundStatus = refundOrders.Where(x => x.RESID == item.Resid).Any() ? OrderRefundStatus.退 : OrderRefundStatus.退;
item.UMID = rcu.FirstOrDefault(m => m.RESID == item.Resid)?.UMID;
}
}
else {
data = new List<OutComplaintDto>();
}
return new PageResult<OutComplaintDto>(dto.PageIndex, dto.PageSize, total, data);
}
public async Task<bool> CreateOutComplaint(CreateOutComplaintDto dto)
{
var date = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
var complaintId = CreateOutComplaintid();
//Log.Information("卧槽");
var customer = await _zxdRepository.GetRepository<RES_CUSTOMER>().Query().FirstOrDefaultAsync(x => x.UMID == dto.Resid.Trim());
if (customer == null)
throw new Exception("客户ID不存在");
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().OrderByDescending(x => x.CTIME)
.FirstOrDefaultAsync(x => x.RESID == customer.RESID && x.deptid == dto.Deptid);
var data = new OutComplaint
{
Deptid = dto.Deptid,
DueStatus = OutComplaintDueStatus.,
ComplaintDueStatus = OutComplaintDueStatus.,
OutComplaintDate = dto.OutComplaintDate.Value,
Cname = dto.Cname,
ComplaintId = complaintId,
BusinessDeadline = await _cacheDomain.AddWorkDays(date, 7),
ComplaintDeadline = await _cacheDomain.AddWorkDays(date, 20),
BusinessStatus = OutComplaintBusinessStatus.,
Content = dto.Content,
CreateTime = DateTime.Now,
Creator = dto.Operator,
Follow = dto.Follow,
OutComplaintChannel = dto.OutComplaintChannel,
Reason = dto.Reason,
OutComplaintStatus = OutComplaintStatus.,
Remark = dto.Remark,
//Resid = dto.Resid,
Resid = customer.RESID,
Eid = order?.eid,
CreateEid = dto.OperatorEid,
VerifyEid = !string.IsNullOrWhiteSpace(dto.Remark) ? dto.OperatorEid : null,
Verifyier = !string.IsNullOrWhiteSpace(dto.Remark) ? dto.Operator : null,
VerifyTime = !string.IsNullOrWhiteSpace(dto.Remark) ? DateTime.Now : null,
RiskSituation = dto.RiskSituation
};
var outComplaintFollowUp = new OutComplaintFollowUp
{
ComplaintId = complaintId,
Content = dto.Content,
CreateTime = DateTime.Now,
Creator = dto.Operator,
Follow = dto.Follow,
CreateEid = dto.OperatorEid
};
//var orders = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query()
// .Where(x => x.RESID == dto.Resid)
// .Where(x => x.ORDERSTATUS == "90")
// .Select(x => new OutComplaintOrder
// {
// Orderid = x.ORDERID,
// ComplaintId = complaintId,
// Price = x.NEEDPAY
// })
// .ToListAsync();
var transaction = await _zxdRepository.BeginTransactionAsync();
try
{
await _zxdRepository.GetRepository<OutComplaint>().InsertAsync(data);
await _zxdRepository.GetRepository<OutComplaintFollowUp>().InsertAsync(outComplaintFollowUp);
//发送消息
var sendMessage = new
{
Message = $"{dto.Operator}({dto.Resid})提交了外诉,投诉原因是:{dto.ReasonList}请在7个工作日内及时处理好。",
Eid = order?.eid,
Deptid = dto.Deptid,
Method = "ComplaintMessage",
IsDepartment = true,
};
//Log.Information("卧槽44");
//await _cacheDomain.SendCrmMessage(sendMessage);
//创建客户标签
await _cacheDomain.ComplaintLabel(dto.Resid, dto.Cname, order?.companycode, "外诉");
await transaction.CommitAsync();
return true;
}
catch
{
await transaction.RollbackAsync();
await transaction.DisposeAsync();
throw;
}
}
/// <summary>
/// 更新外诉记录
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
/// <exception cref="ApiException"></exception>
public async Task<bool> UpdateOutComplaint(UpdateOutComplaintDto dto)
{
var data = await _zxdRepository.GetRepository<OutComplaint>().Query().FirstOrDefaultAsync(x => x.Id == dto.Id);
if (data == null) throw new ApiException("外诉数据不存在或已删除!");
data.Content = dto.Content;
data.Follow = dto.Follow;
data.Updater = dto.Operator;
data.UpdateTime = DateTime.Now;
data.UpdateEid = dto.OperatorEid;
data.RiskSituation = dto.RiskSituation;
if (data.Remark != dto.Remark)
{
data.Remark = dto.Remark;
data.VerifyTime = DateTime.Now;
data.VerifyEid = dto.OperatorEid;
data.Verifyier = dto.Operator;
}
await _zxdRepository.GetRepository<OutComplaint>().UpdateAsync(data, x => new { x.RiskSituation ,x.Content, x.Follow, x.Remark, x.UpdateTime, x.Updater, x.Verifyier, x.VerifyTime, x.VerifyEid });
return true;
}
private static string CreateOutComplaintid()
{
//return $"WS{DateTimeOffset.Now.ToUnixTimeMilliseconds()}";
return $"WS{DateTime.Now.ToString("yyyyMMddHHmmssffff")}";
}
/// <summary>
/// 添加跟进记录
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<bool> CreateOutComlpaintFollowUp(CreateOutComplaintFollowUpDto dto)
{
var data = await _zxdRepository.GetRepository<OutComplaint>().Query().FirstOrDefaultAsync(x => x.Id == dto.Id);
if (data == null) throw new ApiException("外诉数据不存在或已删除!");
var outComplaintFollowUp = new OutComplaintFollowUp
{
ComplaintId = data.ComplaintId,
Content = dto.Content,
CreateTime = DateTime.Now,
Creator = dto.Operator,
Follow = dto.Follow,
CreateEid = dto.OperatorEid
};
data.Updater = dto.Operator;
data.UpdateEid = dto.OperatorEid;
data.UpdateTime = DateTime.Now;
var transaction = await _zxdRepository.BeginTransactionAsync();
try
{
await _zxdRepository.GetRepository<OutComplaint>().UpdateAsync(data, x => new { x.Updater, x.UpdateEid, x.UpdateTime });
await _zxdRepository.GetRepository<OutComplaintFollowUp>().InsertAsync(outComplaintFollowUp);
await transaction.CommitAsync();
return true;
}
catch (Exception ex)
{
await transaction.RollbackAsync();
await transaction.DisposeAsync();
throw;
}
}
public async Task<bool> UpdateOutComlpaintFollowUp(CreateOutComplaintFollowUpDto dto)
{
var complaintFollowUp = await _zxdRepository.GetRepository<OutComplaintFollowUp>().Query().FirstOrDefaultAsync(x => x.Id == dto.Id);
if(complaintFollowUp == null)
throw new ApiException("跟进数据不存在或已删除!");
complaintFollowUp.Content = dto.Content;
complaintFollowUp.Follow = dto.Follow;
complaintFollowUp.UpdateEid = dto.OperatorEid;
complaintFollowUp.Updater = dto.Operator;
complaintFollowUp.UpdateTime = DateTime.Now;
await _zxdRepository.GetRepository<OutComplaintFollowUp>().UpdateAsync(complaintFollowUp);
return true;
}
/// <summary>
/// 获取用户信息
/// </summary>
/// <param name="resid"></param>
/// <returns></returns>
public async Task<ComplaintCustomerInfoDto> GetComplaintCustomerInfo(string? resid)
{
//var customerInfo = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query()
// .Where(x => x.RESID == resid)
// .Where(x => x.ORDERSTATUS == "220")
// .GroupBy(x => new
// {
// x.RESID,
// x.CNAME,
// x.eid,
// })
// .Select(x => new ComplaintCustomerInfoDto
// {
// Resid = x.Key.RESID,
// Cname = x.Key.CNAME,
// Eid = x.Key.eid,
// DeptInfos = x.GroupBy(y=>new { y.CHANNEL,y.companycode}).Select(y => new ComplaintCustomerDeptInfo
// {
// Channel = y.Key.CHANNEL,
// CompanyCode = y.Key.companycode
// }).ToList()
// })
// .FirstOrDefaultAsync();
var orderList = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query()
.Where(x => x.RESID == resid)
.Where(x => x.ORDERSTATUS == "220").ToListAsync();
var customerInfo = orderList.GroupBy(x => new { x.RESID, x.CNAME, x.eid })
.Select(x => new ComplaintCustomerInfoDto
{
Resid = x.Key.RESID,
Cname = x.Key.CNAME,
Eid = x.Key.eid,
DeptInfos = orderList.GroupBy(y => new { y.CHANNEL, y.companycode }).Select(y => new ComplaintCustomerDeptInfo
{
Channel = y.Key.CHANNEL,
CompanyCode = y.Key.companycode
}).ToList()
}).FirstOrDefault();
var deptList = await _cacheDomain.GetDeptments();
if (customerInfo != null && customerInfo.DeptInfos != null && customerInfo.DeptInfos.Any())
{
customerInfo.DeptInfos.ForEach(x =>
{
var dept = deptList.FirstOrDefault(y => y.DeptmentCampains.Any(a => x.Channel >= a.StartCampainId && x.Channel <= a.EndCampainId));
x.Deptname = dept?.Title;
x.Deptid = dept?.Id;
});
}
return customerInfo;
}
public async Task<OutComplaintFollowUpDto> GetOutComplaintFollowUp(int? id)
{
var query = from a in _zxdRepository.GetRepository<OutComplaint>().Query()
where a.Id == id
select new OutComplaintFollowUpDto
{
Id = a.Id,
Cname = a.Cname,
OutComplaintStatus = a.OutComplaintStatus,
ComplaintId = a.ComplaintId,
Resid = a.Resid
};
var data = await query.FirstOrDefaultAsync();
if (data == null) throw new ApiException("外诉数据不存在或已删除!");
data.Details = await _zxdRepository.GetRepository<OutComplaintFollowUp>().Query()
.Where(x => x.ComplaintId == data.ComplaintId)
.Select(x => new OutComplaintFollowUpDetailDto
{
Id = x.Id,
Content = x.Content,
CreateTime = x.CreateTime,
Creator = x.Creator,
Follow = x.Follow,
Updater = x.Updater,
UpdateTime = x.UpdateTime,
}).ToListAsync();
var index = 1;
data.Details?.ForEach(x =>
{
x.Index = $"第{index}次投诉";
index++;
});
return data;
}
public async Task<OutComplaintRestoreDto> GetOutComplaintRestoreInfo(int id)
{
var data = await _zxdRepository.GetRepository<OutComplaint>().Query().FirstOrDefaultAsync(x => x.Id == id);
if (data == null) throw new ApiException("外诉数据不存在或已删除!");
var restore = await _zxdRepository.GetRepository<OutComplaintRestore>().Query().Where(x => x.ComplaintId == data.ComplaintId).OrderBy(x=>x.CreateTime).ToListAsync();
var result = new OutComplaintRestoreDto
{
Deptid = data.Deptid,
Cname = data.Cname,
ComplaintId = data.ComplaintId,
Resid = data.Resid,
OutComplaintStatus = data.OutComplaintStatus,
Id = data.Id,
OutComplaintBusinessStatus = data.BusinessStatus,
Restores = restore.Select(x => new RestoreDto
{
Id = x.Id,
Content = x.Content,
Creator = x.Creator,
Attachment = x.Attachment,
ComplaintId = x.ComplaintId,
CreateTime = x.CreateTime,
CreatorEid = x.CreatorEid,
RestoreType = x.RestoreType
}).ToList()
};
var deptList = await _cacheDomain.GetDeptments();
var dept = deptList.FirstOrDefault(y => y.Id == data.Deptid);
result.Deptname = dept?.Title;
return result;
}
public async Task<bool> CreateOutComplaintRestore(CreateOutComplaintRestoreDto dto)
{
var data = await _zxdRepository.GetRepository<OutComplaint>().Query().FirstOrDefaultAsync(x => x.Id == dto.Id);
if (data == null) throw new ApiException("外诉数据不存在或已删除!");
if (dto.RestoreType == RestoreType.)
{
data.BusinessStatus = dto.BusinessStatus;
}
else if (dto.RestoreType == RestoreType.)
{
data.Updater = dto.Creator;
data.UpdateEid = dto.CreatorEid;
data.UpdateTime = DateTime.Now;
}
var restore = new OutComplaintRestore
{
ComplaintId = data.ComplaintId,
Content = dto.Content,
CreateTime = DateTime.Now,
Creator = dto.Creator,
CreatorEid = dto.CreatorEid,
Attachment = dto.Attachment,
RestoreType = dto.RestoreType
};
await _zxdRepository.GetRepository<OutComplaintRestore>().InsertAsync(restore);
await _zxdRepository.GetRepository<OutComplaint>().UpdateAsync(data, x => new { x.BusinessStatus, x.UpdateEid, x.Updater, x.UpdateTime });
return true;
}
public async Task<bool> UpdateOutComplaintStatus(UpdateOutComplaintStatusDto dto)
{
var data = await _zxdRepository.GetRepository<OutComplaint>().Query().FirstOrDefaultAsync(x => x.Id == dto.Id);
if (data == null) throw new ApiException("外诉数据不存在或已删除!");
data.UpdateEid = dto.OperatorEid;
data.Updater = dto.Operator;
data.UpdateTime = DateTime.Now;
data.OutComplaintStatus = dto.OutComplaintStatus;
//if (dto.OutComplaintStatus == OutComplaintStatus.已办结)
//{
// data.BusinessStatus = OutComplaintBusinessStatus.已办结;
//}
await _zxdRepository.GetRepository<OutComplaint>().UpdateAsync(data, x => new
{
x.UpdateEid,
x.Updater,
x.OutComplaintStatus,
x.UpdateTime,
x.BusinessStatus
});
return true;
}
public async Task SyncOutComplaintStatus()
{
var now = DateTime.Now;
var data = await _zxdRepository.GetRepository<OutComplaint>().Query()
.Where(x => (x.DueStatus == OutComplaintDueStatus. || x.ComplaintDueStatus == OutComplaintDueStatus.)
&& (x.BusinessDeadline <= now || x.ComplaintDeadline <= now))
//&& x.ComplaintDeadline <= now)
.ToListAsync();
foreach (var item in data)
{
if(item.BusinessDeadline <= now)
item.DueStatus = OutComplaintDueStatus.;
if(item.ComplaintDeadline <= now)
item.ComplaintDueStatus = OutComplaintDueStatus.;
if (item.BusinessStatus == OutComplaintBusinessStatus. && item.BusinessDeadline <= now)
{
item.BusinessStatus = OutComplaintBusinessStatus.;
}
}
if (data.Any())
{
await _zxdRepository.GetRepository<OutComplaint>().BatchUpdateAsync(data, x => new { x.DueStatus, x.BusinessStatus, x.ComplaintDueStatus });
}
}
public async Task<ComplaintLabelDto> ComplaintLabel(string resId)
{
if (string.IsNullOrWhiteSpace(resId))
throw new Exception("请输入客户ID");
var inComplaint = await _zxdRepository.GetRepository<InComplaint>().FirstOrDefaultAsync(x => x.Resid == resId);
var outComplaint = await _zxdRepository.GetRepository<OutComplaint>().FirstOrDefaultAsync(x => x.Resid == resId);
var result = new ComplaintLabelDto
{
InComplaint = inComplaint != null,
OutComplaint = outComplaint != null
};
return result;
}
public async Task<bool> Import(List<OutComplaintImportDto> dtos)
{
dtos = dtos.Where(x => !string.IsNullOrWhiteSpace(x.Resid)).ToList();
dtos.ForEach(x => x.Resid = x.Resid.Replace("", "("));
dtos.ForEach(x => x.Resid = x.Resid.Contains("(") ? x.Resid.Substring(0, x.Resid.IndexOf("(")) : x.Resid);
var complaints = new List<OutComplaint>();
var follows = new List<OutComplaintFollowUp>();
var orderStatus = new List<string> { "220", "80", "90" };
var resIds = dtos.Select(x => x.Resid).ToList();
var orders = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().Where(x => resIds.Contains(x.RESID) && orderStatus.Contains(x.ORDERSTATUS)).OrderByDescending(x => x.CTIME).ToListAsync();
var deptList = await _cacheDomain.GetDeptments();
foreach (var dto in dtos)
{
var order = orders.FirstOrDefault(x => x.RESID == dto.Resid && x.ORDERSTATUS == "90");
if (order == null)
order = orders.FirstOrDefault(x => x.RESID == dto.Resid && x.ORDERSTATUS == "220");
if (order == null)
order = orders.FirstOrDefault(x => x.RESID == dto.Resid && x.ORDERSTATUS == "80");
//if (order == null)
// throw new Exception($"客户{dto.Resid}订单不存在");
var dept = deptList.FirstOrDefault(x => x.Title == dto.DeptName);
//if (dept == null)
// throw new Exception($"部门{dto.DeptName}不存在");
if (!System.Enum.TryParse(dto.OutComplaintChannel, out OutComplaintChannel complaintChannel))
{
if (dto.OutComplaintChannel.Contains("110"))
complaintChannel = OutComplaintChannel.110;
if (dto.OutComplaintChannel.Contains("123"))
complaintChannel = OutComplaintChannel.线12386;
if (dto.OutComplaintChannel.Contains("工商局"))
complaintChannel = OutComplaintChannel.广;
//else
//throw new Exception($"不存在:{dto.ComplaintChannel}内诉来源");
}
if (!System.Enum.TryParse(dto.Reasons, out OutComplaintReason outComplaintReason))
{
if (dto.Reasons.Contains("拖延"))
outComplaintReason = OutComplaintReason.退;
if (dto.Reasons.Contains("好股来外部引流"))
outComplaintReason = OutComplaintReason.;
if (dto.Reasons.Contains("拒绝退款"))
outComplaintReason = OutComplaintReason.退;
//else
//throw new Exception($"不存在:{dto.Reasons}投诉原因");
}
if (!System.Enum.TryParse(dto.ComplaintStatus, out OutComplaintStatus complaintStatus))
{
//throw new Exception($"不存在:{dto.ComplaintStatus}内诉状态");
}
var complaintId = CreateOutComplaintid();
var data = new OutComplaint
{
Deptid = dept?.Id,
DueStatus = await _cacheDomain.AddWorkDays(dto.OutComplaintDate.Value, 7) > DateTime.Now ? OutComplaintDueStatus. : OutComplaintDueStatus.,
ComplaintDueStatus = await _cacheDomain.AddWorkDays(dto.OutComplaintDate.Value, 20) > DateTime.Now ? OutComplaintDueStatus. : OutComplaintDueStatus.,
OutComplaintDate = dto.OutComplaintDate.Value,
Cname = dto.Cname,
ComplaintId = complaintId,
BusinessDeadline = await _cacheDomain.AddWorkDays(dto.OutComplaintDate.Value, 7),
ComplaintDeadline = await _cacheDomain.AddWorkDays(dto.OutComplaintDate.Value, 20),
BusinessStatus = complaintStatus == OutComplaintStatus. ? OutComplaintBusinessStatus. : OutComplaintBusinessStatus.,
Content = dto.Content,
CreateTime = dto.OutComplaintDate.Value,
Creator = "李冰华",
Follow = dto.Follow,
OutComplaintChannel = (int)complaintChannel == 0 ? null : complaintChannel,
Reason = (int)outComplaintReason == 0 ? null : (int)outComplaintReason,
OutComplaintStatus = complaintStatus == 0 ? null : complaintStatus,
Remark = dto.Remark,
Resid = dto.Resid,
Eid = order?.eid,
CreateEid = 6022,
VerifyEid = !string.IsNullOrWhiteSpace(dto.Remark) ? 6022 : null,
Verifyier = !string.IsNullOrWhiteSpace(dto.Remark) ? "李冰华" : null,
VerifyTime = !string.IsNullOrWhiteSpace(dto.Remark) ? DateTime.Now : null,
RiskSituation = ""
};
var outComplaintFollowUp = new OutComplaintFollowUp
{
ComplaintId = complaintId,
Content = dto.Content,
CreateTime = dto.OutComplaintDate.Value,
Creator = "李冰华",
Follow = dto.Follow,
CreateEid = 6022
};
complaints.Add(data);
follows.Add(outComplaintFollowUp);
//创建客户标签
if(dept!= null)
await _cacheDomain.ComplaintLabel(dto.Resid, dto.Cname, dept.CompanyCode, "外诉");
}
await _zxdRepository.GetRepository<OutComplaint>().BatchInsertAsync(complaints);
await _zxdRepository.GetRepository<OutComplaintFollowUp>().BatchInsertAsync(follows);
return true;
}
public async Task<bool> UpdateEid()
{
//var inComplaints = await _zxdRepository.GetRepository<InComplaint>().Query().Where(x => x.Eid == null).ToListAsync();
//var outComplaints = await _zxdRepository.GetRepository<OutComplaint>().Query().Where(x => x.Eid == null).ToListAsync();
var inComplaints = await _zxdRepository.GetRepository<InComplaint>().Query().Where(x => x.Deptid == null).ToListAsync();
var outComplaints = await _zxdRepository.GetRepository<OutComplaint>().Query().Where(x => x.Deptid == null).ToListAsync();
var updateInComplaints = new List<InComplaint>();
var updateOutComplaints = new List<OutComplaint>();
var orderStatus = new List<string> { "220", "80", "90", "200", "205" };
foreach (var item in inComplaints)
{
//var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().OrderByDescending(x=>x.CTIME).FirstOrDefaultAsync(x => x.RESID == item.Resid && x.eid.HasValue);
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().OrderByDescending(x=>x.CTIME).FirstOrDefaultAsync(x => x.RESID == item.Resid && x.deptid.HasValue);
if (order == null)
continue;
//item.Eid = order.eid;
item.Deptid = order.deptid;
updateInComplaints.Add(item);
}
foreach (var item in outComplaints)
{
//var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().OrderByDescending(x => x.CTIME).FirstOrDefaultAsync(x => x.RESID == item.Resid && x.eid.HasValue);
var order = await _zxdRepository.GetRepository<WX_SZZYORDER>().Query().OrderByDescending(x => x.CTIME).FirstOrDefaultAsync(x => x.RESID == item.Resid && x.deptid.HasValue);
if (order == null)
continue;
//item.Eid = order.eid;
item.Deptid = order.deptid;
updateOutComplaints.Add(item);
}
if (updateInComplaints.Any())
await _zxdRepository.GetRepository<InComplaint>().BatchUpdateAsync(updateInComplaints, x => new { x.Deptid});
if (updateOutComplaints.Any())
await _zxdRepository.GetRepository<OutComplaint>().BatchUpdateAsync(updateOutComplaints, x => new { x.Deptid });
return true;
}
}
}