using Edge.Core.Core.database;
using Edge.Core.Domain.FccMachineInfo.Input;
using Edge.Core.Domain.FccMachineInfo.Output;
using Edge.Core.Domain.FccTankInfo.Output;
using Microsoft.EntityFrameworkCore;

namespace FccLite.Web.Repositories.FccMachineInfo
{
    public class MachineReposity : IMachineRepository
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        private MysqlDbContext _dbContext;

        public MachineReposity(MysqlDbContext dbContext) 
        {
            _dbContext = dbContext;
        }

        public async Task<List<MachineInfoOutput>> GetMachine(long stationId)
        {
            var result = await _dbContext.FccMachineInfos
                    .Where(machine => machine.StationId == stationId) // 过滤指定加油站的油机
                    .GroupJoin(
                            _dbContext.NozzleInfos, // 关联的加油枪表
                            machine => machine.Id,        // 油机 的主键
                            nozzle => nozzle.MachineId,// NozzleInfos 的外键
                            (machine, nozzles) => new MachineInfoOutput
                            {
                                Id = machine.Id,
                                Name = machine.Name,
                                Port = machine.Port,
                                Nozzles = nozzles.Select(nozzle => new NozzleInfo
                                {
                                    Id = nozzle.Id,
                                    NozzleNumer = nozzle.ExternalNum
                                }).ToList()
                            })
                    .ToListAsync(); // 异步查询
            return result;


        }

        public async Task<int> UploadMachine(UploadMachineInfo uploadMachineInfo)
        {
            if(uploadMachineInfo.Id == null)
            {
                Edge.Core.Domain.FccMachineInfo.FccMachineInfo fccMachineInfo = uploadMachineInfo.ToComponent();
                _dbContext.FccMachineInfos.Add(fccMachineInfo);
            } else
            {
                Edge.Core.Domain.FccMachineInfo.FccMachineInfo? fccMachineInfo = await _dbContext.FccMachineInfos.FirstOrDefaultAsync(machine => machine.Id == uploadMachineInfo.Id);
                if(fccMachineInfo == null)
                {
                    Logger.Error($"UploadMachine:find machine by id {uploadMachineInfo.Id} is null");
                    return 0;
                }
                fccMachineInfo.Name = uploadMachineInfo.Name;
                fccMachineInfo.Port = uploadMachineInfo.Port;
            }

            return await _dbContext.SaveChangesAsync();
        }

        public async Task<Edge.Core.Domain.FccMachineInfo.FccMachineInfo?> DeleteMachine(long machineId)
        {
            Edge.Core.Domain.FccMachineInfo.FccMachineInfo? fccMachineInfo = await _dbContext.FccMachineInfos.FirstOrDefaultAsync(machine => machine.Id == machineId);
            if(fccMachineInfo == null)
            {
                Logger.Error($"DeleteMachine:find machine by id {machineId} is null");
                return null;
            }

            //删除关联油枪
            List<Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo> fccNozzleInfos = await _dbContext.NozzleInfos.Where(nozzle => nozzle.MachineId == machineId).ToListAsync();
            _dbContext.RemoveRange(fccNozzleInfos);

            //删除油机
            _dbContext.FccMachineInfos.Remove(fccMachineInfo);

            await _dbContext.SaveChangesAsync();

            return fccMachineInfo;
        }

        
    }
}
