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

namespace Hotline.Share.Dtos.JudicialManagement
{
    /// <summary>
    /// 左上工单数据模块
    /// </summary>
    public class OrderModuleDto
    {
        /// <summary>
        /// 行政执法工单
        /// </summary>
        public int EnforcementOrderNum { get; set; }

        /// <summary>
        /// 线索属实
        /// </summary>
        public int TheClueIsTrueNum { get; set; }

        /// <summary>
        /// 非执法工单
        /// </summary>
        public int TheClueIsNotTrueNum { get; set; }

        /// <summary>
        /// 推诿工单
        /// </summary>
        public int PassTheBuckOrderNum { get; set; }

        /// <summary>
        /// 满意工单
        /// </summary>
        public int SatisfiedCount { get; set; }

        /// <summary>
        /// 工单总量
        /// </summary>
        public int OrderCountNum { get; set; }


        /// <summary>
        /// 待核实订单
        /// </summary>
        public int ToBeVerifiedOrderCount { get; set; }


    }

    /// <summary>
    /// 左中受理类型
    /// </summary>
    public class OrderAcceptTypeStatisticsDto
    {
        /// <summary>
        /// 类型名称
        /// </summary>
        public string AcceptType { get; set; }

        /// <summary>
        /// 总量
        /// </summary>
        public int SumCount { get; set; }

        /// <summary>
        /// 在办
        /// </summary>
        public int HandlingCount { get; set; }

        /// <summary>
        /// 已办
        /// </summary>
        public int FiledCount { get; set; }

        /// <summary>
        /// 超期
        /// </summary>
        public int OverTimeCount { get; set; }
    }

    /// <summary>
    /// 左下高频事项统计
    /// </summary>
    public class OrderEventTypeStatisticsDto
    {
        /// <summary>
        /// id
        /// </summary>
        public string EventTypeId { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string EventTypeName { get; set; }

        /// <summary>
        /// 总量
        /// </summary>
        public int SumCount { get; set; }
    }

    /// <summary>
    /// 中上区域数据统计
    /// </summary>
    public class OrderAreaQueryDto
    {
        /// <summary>
        /// 区域Id
        /// </summary>
        public string AreaCode { get; set; }

        /// <summary>
        /// 区域名称
        /// </summary>
        public string AreaName { get; set; }

        /// <summary>
        /// 行政执法工单
        /// </summary>
        public int EnforcementOrderNum { get; set; }

        /// <summary>
        /// 线索属实
        /// </summary>
        public int TheClueIsTrueNum { get; set; }

        /// <summary>
        /// 线索不属实
        /// </summary>
        public int TheClueIsNotTrueNum { get; set; }

        /// <summary>
        /// 推诿工单
        /// </summary>
        public int PassTheBuckOrderNum { get; set; }

        /// <summary>
        /// 工单总量
        /// </summary>
        public int OrderCountNum { get; set; }
    }

    /// <summary>
    /// 中下行政执法工单概览
    /// </summary>
    public class OrderOverviewDto
    {
        public DateTime CreationTime { get; set; }
        /// <summary>
        /// 执法部门
        /// </summary>
        public List<Kv> EnforcementOrdersHandler { get; set; }
        public string Id { get; set; }
        public string WorkflowId { get; set; }

        /// <summary>
        /// 来源渠道
        /// </summary>
        public string? SourceChannel { get; set; }
        public string? SourceChannelCode { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 受理类型
        /// </summary>
        public string? AcceptType { get; set; }
        public string? AcceptTypeCode { get; set; }

        /// <summary>
        /// 热点
        /// </summary>
        public string? HotspotId { get; set; }
        public string? HotspotName { get; set; }

        /// <summary>
        /// 实际办理部门名称
        /// </summary>
        public string? ActualHandleOrgName { get; set; }

        /// <summary>
        /// 实际办理部门编码
        /// </summary>
        public string? ActualHandleOrgCode { get; set; }

        /// <summary>
        /// 过期状态
        /// </summary>
        public EExpiredStatus? ExpiredStatus => CalculateExpiredState();

        /// <summary>
        /// 过期状态
        /// </summary>
        public string ExpiredStatusText => ExpiredStatus?.GetDescription() ?? string.Empty;

        /// <summary>
        /// 工单状态
        /// </summary>
        public EOrderStatus Status { get; set; }
        public string StatusText => Status.GetDescription();

        /// <summary>
        /// 实际办理时间
        /// </summary>
        public DateTime? ActualHandleTime { get; set; }

        /// <summary>
        /// 超期时间（期满时间）
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 即将超期时间
        /// </summary>
        public DateTime? NearlyExpiredTime { get; set; }

        /// <summary>
        /// 工单开始时间(受理/接办时间=流程开启时间)
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 区域
        /// </summary>
        public string AreaName { get; set; }

        public EExpiredStatus? CalculateExpiredState()
        {
            DateTime? dateTime = DateTime.Now;
            if (Status >= EOrderStatus.Filed)
            {
                dateTime = ActualHandleTime;
            }
            //ExpiredStatus
            if (ExpiredTime.HasValue)
            {
                if (dateTime < NearlyExpiredTime)
                {
                    return EExpiredStatus.Normal;
                }
                else if (dateTime > NearlyExpiredTime && dateTime < ExpiredTime)
                {
                    return EExpiredStatus.GoingToExpired;
                }
                else
                {
                    return EExpiredStatus.Expired;
                }
            }
            return null;
        }
    }

    /// <summary>
    /// 右上满意度排行榜
    /// </summary>
    public class OrderVisitOrgStatisticsDto
    {
        /// <summary>
        /// 部门Code
        /// </summary>
        public string VisitOrgCode { get; set; }

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

        /// <summary>
        /// 满意量
        /// </summary>
        public int SatisfiedCount { get; set; }

        /// <summary>
        /// 总量
        /// </summary>
        public int VisitCount { get; set; }

        /// <summary>
        /// 满意率
        /// </summary>
        public double SatisfiedRate => CalcSatisfiedRate();

        public double CalcSatisfiedRate()
        {
            if (SatisfiedCount == 0 || VisitCount == 0)
            {
                return 0;
            }
            return Math.Round((SatisfiedCount / (double)VisitCount) * 100, 2);
        }
    }

    /// <summary>
    /// 工单月份趋势图
    /// </summary>
    public class OrderMonthlyTrendDto
    {
        public int Month { get; set; }

        /// <summary>
        /// 非执法工单数
        /// </summary>
        public int UnEnforcementOrderNum { get; set; }

        /// <summary>
        /// 执法工单数
        /// </summary>
        public int EnforcementOrderNum { get; set; }
    }

    /// <summary>
    /// 右下占比分析
    /// </summary>
    public class OrderProportionStatisticsDto
    {
        public string Name { get; set; }

        public int SumCount { get; set; }

        public int HasCount { get; set; }

        public double HasRate => CalcHasRate();

        public double CalcHasRate()
        {
            if (HasCount == 0 || SumCount == 0)
            {
                return 0;
            }
            return Math.Round((HasCount / (double)SumCount) * 100, 2);
        }
    }
}
