using Hotline.Share.Dtos.ObservationPiece;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Application.ObservationPiece
{
    public class ObservationPieceApplication : IObservationPieceApplication, IScopeDependency
    {
        private readonly ISessionContext _sessionContext;
        private readonly IRepository<Hotline.Orders.ObservationPiece> _observationPieceRepository;

        public ObservationPieceApplication(
            ISessionContext sessionContext,
            IRepository<Hotline.Orders.ObservationPiece> observationPieceRepository
            )
        {
            _sessionContext = sessionContext;
            _observationPieceRepository = observationPieceRepository;
        }

        /// <summary>
        /// 观察件回复
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ISugarQueryable<ObservationPieceListDto> GetObservationPieceReplyList(ObservationPieceRequestDto dto)
        {
            var query = _observationPieceRepository.Queryable()
                .Includes(p => p.Order)
                .WhereIF(!string.IsNullOrEmpty(dto.No), p => p.No.Contains(dto.No))
                .WhereIF(!string.IsNullOrEmpty(dto.Title), p => p.Order.Title.Contains(dto.Title))
                .WhereIF(!string.IsNullOrEmpty(dto.AcceptType), p => p.Order.AcceptTypeCode == dto.AcceptType) //受理类型
                .WhereIF(dto.AcceptanceStartTime.HasValue, p => p.Order.CreationTime >= dto.AcceptanceStartTime)
                .WhereIF(dto.AcceptanceEndTime.HasValue, p => p.Order.CreationTime <= dto.AcceptanceEndTime)
                .WhereIF(!string.IsNullOrEmpty(dto.Hotspot), p => p.Order.HotspotSpliceName != null && p.Order.HotspotSpliceName.Contains(dto.Hotspot))
                .WhereIF(dto.ExpiredStartTime.HasValue, p => p.Order.ExpiredTime >= dto.ExpiredStartTime) //期满时间开始
                .WhereIF(dto.ExpiredEndTime.HasValue, p => p.Order.ExpiredTime <= dto.ExpiredEndTime) //期满时间结束
                .WhereIF(!string.IsNullOrEmpty(dto.ActualHandleOrgName), p => p.Order.ActualHandleOrgName.Contains(dto.ActualHandleOrgName)) //接办部门（综合查询模糊）

                .WhereIF(dto.FiledStartTime.HasValue, p => p.Order.FiledTime >= dto.FiledStartTime) //办结时间开始
                .WhereIF(dto.FiledEndTime.HasValue, p => p.Order.FiledTime <= dto.FiledEndTime) //办结时间结束

                 .WhereIF(dto.ReplyStartTime.HasValue, p => p.ReplyTime >= dto.ReplyStartTime) //回复时间开始
                .WhereIF(dto.ReplyEndTime.HasValue, p => p.ReplyTime <= dto.ReplyEndTime) //回复时间结束
                .WhereIF(!string.IsNullOrEmpty(dto.ReplyUserName), p => p.ReplyUserName.Contains(dto.ReplyUserName))
                 .WhereIF(dto.CreationStartTime.HasValue, p => p.CreationTime >= dto.CreationStartTime) //设置时间开始
                .WhereIF(dto.CreationEndTime.HasValue, p => p.CreationTime <= dto.CreationEndTime) //设置时间结束
                .WhereIF(!string.IsNullOrEmpty(dto.CreatorName), p => p.CreatorName.Contains(dto.CreatorName))
                .WhereIF(dto.IsReply.HasValue && dto.IsReply == true, p => p.IsReply == true) //是否设置
                 .WhereIF(dto.IsReply.HasValue && dto.IsReply == false, p => p.IsReply == false)
                 .WhereIF(!string.IsNullOrEmpty(dto.Type) && dto.Type == "0", p => p.IsReply == false)//查询类型  待回复或者已回复
                 .WhereIF(!string.IsNullOrEmpty(dto.Type) && dto.Type == "1", p => p.IsReply == true)

                .WhereIF(_sessionContext.OrgIsCenter == false && dto.Type == "0" && dto.DataSoure == "0", p => p.ObserveOrgId.StartsWith(_sessionContext.RequiredOrgId))
                .WhereIF(dto.Type == "0" && dto.DataSoure == "1", p => p.ObserveOrgId == _sessionContext.RequiredOrgId)

                .WhereIF(_sessionContext.OrgIsCenter == false && dto.Type == "1" && dto.DataSoure == "0", p => p.ReplyOrgId.StartsWith(_sessionContext.RequiredOrgId))
                .WhereIF(dto.Type == "1" && dto.DataSoure == "1", p => p.ObserveOrgId == _sessionContext.RequiredOrgId)
                .Select(p => new ObservationPieceListDto
                {
                    Id = p.Id,
                    OrderId = p.OrderId,
                    No = p.No,
                    Title = p.Order.Title,
                    AcceptType = p.Order.AcceptType,
                    HotspotName = p.Order.HotspotName,
                    HotspotSpliceName = p.Order.HotspotSpliceName,
                    AcceptanceTime = p.Order.CreationTime,
                    ExpiredTime = p.Order.ExpiredTime,
                    ActualHandleOrgName = p.Order.ActualHandleOrgName,
                    CurrentStepAcceptTime = p.Order.CurrentStepAcceptTime,
                    FiledTime = p.Order.FiledTime,
                    CreationTime = p.CreationTime,
                    CreatorName = p.CreatorName,
                    ReplyTime = p.ReplyTime,
                    ReplyUserId = p.ReplyUserId,
                    ReplyUserName = p.ReplyUserName,
                    ReplyOrgId = p.ReplyOrgId,
                    ReplyOrgName = p.ReplyOrgName,
                    IsReply = p.IsReply
                })
                .OrderByIF(dto.Type == "1", p => p.ReplyTime, OrderByType.Desc)
                .OrderByIF(dto.Type == "0", p => p.CreationTime, OrderByType.Desc);
            return query;
        }

        /// <summary>
        /// 观察件列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ISugarQueryable<ObservationPieceListDto> GetObservationPieceList(ObservationPieceRequestDto dto)
        {
            var query = _observationPieceRepository.Queryable()
                .Includes(p => p.Order)
                .WhereIF(!string.IsNullOrEmpty(dto.No), p => p.No.Contains(dto.No))
                .WhereIF(!string.IsNullOrEmpty(dto.Title), p => p.Order.Title.Contains(dto.Title))
                .WhereIF(!string.IsNullOrEmpty(dto.AcceptType), p => p.Order.AcceptTypeCode == dto.AcceptType) //受理类型
                .WhereIF(dto.AcceptanceStartTime.HasValue, p => p.Order.CreationTime >= dto.AcceptanceStartTime)
                .WhereIF(dto.AcceptanceEndTime.HasValue, p => p.Order.CreationTime <= dto.AcceptanceEndTime)
                .WhereIF(!string.IsNullOrEmpty(dto.Hotspot), p => p.Order.HotspotSpliceName != null && p.Order.HotspotSpliceName.Contains(dto.Hotspot))
                .WhereIF(dto.ExpiredStartTime.HasValue, p => p.Order.ExpiredTime >= dto.ExpiredStartTime) //期满时间开始
                .WhereIF(dto.ExpiredEndTime.HasValue, p => p.Order.ExpiredTime <= dto.ExpiredEndTime) //期满时间结束
                .WhereIF(!string.IsNullOrEmpty(dto.ActualHandleOrgName), p => p.Order.ActualHandleOrgName.Contains(dto.ActualHandleOrgName)) //接办部门（综合查询模糊）

                .WhereIF(dto.FiledStartTime.HasValue, p => p.Order.FiledTime >= dto.FiledStartTime) //办结时间开始
                .WhereIF(dto.FiledEndTime.HasValue, p => p.Order.FiledTime <= dto.FiledEndTime) //办结时间结束

                 .WhereIF(dto.ReplyStartTime.HasValue, p => p.ReplyTime >= dto.ReplyStartTime) //回复时间开始
                .WhereIF(dto.ReplyEndTime.HasValue, p => p.ReplyTime <= dto.ReplyEndTime) //回复时间结束
                .WhereIF(!string.IsNullOrEmpty(dto.ReplyUserName), p => p.ReplyUserName.Contains(dto.ReplyUserName))
                 .WhereIF(dto.CreationStartTime.HasValue, p => p.CreationTime >= dto.CreationStartTime) //设置时间开始
                .WhereIF(dto.CreationEndTime.HasValue, p => p.CreationTime <= dto.CreationEndTime) //设置时间结束
                .WhereIF(!string.IsNullOrEmpty(dto.CreatorName), p => p.CreatorName.Contains(dto.CreatorName))
                .WhereIF(dto.IsReply.HasValue && dto.IsReply == true, p => p.IsReply == true) //是否设置
                 .WhereIF(dto.IsReply.HasValue && dto.IsReply == false, p => p.IsReply == false)
                 .WhereIF(!string.IsNullOrEmpty(dto.Type) && dto.Type == "0", p => p.IsReply == false)//查询类型  待回复或者已回复
                 .WhereIF(!string.IsNullOrEmpty(dto.Type) && dto.Type == "1", p => p.IsReply == true)

                .WhereIF(_sessionContext.OrgIsCenter == false, p => p.ObserveOrgId.StartsWith(_sessionContext.RequiredOrgId))

                .Select(p => new ObservationPieceListDto
                {
                    Id = p.Id,
                    OrderId = p.OrderId,
                    No = p.No,
                    Title = p.Order.Title,
                    AcceptType = p.Order.AcceptType,
                    HotspotName = p.Order.HotspotName,
                    HotspotSpliceName = p.Order.HotspotSpliceName,
                    AcceptanceTime = p.Order.CreationTime,
                    ExpiredTime = p.Order.ExpiredTime,
                    ActualHandleOrgName = p.Order.ActualHandleOrgName,
                    CurrentStepAcceptTime = p.Order.CurrentStepAcceptTime,
                    FiledTime = p.Order.FiledTime,
                    CreationTime = p.CreationTime,
                    CreatorName = p.CreatorName,
                    ReplyTime = p.ReplyTime,
                    ReplyUserId = p.ReplyUserId,
                    ReplyUserName = p.ReplyUserName,
                    ReplyOrgId = p.ReplyOrgId,
                    ReplyOrgName = p.ReplyOrgName,
                    IsReply = p.IsReply
                })
              .OrderByDescending(p => p.CreationTime);

            return query;
        }

    }
}
