using Edge.Core.Core.database;
using Edge.Core.Domain.FccOrderInfo.Input;
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<Edge.Core.Domain.FccOrderInfo.FccOrderInfo>> GetPage(OrderInfoPageInput orderInfoPageInput)
        {
            IQueryable<Edge.Core.Domain.FccOrderInfo.FccOrderInfo> query = _dbContext.FccOrderInfos.AsQueryable();

            query = query.Where(order => order.IsDelete == 0);
            query = query.WhereIf(orderInfoPageInput.oilName != null && "".Equals(orderInfoPageInput.oilName),order => order.OilName.Equals(orderInfoPageInput.oilName));
            query = query.WhereIf(orderInfoPageInput.nozzleNum != null, order => order.NozzleNum == orderInfoPageInput.nozzleNum);
            //query = query.WhereIf(orderInfoPageInput.ttc != null, order => order.Ttc == orderInfoPageInput.ttc);
            if (orderInfoPageInput.startCheckTime != null && orderInfoPageInput.endCheckTime != null)
            {
                query = query.Where(order => order.EndTime != null && (order.EndTime >= orderInfoPageInput.startCheckTime && order.EndTime <= orderInfoPageInput.endCheckTime));
            }
            query = query.WhereIf(orderInfoPageInput.paymentType != null, order => order.PaymentName.Equals(orderInfoPageInput.paymentType));
            query = query.WhereIf(orderInfoPageInput.phoneNumberOruserName != null, order => order.UserName.Equals(orderInfoPageInput.phoneNumberOruserName) || order.PhoneNumber.Equals(orderInfoPageInput.phoneNumberOruserName));
            return await query.Skip((orderInfoPageInput.page - 1) * orderInfoPageInput.pageSize).Take(orderInfoPageInput.pageSize).ToListAsync();
        }
    }
}
