439 lines
17 KiB
C#
439 lines
17 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using MySqlConnector;
|
|
using Newtonsoft.Json;
|
|
using System.Reflection;
|
|
using Zxd.Core.Domain.Dto.TodoItem;
|
|
using Zxd.Entity.Action;
|
|
|
|
namespace Zxd.Core.Domain
|
|
{
|
|
public class TodoItemDomain : ITodoItemDomain
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
private readonly IBaseRepository<CrmCloudDbContext> _hgActionRepository;
|
|
|
|
private readonly IBaseRepository<ZxdDbContext> _zxdRepository;
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private readonly IDeptmentDomain _deptDomain;
|
|
|
|
private readonly IBaseRepository<DncmsbaseDbContext> _dncmsBaseRepository;
|
|
|
|
private readonly SystemConfig _systemConfig;
|
|
|
|
private readonly IEarlyWarningDomain _earlyWarningDomain;
|
|
|
|
private readonly ICacheDomain _cacheDomain;
|
|
|
|
public TodoItemDomain(
|
|
IServiceProvider serviceProvider,
|
|
IBaseRepository<CrmCloudDbContext> hgActionRepository,
|
|
IBaseRepository<ZxdDbContext> zxdRepository,
|
|
IConfiguration configuration,
|
|
IDeptmentDomain deptDomain,
|
|
IEarlyWarningDomain earlyWarningDomain,
|
|
IBaseRepository<DncmsbaseDbContext> dncmsBaseRepository,
|
|
ICacheDomain cacheDomain)
|
|
{
|
|
_systemConfig = configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
|
_serviceProvider = serviceProvider;
|
|
_hgActionRepository = hgActionRepository;
|
|
_zxdRepository = zxdRepository;
|
|
_configuration = configuration;
|
|
_deptDomain = deptDomain;
|
|
_earlyWarningDomain = earlyWarningDomain;
|
|
_dncmsBaseRepository = dncmsBaseRepository;
|
|
_cacheDomain = cacheDomain;
|
|
}
|
|
|
|
public async Task<PageResult<TodoItemDto>> GetListNewAsync(GetListRequest request)
|
|
{
|
|
var depts = await _deptDomain.GetDeptments();
|
|
var tableFilter = "";
|
|
var filter = new List<string>();
|
|
var ps = new List<MySqlParameter>();
|
|
|
|
var setting = await _cacheDomain.GetValueParameter("ToDoItemSetting");
|
|
var sets = new List<ToDoItemSettingItem>();
|
|
if (!string.IsNullOrWhiteSpace(setting))
|
|
{
|
|
sets = JsonHelper.FromJson<ToDoItemSetting>(setting).setting;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.lineid))
|
|
{
|
|
var lineids = GetIds<int>(request.lineid);
|
|
if (lineids.Count > 0)
|
|
{
|
|
filter.Add($"t1.deptid in ({string.Join(",", lineids)})");
|
|
}
|
|
}
|
|
|
|
if (request.eid.HasValue)
|
|
{
|
|
filter.Add($"t1.eid = @eid");
|
|
ps.Add(new MySqlParameter("eid", request.eid));
|
|
}
|
|
else
|
|
{
|
|
if (request.deptid.HasValue)
|
|
{
|
|
tableFilter = $"{Environment.NewLine}JOIN employee_department_detail t3 on t3.eid = t1.eid";
|
|
filter.Add($"t3.department_id = @deptid");
|
|
filter.Add($"t3.is_deleted = 0");
|
|
ps.Add(new MySqlParameter("deptid", request.deptid));
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.resid))
|
|
{
|
|
filter.Add($"(t1.resid = @resid or t1.umid = @resid)");
|
|
ps.Add(new MySqlParameter("resid", request.resid));
|
|
}
|
|
|
|
if (request.customerid.HasValue)
|
|
{
|
|
filter.Add($"t1.customerid = @customerid");
|
|
ps.Add(new MySqlParameter("customerid", request.customerid));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.cname))
|
|
{
|
|
filter.Add($"t1.Nickname like @cname");
|
|
ps.Add(new MySqlParameter("cname", $"%{request.cname}%"));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.eventtypename))
|
|
{
|
|
var eventids = GetIds<int>(request.eventtypename);
|
|
if (eventids.Count > 0)
|
|
{
|
|
filter.Add($"t1.neweventid in ({string.Join(",", eventids)})");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(request.eventid))
|
|
{
|
|
var eventids = GetIds<int>(request.eventid);
|
|
if (eventids.Count > 0)
|
|
{
|
|
filter.Add($"t1.neweventid in ({string.Join(",", eventids)})");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.eventmemo))
|
|
{
|
|
filter.Add($"t1.Content like @eventmemo");
|
|
ps.Add(new MySqlParameter("eventmemo", $"%{request.eventmemo}%"));
|
|
}
|
|
|
|
if (request.noticetimeBegin.HasValue)
|
|
{
|
|
var dt = request.noticetimeBegin.GetValueOrDefault().Date;
|
|
filter.Add($"t1.act_date >= @noticetimeBegin");
|
|
ps.Add(new MySqlParameter("noticetimeBegin", dt));
|
|
}
|
|
|
|
if (request.noticetimeEnd.HasValue)
|
|
{
|
|
var dt = request.noticetimeEnd.GetValueOrDefault().Date.AddDays(1);
|
|
filter.Add($"t1.act_date < @noticetimeEnd");
|
|
ps.Add(new MySqlParameter("noticetimeEnd", dt));
|
|
}
|
|
|
|
if (request.isread.HasValue)
|
|
{
|
|
filter.Add($"t1.isread = {request.isread}");
|
|
}
|
|
|
|
if (request.readtimeBegin.HasValue)
|
|
{
|
|
var dt = request.readtimeBegin.GetValueOrDefault().Date;
|
|
filter.Add($"t1.readtime >= @readtimeBegin");
|
|
ps.Add(new MySqlParameter("readtimeBegin", dt));
|
|
}
|
|
|
|
if (request.readtimeEnd.HasValue)
|
|
{
|
|
var dt = request.readtimeEnd.GetValueOrDefault().Date.AddDays(1);
|
|
filter.Add($"t1.readtime < @readtimeEnd");
|
|
ps.Add(new MySqlParameter("readtimeEnd", dt));
|
|
}
|
|
|
|
var where = "";
|
|
if (filter.Count > 0)
|
|
{
|
|
where = $"{Environment.NewLine}where {string.Join(" and ", filter)}";
|
|
}
|
|
|
|
var tables = $@"
|
|
ac_employee_todoitem t1{tableFilter}";
|
|
|
|
var fields = $@"
|
|
t1.id,
|
|
t1.deptid,
|
|
t1.eid,
|
|
t1.ename,
|
|
t1.resid,
|
|
t1.umid,
|
|
t1.customerid,
|
|
t1.Nickname cname,
|
|
t1.eventid,
|
|
t1.neweventid,
|
|
t1.neweventname eventname,
|
|
t1.Content eventmemo,
|
|
t1.ctime noticetime,
|
|
t1.eventtypename,
|
|
t1.isread,
|
|
t1.readtime,
|
|
t1.appid,
|
|
t1.appuserid,
|
|
t1.sceneid";
|
|
var skip = (request.PageIndex - 1) * request.PageSize;
|
|
var take = request.PageSize;
|
|
using (var scope = _serviceProvider.CreateAsyncScope())
|
|
{
|
|
var repository = scope.ServiceProvider.GetRequiredService<IBaseRepository<CrmCloudDbContext>>();
|
|
var sql = $"select count(1) from {tables}{where}";
|
|
var total = await repository.ExecuteSqlToCountLongAsync(sql, ps.ToArray());
|
|
sql = $"select {fields} from {tables}{where} order by t1.id desc limit {skip},{take}";
|
|
if (!string.IsNullOrEmpty(request.eventmemo))// 如果包含了模糊查找,那么就需要进行子查询,会快一点
|
|
{
|
|
sql = $"select {fields} from ac_employee_todoitem t1 where t1.id in( select t1.id from {tables}{where} ) order by t1.id desc limit {skip},{take}";
|
|
}
|
|
var items = await repository.ExecuteSqlToListAsync<TodoItemDto>(sql, ps.ToArray());
|
|
foreach (var item in items)
|
|
{
|
|
//这里要关联Id而不是DepartmentId
|
|
var dept = depts.Where(w => w.Id == item.deptid).FirstOrDefault();
|
|
if (dept != null)
|
|
{
|
|
item.deptname = dept.Title;
|
|
}
|
|
|
|
var set = sets.FirstOrDefault(x => x.neweventid == item.neweventid);
|
|
if (set != null && !string.IsNullOrWhiteSpace(set.linkUrl))
|
|
{
|
|
item.linkurl = GetContent(item, set.linkUrl);
|
|
}
|
|
}
|
|
return new PageResult<TodoItemDto>(request.PageIndex, request.PageSize, Convert.ToInt32(total), items);
|
|
}
|
|
}
|
|
|
|
private string GetContent(TodoItemDto item, string linkUrl)
|
|
{
|
|
var content = linkUrl;
|
|
Type type = typeof(TodoItemDto);
|
|
PropertyInfo[] propertyInfo = type.GetProperties();
|
|
foreach (var property in propertyInfo)
|
|
{
|
|
var value = property.GetValue(item);
|
|
var setValue = value == null ? "" : value.ToString();
|
|
var name = "{" + property.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;
|
|
}
|
|
|
|
private List<T> GetIds<T>(string input)
|
|
{
|
|
var ids = new List<T>();
|
|
var parts = input.Split(',');
|
|
var t = typeof(T);
|
|
if (t == typeof(int))
|
|
{
|
|
foreach (var part in parts)
|
|
{
|
|
if (!int.TryParse(part, out int temp))
|
|
{
|
|
throw new Exception("无效的输入");
|
|
}
|
|
|
|
ids.Add((T)(object)temp);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("无效的类型T");
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
|
|
public async Task<List<SelectItem>> GetEventTypeSelectAsync(GetEventTypeSelectRequest request)
|
|
{
|
|
var items = new List<SelectItem>();
|
|
var setting = await _zxdRepository.GetRepository<BAS_PARAMETER>().Query().FirstOrDefaultAsync(n => n.PARAKEY == "ToDoItemSetting");
|
|
if (setting != null && !string.IsNullOrWhiteSpace(setting.PARAVALUE))
|
|
{
|
|
var config = JsonConvert.DeserializeObject<ToDoItemSetting>(setting.PARAVALUE);
|
|
if (config != null && config.setting != null && config.setting.Count > 0)
|
|
{
|
|
if (!string.IsNullOrEmpty(request.eventid))
|
|
{
|
|
var ids = GetIds<int>(request.eventid);
|
|
items.AddRange(config.setting.Where(w => ids.Contains(w.neweventid.GetValueOrDefault())).Select(o => new SelectItem(o.memo, o.neweventid)));
|
|
}
|
|
else
|
|
{
|
|
var groups = config.setting.GroupBy(g => new { g.remark, g.Sort });
|
|
groups = groups.OrderBy(n => n.Key.Sort);
|
|
foreach (var group in groups)
|
|
{
|
|
items.Add(new SelectItem(group.Key.remark, string.Join(",", group.Select(o => o.neweventid.GetValueOrDefault()))));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改已读
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> EditReadAsync(EditReadRequest request)
|
|
{
|
|
var model = await _hgActionRepository.GetRepository<EmployeeTodoitem>().FirstOrDefaultAsync(f => f.id == request.Id);
|
|
if (model != null && model.isread != 1 && model.readtime.HasValue == false)
|
|
{
|
|
model.isread = 1;
|
|
model.readtime = DateTime.Now;
|
|
await _hgActionRepository.GetRepository<EmployeeTodoitem>().UpdateAsync(model);
|
|
}
|
|
return "修改成功";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重要线索消息通知
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task NoticeAsync()
|
|
{
|
|
try
|
|
{
|
|
var now = DateTime.Now;
|
|
if (now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday)
|
|
return;
|
|
|
|
//获取参数配置中的消息可通知时间段
|
|
var todoItemNotice = await _zxdRepository.GetRepository<BAS_PARAMETER>().Query().FirstOrDefaultAsync(n => n.PARAKEY == "ToDoItemNotice");
|
|
if (todoItemNotice != null && !string.IsNullOrWhiteSpace(todoItemNotice.PARAVALUE))
|
|
{
|
|
var noticeDto = JsonConvert.DeserializeObject<ToDoItemNoticeDto>(todoItemNotice.PARAVALUE);
|
|
|
|
if (!noticeDto.IsNotice)
|
|
return;
|
|
|
|
DateTime.TryParse($"{now.Date.ToString("yyyy-MM-dd")} {noticeDto.StartTime}", out DateTime startTime);
|
|
DateTime.TryParse($"{now.Date.ToString("yyyy-MM-dd")} {noticeDto.EndTime}", out DateTime endTime);
|
|
|
|
if (now >= startTime && now <= endTime)
|
|
{
|
|
//获取重要线索
|
|
var todoItems = await _hgActionRepository.GetRepository<EmployeeTodoitem>().Query()
|
|
.Where(x => x.ctime >= now.Date && x.isread == 0)
|
|
.GroupBy(x => new { x.eid, x.deptid })
|
|
.Select(x => new
|
|
{
|
|
Eid = x.Key.eid,
|
|
DeptId = x.Key.deptid,
|
|
Items = x.Select(y => y.eventtypename),
|
|
Count = x.Count()
|
|
})
|
|
.ToListAsync();
|
|
|
|
//企微应用的ID
|
|
var weworkAgents = await _dncmsBaseRepository.GetRepository<WeworkAgent>().Query().Where(x => x.name == "客服达量通知").ToListAsync();
|
|
//获取员工的信息
|
|
var weworks = await _dncmsBaseRepository.GetRepository<weworkuser2eid>().Query().ToListAsync();
|
|
foreach (var todoItem in todoItems)
|
|
{
|
|
var weworkUsers = weworks.Where(x => x.eid == todoItem.Eid && x.deptid == todoItem.DeptId);
|
|
if (!weworkUsers.Any())
|
|
continue;
|
|
|
|
//重要线索列表页路径
|
|
var app = _systemConfig.Apps.Where(x => x.Deptids.Contains(weworkUsers.FirstOrDefault().deptid.Value)).FirstOrDefault();
|
|
var toDoItemListUrl = $"{app?.CrmUrl}Csvr/ToDoItem/ImportantNew?urlTitle=我的重要线索客户";
|
|
|
|
var message = $"有<font color=\"warning\">{todoItem.Count}条</font>重要线索未读 {now},<a href=\"{toDoItemListUrl}\">【点击更多】</a>";
|
|
|
|
var items = todoItem.Items.GroupBy(x => x).Select(x => new { Name = x.Key, Count = x.Count() });
|
|
foreach (var item in items)
|
|
{
|
|
message += $"\n{item.Name}:<font color=\"warning\">{item.Count}条</font>";
|
|
}
|
|
|
|
foreach (var weworkUser in weworkUsers)
|
|
{
|
|
var weworkAgent = weworkAgents.FirstOrDefault(x => x.appid == weworkUser.appid);
|
|
if (weworkAgent == null)
|
|
continue;
|
|
|
|
await _earlyWarningDomain.WeworkSend(weworkUser.userid, weworkUser.appid, weworkAgent.agentid.ToString(), message);
|
|
//await _earlyWarningDomain.WeworkSend("chenshidong", "ww89347c2378b6e050", "1000028", message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error($"重要线索消息通知报错", ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class ToDoItemSetting
|
|
{
|
|
public int delaytime { get; set; }
|
|
|
|
public List<ToDoItemSettingItem> setting { get; set; }
|
|
}
|
|
|
|
public class ToDoItemSettingItem
|
|
{
|
|
public int? eventid { get; set; }
|
|
|
|
public string remark { get; set; }
|
|
|
|
public string memo { get; set; }
|
|
|
|
public string template { get; set; }
|
|
|
|
public bool? allNotice { get; set; }
|
|
|
|
public bool? firstCtime { get; set; }
|
|
|
|
public bool? is_wework { get; set; }
|
|
|
|
public bool? is_mobile { get; set; }
|
|
|
|
public bool? is_belong { get; set; }
|
|
|
|
public List<int> scenetype { get; set; }
|
|
|
|
public List<string> sceneid { get; set; }
|
|
|
|
public List<int> deptids { get; set; }
|
|
public int? neweventid { get; set; }
|
|
public int? Sort { get; set; }
|
|
public string? linkUrl { get; set; }
|
|
}
|
|
} |