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

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

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

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

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

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

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

            // MoldId具体值请去PE_ContentManage_ContentMold表查看具体MoldId值。
            content.MoldId = 4;
            content.BelongSiteId = this.SiteId;
            content.Status = Status.PendingAudit;

            var extentcontent = string.Empty;
            var friendSiteLinkType = string.Empty;
            var logoUrl = MigrateHelper.GetColumnValueString(sourceDataRow, "DefaultPicUrl");
            if (!string.IsNullOrEmpty(logoUrl))
            {
                logoUrl = MigrateHelper.AddUploadPathFlag(logoUrl, this.UploadPath);
                logoUrl = "<LogoUrl>" + logoUrl + "</LogoUrl>";
                friendSiteLinkType = "<FriendSiteLinkType>图片类型</FriendSiteLinkType>";
            }
            else
            {
                logoUrl = "<LogoUrl />";
                friendSiteLinkType = "<FriendSiteLinkType>文字类型</FriendSiteLinkType>";
            }

            extentcontent = "<ExtendContent>" + logoUrl + friendSiteLinkType + "</ExtendContent>";
            content.ExtendContent = extentcontent;

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

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

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

            content.Intro = MigrateHelper.GetColumnValueString(sourceDataRow, "Synopsis");

            content.LinkUrl = MigrateHelper.GetColumnValueString(sourceDataRow, "SiteUrl");

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

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

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

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

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

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

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

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

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

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

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