using Hotline.Orders;
using Hotline.SeedData;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.Order;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Application.OrderApp
{
    public class OrderSendBackAuditApplication : IOrderSendBackAuditApplication, IScopeDependency
    {
        private readonly IRepository<OrderSendBackAudit> _orderSendBackAuditRepository;
        private readonly ISessionContext _sessionContext;
        public OrderSendBackAuditApplication(IRepository<OrderSendBackAudit> orderSendBackAuditRepository, ISessionContext sessionContext)
        {
            _orderSendBackAuditRepository = orderSendBackAuditRepository;
            _sessionContext = sessionContext;
        }

        public ISugarQueryable<OrderSendBackAudit> AuditList(SendBackListDto dto)
        {
            return _orderSendBackAuditRepository.Queryable()
                .Includes(x => x.Order)
                .WhereIF(!string.IsNullOrEmpty(dto.Keyword),
                    x => x.Order.Title.Contains(dto.Keyword!) || x.Order.No.Contains(dto.Keyword!))
                .WhereIF(!string.IsNullOrEmpty(dto.Title), x => x.Order.Title.Contains(dto.Title!))
                .WhereIF(!string.IsNullOrEmpty(dto.No), x => x.Order.No.Contains(dto.No!))
                .WhereIF(dto.IsProvince.HasValue && dto.IsProvince == true, x => x.Order.IsProvince == true)
                .WhereIF(dto.IsProvince.HasValue && dto.IsProvince == false, x => x.Order.IsProvince == false)
                .WhereIF(!string.IsNullOrEmpty(dto.Channel), x => x.Order.SourceChannelCode == dto.Channel)    //来源渠道
                .WhereIF(!string.IsNullOrEmpty(dto.AcceptTypeCode), x => x.Order.AcceptTypeCode == dto.AcceptTypeCode)
                .WhereIF(!string.IsNullOrEmpty(dto.Hotspot), x => x.Order.HotspotSpliceName != null && x.Order.HotspotSpliceName.Contains(dto.Hotspot)) //热点
                .WhereIF(!string.IsNullOrEmpty(dto.CenterToOrgHandlerName), x => x.Order.CenterToOrgHandlerName.Contains(dto.CenterToOrgHandlerName))   //最近派单员              
                .WhereIF(!string.IsNullOrEmpty(dto.SendBackStepName), x => x.SendBackStepName.Contains(dto.SendBackStepName))                           //退回节点                   
                .WhereIF(!string.IsNullOrEmpty(dto.NameOrNo), x => x.Order.AcceptorName == dto.NameOrNo! || x.Order.AcceptorStaffNo == dto.NameOrNo!)   //受理人/坐席
                .WhereIF(!string.IsNullOrEmpty(dto.OrgLevelOneName), x => x.Order.OrgLevelOneName.Contains(dto.OrgLevelOneName))                        //一级部门 
                .WhereIF(!string.IsNullOrEmpty(dto.Content), x => x.Order.Content.Contains(dto.Content))                                                //受理内容 
                .WhereIF(!string.IsNullOrEmpty(dto.ApplyName), x => x.CreatorName.Contains(dto.ApplyName))                                              //申请人 
                .WhereIF(!string.IsNullOrEmpty(dto.ApplyOrgName), x => x.ApplyOrgName.Contains(dto.ApplyOrgName))                                       //申请部门 
                .WhereIF(!string.IsNullOrEmpty(dto.Opinion), x => SqlFunc.JsonField(x.SendBackData, "Opinion").Contains(dto.Opinion))          //申请理由

                .WhereIF(dto.DataScope is 1, x => x.CreatorId == _sessionContext.RequiredUserId)
                .WhereIF(dto.StartTime.HasValue, x => x.CreationTime >= dto.StartTime)
                .WhereIF(dto.EndTime.HasValue, x => x.CreationTime <= dto.EndTime)
                .WhereIF(dto.AuditState == 1, x => x.State == ESendBackAuditState.Apply)
                .WhereIF(dto is { AuditState: 2, State: null }, x => x.State > ESendBackAuditState.Apply)
                .WhereIF(dto.AuditState is 2 or 3 && dto.State.HasValue && dto.State != ESendBackAuditState.All, x => x.State == dto.State)
                .WhereIF(dto.AuditState == 3 && _sessionContext.RequiredOrgId != OrgSeedData.CenterId, x => x.ApplyOrgId.StartsWith(_sessionContext.OrgId))
                .WhereIF(_sessionContext.Roles.Contains("role_sysadmin") == false && dto.AuditState != 3, x => x.SendBackOrgId == _sessionContext.OrgId) // 123 系统管理员;
                .WhereIF(dto.ExpiredTimeStart.HasValue, x => x.Order.ExpiredTime >= dto.ExpiredTimeStart)
                .WhereIF(dto.ExpiredTimeEnd.HasValue, x => x.Order.ExpiredTime <= dto.ExpiredTimeEnd)
            .OrderByDescending(x => x.CreationTime);
        }
    }
}
