using Edge.Core.Core.database;
using Edge.Core.Domain.FccOrderInfo.Input;
using Edge.Core.Domain.FccOrderInfo.Output;
using Microsoft.EntityFrameworkCore;

namespace FccLite.Web.Repositories.FccOrderInfo
{
    public class OrderInfoReposity : IOrderInfoReposity
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        private readonly MysqlDbContext _dbContext;
        public OrderInfoReposity(MysqlDbContext mysqlDbContext) 
        {
            _dbContext = mysqlDbContext;
        }

        /// <summary>
        /// 分页查询订单
        /// </summary>
        /// <param name="orderInfoPageInput"></param>
        /// <returns></returns>
        public async Task<IEnumerable<OrderInfo>> GetPage(OrderInfoPageInput orderInfoPageInput)
        {
            Edge.Core.Domain.FccStationInfo.FccStationInfo? fccStationInfo = await _dbContext.FccStationInfos.FirstOrDefaultAsync();
            var stationName = fccStationInfo?.Name ?? "";

            var query = from qo in _dbContext.FccOrderInfos
                        join qn in _dbContext.NozzleInfos on qo.NozzleNum equals qn.ExternalNum
                        join qm in _dbContext.FccMachineInfos on qn.MachineId equals qm.Id
                        where qo.IsDelete == 0
                        where string.IsNullOrEmpty(orderInfoPageInput.oilName) || qo.OilName == orderInfoPageInput.oilName
                        where orderInfoPageInput.nozzleNum == null || qo.NozzleNum == orderInfoPageInput.nozzleNum
                        where orderInfoPageInput.paymentType == null || qo.PayType == orderInfoPageInput.paymentType
                        where (orderInfoPageInput.startCheckTime == null || orderInfoPageInput.endCheckTime == null || qo.EndTime == null)
                                || qo.EndTime >= orderInfoPageInput.startCheckTime && qo.EndTime <= orderInfoPageInput.endCheckTime
                        where string.IsNullOrEmpty(orderInfoPageInput.phoneNumberOruserName) || qo.UserName == orderInfoPageInput.phoneNumberOruserName || qo.PhoneNumber == orderInfoPageInput.phoneNumberOruserName
                        orderby qo.AuthorizationTime descending
                        select new OrderInfo(qo, stationName, qm.Name);

            return await query.Skip((orderInfoPageInput.page - 1) * orderInfoPageInput.pageSize).Take(orderInfoPageInput.pageSize).ToListAsync();
        }

        public async Task<IEnumerable<OrderInfo>> GetOrderFilter(OrderInfoPageInput orderInfoPageInput)
        {
            Edge.Core.Domain.FccStationInfo.FccStationInfo? fccStationInfo = await _dbContext.FccStationInfos.FirstOrDefaultAsync();
            var stationName = fccStationInfo?.Name ?? "";

            var query = from qo in _dbContext.FccOrderInfos
                        join qn in _dbContext.NozzleInfos on qo.NozzleNum equals qn.ExternalNum
                        join qm in _dbContext.FccMachineInfos on qn.MachineId equals qm.Id
                        where qo.IsDelete == 0
                        where string.IsNullOrEmpty(orderInfoPageInput.oilName) || qo.OilName == orderInfoPageInput.oilName
                        where orderInfoPageInput.nozzleNum == null || qo.NozzleNum == orderInfoPageInput.nozzleNum
                        where orderInfoPageInput.paymentType == null || qo.PayType == orderInfoPageInput.paymentType
                        where (orderInfoPageInput.startCheckTime == null || orderInfoPageInput.endCheckTime == null || qo.EndTime == null)
                                || qo.EndTime >= orderInfoPageInput.startCheckTime && qo.EndTime <= orderInfoPageInput.endCheckTime
                        where string.IsNullOrEmpty(orderInfoPageInput.phoneNumberOruserName) || qo.UserName == orderInfoPageInput.phoneNumberOruserName || qo.PhoneNumber == orderInfoPageInput.phoneNumberOruserName
                        orderby qo.AuthorizationTime descending
                        select new OrderInfo(qo, stationName, qm.Name);
            return await query.ToListAsync();
        }
    }
}
