Zxd.Core/code/ToDoWorker/Domain/EventDomain.cs

495 lines
22 KiB
C#

using DG.EntityFramework;
using DG.Redis;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ToDoWorker.Helper;
using Zxd.Core.Shared;
using Zxd.Core.Shared.Helpers;
using Zxd.Entity.Action;
using Zxd.Entity.dim;
using Zxd.Entity.Zxd;
using Zxd.EntityFramework;
namespace ToDoWorker.Domain
{
public class EventDomain : IEventDomain
{
private readonly IBaseRepository<ZxdDbContext> _repository;
private readonly IBaseRepository<CrmCloudDbContext> _actRepository;
private readonly IConfiguration _configuration;
private readonly SystemConfig? _systemConfig;
private readonly IHttpClient _httpClient;
private readonly IRedisManager _redisManager;
private static string _queueskey = "queue:todoItemQueues";
private static string _cachekey = "queue:cacheItem";
private readonly IMapper _mapper;
public EventDomain(
IBaseRepository<ZxdDbContext> zxdRepository, IBaseRepository<CrmCloudDbContext> actRepository, IMapper mapper,
IConfiguration configuration, IHttpClient httpClient, IRedisManager redisManager)
{
_repository = zxdRepository;
_mapper = mapper;
_actRepository = actRepository;
_configuration = configuration;
_systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
_httpClient = httpClient;
_redisManager = redisManager;
}
public async Task<bool> RunEvent(EventDto eventDto)
{
if (!eventDto.deptid.HasValue)
{
Log.Error($"无部门不处理{eventDto.ToJson()}");
return false;
}
try
{
var deptSet = await GetSetting(eventDto);
if (deptSet == null)
{
Log.Error($"没找到相关的业务线配置{eventDto.ToJson()}");
return false;
}
CustomerBehaviorLog newModel = new CustomerBehaviorLog
{
act_date = Convert.ToDateTime(eventDto.act_date),
appid = eventDto.appid,
umid = eventDto.umid,
appuserid = eventDto.appuserid,
resid = eventDto.resid,
uid = eventDto.uid,
unionid = eventDto.unionid,
customerid = eventDto.customerid,
user_type = eventDto.user_type,
eventid = eventDto.eventid,
eventname = eventDto.eventname,
eventmemo = eventDto.eventmemo,
others = eventDto.others,
scenetype = eventDto.scenetype,
scenetypename = eventDto.scenetypename,
sceneid = eventDto.sceneid,
sceneidname = eventDto.sceneidname,
scenename = eventDto.scenename,
sceneext = eventDto.sceneext,
ip = eventDto.ip,
channel = eventDto.channel,
deptid = eventDto.deptid.GetValueOrDefault(),
productplat = eventDto.productplat,
productplatname = eventDto.productplatname,
sinkClickhouseTable = eventDto.sinkClickhouseTable,
updatetime = eventDto.update_time,
ctime = DateTime.Now,
eventtypename = deptSet.memo,
neweventid = deptSet.neweventid,
neweventname = deptSet.remark,
};
if (eventDto.act_time.HasValue)
{
newModel.act_time = ConvertHelper.JavaLongToDateTime(eventDto.act_time.Value);
}
if (eventDto.leavetime.HasValue)
{
newModel.leavetime = ConvertHelper.JavaLongToDateTime(eventDto.leavetime.Value);
}
newModel.Content = GetNoticeContent(newModel, deptSet);
//请求大数据接口获取用户昵称和头像
// 新增优品信息
using var transaction = await _actRepository.BeginTransactionAsync();
var url = $"{_systemConfig.BdMarkting}/user/relations?uid={newModel.uid}";
var empData = await _httpClient.GetAsync<CommonApiResult<List<CustomerEmployeeMap>>>(url);
if (string.IsNullOrWhiteSpace(newModel.umid))
{
Log.Information($"bigdata: {newModel.uid} {empData.ToJson()}");
//var resid = empData.data.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.resid))?.resid;
var umid = empData.data.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.umid))?.umid;
//newModel.resid = resid;
newModel.umid = umid;
if (string.IsNullOrWhiteSpace(newModel.umid))
{
// 任务重试入队
// 只重试两次
if (eventDto.RetryCount <= 1)
{
var setting = await GetDelaySec();
var delaySec = setting == null ? 120 : setting.delaytime.Value;
eventDto.RetryCount++;
eventDto.TaskTime = DateTime.Now.AddSeconds(delaySec);
await _redisManager.EnqueueAsync(_queueskey, eventDto);
Log.Information($"reTry{eventDto.uid}【{umid}】");
return true;
}
}
}
//插入行为日志
await _actRepository.GetRepository<CustomerBehaviorLog>().InsertAsync(newModel);
List<EmployeeTodoitem> todoItemList = new List<EmployeeTodoitem>();
if (empData != null && empData.code == 0)
{
var customerEmps = empData.data.Where(n => n.deptid == newModel.deptid).ToList();
if (!deptSet.allNotice)
{
if (deptSet.firstCtime.HasValue && deptSet.firstCtime.Value)
{
var firemp = customerEmps.OrderBy(n => n.create_time).FirstOrDefault();
if (firemp != null)
{
customerEmps = new List<CustomerEmployeeMap> { firemp };
}
}
}
if (customerEmps.Count == 0)
{
if (deptSet.isforce.Value)
{
EmployeeTodoitem todoitem = new EmployeeTodoitem
{
logid = newModel.id,
done = false,
deptid = newModel.deptid,
ctime = DateTime.Now,
utime = DateTime.Now,
act_date = Convert.ToDateTime(newModel.act_date),
act_time = newModel.act_time,
appid = newModel.appid,
appuserid = newModel.appuserid,
resid = newModel.resid,
umid = newModel.umid,
unionid = newModel.unionid,
customerid = newModel.customerid,
uid = newModel.uid,
user_type = newModel.user_type,
eventid = newModel.eventid,
eventname = newModel.eventname,
eventtypename = newModel.eventtypename,
eventmemo = newModel.eventmemo,
others = newModel.others,
scenetype = newModel.scenetype,
scenetypename = newModel.scenetypename,
sceneid = newModel.sceneid,
sceneidname = newModel.sceneidname,
scenename = newModel.scenename,
sceneext = newModel.sceneext,
ip = newModel.ip,
channel = newModel.channel,
productplat = newModel.productplat,
productplatname = newModel.productplatname,
leavetime = newModel.leavetime,
neweventid = newModel.neweventid,
neweventname = newModel.neweventname,
content = newModel.Content,
isread = 0
};
await _actRepository.GetRepository<EmployeeTodoitem>().InsertAsync(todoitem);
}
await transaction.CommitAsync();
return true;
}
//只通知 企微好友 归属关系
List<CustomerEmployeeMap> resultEmp = new List<CustomerEmployeeMap>();
if (deptSet.is_wework)
{
resultEmp.AddRange(customerEmps.Where(n => n.is_wework == 1).ToList());
}
if (deptSet.is_mobile)
{
resultEmp.AddRange(customerEmps.Where(n => n.is_mobile == 1).ToList());
}
if (deptSet.is_belong)
{
resultEmp.AddRange(customerEmps.Where(n => n.is_belong == 1).ToList());
}
resultEmp = resultEmp.Distinct().ToList();
foreach (var item in resultEmp)
{
EmployeeTodoitem todoitem = new EmployeeTodoitem
{
logid = newModel.id,
eid = item.eid.Value,
ename = item.employee_name,
done = false,
deptid = newModel.deptid,
ctime = DateTime.Now,
utime = DateTime.Now,
Nickname = !string.IsNullOrWhiteSpace(item.Username) ? item.Username : item.Nickname,
Headimgurl = item.Headimgurl,
act_date = Convert.ToDateTime(newModel.act_date),
act_time = newModel.act_time,
appid = newModel.appid,
appuserid = newModel.appuserid,
resid = newModel.resid,
unionid = newModel.unionid,
umid = newModel.umid,
customerid = newModel.customerid,
uid = newModel.uid,
user_type = newModel.user_type,
eventid = newModel.eventid,
eventname = newModel.eventname,
eventtypename = newModel.eventtypename,
eventmemo = newModel.eventmemo,
others = newModel.others,
scenetype = newModel.scenetype,
scenetypename = newModel.scenetypename,
sceneid = newModel.sceneid,
sceneidname = newModel.sceneidname,
scenename = newModel.scenename,
sceneext = newModel.sceneext,
ip = newModel.ip,
channel = newModel.channel,
productplat = newModel.productplat,
productplatname = newModel.productplatname,
leavetime = newModel.leavetime,
neweventid = newModel.neweventid,
neweventname = newModel.neweventname,
content = newModel.Content,
isread = 0
};
todoItemList.Add(todoitem);
}
if (todoItemList.Count > 0)
{
await _actRepository.GetRepository<EmployeeTodoitem>().BatchInsertAsync(todoItemList);
}
await transaction.CommitAsync();
}
}
catch (Exception ex)
{
Log.Error($"处理待办事项出错{ex.Message}【{eventDto.ToJson()}】");
}
return true;
}
public async Task<ToDoItemDeptMap> GetSetting(EventDto eventDto)
{
var cacheKey = "ToDoItemSetting";
var set = await _redisManager.GetAsync<ToDoItemSetting>(cacheKey);
if (set == null)
{
var setting = await _repository.GetRepository<BAS_PARAMETER>().Query().FirstOrDefaultAsync(n => n.PARAKEY == "ToDoItemSetting");
if (setting != null && !string.IsNullOrWhiteSpace(setting.PARAVALUE))
{
set = JsonHelper.FromJson<ToDoItemSetting>(setting.PARAVALUE);
await _redisManager.SetAsync<ToDoItemSetting>(cacheKey, set);
}
}
var deptSetList = set.setting.Where(n => n.eventid == eventDto.eventid).ToList();
var deptSet = deptSetList.FirstOrDefault();
foreach (var item in deptSetList)
{
deptSet = null;
if (!string.IsNullOrWhiteSpace(item.source) && !item.source.Contains(eventDto.sinkClickhouseTable))
{
continue;
}
if (item.scenetype.Count > 0 && !item.scenetype.Contains(eventDto.scenetype))
{
continue;
}
if (item.sceneid.Count > 0 && !item.sceneid.Contains(eventDto.sceneid))
{
continue;
}
if (item.deptids.Count > 0 && !item.deptids.Contains(eventDto.deptid.Value))
{
continue;
}
if (item.appids.Count > 0 && !item.appids.Contains(eventDto.appid))
{
continue;
}
//找到了 看是否配置了场景
deptSet = item;
var sceneItem = deptSetList.FirstOrDefault(n => n.scenetype.Contains(eventDto.scenetype));
if (sceneItem != null)
{
deptSet = sceneItem;
}
break;
}
return deptSet;
}
public async Task<ToDoItemSetting> GetDelaySec()
{
var cacheKey = "ToDoItemSetting";
var set = await _redisManager.GetAsync<ToDoItemSetting>(cacheKey);
if (set == null)
{
var setting = await _repository.GetRepository<BAS_PARAMETER>().Query().FirstOrDefaultAsync(n => n.PARAKEY == "ToDoItemSetting");
if (setting != null && !string.IsNullOrWhiteSpace(setting.PARAVALUE))
{
set = JsonHelper.FromJson<ToDoItemSetting>(setting.PARAVALUE);
await _redisManager.SetAsync(cacheKey, set);
}
}
return set;
}
private string GetNoticeContent(CustomerBehaviorLog log, ToDoItemDeptMap set)
{
var content = set.template;
Type type = typeof(CustomerBehaviorLog);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (var item in propertyInfo)
{
var value = item.GetValue(log);
var setValue = value == null ? "" : value.ToString();
var name = "{" + item.Name + "}";
if (name == "{act_date}" && content.Contains(name))
{
content = content.Replace(name, Convert.ToDateTime(setValue).ToString("yyyy-MM-dd"));
continue;
}
if (content.Contains(name))
{
content = content.Replace(name, setValue);
}
}
content = content.Replace("----", "-").Replace("---", "-").Replace("--", "-").Trim('-');
return content;
}
/// <summary>
/// 过滤登录事件
/// </summary>
/// <param name="eventDto"></param>
/// <returns></returns>
public async Task<bool> FilterLogEvent(EventDto eventDto, ToDoItemDeptMap setting)
{
List<string> redisKey = new List<string>();
var residkey = setting.residkey;
var useridkey = setting.key;
if (string.IsNullOrWhiteSpace(useridkey))
{
return true;
}
redisKey.Add(useridkey);
if (!string.IsNullOrWhiteSpace(residkey))
{
redisKey.Add(residkey);
}
if (eventDto.eventid == (int)EventEnum. && eventDto.sceneid == "soft.login")
{
return false;
}
if (string.IsNullOrWhiteSpace(eventDto.appuserid))
{
Log.Information($"appuserid为空{eventDto.uid}");
return false;
}
foreach (var rkey in redisKey)
{
//resid缓存 resid不等于空才生效
if (rkey.Contains("resid") && string.IsNullOrWhiteSpace(eventDto.resid))
{
continue;
}
var key = rkey.ToString();
Type type = typeof(EventDto);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (var item in propertyInfo)
{
var value = item.GetValue(eventDto);
var setValue = value == null ? "" : value.ToString();
var name = "{" + item.Name + "}";
if (key.Contains(name))
{
key = key.Replace(name, setValue);
}
}
if (key.Contains("date_"))
{
var replaceStr = key.Split("date_").LastOrDefault();
var date = DateTime.Now.ToString(replaceStr.Trim('}').ToString());
var dateStr = "{date_" + replaceStr;
key = key.Replace(dateStr, date);
}
//如果redis存在对应的键值 则只出现一次通知
if (await _redisManager.ExistsAsync(key))
{
return false;
}
else
{
await _redisManager.SetAsync(key, 1, TimeSpan.FromDays(1));
}
}
return true;
}
public async Task<bool> Repairdata()
{
var sql = @"select * from eventdwd";
var data = await _actRepository.ExecuteSqlToListAsync<Eventdwd>(sql);
foreach (var item in data)
{
EventDto eventDto = new EventDto()
{
act_date = item.act_date.ToString(),
appid = item.appid,
appuserid = item.appuserid,
resid = item.resid,
unionid = item.unionid,
channel = item.channel,
customerid = item.customerid,
deptid = item.deptid,
eventid = item.eventid,
eventmemo = item.eventmemo,
eventname = item.eventname,
ip = item.ip,
others = item.others,
productplat = item.productplat,
productplatname = item.productplatname,
sceneext = item.sceneext,
sceneid = item.sceneid,
sceneidname = item.sceneidname,
scenetype = item.scenetype,
scenetypename = item.scenetypename,
sinkClickhouseTable = "手工推送",
uid = item.uid,
user_type = item.user_type,
with_eid = item.with_eid,
with_groupid = item.with_groupid,
with_orgid = item.with_orgid
};
if (item.act_time.HasValue)
{
eventDto.act_time = ConvertDataTimeLong(item.act_time.Value);
}
if (item.leavetime.HasValue)
{
eventDto.leavetime = ConvertDataTimeLong(item.leavetime.Value);
}
await RunEvent(eventDto);
}
return true;
}
/// <summary>
/// 将DateTime类型转换为long类型
/// </summary>
/// <param name="dt">时间</param>
/// <returns></returns>
public static long ConvertDataTimeLong(DateTime dt)
{
//dateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000
DateTime dtBase = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan toNow = dt.ToUniversalTime().Subtract(dtBase);
long timeStamp = toNow.Ticks / 10000;
return timeStamp;
}
}
}