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 PublicInformationMigration : BaseContentMoldDataMigrationItem
    {
        /// <summary>
        /// 模型名字
        /// </summary>
        public override string ModelName { get; set; } = "信息公开";

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

        /// <summary>
        /// 迁移项目名称。
        /// </summary>
        public override string MigrationItemName { get; set; } = "迁移信息公开";

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

        /// <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_GV_PublicInformation as a on cm.ItemID=a.ID where GeneralID > {newContentId} and TableName='PE_GV_PublicInformation' and PE_GV_PublicInformation.Title!='' order by GeneralID";

            // 获取源数据集合。
            this.SourceDataTable = DatabaseService.GetSourceDatabaseProvider(this.MigrationProgrammeIdentifier).ExecuteDataTable(sql);
        }

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

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

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

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

            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?.ToString(), this.UploadPath);
            publicInformation.Content = content;

            publicInformation.Intro = MigrateHelper.FilterHTMLTag(content);

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

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

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

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

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

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

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

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

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

            publicInformation.DocumentNumber = MigrateHelper.GetColumnValueString(sourceDataRow, "DocumentNumber");

            publicInformation.MetadataDate = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "InputTime");

            publicInformation.Date = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "InputTime");

            publicInformation.EffectDate = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "InputTime");

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