1、 首先定义如下,使用func带返回类型的方式
private readonly Dictionary<string, Func<string, IEnumerable<GetInnvationPlatformHotWordsQto>, List<GetProductCategoryQto>, Task>> _updateInnvateBaseNormalizedDic = new Dictionary<string, Func<string, IEnumerable<GetInnvationPlatformHotWordsQto>, List<GetProductCategoryQto>, Task>>();
2、定义方法
/// <summary>
/// 关联中游产业链
/// </summary>
/// <param name="type"></param>
/// <param name="num"></param>
/// <param name="qtos"></param>
/// <param name="dtos"></param>
/// <returns></returns>
private async Task CreateInnovationProductCategoryAsync(string type, IEnumerable<GetInnvationPlatformHotWordsQto> qtos,
List<GetProductCategoryQto> dtos)
{
var keys = new List<string>();
List<TalentInnoPfChainMapping> talentInnoPfChains = new List<TalentInnoPfChainMapping>();
foreach (var item in qtos)
{
keys = item.HotWords.Split(SystemConstants.SpiltConstDHStr, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
talentInnoPfChains.AddRange(dtos.Where(s => keys.Any(k => s.Name.Contains(k))).Select(s => new TalentInnoPfChainMapping(s.ProductCategoryId, s.Id, item.Id, nameof(InnovationPlatform))));
}
await _talentInfoPfRepository.DeleteAsync(t => talentInnoPfChains.Select(s => s.EntityId).Contains(t.EntityId) && t.EntityType == nameof(InnovationPlatform));
int rows = (Int32)(Math.Ceiling(talentInnoPfChains.Count() / 1000.00));
for (int i = 1; i <= rows; i++)
{
await _talentInfoPfRepository.InsertManyAsync(talentInnoPfChains.Skip((i - 1) * 1000).Take(1000));
}
}
为啥用func而不用action因为,这里调用各自方法时的,方法定义的是异步的,返回的是task
如果使用action定义时,会没有返回类型,导致给_updateInnvateBaseNormalizedDic添加对应的方法时会报错,如下
注意:这里用到的知识点有字典泛型、委托、简单工厂
3、完整代码如下:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUglify.JavaScript.Syntax;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tzkj.Core.Constants;
using Tzkj.GreenIndustry.Migrations;
using Tzkj.GreenIndustry.Query.InnovationPlatforms;
using Tzkj.GreenIndustry.Query.InnovationPlatforms.Qto;
using Tzkj.GreenIndustry.Query.ScientificResearchs;
using Tzkj.GreenIndustry.Query.ScientificResearchs.Qtos;
using Tzkj.GreenIndustry.Query.Talents;
using Tzkj.GreenIndustry.ScientificResearch;
using Tzkj.GreenIndustry.SyncJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
namespace Tzkj.GreenIndustry.Jobs
{
/// <summary>
/// 创新平台上中下游产业链关联作业job
/// </summary>
public class InnovatePfIndustryChainJob : IJob, ITransientDependency
{
private readonly IRepository<SyncJobConfigNew, Guid> _syncJobConfigRepository;
private readonly IRepository<InnovationPlatform> _innovatePlatformRepository;
private readonly IRepository<TalentInnoFfChainMapping> _talentInfoFfRepository;
private readonly IRepository<TalentInnoPfChainMapping> _talentInfoPfRepository;
private readonly IRepository<TalentInnoMfChainMapping> _talentInfoMfRepository;
private readonly InnovatePlatformQueryRepository _innovatePlatformQueryRepository;
private readonly TalentInnoPlatQueryRepository _talentInnoPlatQueryRepository;
private readonly SyncJobLogBase _syncJobLogBase;
private readonly Dictionary<string, Func<string, Task<IEnumerable<GetProductCategoryQto>>>> _getIndustryBaseNormalizedDicAsync = new Dictionary<string, Func<string, Task<IEnumerable<GetProductCategoryQto>>>>();
private readonly Dictionary<string, Func<string, IEnumerable<GetInnvationPlatformHotWordsQto>, List<GetProductCategoryQto>, Task>> _updateInnvateBaseNormalizedDic = new Dictionary<string, Func<string, IEnumerable<GetInnvationPlatformHotWordsQto>, List<GetProductCategoryQto>, Task>>();
public InnovatePfIndustryChainJob(IRepository<SyncJobConfigNew, Guid> syncJobConfigRepository, IRepository<InnovationPlatform> innovatePlatformRepository,
IRepository<TalentInnoFfChainMapping> talentInfoFfRepository, IRepository<TalentInnoPfChainMapping> talentInfoPfRepository,
IRepository<TalentInnoMfChainMapping> talentInfoMfRepository, TalentInnoPlatQueryRepository talentInnoPlatQueryRepository,
InnovatePlatformQueryRepository innovatePlatformQueryRepository, SyncJobLogBase syncJobLogBase)
{
_syncJobConfigRepository = syncJobConfigRepository;
_innovatePlatformRepository = innovatePlatformRepository;
_talentInfoFfRepository = talentInfoFfRepository;
_talentInfoPfRepository = talentInfoPfRepository;
_talentInfoMfRepository = talentInfoMfRepository;
_talentInnoPlatQueryRepository = talentInnoPlatQueryRepository;
_innovatePlatformQueryRepository = innovatePlatformQueryRepository;
_syncJobLogBase = syncJobLogBase;
_getIndustryBaseNormalizedDicAsync.Add(nameof(TalentInnoFfChainMapping), async (string arg) => { return await _talentInnoPlatQueryRepository.GetFactorCategoriesAsync(); });
_getIndustryBaseNormalizedDicAsync.Add(nameof(TalentInnoPfChainMapping), async (string arg) => { return await _talentInnoPlatQueryRepository.GetProductCategoriesAsync(); });
_getIndustryBaseNormalizedDicAsync.Add(nameof(TalentInnoMfChainMapping), async (string arg) => { return await _talentInnoPlatQueryRepository.GetMarketCategoriesAsync(); });
_updateInnvateBaseNormalizedDic.Add(nameof(TalentInnoFfChainMapping), CreateInnovationProductCategoryAsync);
_updateInnvateBaseNormalizedDic.Add(nameof(TalentInnoPfChainMapping), CreateInnovationProductCategoryAsync);
_updateInnvateBaseNormalizedDic.Add(nameof(TalentInnoMfChainMapping), CreateInnovationMarketCategoryAsync);
}
/// <summary>
/// 开始执行
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
[UnitOfWork]
public virtual async Task Execute(IJobExecutionContext context)
{
var id = context.MergedJobDataMap.GetString("id");
var type = context.MergedJobDataMap.GetString("type");
//1.根据id查找job
var job = await _syncJobConfigRepository.GetAsync(Guid.Parse(id));
await _syncJobLogBase.CreateSyncJobLogAsync(job, type);
var dynamicObj = JsonConvert.DeserializeObject<dynamic>(job.JobProperty);
JArray itemList = dynamicObj.Parameters;
var param = itemList.FirstOrDefault();
if (param == null)
{
return;
}
string dicType = (string)param["paramValue"];
var qtos = await _innovatePlatformQueryRepository.GetInnovationPlatformHotWordsAsync(dicType);
if (_getIndustryBaseNormalizedDicAsync.TryGetValue(dicType, out Func<string, Task<IEnumerable<GetProductCategoryQto>>> func))
{
var keys = new List<string>();
var categories = new List<GetProductCategoryQto>();
var result = await func(dicType);
foreach (var item in qtos)
{
keys = item.HotWords.Split(SystemConstants.SpiltConstDHStr, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
categories.AddRange(result.Where(s => keys.Any(k => s.Name.Contains(k))));
}
int rows = (Int32)(Math.Ceiling(categories.Count() / 1000.00));
if (_updateInnvateBaseNormalizedDic.TryGetValue(dicType, out Func<string, IEnumerable<GetInnvationPlatformHotWordsQto>, List<GetProductCategoryQto>, Task> action))
{
await action(dicType, qtos, categories);
}
}
}
/// <summary>
/// 关联上游产业链
/// </summary>
/// <param name="type"></param>
/// <param name="num"></param>
/// <param name="qtos"></param>
/// <param name="dtos"></param>
/// <returns></returns>
private async Task CreateInnovationFactorCategoryAsync(string type, IEnumerable<GetInnvationPlatformHotWordsQto> qtos,
List<GetProductCategoryQto> dtos)
{
var keys = new List<string>();
List<TalentInnoFfChainMapping> talentInnoPfChains = new List<TalentInnoFfChainMapping>();
foreach (var item in qtos)
{
keys = item.HotWords.Split(SystemConstants.SpiltConstDHStr, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
talentInnoPfChains.AddRange(dtos.Where(s => keys.Any(k => s.Name.Contains(k))).Select(s => new TalentInnoFfChainMapping(s.ProductCategoryId, s.Id, item.Id, nameof(InnovationPlatform))));
}
await _talentInfoFfRepository.DeleteAsync(t => talentInnoPfChains.Select(s => s.EntityId).Contains(t.EntityId) && t.EntityType == nameof(InnovationPlatform));
int rows = (Int32)(Math.Ceiling(talentInnoPfChains.Count() / 1000.00));
for (int i = 1; i <= rows; i++)
{
await _talentInfoFfRepository.InsertManyAsync(talentInnoPfChains.Skip((i - 1) * 1000).Take(1000));
}
}
/// <summary>
/// 关联中游产业链
/// </summary>
/// <param name="type"></param>
/// <param name="num"></param>
/// <param name="qtos"></param>
/// <param name="dtos"></param>
/// <returns></returns>
private async Task CreateInnovationProductCategoryAsync(string type, IEnumerable<GetInnvationPlatformHotWordsQto> qtos,
List<GetProductCategoryQto> dtos)
{
var keys = new List<string>();
List<TalentInnoPfChainMapping> talentInnoPfChains = new List<TalentInnoPfChainMapping>();
foreach (var item in qtos)
{
keys = item.HotWords.Split(SystemConstants.SpiltConstDHStr, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
talentInnoPfChains.AddRange(dtos.Where(s => keys.Any(k => s.Name.Contains(k))).Select(s => new TalentInnoPfChainMapping(s.ProductCategoryId, s.Id, item.Id, nameof(InnovationPlatform))));
}
await _talentInfoPfRepository.DeleteAsync(t => talentInnoPfChains.Select(s => s.EntityId).Contains(t.EntityId) && t.EntityType == nameof(InnovationPlatform));
int rows = (Int32)(Math.Ceiling(talentInnoPfChains.Count() / 1000.00));
for (int i = 1; i <= rows; i++)
{
await _talentInfoPfRepository.InsertManyAsync(talentInnoPfChains.Skip((i - 1) * 1000).Take(1000));
}
}
/// <summary>
/// 关联下游产业链
/// </summary>
/// <param name="type"></param>
/// <param name="num"></param>
/// <param name="qtos"></param>
/// <param name="dtos"></param>
/// <returns></returns>
private async Task CreateInnovationMarketCategoryAsync(string type, IEnumerable<GetInnvationPlatformHotWordsQto> qtos,
List<GetProductCategoryQto> dtos)
{
var keys = new List<string>();
List<TalentInnoMfChainMapping> talentInnoPfChains = new List<TalentInnoMfChainMapping>();
foreach (var item in qtos)
{
keys = item.HotWords.Split(SystemConstants.SpiltConstDHStr, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
talentInnoPfChains.AddRange(dtos.Where(s => keys.Any(k => s.Name.Contains(k))).Select(s => new TalentInnoMfChainMapping(s.ProductCategoryId, s.Id, item.Id, nameof(InnovationPlatform))));
}
await _talentInfoMfRepository.DeleteAsync(t => talentInnoPfChains.Select(s => s.EntityId).Contains(t.EntityId) && t.EntityType == nameof(InnovationPlatform));
int rows = (Int32)(Math.Ceiling(talentInnoPfChains.Count() / 1000.00));
for (int i = 1; i <= rows; i++)
{
await _talentInfoMfRepository.InsertManyAsync(talentInnoPfChains.Skip((i - 1) * 1000).Take(1000));
}
}
}
}