using EntityFrameworkCore.Triggered;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRaCIS.Core.Application.Triggers.AfterSaveTrigger
{

    /// <summary>
    /// 维护用户角色表中的账户信息
    /// </summary>
    /// <param name="_identityUserRepository"></param>
    /// <param name="_userRoleRepository"></param>
    public class IdenttiyUserRoleInfoTrigger(IRepository<IdentityUser> _identityUserRepository, IRepository<UserRole> _userRoleRepository) : IAfterSaveTrigger<IdentityUser>
    {
        public async Task AfterSave(ITriggerContext<IdentityUser> context, CancellationToken cancellationToken)
        {
            var identityUser = context.Entity;

            //保持用户角色表和用户表信息一致
            await _userRoleRepository.BatchUpdateNoTrackingAsync(t => t.IdentityUserId == identityUser.Id, u => new UserRole()
            {
                UserName = identityUser.UserName,
                FirstName = identityUser.FirstName,
                LastName = identityUser.LastName,
                EMail = identityUser.EMail,
            });
        }
    }


    
}
