﻿using BilibiliJJ_BarrageSystem.COM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BilibiliJJ_BarrageSystem.DAL {
    public static class TianYi {
        public static string GetDownUrl(string Url) {
            using (SqlDataContext Sql = new SqlDataContext()) {
                var T = Sql.TB_TianYiDownUrl;
                var S = T.Where(a => a.z_Url == Url && NewC.ConvertDateTimeInt(DateTime.Now) - a.z_Time < 86400);
                if (S.Count() > 0) {
                    return S.ToList()[0].z_DownUrl;
                }
                else {
                    string DownUrl = NewC.Get302Url(Url);
                    if (!string.IsNullOrEmpty(DownUrl)) {
                        var D = T.Where(a => a.z_Url == Url); 
                        if (D.Count() > 0) {
                            D.ToList()[0].z_DownUrl = DownUrl;
                        }
                        else T.InsertOnSubmit(new TB_TianYiDownUrl() { z_Url = Url, z_DownUrl = DownUrl, z_Time = NewC.ConvertDateTimeInt(DateTime.Now) });
                    }
                    Sql.SubmitChanges();
                    return DownUrl;
                }
            }
            return Url;
        }
    }
}
