using Edge.Core.Core.database;
using Edge.Core.Domain.FccOilInfo.Input;
using Microsoft.EntityFrameworkCore;

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

        /// <summary>
        /// 通过id 删除油品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Edge.Core.Domain.FccOilInfo.FccOilInfo?> DeleteByID(long id)
        {
            Edge.Core.Domain.FccOilInfo.FccOilInfo? oilInfo = await _dbContext.OilInfos.FirstOrDefaultAsync(info => info.Id == id);
            if (oilInfo == null)
            {
                Logger.Info($"find oil info with id:{id} for delete,result is null");
                return null;
            }
            //批量将油罐关联的油品置为null
            List<Edge.Core.Domain.FccTankInfo.FccTankInfo> fccTankInfos = await _dbContext.TankInfos.Where(info => info.OilId == oilInfo.Id).ToListAsync();
            foreach(var tank in fccTankInfos)
            {
                _dbContext.Entry(tank).State = EntityState.Modified;
                tank.OilId = null;
                tank.OilName = null;
            }

            _dbContext.OilInfos.Remove(oilInfo);
            await _dbContext.SaveChangesAsync();
            return oilInfo;
        }

        

        /// <summary>
        /// 分页获取油品信息
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="size">页数</param>
        /// <param name="id">过滤信息——id</param>
        /// <param name="name">过滤信息——油品名</param>
        /// <param name="code">过滤信息——油品码</param>
        /// <returns></returns>
        public async Task<IEnumerable<Edge.Core.Domain.FccOilInfo.FccOilInfo>> GetPage(int page, int size, long? id, string? name, string? code)
        {
            IQueryable<Edge.Core.Domain.FccOilInfo.FccOilInfo> query = _dbContext.OilInfos.AsQueryable();

            query = query.WhereIf(id != null, info => info.Id == id);
            query = query.WhereIf(!string.IsNullOrEmpty(name), info => info.Name.Contains(name));
            query = query.WhereIf(code != null, info => info.Code == code);

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

        /// <summary>
        /// 新增、更改油品信息
        /// </summary>
        /// <param name="oilInfoInput"></param>
        /// <returns></returns>
        public async Task<long> UploadOilInfo(OilInfoInput oilInfoInput)
        {
            if(oilInfoInput.Id != null)
            {
                Edge.Core.Domain.FccOilInfo.FccOilInfo? info = await _dbContext.OilInfos.FindAsync(oilInfoInput.Id);
                if (info == null)
                {
                    Logger.Info($"find oil info with id: {oilInfoInput.Id} for update,result is null");
                    return 0;
                }
                info.Name = oilInfoInput.Name;
                info.Code = oilInfoInput.Code;
                info.Price = oilInfoInput.Price;
                await _dbContext.SaveChangesAsync();
                return info.Id;
            }
            Edge.Core.Domain.FccOilInfo.FccOilInfo fccOilInfo = oilInfoInput.ToComponent();
            _dbContext.OilInfos.Add(fccOilInfo);
            await _dbContext.SaveChangesAsync();
            return fccOilInfo.Id;
        }


        /// <summary>
        /// 获取所有油品
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Edge.Core.Domain.FccOilInfo.FccOilInfo>> GetOilInfo()
        {
            return await _dbContext.OilInfos.ToListAsync();
        }
    }
}
