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 SoftMigration : BaseContentMoldDataMigrationItem
    {
        /// <summary>
        /// 模型名字。
        /// </summary>
        public override string ModelName { get; set; } = "软件";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            soft.FeaturedImage = MigrateHelper.AddUploadPathFlag(MigrateHelper.GetColumnValueString(sourceDataRow, "DefaultPicUrl"), this.UploadPath);

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

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

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

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

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

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

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

            soft.DownloadUrl = MigrateHelper.GetColumnValueString(sourceDataRow, "DownloadUrl");

            soft.Content = MigrateHelper.GetColumnValueString(sourceDataRow, "SoftIntro");

            soft.SoftSize = MigrateHelper.GetColumnValueInt(sourceDataRow, "SoftSize");

            soft.SoftVersion = MigrateHelper.GetColumnValueString(sourceDataRow, "SoftVersion");

            soft.SoftType = MigrateHelper.GetColumnValueInt(sourceDataRow, "SoftType");

            soft.DecompressPassword = MigrateHelper.GetColumnValueString(sourceDataRow, "DecompressPassword");

            soft.DemoUrl = MigrateHelper.GetColumnValueString(sourceDataRow, "DemoUrl");

            soft.Intro = MigrateHelper.GetColumnValueString(sourceDataRow, "SoftIntro");

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