using System.Data;
using System.Text.RegularExpressions;
using PowerEasy.WebFuture.DataMigrations.Foundation.Enum;
using PowerEasy.WebFuture.DataMigrations.Foundation.Service;
using PowerEasy.WebFuture.DataMigrations.Foundation.Utilities;
using PowerEasy.WebFuture.DataMigrations.MigrationProgramme.SiteFactoryProgramme.Model;

namespace PowerEasy.WebFuture.DataMigrations.MigrationProgramme.SiteFactoryProgramme
{
    /// <summary>
    /// 文章迁移。
    /// </summary>
    public class ArticleMigration : BaseContentMoldDataMigrationItem
    {
        /// <summary>
        /// 模型名字。
        /// </summary>
        public override string ModelName { get; set; } = "文章";

        /// <summary>
        /// 迁移项目的排序，用于执行和工具显示的顺序，从小到大排序。
        /// </summary>
        public override int Order { get; set; } = 2;

        /// <summary>
        /// 迁移项目名称。
        /// </summary>
        public override string MigrationItemName { get; set; } = "迁移文章";

        /// <summary>
        /// 目标数据库表名。
        /// </summary>
        public override string TargetDatabaseTableName { get; set; } = "PE_ContentManage_Article";

        /// <summary>
        /// 来源数据库表的主键名称。
        /// </summary>
        public override string SourcePrimaryKeyName { get; set; } = "GeneralID";

        /// <summary>
        /// 来源数据库表的节点名称。
        /// </summary>
        public override string SourceNodeIdName { get; set; } = "NodeID";

        /// <summary>
        /// 上传目录。
        /// </summary>
        public override string UploadPath { get; set; } = "$/History";

        /// <summary>
        /// 目标站点id。
        /// </summary>
        public override int SiteId { get; set; } = 1;

        /// <summary>
        /// 获取来源要迁移的数据集合。
        /// </summary>
        /// <param name="newContentId">最新迁移的数据主键。</param>
        public override void LoadSourceDataTable(int newContentId)
        {
            // 获取源数据集合。
            var sql = $"select top 10000 * from PE_CommonModel as cm left join PE_U_Article as a on cm.ItemID=a.ID where GeneralID > {newContentId} and TableName='PE_U_Article' and Title!='' order by GeneralID";

            this.SourceDataTable = DatabaseService.GetSourceDatabaseProvider(this.MigrationProgrammeIdentifier).ExecuteDataTable(sql);
        }

        /// <summary>
        /// 字段映射。（没有赋值的属性都使用了Article实体中的默认值，如果需要，请在本方法重新赋值。）
        /// </summary>
        /// <param name="sourceDataRow">源数据表行数据。</param>
        /// <param name="targetDataRow">目标数据表行数据。</param>
        /// <returns>返回映射后的行数据。</returns>
        public override DataRow MapFields(DataRow sourceDataRow, DataRow targetDataRow)
        {
            var article = new Article();

            // MoldId具体值请去PE_ContentManage_ContentMold表查看具体MoldId值。
            article.MoldId = 1;
            article.BelongSiteId = this.SiteId;
            article.ExtendContent = "<ExtendContent />";
            article.Status = Status.PendingAudit;

            article.HistoryId = MigrateHelper.GetColumnValueInt(sourceDataRow, this.SourcePrimaryKeyName);

            article.Title = MigrateHelper.GetColumnValueString(sourceDataRow, "title");

            article.Subheading = MigrateHelper.GetColumnValueString(sourceDataRow, "Subheading");

            article.TitleIntact = MigrateHelper.GetColumnValueString(sourceDataRow, "TitleIntact");

            var content = MigrateHelper.GetColumnValueString(sourceDataRow, "Content");
            content = content.Replace("{PE.SiteConfig.ApplicationPath/}", string.Empty);
            content = content.Replace("{PE.SiteConfig.ApplicationPath/}{PE.SiteConfig.uploaddir/}", string.Empty);
            content = content.Replace("{PE.SiteConfig.uploaddir/}", string.Empty);
            content = MigrateHelper.ReplaceContentUploadPath(content, this.UploadPath);
            article.Content = content;

            article.Intro = MigrateHelper.GetColumnValueString(sourceDataRow, "Intro") == string.Empty ? MigrateHelper.FilterHTMLTag(content) : MigrateHelper.GetColumnValueString(sourceDataRow, "Intro");

            article.Keyword = MigrateHelper.GetColumnValueString(sourceDataRow, "Keyword");

            article.Author = MigrateHelper.GetColumnValueString(sourceDataRow, "Author");

            article.CopyFrom = MigrateHelper.GetColumnValueString(sourceDataRow, "CopyFrom");

            var featuredimage = MigrateHelper.AddUploadPathFlag(MigrateHelper.GetColumnValueString(sourceDataRow, "DefaultPicUrl"), this.UploadPath);
            article.FeaturedImage = featuredimage.Replace("\\'", string.Empty).Trim('\'').Replace("'", "‘").TrimEnd('\\');

            article.Hits = MigrateHelper.GetColumnValueInt(sourceDataRow, "Hits");

            article.PublishTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "UpdateTime");

            article.CreateTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "InputTime");

            article.Priority = MigrateHelper.GetColumnValueInt(sourceDataRow, "Priority");

            article.UpdateTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "UpdateTime");

            article.StatusUpdateTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "UpdateTime");

            article.Inputter = MigrateHelper.GetColumnValueString(sourceDataRow, "Inputer");

            targetDataRow = MigrateHelper.GetMapperDataRow(article, targetDataRow);
            return targetDataRow;
        }
    }
}
