using Fuel.Core.Nozzle.Dto;
using FuelServer.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using static FreeSql.Internal.GlobalFilter;

namespace Fuel.Application.Service
{
    public class NozzleService : INozzleService
    {
        private readonly EntityHelper _entityHelper;
        public NozzleService(EntityHelper entityHelper)
        {
            _entityHelper = entityHelper;
        }
        /// <summary>
        /// 上传油枪
        /// </summary>
        /// <param name="uploadNozzle"></param>
        /// <returns></returns>
        public async Task<bool> uploadNozzle(UploadNozzle uploadNozzle)
        {
            //RedisHelper.HSetAsync("Transaction", "11:22:33:44", "3232");
            RedisHelper.SetAsync("33:22:33:44", "qweqweqwe", 3600);
            var fsdds = RedisHelper.GetAsync("33:22:33:44");
            //var das = RedisHelper.ExpireAsync("33:22:33:44", 10);
            Guid guid = Guid.Parse("12345678-9abc-def0-1234-56789abcdef0");
            if (uploadNozzle.type == 1)
            {
                var isproduct = await _entityHelper.GetEntitiesAsync<product>(_ => _.Buid == guid && _.ProductName == uploadNozzle.ProductName);
                var istanks = await _entityHelper.GetEntitiesAsync<tanks>(_ => _.Buid == guid && _.TankNumber == uploadNozzle.TankNumber);
                var isnozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == uploadNozzle.ExternalGunNumber);
                if (isproduct.Count > 0 || istanks.Count > 0 || isnozzle.Count > 0)
                {
                    return false;
                }
                product product = new product();
                product.ProductPrice = uploadNozzle.ProductPrice;
                product.ProductName = uploadNozzle.ProductName;
                product.ProductCode = uploadNozzle.ProductCode;
                product.Buid = guid;

                var productid = await _entityHelper.InsertEntityAsync(product);

                tanks tanks = new tanks();
                tanks.Buid = guid;
                tanks.TankNumber = uploadNozzle.TankNumber;
                tanks.TankCapacity = uploadNozzle.TankCapacity;
                tanks.ProductId = productid.Id;
                tanks.ProductName = uploadNozzle.ProductName;

                var tanksid = await _entityHelper.InsertEntityAsync(tanks);

                nozzle nozzle = new nozzle();
                nozzle.Buid = guid;
                nozzle.PumpId = uploadNozzle.PumpID;
                nozzle.TankId = tanksid.Id;
                nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber;
                nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber;
                nozzle.ProductID = productid.Id;
                await _entityHelper.InsertEntityAsync(nozzle);
            }
            else if (uploadNozzle.type == 2)
            {

            }
            else if (uploadNozzle.type == 3)
            {
            }
            return true;
        }
        public async Task<List<NozzleInfo>> GetNozzleInfo(int Nozzleid)
        {
            Guid guid = Guid.Parse("12345678-9abc-def0-1234-56789abcdef0");
            return _entityHelper._fsql.Select<nozzle, tanks, product>()
                    .LeftJoin((a, b, c) => a.TankId == b.Id)
                    .LeftJoin((a, b, c) => a.ProductID == c.Id)
                    .Where((a, b, c) => a.Buid == guid && a.ExternalGunNumber == Nozzleid)
                    .ToList((a, b, c) => new NozzleInfo()
                    {
                        Nozzleid = a.ExternalGunNumber,
                        ProductName = c.ProductName,
                        ProductPrice = c.ProductPrice,
                        Status = a.Status,
                        TankNumber = b.TankNumber
                    });
        }
    }
}
