using EntityFrameworkCore.Triggered;
using IP2Region.Net.Abstractions;

namespace IRaCIS.Core.Application.Triggers
{
    public class UserLogTrigger(
       ISearcher _searcher) : IBeforeSaveTrigger<UserLog>
    {


        //国家|区域|省份|城市|ISP   缺省的地域信息默认是0
        //0|0|0|内网IP|内网IP
        // 中国|0|湖北省|武汉市|电信
        public async Task BeforeSave(ITriggerContext<UserLog> context, CancellationToken cancellationToken)
        {
            var userLog = context.Entity;

            if (context.ChangeType == ChangeType.Added)
            {

                var ipinfo = _searcher.Search(userLog.IP);

                userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));


            }
        }
    }
}
