using Hotline.Settings;
using Hotline.Share.Enums.Order;
using Hotline.Share.Tools;
using XF.Domain.Repository;
using XF.Domain.Repository.Events;

namespace Hotline.Orders.DatabaseEventHandler;

/// <summary>
/// OrderVisit 更新数据后执行事务处理
/// 更新 order 表中的 OrgProcessingResults 和 SeatEvaluate 字段
/// </summary>
public class OrderVisitDetailEventHandler : IUpdateDatabaseEvent<OrderVisitDetail>
{
    private readonly IRepository<OrderVisitDetail> _orderVisitDetailRepository;
    private readonly IOrderRepository _orderRepository;
    private readonly IRepository<OrderVisit> _orderVisitRepository;
    private readonly ISystemLogRepository _systemLogRepository;

    public OrderVisitDetailEventHandler(IOrderRepository orderRepository, IRepository<OrderVisitDetail> orderVisitDetailRepository, IRepository<OrderVisit> orderVisitRepository, ISystemLogRepository systemLogRepository)
    {
        _orderRepository = orderRepository;
        _orderVisitDetailRepository = orderVisitDetailRepository;
        _orderVisitRepository = orderVisitRepository;
        _systemLogRepository = systemLogRepository;
    }

    public void OnInserted(OrderVisitDetail entity)
    {
        OrderUpdate(entity);
    }

    public void OnUpdated(OrderVisitDetail entity)
    {
        OrderUpdate(entity);
    }

    public void OnUpdating(OrderVisitDetail entity)
    {

    }

    private void OrderUpdate(OrderVisitDetail visit)
    {
        var name = "回填Order回访字段";
        if (visit.VisitTarget == EVisitTarget.Org && visit.OrgProcessingResults != null)
        {
            var orderId = _orderVisitRepository.Queryable()
               .Where(m => m.Id == visit.VisitId)
               .Select(m => new { m.OrderId, m.No, m.VisitState })
               .First();

            if (visit.OrgProcessingResults.Value == "不满意" && orderId.VisitState!= EVisitState.Visited) return;
           
            _orderRepository.Updateable()
                .SetColumns(m => m.OrgProcessingResults == visit.OrgProcessingResults)
                .Where(m => m.Id == orderId.OrderId && m.Status >= EOrderStatus.Published)
                .ExecuteCommand();
            _systemLogRepository.Add(name, $"OrgProcessingResults: {visit.OrgProcessingResults.ToJson()}", orderId.No, orderId.No, 1);
        }

        if (visit.VisitTarget == EVisitTarget.Seat && visit.SeatEvaluate != null)
        {
            var orderId = _orderVisitRepository.Queryable()
               .Where(m => m.Id == visit.VisitId)
               .Select(m => new { m.OrderId, m.No,m.VisitState })
               .First();

            if ((visit.SeatEvaluate == ESeatEvaluate.NoSatisfied || visit.SeatEvaluate == ESeatEvaluate.VeryNoSatisfied) && orderId.VisitState!= EVisitState.Visited) return;
           
            _orderRepository.Updateable()
                .SetColumns(m => m.SeatEvaluate == visit.SeatEvaluate)
                .Where(m => m.Id == orderId.OrderId && m.Status >= EOrderStatus.Published)
                .ExecuteCommand();
            _systemLogRepository.Add(name, $"SeatEvaluate: {visit.SeatEvaluate}",orderId.No, orderId.No, 1);
        }

    }
}
