using Hotline.Settings;
using Hotline.Share.Enums.FlowEngine;
using Hotline.Share.Enums.Order;
using XF.Utility.EnumExtensions;

namespace Hotline.Share.Requests
{
    public class DepartmentalProcessingStatisticsDto
    {
        public string Id { get; set; }

        public string OrgCode { get; set; }

        public string ActualHandleOrgCode { get; set; }

        public EOrderStatus? Status { get; set; }

        public DateTime? ExpiredTime { get; set; }

        public DateTime? ActualHandleTime { get; set; }

        public ECounterSignType? CounterSignType { get; set; }

        public bool PublishState { get; set; }
    }

    public class DepartmentalProcessingStatisticsDataDto
    {
        #region 办件信息
        /// <summary>
        /// 部门Code
        /// </summary>
        public string OrgCode { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string OrgName { get; set; }

        /// <summary>
        /// 部门类型
        /// </summary>
        public EOrgType OrgType { get; set; }
        public string OrgTypeText => OrgType.GetDescription();

        /// <summary>
        /// 信件总量
        /// </summary>
        public int OrderCountNum => YbOrderCountNum + ZbOrderCountNum;

        /// <summary>
        /// 信件已办总量
        /// </summary>
        public int YbOrderCountNum { get; set; }

        /// <summary>
        /// 信件未办总量
        /// </summary>
        public int ZbOrderCountNum { get; set; }

        /// <summary>
        /// 按时办结
        /// </summary>
        public int CompleteOnTime { get; set; }

        /// <summary>
        /// 按时办结率
        /// </summary>
        public string CompleteOnTimeRate => CalcRate(YbOrderCountNum, CompleteOnTime);

        /// <summary>
        /// 办结率
        /// </summary>
        public string OrderCompletionRate => CalcRate(OrderCountNum, YbOrderCountNum);
        #endregion

        #region 归档
        /// <summary>
        /// 已归档
        /// </summary>
        public int Archived { get; set; }

        /// <summary>
        /// 待归档
        /// </summary>
        public int ToBeArchived { get; set; }
        #endregion

        #region 发布
        /// <summary>
        /// 待发布  --已归档的就是待发布
        /// </summary>
        public int WaitPublished { get; set; }

        /// <summary>
        /// 已发布公开
        /// </summary>
        public int PublishedOpen { get; set; }

        /// <summary>
        /// 已发布不公开
        /// </summary>
        public int PublishedNoOpen { get; set; }
        #endregion

        #region 超期信息
        /// <summary>
        /// 已办超期
        /// </summary>
        public int YbOverdue { get; set; }

        /// <summary>
        /// 待办超期
        /// </summary>
        public int ZbOverdue { get; set; }

        /// <summary>
        /// 会签已办超期
        /// </summary>
        public int HqybOverdue { get; set; }

        /// <summary>
        /// 会签待办超期
        /// </summary>
        public int HqzbOverdue { get; set; }

        /// <summary>
        /// 超期件数
        /// </summary>
        public int SubtotalOverdue => YbOverdue + ZbOverdue + HqybOverdue + HqzbOverdue;

        /// <summary>
        /// 会签超期率
        /// </summary>
        public string HqOverdueRate => CalcRate(OrderCountNum, SubtotalOverdue);
        #endregion

        #region 会签信息
        /// <summary>
        /// 会签已办
        /// </summary>
        public int DelayEnd { get; set; }

        /// <summary>
        /// 会签待办
        /// </summary>
        public int DelayWait { get; set; }
        #endregion

        #region 延期
        /// <summary>
        /// 工单延期次数
        /// </summary>
        public int OrderDelayCount { get; set; }

        /// <summary>
        /// 延期率
        /// </summary>
        public string OrderDelayRate => CalcRate(OrderCountNum, OrderDelayCount);
        #endregion

        #region 甄别
        /// <summary>
        /// 申请总量
        /// </summary>
        public int ScreenCount { get; set; }

        /// <summary>
        /// 审批中
        /// </summary>
        public int ScreenApproval { get; set; }

        /// <summary>
        /// 通过
        /// </summary>
        public int ScreenPass { get; set; }

        /// <summary>
        /// 不通过
        /// </summary>
        public int ScreenNotPass { get; set; }
        #endregion

        #region 满意度
        /// <summary>
        /// 满意
        /// </summary>
        public int SatisfactionCount { get; set; }

        /// <summary>
        /// 不满意
        /// </summary>
        public int NotSatisfactionCount { get; set; }

        /// <summary>
        /// 满意率
        /// </summary>
        public string SatisfactionRate => CalcRate(SatisfactionCount + NotSatisfactionCount, SatisfactionCount);
        #endregion


        /// <summary>
        /// 计算率
        /// </summary>
        /// <returns></returns>
        public string CalcRate(int Count, int Quantity)
        {
            if (Count != 0 && Quantity != 0)
            {
                return Math.Round((Quantity / (double)Count) * 100, 2) + "%";
            }
            return "0%";
        }

    }

    public record DepartmentalProcessingStatisticsRequest : PagedRequest
    {
        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 部门Code
        /// </summary>
        public string OrgCode { get; set; }

        /// <summary>
        /// 父级ID
        /// </summary>
        public string ParentOrgCode { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string? OrgName { get; set; }

        /// <summary>
        /// 数据查询类别
        /// </summary>
        public EStatisticsType StatisticsType { get; set; }

        /// <summary>
        /// 是否省来源
        /// </summary>
        public bool? IsProvince { get; set; }

        /// <summary>
        /// 来电/信人身份0：全部 ，1：市民，2：企业
        /// </summary>
        public int? TypeId { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string? SortField { get; set; }

        /// <summary>
        /// 排序方式 0 升序 1 降序
        /// </summary>
        public int? SortRule { get; set; }
    }

    public class SelectOrderId
    {
        public string Id { get; set; }
    }
}
