using Hotline.FlowEngine.Workflows;
using Hotline.Settings;
using Hotline.Settings.Hotspots;
using Hotline.Share.Enums.KnowledgeBase;
using Hotline.Users;
using SqlSugar;
using System.ComponentModel;
using Hotline.Share.Dtos.File;
using XF.Domain.Repository;
using Hotline.Share.Dtos;
using Hotline.Orders;
using Hotline.Identity.Accounts;

namespace Hotline.KnowledgeBase;

/// <summary>
/// 知识库文档内容
/// </summary>
[Description("知识库文档")]
[SugarIndex("index_knowledge_hotspotId", nameof(Knowledge.HotspotId), OrderByType.Desc)]
[SugarIndex("index_knowledge_creatorOrgId", nameof(Knowledge.CreatorOrgId), OrderByType.Desc)]
public class Knowledge : WorkflowEntity//   WorkflowEntity  FullStateEntity
{
    ///// <summary>
    ///// 知识分类
    ///// </summary>
    //[SugarColumn(ColumnDescription = "知识分类")]
    //public string KnowledgeTypeId { get; set; }



    /// <summary>
    /// 热点
    /// </summary>
    [SugarColumn(ColumnDescription = "热点")]
    public string? HotspotId { get; set; }

    /// <summary>
    /// 知识编号
    /// </summary>
    [SugarColumn(ColumnDescription = "知识编号")]
    public string? Code { get; set; }

    /// <summary>
    /// 标题
    /// </summary>
    [SugarColumn(ColumnDescription = "标题")]
    public string Title { get; set; }

    /// <summary>
    /// 摘要
    /// </summary>
    [SugarColumn(ColumnDataType = "varchar(1000)", IsNullable = true, ColumnDescription = "摘要")]
    public string? Summary { get; set; }

    /// <summary>
    /// 内容
    /// </summary>
    [SugarColumn(ColumnDataType = "text", ColumnDescription = "内容")]
    public string Content { get; set; }

    /// <summary>
    /// 浏览量
    /// </summary>
    [SugarColumn(ColumnDescription = "浏览量")]
    public int PageView { get; set; } = 0;

    /// <summary>
    /// 是否公开
    /// </summary>
    [SugarColumn(ColumnDescription = "是否公开")]
    public bool? IsPublic { get; set; }

    /// <summary>
    /// 文档状态
    /// </summary>
    [SugarColumn(ColumnDescription = "文档状态")]
    public EKnowledgeStatus Status { get; set; }

    /// <summary>
    /// 上架时间
    /// </summary>
    [SugarColumn(ColumnDescription = "上架时间")]
    public DateTime? OnShelfTime { get; set; }

    /// <summary>
    /// 下架时间
    /// </summary>
    [SugarColumn(ColumnDescription = "下架时间")]
    public DateTime? OffShelfTime { get; set; }

    /// <summary>
    /// 过期时间
    /// </summary>
    [SugarColumn(ColumnDescription = "过期时间")]
    public DateTime? ExpiredTime { get; set; }

    /// <summary>
    /// 关键词
    /// </summary>
    [SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true, ColumnDescription = "关键词")]
    public List<string>? Keywords { get; set; }

    /// <summary>
    /// 版本号
    /// </summary>
    [SugarColumn(ColumnDescription = "版本号")]
    public int? Version { get; set; } = 0;

    /// <summary>
    /// 外部数据（为前端提供级联功能）
    /// </summary>
    [SugarColumn(IsNullable = true, ColumnDescription = "外部数据（为前端提供级联功能）")]
    public string? HotspotExternal { get; set; }

    /// <summary>
    /// 附件
    /// </summary>
    [SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true)]
    public List<string>? Additions { get; set; }

    /// <summary>
    /// 关联知识
    /// </summary>
    [SugarColumn(ColumnDataType = "json", ColumnDescription = "关联知识", IsJson = true, IsNullable = true)]
    public List<string> Knowledges { get; set; }

    /// <summary>
    /// 知识库类型关联ID
    /// </summary>
    [Navigate(NavigateType.OneToMany, nameof(KnowledgeRelationType.KnowledgeId))]
    public List<KnowledgeRelationType> KnowledgeType { get; set; }

	/// <summary>
	/// 知识库分类
	/// </summary>
	[Navigate(typeof(KnowledgeRelationType), nameof(KnowledgeRelationType.KnowledgeId), nameof(KnowledgeRelationType.KnowledgeTypeId))]
    public List<KnowledgeType> KnowledgeTypes { get; set; }

    /// <summary>
    /// 知识库归属
    /// </summary>
    [Navigate(typeof(KnowledgeRelationOrg), nameof(KnowledgeRelationOrg.KnowledgeId), nameof(KnowledgeRelationOrg.OrgId))]
	public List<SystemOrganize> KnowledgeOrganizes { get; set; }

	/// <summary>
	/// 部门
	/// </summary>
	[Navigate(NavigateType.OneToOne, nameof(CreatorOrgId))]//一对一 
    public SystemOrganize SystemOrganize { get; set; }

    /// <summary>
    /// 用户
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(CreatorId))]//一对一 
    public User User { get; set; }

    /// <summary>
    /// 热点
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(HotspotId))]//一对一 
    public Hotspot HotspotType { get; set; }

    /// <summary>
    /// 知识归属
    /// </summary>
    [SugarColumn(ColumnDescription = "知识归属")]
    public string? Attribution { get; set; }

    /// <summary>
    /// 搜索量
    /// </summary>
    [SugarColumn(ColumnDescription = "搜索量")]
    public int? SearchNum { get; set; } = 0;

    /// <summary>
    /// 评分
    /// </summary>
    [SugarColumn(ColumnDescription = "评分")]
    public decimal? Score { get; set; } = decimal.Zero;

    /// <summary>
    /// 评论数
    /// </summary>
    [SugarColumn(ColumnDescription = "评论数")]
    public int? CommentNum { get; set; } = 0;

    /// <summary>
    /// 来源部门
    /// </summary>
    [SugarColumn(ColumnDescription = "来源部门")]
    public string? SourceOrganizeId { get; set; }

    /// <summary>
    /// 是否更新
    /// </summary>
    [SugarColumn(ColumnDescription = "是否更新")]
    public bool? Renewaln { get; set; } = false;

    /// <summary>
    /// 文档号
    /// </summary>
    public string? DocumentNo { get; set; }

    /// <summary>
    /// 版本说明
    /// </summary>
    public string? VersionDescription { get; set; }

    /// <summary>
    /// 索引号
    /// </summary>
    public string? IndexNo { get; set; }

    /// <summary>
    /// 文号
    /// </summary>
    public string? FileNo { get; set; }

    /// <summary>
    /// 预案分类id
    /// </summary>
    public string? PlanTypeId { get; set; }


    /// <summary>
    /// 来源部门
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(SourceOrganizeId))]//一对一 
    public SystemOrganize SourceOrganize { get; set; }

    [Navigate(NavigateType.OneToOne, nameof(WorkflowId))]
    public Workflow Workflow { get; set; }

    /// <summary>
    /// 是否可办理
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public bool? IsCanHandle { get; set; }

    [SugarColumn(ColumnDataType = "json", IsJson = true, IsNullable = true)]
    public List<FileJson>? FileJson { get; set; }

    /// <summary>
    /// 收藏数量
    /// </summary>
    [SugarColumn(ColumnDescription = "收藏数量")]
    public int? CollectCount { get; set; } = 0;

    /// <summary>
    /// 知识来源
    /// </summary>
    [SugarColumn(ColumnDescription = "知识来源")]
    public string? Source { get; set; }

    /// <summary>
    /// 知识库类型关联ID
    /// </summary>
    [Navigate(NavigateType.OneToMany, nameof(KnowledgeApprove.KnowledgeId))]
    public List<KnowledgeApprove> KnowledgeApproves { get; set; }

    #region method

    /// <summary>
    /// 上架
    /// </summary>
    public void OnShelf()
    {
        Status = EKnowledgeStatus.OnShelf;
        OnShelfTime = System.DateTime.Now;
    }

    /// <summary>
    /// 下架
    /// </summary>
    public void OffShelf()
    {
        Status = EKnowledgeStatus.OffShelf;
        OffShelfTime = System.DateTime.Now;
        OnShelfTime = null;
    }

    #endregion
}