using System.Data;
using Hotline.FlowEngine.Definitions;
using Hotline.FlowEngine.Workflows;
using Hotline.Orders;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.DataSharing.PusherHotlineDto;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Order.Publish;
using Hotline.Share.Enums.Settings;
using Hotline.Share.Requests;
using Hotline.Users;
using SqlSugar;
using XF.Domain.Exceptions;

namespace Hotline.Application.OrderApp
{
    public interface IOrderApplication
    {
        /// <summary>
        /// 派单量统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<List<SendOrderReportOutDto>> SendOrderReportAsync(QuerySendOrderRequest dto);

        ISugarQueryable<Order> QuerySendOrderDetail(QuerySendOrderDetailRequest dto);

        /// <summary>
        /// 更新工单办理期满时间
        /// 1.更新工单 2.更新流程
        /// </summary>
        /// <returns></returns>
        Task DelayOrderExpiredTimeAsync(string orderId, int timeCount, ETimeType timeType, bool IsProDelay, CancellationToken cancellationToken);

        // /// <summary>
        // /// 新增工单办理流程记录
        // /// </summary>
        // Task AddOrderTracesAsync(string orderId, ICollection<WorkflowTraceDto> traces, CancellationToken cancellationToken);

        /// <summary>
        /// 撤销工单
        /// </summary>
        Task CancelOrderAsync(string orderId, string opinion, CancellationToken cancellationToken);

        ISugarQueryable<Order> GetToExpireAsync(AboutToExpireListDto dto);
        //Task<PagedDto<WorkflowOrderDto>> GetToExpireNodeAsync(AboutToExpireListDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 即将超期列表优化V1版本
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> GetAboutToExpireAsync(AboutToExpireListDto dto);

        //Task<PagedDto<WorkflowOrderDto>> GetAboutToExpireNodeAsync(AboutToExpireListDto dto, CancellationToken cancellationToken);
        Task OrderParticiple(string inputStr, string orderId, string no, string title, DateTime time, CancellationToken cancellationToken);
        Task OrderSensitiveParticiple(string inputStr, string orderId, CancellationToken cancellationToken);

        /// <summary>
        /// 接收外部平台工单
        /// </summary>
        Task<AddOrderResponse> ReceiveOrderFromExternalAsync(AddOrderDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 接收外部平台修改工单附件
        /// </summary>
        Task UpdateOrderFilesAnonymousAsync(UpdateOrderFilesDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 工单回访
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task OrderVisitWeb(OrderVisitWebDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 回访保存
        /// 调用本方法前, 先调用 _callApplication.GetOrSetCallIdAsync 转换 CallId
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task SaveOrderVisit(VisitDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 发送回访短信
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task VisitPushSMSAsync(VisitSmsInDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 回访来源统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<IList<OrderVisitSourceChannelDto>> QueryOrderVisitSourceChannelAsync(QueryOrderVisitSourceChannelDto dto);

        /// <summary>
        /// 发布量统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<(int, IList<PublishedOrderStatisticsDto>)> QueryPublishedOrderAsync(QueryOrderPublishStatisticsDto dto, bool isFull);

        /// <summary>
        /// 部门发布量统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        Task<(int, IList<PublishedOrderStatisticsDto>)> QueryPublishedOrderDepartmentAsync(QueryOrderPublishStatisticsAllDto dto, bool isFull);

        #region 工单办理

        ISugarQueryable<Order> QueryOrders(QueryOrderDto dto);
        ISugarQueryable<Order> QueryTravel(QueryOrderDto dto);
        ISugarQueryable<Order> QueryEnterprise(QueryOrderDto dto);

        /// <summary>
        /// 保存工单办理时页面填写的数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<Order> SaveOrderWorkflowInfo(NextWorkflowDto<OrderHandleFlowDto> dto, CancellationToken cancellationToken);

        #endregion

        /// <summary>
        /// 未签收统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order, WorkflowStep> QueryUnsignedOrders(QueryUnsignedOrdersRequest dto);

        /// <summary>
        /// 信件来源统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> QueryOrderSource(QueryOrderSourceRequest dto);

        /// <summary>
        /// 信件来源统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> QueryOrderSourceDetail(QueryOrderSourceDetailRequest dto);

        /// <summary>
        /// 部门超期统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderBiOrgDataListVo> QueryOrgDataList(ReportPagedRequest dto);

        /// <summary>
        /// 部门超期统计明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> QueryOrgDataListDetail(OrgDataListDetailRequest dto);

        /// <summary>
        /// 部门全量超期统计明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> QueryOrgDataListDetail(OrgDataListAllDetailRequest dto);

        /// <summary>
        /// 回退错件统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderReTransactVo> OrderReTransact(QueryOrderReTransactRequest dto);

        /// <summary>
        /// 回退错件明细统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderSpecialDetail> QueryOrderSourceDetail(QueryOrderReTransactDetailRequest dto);

        /// <summary>
        /// 部门满意度统计
        /// </summary>
        /// <returns></returns>
        Task<List<VisitAndOrgSatisfactionStatisticsDto>> VisitAndOrgSatisfactionStatistics(PagedKeywordSonRequest dto);

        /// <summary>
        /// 子部门满意度
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<List<VisitAndOrgSatisfactionStatisticsDto>> VisitAndOrgStatisfactionOrgDetail(PagedKeywordSonRequest dto);


        /// <summary>
        /// 热点-区域统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<(List<SystemArea> area, object items)> HotspotAndAreaStatistics(HotspotAndAreaStatisticsReq dto);


        /// <summary>
        /// 热点-区域统计导出
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<DataTable> HotspotAndAreaStatisticsExport(HotspotAndAreaStatisticsReq dto);

        /// <summary>
        /// 热点-区域统计
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="AddColumnName"></param>
        /// <param name="HotspotLevel"></param>
        /// <returns></returns>
        DataTable InitHotspotTable(DataTable dt, List<string> AddColumnName, int HotspotLevel);


        /// <summary>
        /// 热点类型满意度统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<List<VisitAndHotspotSatisfactionStatisticsDto>> VisitAndHotspotSatisfactionStatistics(VisitAndHotspotPagedKeywordRequest dto);

        /// <summary>
        /// 热点满意度明细统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisitDetail> VisitAndHotspotSatisfactionDetail(VisitAndHotspotPagedKeywordRequest dto);

        /// <summary>
        /// 话务员办件统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderBiCentreDataListVo> CentreDataList(ReportPagedRequest dto);

        /// <summary>
        /// 话务员办件统计--自贡专用
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderBiCentreDataListVo> CentreDataListZG(ReportPagedRequest dto);

        /// <summary>
        /// 话务员办件统计明细--自贡专用
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> CentreDataListDetailZG(CentreDataListDetailRequest dto);

        /// <summary>
        /// 工单业务量统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVolumeDataListVo> OrderVolumeStatistics(ReportPagedRequest dto);

        /// <summary>
        /// 工单业务量列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> OrderVolumeList(QueryOrderVolumeDto dto);

        /// <summary>
        /// 热点受理类型统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<(List<SystemDicData> acceptTypes, object items)> HotspotAndAcceptTypeStatistics(HotspotAndAcceptTypeStatisticsReq dto);


        /// <summary>
        /// 热点受理类型统计--导出
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<DataTable> HotspotAndAcceptTypeStatisticsExport(HotspotAndAcceptTypeStatisticsReq dto);

        /// <summary>
        /// 热点受理类型明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> HotspotAndAcceptTypeStatisticsDetail(HotspotAndAcceptTypeStatisticsDetailReq dto);

        /// <summary>
        /// 甄别申请统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderScreenApplyVo> OrderScreenApply(OrderScreenApplyPagedRequest dto);

        /// <summary>
        /// 甄别审批统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderScreenAuditVo> OrderScreenAudit(OrderScreenAuditPagedRequest dto);


        /// <summary>
        /// 中心受理统计（日期）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderCenterAcceptVo> OrderCenterAccept(OrderCenterAcceptPagedRequest dto);

        /// <summary>
        /// 中心受理统计（值班坐席）
        /// </summary>
        /// <returns></returns>
        ISugarQueryable<OrderCenterAcceptUserVo> OrderCenterAcceptUser(OrderCenterAcceptPagedRequest dto);

        /// <summary>
        /// 扭转信件统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisitJudeStatisticsRep> OrderVisitJudeStatistics(OrderVisitJudeStatisticsReq dto);

        /// <summary>
        /// 观察关注列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderObserve> OrderObserveList(OrderObserveListDto dto);

        /// <summary>
        /// 终止列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderTerminate> OrderTerminateList(OrderTerminateListDto dto);

        /// <summary>
        /// 甄别列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderScreen> OrderScreenList(ScreenListDto dto);

        /// <summary>
        /// 待申请列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisitDetail> MayScreenList(MayScreenListDto dto);

        ISugarQueryable<Order> QueryWaitedForSeat(QueryOrderWaitedDto dto);

        ISugarQueryable<Order> QueryWaited(QueryOrderWaitedDto dto);

        /// <summary>
        /// 受理类型前10
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        Task<(List<SystemDicData> acceptTypes, object items, DataTable data)> AcceptTypeTop10List(ReportPagedRequest dto, bool isExport);

        Task SpecialVerify(OrderSpecialAddDto dto, Order order, CancellationToken cancellationToken);

        /// <summary>
        /// 省要求重办、省要求撤单 删除甄别等数据
        /// </summary>
        /// <param name="order"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task SpecialNewVerify(Order order, CancellationToken cancellationToken);

        Task EndCountersignAsync(EndCountersignDto dto, CancellationToken cancellationToken);
        ISugarQueryable<OrderPublish> GetPublishedOrder(PublishedPagedRequest dto);
        ISugarQueryable<Order> GetPublishOrderList(QueryOrderPublishDto dto);

        ISugarQueryable<Order> GetPublishTempOrderList(QueryOrderPublishDto dto);

        /// <summary>
        /// 回访列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisit> QueryOrderVisitList(QueryOrderVisitDto dto);

        /// <summary>
        /// 回访量明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisit> QueryOrderVisitQuantityListAsync(QueryOrderVisitListDto dto);

        /// <summary>
        /// 回访量明细自贡
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisitRecord> QueryOrderVisitQuantityListByZgAsync(QueryOrderVisitListDto dto);


        /// <summary>
        /// 热点类型小类统计明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<Order> HotspotStatisticsDetail(HotspotStatisticsRep dto);

        /// <summary>
        /// 坐席总体满意度分析
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<SeatSatisfactionStatisticsDto> SeatSatisfactionStatistics(PagedKeywordRequest dto);

        /// <summary>
        /// 坐席满意度明细表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<SeatSatisfactionStatisticsListDetailDto> SeatSatisfactionStatisticsListDetail(PagedKeywordRequest dto);

        /// <summary>
        /// 坐席满意度工单明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderVisitDetail> QuerySeatSatisfactionOrderVisitList(SeatSatisfactionOrderVisitRequest dto);

        /// <summary>
        /// 工单热词分析
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderTsDetailsDto> QueryOrderTsDetailsList(PagedKeywordRequest dto);

        /// <summary>
        /// 知识库引用
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="title"></param>
        /// <param name="no"></param>
        /// <param name="knowledgeQuote"></param>
        /// <returns></returns>
        Task AddKnowledgeQuote(string orderId, string title, string no, List<Kv> knowledgeQuote, CancellationToken cancellationToken);

        /// <summary>
        /// 知识库引用
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderTsDetailsDto> QueryKnowledgeQuoteList(PagedKeywordRequest dto);

        /// <summary>
        /// 将工单从网格员节点办理至工单标记节点
        /// </summary>
        /// <returns></returns>
        Task HandleFromWanggeyuanToMaskAsync(string orderId, string opinion, CancellationToken cancellation);

        /// <summary>
        /// 查询退回操作目标节点的指派对象
        /// </summary>
        Task<StepAssignInfo?> GetOrderPreviousAssignInfoAsync(Workflow workflow, StepDefine prevStepDefine, WorkflowStep prevStep,
            CancellationToken cancellationToken);

        /// <summary>
        /// 查询特提操作目标节点的指派对象
        /// </summary>
        Task<StepAssignInfo?> GetOrderRecallAssignInfoAsync(Workflow workflow, StepDefine targetStepDefine, WorkflowStep targetStep,
            BasicWorkflowDto dto, CancellationToken cancellationToken);

        /// <summary>
        /// 工单特提
        /// </summary>
        Task<(Workflow, StepDefine, WorkflowStep, WorkflowStep, WorkflowStep, bool)> RecallAsync
            (AuditOrderSpecialDto dto, OrderSpecial special, Order? order, CancellationToken cancellation);

        /// <summary>
        /// 超期退回统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<ExtendedSendBackVo> ExtendedSendBackStatistics(PagedKeywordRequest dto);

        /// <summary>
        /// 超期退回统计明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderSendBackAudit> ExtendedSendBackDetail(ExtendedSendBackRequest dto);

        /// <summary>
        /// 延期列表
        /// </summary>
        /// <returns></returns>
        ISugarQueryable<OrderDelay> DelayList(DelayListDto dto);

        /// <summary>
        /// 延期列表
        /// </summary>
        /// <returns></returns>
        ISugarQueryable<OrderDelay> WaitDelayList(DelayListDto dto);

        Task OrderPrevious(OrderSendBackAudit sendBack, Order order, CancellationToken cancellationToken);

        /// <summary>
        /// 依据当前待办节点随意获取一个合法办理人
        /// </summary>
        Task<User> GetHandlerRandomAsync(WorkflowStep step, CancellationToken cancellationToken);

        /// <summary>
        /// 部门退回中心统计
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrgSendBackAuditListVo> OrgSendBackAuditList(OrgSendBackAuditListDto dto);

        /// <summary>
        /// 部门退回中心统计明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderSendBackAudit> OrgSendBackAuditDetail(OrgSendBackAuditListDto dto);

        /// <summary>
        /// 坐席退回统计表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<SeatSendBackStatisticsVo> SeatSendBackStatistics(PagedKeywordRequest dto);

        /// <summary>
        /// 坐席退回统计表明细
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        ISugarQueryable<OrderDto> SeatSendBackStatisticsDetail(SeatSendBackStatisticsDetail dto);

        /// <summary>
        /// 自动延期记录写入
        /// </summary>
        /// <returns></returns>
        Task OrderDelayAutomatic();

        /// <summary>
        /// 自动延期处理
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        Task OrderDelayAutomaticHandle(EOrderDelayAutomaticType type);

    }
}